Skip to content

Commit

Permalink
refactororing
Browse files Browse the repository at this point in the history
  • Loading branch information
mendejumrukovski committed Dec 1, 2023
1 parent 1062943 commit f98e059
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ import kotlinx.coroutines.launch
@HiltViewModel
class FavouritesViewModel @Inject constructor(
private val localRepository: LocalRepositoryImpl
) :
ViewModel() {
) : ViewModel() {

private val _uiState: MutableStateFlow<UIState<List<Quote>>> =
MutableStateFlow(UIState.Loading)
val uiState: StateFlow<UIState<List<Quote>>> = _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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class HomeViewModel @Inject constructor(private val remoteRepository: RemoteRepo
val uiState: StateFlow<UIState<List<Quote>>> = _uiState

init {
viewModelScope.launch {
getQuotes()
}
viewModelScope.launch { getQuotes() }
}

// todo get paginated quotes list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ class TagsViewModel @Inject constructor(private val remoteRepository: RemoteRepo
private val _uiState: MutableStateFlow<UIState<List<Tag>>> = MutableStateFlow(UIState.Loading)
val uiState: StateFlow<UIState<List<Tag>>> = _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
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit f98e059

Please sign in to comment.