diff --git a/flowcats/src/main/java/otus/homework/flowcats/ApiResult.kt b/flowcats/src/main/java/otus/homework/flowcats/ApiResult.kt new file mode 100644 index 00000000..c050bbec --- /dev/null +++ b/flowcats/src/main/java/otus/homework/flowcats/ApiResult.kt @@ -0,0 +1,9 @@ +package otus.homework.flowcats + +sealed class ApiResult { + + class Success(val data: T): ApiResult() + class Error(val e: Throwable): ApiResult() + + data object Loading: ApiResult() +} \ No newline at end of file diff --git a/flowcats/src/main/java/otus/homework/flowcats/CatsRepository.kt b/flowcats/src/main/java/otus/homework/flowcats/CatsRepository.kt index 10fcb77d..0655c893 100644 --- a/flowcats/src/main/java/otus/homework/flowcats/CatsRepository.kt +++ b/flowcats/src/main/java/otus/homework/flowcats/CatsRepository.kt @@ -1,6 +1,7 @@ package otus.homework.flowcats import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow class CatsRepository( @@ -8,9 +9,15 @@ class CatsRepository( private val refreshIntervalMs: Long = 5000 ) { - fun listenForCatFacts() = flow { + fun listenForCatFacts(): Flow> = flow { while (true) { - val latestNews = catsService.getCatFact() + emit(ApiResult.Loading) + val latestNews = try { + val fact = catsService.getCatFact() + ApiResult.Success(fact) + } catch (e: Exception){ + ApiResult.Error(e) + } emit(latestNews) delay(refreshIntervalMs) } diff --git a/flowcats/src/main/java/otus/homework/flowcats/CatsViewModel.kt b/flowcats/src/main/java/otus/homework/flowcats/CatsViewModel.kt index 0d8ba8a7..7f28e8a2 100644 --- a/flowcats/src/main/java/otus/homework/flowcats/CatsViewModel.kt +++ b/flowcats/src/main/java/otus/homework/flowcats/CatsViewModel.kt @@ -2,6 +2,9 @@ package otus.homework.flowcats import androidx.lifecycle.* import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -10,14 +13,14 @@ class CatsViewModel( private val catsRepository: CatsRepository ) : ViewModel() { - private val _catsLiveData = MutableLiveData() - val catsLiveData: LiveData = _catsLiveData + private val _catsStF = MutableStateFlow>(ApiResult.Loading) + val catsStF: StateFlow> = _catsStF.asStateFlow() init { viewModelScope.launch { withContext(Dispatchers.IO) { catsRepository.listenForCatFacts().collect { - _catsLiveData.value = it + _catsStF.value = it } } } @@ -26,6 +29,6 @@ class CatsViewModel( class CatsViewModelFactory(private val catsRepository: CatsRepository) : ViewModelProvider.NewInstanceFactory() { - override fun create(modelClass: Class): T = + override fun create(modelClass: Class): T = CatsViewModel(catsRepository) as T } \ No newline at end of file diff --git a/flowcats/src/main/java/otus/homework/flowcats/MainActivity.kt b/flowcats/src/main/java/otus/homework/flowcats/MainActivity.kt index edea434b..031ade44 100644 --- a/flowcats/src/main/java/otus/homework/flowcats/MainActivity.kt +++ b/flowcats/src/main/java/otus/homework/flowcats/MainActivity.kt @@ -2,7 +2,11 @@ package otus.homework.flowcats import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.widget.Toast import androidx.activity.viewModels +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.launch class MainActivity : AppCompatActivity() { @@ -14,8 +18,16 @@ class MainActivity : AppCompatActivity() { val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView setContentView(view) - catsViewModel.catsLiveData.observe(this){ - view.populate(it) + lifecycleScope.launchWhenResumed { + catsViewModel.catsStF.collectLatest { result -> + when (result) { + is ApiResult.Error -> Toast.makeText(this@MainActivity, result.e.toString(), Toast.LENGTH_LONG).show() + ApiResult.Loading -> {} + is ApiResult.Success -> view.populate(result.data) + } + + } } + } } \ No newline at end of file diff --git a/operators/src/main/java/otus/homework/flow/SampleInteractor.kt b/operators/src/main/java/otus/homework/flow/SampleInteractor.kt index 1993c064..84312795 100644 --- a/operators/src/main/java/otus/homework/flow/SampleInteractor.kt +++ b/operators/src/main/java/otus/homework/flow/SampleInteractor.kt @@ -18,7 +18,12 @@ class SampleInteractor( * 6) возвращает результат */ fun task1(): Flow { - return flowOf() + return sampleRepository.produceNumbers() + .map { it * 5 } + .filter { it > 20 } + .filter { it % 2 != 0 } + .map { "$it won" } + .take(3) } /** @@ -29,7 +34,15 @@ class SampleInteractor( * Если число не делится на 3,5,15 - эмитим само число */ fun task2(): Flow { - return flowOf() + return sampleRepository.produceNumbers() + .transform { + emit(it.toString()) + when { + it % 15 == 0 ->{emit("FizzBuzz")} + it % 5 == 0 ->{emit("Buzz")} + it % 3 == 0 ->{emit("Fizz")} + } + } } /** @@ -38,7 +51,9 @@ class SampleInteractor( * Если айтемы в одно из флоу кончились то результирующий флоу также должен закончится */ fun task3(): Flow> { - return flowOf() + return sampleRepository.produceColors().zip(sampleRepository.produceForms()){ s, s2 -> + s to s2 + } } /** @@ -48,6 +63,10 @@ class SampleInteractor( * При любом исходе, будь то выброс исключения или успешная отработка функции вызовите метод dotsRepository.completed() */ fun task4(): Flow { - return flowOf() + return sampleRepository.produceNumbers() + .catch { + it.takeIf { it is IllegalArgumentException }?.let { emit(-1) } ?: run { throw it } + } + .onCompletion { sampleRepository.completed() } } } \ No newline at end of file