Skip to content

Commit aecf597

Browse files
committed
updated: flows coroutine usages
1 parent 003f8ae commit aecf597

File tree

4 files changed

+102
-91
lines changed

4 files changed

+102
-91
lines changed

app/src/main/java/dev/enesky/doodle/app/ui/main/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import androidx.activity.compose.setContent
2222
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
2323
import androidx.core.view.WindowCompat
2424
import androidx.lifecycle.lifecycleScope
25-
import dev.enesky.core.domain.manager.RemoteConfigManager
25+
import dev.enesky.core.domain.manager.remoteconfig.RemoteConfigManager
2626
import dev.enesky.doodle.app.ui.DoodleApp
2727
import kotlinx.coroutines.Dispatchers
2828
import kotlinx.coroutines.launch

feature/details/src/main/java/dev/enesky/feature/details/DetailsViewModel.kt

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import dev.enesky.feature.details.helpers.DetailsEvents
1717
import dev.enesky.feature.details.helpers.DetailsUiState
1818
import kotlinx.coroutines.Dispatchers
1919
import kotlinx.coroutines.flow.distinctUntilChanged
20+
import kotlinx.coroutines.flow.flowOn
2021
import kotlinx.coroutines.flow.launchIn
2122
import kotlinx.coroutines.flow.onEach
2223
import kotlinx.coroutines.launch
@@ -42,55 +43,59 @@ class DetailsViewModel(
4243
}
4344

