From f98e059dea5116957866ea0aa707cee0912682d8 Mon Sep 17 00:00:00 2001 From: mendejumrukovski Date: Sat, 2 Dec 2023 00:59:11 +0100 Subject: [PATCH] refactororing --- .../ui/screen/favourites/FavouritesScreen.kt | 5 ---- .../screen/favourites/FavouritesViewModel.kt | 19 ++++++------ .../ui/screen/home/HomeViewModel.kt | 4 +-- .../ui/screen/tags/TagsScreen.kt | 5 ---- .../ui/screen/tags/TagsViewModel.kt | 30 +++++++++---------- .../tags/selected/SelectedTagViewModel.kt | 4 --- 6 files changed, 25 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesScreen.kt b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesScreen.kt index 919f87d..8ce7049 100644 --- a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesScreen.kt +++ b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesScreen.kt @@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.res.stringResource @@ -26,10 +25,6 @@ fun FavouritesScreen( ) { val uiState by viewModel.uiState.collectAsStateWithLifecycle() - LaunchedEffect(Unit) { - viewModel.getAllFavourites() - } - Column { TopBar( title = stringResource(id = R.string.screen_favourites), diff --git a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesViewModel.kt b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesViewModel.kt index 50fdaf1..4832ceb 100644 --- a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesViewModel.kt +++ b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/favourites/FavouritesViewModel.kt @@ -15,20 +15,21 @@ import kotlinx.coroutines.launch @HiltViewModel class FavouritesViewModel @Inject constructor( private val localRepository: LocalRepositoryImpl -) : - ViewModel() { +) : ViewModel() { private val _uiState: MutableStateFlow>> = MutableStateFlow(UIState.Loading) val uiState: StateFlow>> = _uiState - fun getAllFavourites() { - viewModelScope.launch { - localRepository.getAllFavouriteQuotesAsync().collectLatest { - when (it.isEmpty()) { - true -> _uiState.value = UIState.SuccessWithNoData - false -> _uiState.value = UIState.SuccessWithData(it) - } + init { + viewModelScope.launch { getAllFavourites() } + } + + private suspend fun getAllFavourites() { + localRepository.getAllFavouriteQuotesAsync().collectLatest { + _uiState.value = when (it.isEmpty()) { + true -> UIState.SuccessWithNoData + false -> UIState.SuccessWithData(it) } } } diff --git a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/home/HomeViewModel.kt b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/home/HomeViewModel.kt index 63a5ca0..8792c08 100644 --- a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/home/HomeViewModel.kt +++ b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/home/HomeViewModel.kt @@ -18,9 +18,7 @@ class HomeViewModel @Inject constructor(private val remoteRepository: RemoteRepo val uiState: StateFlow>> = _uiState init { - viewModelScope.launch { - getQuotes() - } + viewModelScope.launch { getQuotes() } } // todo get paginated quotes list diff --git a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsScreen.kt b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsScreen.kt index 809ae1c..e36820d 100644 --- a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsScreen.kt +++ b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsScreen.kt @@ -7,7 +7,6 @@ import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource @@ -25,10 +24,6 @@ fun TagsScreen( ) { val uiState by viewModel.uiState.collectAsStateWithLifecycle() - LaunchedEffect(key1 = "items") { - viewModel.getAllTags() - } - Column { TopBar(stringResource(id = R.string.screen_tags)) diff --git a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsViewModel.kt b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsViewModel.kt index 493719e..740af2d 100644 --- a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsViewModel.kt +++ b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/TagsViewModel.kt @@ -17,23 +17,21 @@ class TagsViewModel @Inject constructor(private val remoteRepository: RemoteRepo private val _uiState: MutableStateFlow>> = MutableStateFlow(UIState.Loading) val uiState: StateFlow>> = _uiState - suspend fun getAllTags() { - viewModelScope.launch { - if (_uiState.value is UIState.SuccessWithData) { - return@launch - } + init { + viewModelScope.launch { getAllTags() } + } - _uiState.value = remoteRepository.getAllTags().fold( - onSuccess = { - when (it.isEmpty()) { - true -> UIState.SuccessWithNoData - false -> UIState.SuccessWithData(it) - } - }, - onFailure = { - UIState.ErrorRetrievingData + private suspend fun getAllTags() { + _uiState.value = remoteRepository.getAllTags().fold( + onSuccess = { + when (it.isEmpty()) { + true -> UIState.SuccessWithNoData + false -> UIState.SuccessWithData(it) } - ) - } + }, + onFailure = { + UIState.ErrorRetrievingData + } + ) } } diff --git a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/selected/SelectedTagViewModel.kt b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/selected/SelectedTagViewModel.kt index 51d94c6..0229e1a 100644 --- a/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/selected/SelectedTagViewModel.kt +++ b/app/src/main/java/com/jumrukovski/quotescompose/ui/screen/tags/selected/SelectedTagViewModel.kt @@ -30,10 +30,6 @@ class SelectedTagViewModel @Inject constructor( currentTag = tag - if (_uiState.value !is UIState.Loading) { - _uiState.value = UIState.Loading - } - _uiState.value = remoteRepository.getQuotesForTag(tag).fold( onSuccess = { when (it.isEmpty()) {