-
Notifications
You must be signed in to change notification settings - Fork 222
Flow Homework #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Flow Homework #237
Changes from all commits
3914a18
0d689cb
778670c
c6c8ff1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,24 @@ | ||
| package otus.homework.flowcats | ||
|
|
||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.flow | ||
|
|
||
| class CatsRepository( | ||
| private val catsService: CatsService, | ||
| private val refreshIntervalMs: Long = 5000 | ||
| ) { | ||
|
|
||
| fun listenForCatFacts() = flow { | ||
| fun listenForCatFacts(): Flow<Result<Fact>> = flow { | ||
| while (true) { | ||
| val latestNews = catsService.getCatFact() | ||
| emit(latestNews) | ||
| try { | ||
| val latestNews = catsService.getCatFact() | ||
| emit(Result.Success(latestNews)) | ||
| } catch (e: Exception) { | ||
| Result.Error(e) | ||
| } | ||
| delay(refreshIntervalMs) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут catch снова ничего не делает, он не ловит ошибки из сети, только захватывает и пробрасывает дальше исключения, нужно после этого блока тогда добавить еще один, плюс лучше из репозитория отдавать сразу Result: в выше эмитить во вью модель будет приходить сразу Result, где мы его уже дальше обрабатываем |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,32 @@ | ||
| package otus.homework.flowcats | ||
|
|
||
| import androidx.lifecycle.* | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.collect | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| class CatsViewModel( | ||
| private val catsRepository: CatsRepository | ||
| ) : ViewModel() { | ||
|
|
||
| private val _catsLiveData = MutableLiveData<Fact>() | ||
| val catsLiveData: LiveData<Fact> = _catsLiveData | ||
| private val _catsStateFlow = MutableStateFlow<Result<Fact>>(Result.Success(Fact())) | ||
| val catsStateFlow: StateFlow<Result<Fact>> = _catsStateFlow.asStateFlow() | ||
|
|
||
| init { | ||
| viewModelScope.launch { | ||
| withContext(Dispatchers.IO) { | ||
| catsRepository.listenForCatFacts().collect { | ||
| _catsLiveData.value = it | ||
| } | ||
| catsRepository.listenForCatFacts().collect { result -> | ||
| _catsStateFlow.value = result | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| class CatsViewModelFactory(private val catsRepository: CatsRepository) : | ||
| ViewModelProvider.NewInstanceFactory() { | ||
| override fun <T : ViewModel?> create(modelClass: Class<T>): T = | ||
| @Suppress("UNCHECKED_CAST") | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T = | ||
| CatsViewModel(catsRepository) as T | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package otus.homework.flowcats | ||
|
|
||
| sealed class Result<out T> { | ||
| data class Success<out T>(val data: T) : Result<T>() | ||
| data class Error(val throwable: Throwable? = null) : Result<Nothing>() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вот тут нужно добавить еще
if (e is CancellationException) throw e