4445
private fun getDetailedAnime(animeId: Int) {
45-
viewModelScope.launch(Dispatchers.IO) {
46-
detailedAnimeUseCase(animeId = animeId)
47-
.asResult()
48-
.onEach { resource ->
49-
updateUiState {
50-
when (resource) {
51-
is Result.Loading -> copy(loading = true)
52-
is Result.Success -> copy(
53-
loading = false,
54-
detailedAnime = resource.data,
55-
)
56-
is Result.Error -> copy(
57-
loading = false,
58-
detailedAnime = null,
59-
errorMessage = resource.exception?.message,
60-
)
61-
}
46+
detailedAnimeUseCase(animeId = animeId)
47+
.asResult()
48+
.onEach { resource ->
49+
updateUiState {
50+
when (resource) {
51+
is Result.Loading -> copy(loading = true)
52+
is Result.Success -> copy(
53+
loading = false,
54+
detailedAnime = resource.data,
55+
)
56+
57+
is Result.Error -> copy(
58+
loading = false,
59+
detailedAnime = null,
60+
errorMessage = resource.exception?.message,
61+
)
6262
}
63-
}.launchIn(this)
64-
}
63+
}
64+
}
65+
.flowOn(Dispatchers.IO)
66+
.launchIn(viewModelScope)
67+
6568
}
6669

6770
private fun getAnimeCharacters(animeId: Int) {
68-
viewModelScope.launch(Dispatchers.IO) {
69-
animeCharactersUseCase(animeId = animeId)
70-
.asResult()
71-
.onEach { resource ->
72-
updateUiState {
73-
when (resource) {
74-
is Result.Loading -> copy(loading = true)
75-
is Result.Success -> copy(
76-
loading = false,
77-
characters = resource.data,
78-
)
79-
is Result.Error -> copy(
80-
loading = false,
81-
characters = null,
82-
errorMessage = resource.exception?.message,
83-
)
84-
}
71+
animeCharactersUseCase(animeId = animeId)
72+
.asResult()
73+
.onEach { resource ->
74+
updateUiState {
75+
when (resource) {
76+
is Result.Loading -> copy(loading = true)
77+
is Result.Success -> copy(
78+
loading = false,
79+
characters = resource.data,
80+
)
81+
82+
is Result.Error -> copy(
83+
loading = false,
84+
characters = null,
85+
errorMessage = resource.exception?.message,
86+
)
8587
}
86-
}.launchIn(this)
87-
}
88+
}
89+
}
90+
.flowOn(Dispatchers.IO)
91+
.launchIn(viewModelScope)
8892
}
8993

9094
private fun getAnimeEpisodes(animeId: Int) {
9195
viewModelScope.launch(Dispatchers.IO) {
9296
val popularAnimesFlow = animeEpisodesUseCase(animeId)
9397
.distinctUntilChanged()
98+
.flowOn(Dispatchers.IO)
9499
.cachedIn(viewModelScope)
95100

96101
updateUiState {
@@ -103,25 +108,26 @@ class DetailsViewModel(
103108
}
104109

105110
private fun getAnimeRecommendations(animeId: Int) {
106-
viewModelScope.launch(Dispatchers.IO) {
107-
animeRecommendationsUseCase(animeId = animeId)
108-
.asResult()
109-
.onEach { resource ->
110-
updateUiState {
111-
when (resource) {
112-
is Result.Loading -> copy(loading = true)
113-
is Result.Success -> copy(
114-
loading = false,
115-
recommendations = resource.data,
116-
)
117-
is Result.Error -> copy(
118-
loading = false,
119-
recommendations = null,
120-
errorMessage = resource.exception?.message,
121-
)
122-
}
111+
animeRecommendationsUseCase(animeId = animeId)
112+
.asResult()
113+
.onEach { resource ->
114+
updateUiState {
115+
when (resource) {
116+
is Result.Loading -> copy(loading = true)
117+
is Result.Success -> copy(
118+
loading = false,
119+
recommendations = resource.data,
120+
)
121+
122+
is Result.Error -> copy(
123+
loading = false,
124+
recommendations = null,
125+
errorMessage = resource.exception?.message,
126+
)
123127
}
124-
}.launchIn(this)
125-
}
128+
}
129+
}
130+
.flowOn(Dispatchers.IO)
131+
.launchIn(viewModelScope)
126132
}
127133
}

feature/home/src/main/java/dev/enesky/feature/home/HomeScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fun HomeRoute(
3636
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
3737

3838
if (uiState.errorMessage != null) {
39-
onShowMessage(uiState.errorMessage!!)
39+
onShowMessage(uiState.errorMessage.toString())
4040
}
4141

4242
ObserveAsEvents(flow = viewModel.eventFlow) { homeEvents ->

feature/home/src/main/java/dev/enesky/feature/home/HomeViewModel.kt

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import dev.enesky.feature.home.helpers.HomeEvents
1717
import dev.enesky.feature.home.helpers.HomeUiState
1818
import kotlinx.coroutines.Dispatchers
1919
import kotlinx.coroutines.flow.distinctUntilChanged
20+
import kotlinx.coroutines.flow.flowOn
2021
import kotlinx.coroutines.flow.launchIn
2122
import kotlinx.coroutines.flow.onEach
2223
import kotlinx.coroutines.launch
@@ -41,18 +42,22 @@ class HomeViewModel(
4142
viewModelScope.launch(Dispatchers.IO) {
4243
val popularAnimesFlow = topAnimePagingUseCase(AnimeFilter.POPULARITY)
4344
.distinctUntilChanged()
45+
.flowOn(Dispatchers.IO)
4446
.cachedIn(viewModelScope)
4547

4648
val airingAnimesFlow = topAnimePagingUseCase(AnimeFilter.AIRING)
4749
.distinctUntilChanged()
50+
.flowOn(Dispatchers.IO)
4851
.cachedIn(viewModelScope)
4952

5053
val upcomingAnimesFlow = topAnimePagingUseCase(AnimeFilter.UPCOMING)
5154
.distinctUntilChanged()
55+
.flowOn(Dispatchers.IO)
5256
.cachedIn(viewModelScope)
5357

5458
val favoriteAnimesFlow = topAnimePagingUseCase(AnimeFilter.FAVORITE)
5559
.distinctUntilChanged()
60+
.flowOn(Dispatchers.IO)
5661
.cachedIn(viewModelScope)
5762

5863
updateUiState {
@@ -68,48 +73,48 @@ class HomeViewModel(
6873
}
6974

7075
private fun getPreviewAnime() {
71-
viewModelScope.launch(Dispatchers.IO) {
72-
detailedAnimeUseCase(animeId = RemoteConfigManager.homeScreenPreviewAnimeId)
73-
.asResult()
74-
.onEach { resource ->
75-
when (resource) {
76-
is Result.Loading -> {
77-
updateUiState {
78-
copy(
79-
loading = true,
80-
previewAnime = null,
81-
)
82-
}
76+
detailedAnimeUseCase(animeId = RemoteConfigManager.Values.homeScreenPreviewAnimeId)
77+
.asResult()
78+
.onEach { resource ->
79+
when (resource) {
80+
is Result.Loading -> {
81+
updateUiState {
82+
copy(
83+
loading = true,
84+
previewAnime = null,
85+
)
8386
}
87+
}
8488

85-
is Result.Success -> {
86-
if (resource.data.id == 0) {
87-
updateUiState {
88-
copy(
89-
loading = false,
90-
previewAnime = null,
91-
)
92-
}
93-
return@onEach
94-
}
89+
is Result.Success -> {
90+
if (resource.data.id == 0) {
9591
updateUiState {
9692
copy(
9793
loading = false,
98-
previewAnime = resource.data,
94+
previewAnime = null,
9995
)
10096
}
97+
return@onEach
98+
}
99+
updateUiState {
100+
copy(
101+
loading = false,
102+
previewAnime = resource.data,
103+
)
101104
}
105+
}
102106

103-
is Result.Error -> {
104-
updateUiState {
105-
copy(
106-
loading = false,
107-
previewAnime = null,
108-
)
109-
}
107+
is Result.Error -> {
108+
updateUiState {
109+
copy(
110+
loading = false,
111+
previewAnime = null,
112+
)
110113
}
111114
}
112-
}.launchIn(this)
113-
}
115+
}
116+
}
117+
.flowOn(Dispatchers.IO)
118+
.launchIn(viewModelScope)
114119
}
115120
}

0 commit comments

Comments
 (0)