Skip to content

Commit

Permalink
moved: errorMessages usage from UiState to Events and its usages updated
Browse files Browse the repository at this point in the history
  • Loading branch information
enesky committed Mar 8, 2024
1 parent aecf597 commit 762f82d
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 81 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ object ErrorMessages {
const val GENERAL_ERROR = "An error occurred. Please try again later."
const val NO_INTERNET_CONNECTION = "No internet connection. Please try again later."
}

/**
* General error handler
* @param throwable: Throwable
*/
fun getErrorMessage(throwable: Throwable?): String {
return when {
throwable?.localizedMessage != null -> throwable.localizedMessage.toString()
throwable?.message != null -> throwable.message.toString()
else -> ErrorMessages.GENERAL_ERROR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import kotlinx.coroutines.flow.receiveAsFlow
*/
interface IEvent

interface IErrorEvent : IEvent {
val errorMessage: String
}

/**
* Base interface for event delegation
*/
Expand Down Expand Up @@ -46,7 +50,7 @@ interface Event<T : IEvent> {
*
* @check ObserveAsEvents in core/common/src/main/java/dev/enesky/core/common/utils/ObserveAsEvents.kt
*/
class EventDelegate<T : IEvent> : Event<T> {
class EventDelegate<T : IEvent> : Event<T> {

override val event: Channel<T> = Channel()
override val eventFlow: Flow<T> = event.receiveAsFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import kotlinx.coroutines.flow.StateFlow
* Base interface for defining new UiState data classes
**/
interface IUiState {
val loading: Boolean
var errorMessage: String?
val isLoading: Boolean
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,10 @@ fun DetailsRoute(
viewModel.getThemAll(animeId = animeId.toInt())
}

LaunchedEffect(key1 = uiState.errorMessage) {
if (uiState.errorMessage.isNullOrEmpty().not()) {
viewModel.triggerEvent {
DetailsEvents.OnError(uiState.errorMessage ?: String.Empty)
}
}
}

ObserveAsEvents(flow = viewModel.eventFlow) { event ->
when (event) {
is DetailsEvents.OnError -> {
onShowMessage(event.errorMessage)
uiState.errorMessage = null
}
is DetailsEvents.OnTrailerPlayClick -> {
}
is DetailsEvents.OnError -> onShowMessage(event.errorMessage)
is DetailsEvents.OnTrailerPlayClick -> { }
}
}

Expand All @@ -82,7 +70,7 @@ private fun DetailsScreen(
) {
SwipeRefresh(
modifier = modifier,
isRefreshing = uiState.loading,
isRefreshing = uiState.isLoading,
onRefresh = onRefresh,
) {
val listState = rememberLazyListState()
Expand All @@ -99,19 +87,19 @@ private fun DetailsScreen(
item {
DetailedAnimePreview(
detailedAnime = uiState.detailedAnime,
isLoading = uiState.loading,
isLoading = uiState.isLoading,
)
}
item {
DetailedAnimeSummary(
summary = uiState.detailedAnime?.summary ?: String.Empty,
isLoading = uiState.loading,
isLoading = uiState.isLoading,
)
}
item {
AnimeCharactersRow(
animeCharacters = uiState.characters,
isLoading = uiState.loading,
isLoading = uiState.isLoading,
)
}
item {
Expand All @@ -120,7 +108,7 @@ private fun DetailsScreen(
item {
AnimeRecommendationsRow(
animeRecommendations = uiState.recommendations,
isLoading = uiState.loading,
isLoading = uiState.isLoading,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.enesky.feature.details
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import dev.enesky.core.common.consts.getErrorMessage
import dev.enesky.core.common.delegate.Event
import dev.enesky.core.common.delegate.EventDelegate
import dev.enesky.core.common.delegate.UiState
Expand Down Expand Up @@ -46,19 +47,26 @@ class DetailsViewModel(
detailedAnimeUseCase(animeId = animeId)
.asResult()
.onEach { resource ->
updateUiState {
when (resource) {
is Result.Loading -> copy(loading = true)
is Result.Success -> copy(
loading = false,
when (resource) {
is Result.Loading -> updateUiState { copy(isLoading = true) }
is Result.Success -> updateUiState {
copy(
isLoading = false,
detailedAnime = resource.data,
)

is Result.Error -> copy(
loading = false,
detailedAnime = null,
errorMessage = resource.exception?.message,
)
}
is Result.Error -> {
updateUiState {
copy(
isLoading = false,
detailedAnime = null,
)
}
triggerEvent {
DetailsEvents.OnError(
getErrorMessage(resource.exception)
)
}
}
}
}
Expand All @@ -71,19 +79,26 @@ class DetailsViewModel(
animeCharactersUseCase(animeId = animeId)
.asResult()
.onEach { resource ->
updateUiState {
when (resource) {
is Result.Loading -> copy(loading = true)
is Result.Success -> copy(
loading = false,
when (resource) {
is Result.Loading -> updateUiState { copy(isLoading = true) }
is Result.Success -> updateUiState {
copy(
isLoading = false,
characters = resource.data,
)

is Result.Error -> copy(
loading = false,
characters = null,
errorMessage = resource.exception?.message,
)
}
is Result.Error -> {
updateUiState {
copy(
isLoading = false,
characters = null,
)
}
triggerEvent {
DetailsEvents.OnError(
getErrorMessage(resource.exception)
)
}
}
}
}
Expand All @@ -100,7 +115,7 @@ class DetailsViewModel(

updateUiState {
copy(
loading = false,
isLoading = false,
episodes = popularAnimesFlow,
)
}
Expand All @@ -111,19 +126,26 @@ class DetailsViewModel(
animeRecommendationsUseCase(animeId = animeId)
.asResult()
.onEach { resource ->
updateUiState {
when (resource) {
is Result.Loading -> copy(loading = true)
is Result.Success -> copy(
loading = false,
when (resource) {
is Result.Loading -> updateUiState { copy(isLoading = true) }
is Result.Success -> updateUiState {
copy(
isLoading = false,
recommendations = resource.data,
)

is Result.Error -> copy(
loading = false,
recommendations = null,
errorMessage = resource.exception?.message,
)
}
is Result.Error -> {
updateUiState {
copy(
isLoading = false,
recommendations = null,
)
}
triggerEvent {
DetailsEvents.OnError(
getErrorMessage(resource.exception)
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package dev.enesky.feature.details.helpers

import dev.enesky.core.common.delegate.IErrorEvent
import dev.enesky.core.common.delegate.IEvent

/**
* Created by Enes Kamil YILMAZ on 07/12/2023
*/

sealed interface DetailsEvents : IEvent {
data class OnError(val errorMessage: String) : DetailsEvents
data class OnError(override val errorMessage: String) : DetailsEvents, IErrorEvent
data class OnTrailerPlayClick(val animeId: String) : DetailsEvents
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import kotlinx.coroutines.flow.Flow
*/

data class DetailsUiState(
override val loading: Boolean = false,
override var errorMessage: String? = null,
override val isLoading: Boolean = false,
val detailedAnime: DetailedAnime? = null,
val characters: List<AnimeCharacter>? = null,
val episodes: Flow<PagingData<AnimeEpisode>>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ fun HomeRoute(
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

if (uiState.errorMessage != null) {
onShowMessage(uiState.errorMessage.toString())
}

ObserveAsEvents(flow = viewModel.eventFlow) { homeEvents ->
when (homeEvents) {
is HomeEvents.OnError -> onShowMessage(homeEvents.errorMessage)
Expand Down Expand Up @@ -94,7 +90,7 @@ fun HomeScreen(
item {
AnimePreview(
anime = uiState.previewAnime,
isLoading = uiState.loading,
isLoading = uiState.isLoading,
onNavigateDetailsClick = onNavigateDetailsClick,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.enesky.feature.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import dev.enesky.core.common.consts.ErrorMessages
import dev.enesky.core.common.consts.getErrorMessage
import dev.enesky.core.common.delegate.Event
import dev.enesky.core.common.delegate.EventDelegate
import dev.enesky.core.common.delegate.UiState
Expand Down Expand Up @@ -62,7 +64,7 @@ class HomeViewModel(

updateUiState {
copy(
loading = false,
isLoading = false,
popularAnimes = popularAnimesFlow,
airingAnimes = airingAnimesFlow,
upcomingAnimes = upcomingAnimesFlow,
Expand All @@ -80,7 +82,7 @@ class HomeViewModel(
is Result.Loading -> {
updateUiState {
copy(
loading = true,
isLoading = true,
previewAnime = null,
)
}
Expand All @@ -90,15 +92,15 @@ class HomeViewModel(
if (resource.data.id == 0) {
updateUiState {
copy(
loading = false,
isLoading = false,
previewAnime = null,
)
}
return@onEach
}
updateUiState {
copy(
loading = false,
isLoading = false,
previewAnime = resource.data,
)
}
Expand All @@ -107,10 +109,15 @@ class HomeViewModel(
is Result.Error -> {
updateUiState {
copy(
loading = false,
isLoading = false,
previewAnime = null,
)
}
triggerEvent {
HomeEvents.OnError(
getErrorMessage(resource.exception)
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package dev.enesky.feature.home.helpers

import dev.enesky.core.common.delegate.IErrorEvent
import dev.enesky.core.common.delegate.IEvent

/**
* Created by Enes Kamil YILMAZ on 07/12/2023
*/

sealed interface HomeEvents : IEvent {
data class OnError(val errorMessage: String) : HomeEvents
data class OnError(override val errorMessage: String) : HomeEvents, IErrorEvent
data class OnItemOptionsClick(val animeId: String) : HomeEvents
data class NavigateToDetails(val animeId: String) : HomeEvents
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import kotlinx.coroutines.flow.Flow
*/

data class HomeUiState(
override val loading: Boolean = false,
override var errorMessage: String? = null,
override val isLoading: Boolean = false,
val airingAnimes: Flow<PagingData<Anime>>? = null,
val upcomingAnimes: Flow<PagingData<Anime>>? = null,
val popularAnimes: Flow<PagingData<Anime>>? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.enesky.feature.login.signin.helpers

import dev.enesky.core.common.delegate.IErrorEvent
import dev.enesky.core.common.delegate.IEvent

/**
* Created by Enes Kamil YILMAZ on 28/11/2023
*/

sealed interface SignInEvents : IEvent {
data class OnError(val errorMessage: String) : SignInEvents
data class OnError(override val errorMessage: String) : SignInEvents, IErrorEvent
data object NavigateToHome : SignInEvents
data object NavigateToSignUp : SignInEvents

}
Loading

0 comments on commit 762f82d

Please sign in to comment.