-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from choffmann/implement-category-viewmodel
Implement Category ViewModel and refactoring
- Loading branch information
Showing
57 changed files
with
1,029 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...inder-multiplatform-app/src/commonMain/kotlin/de/hsfl/budgetBinder/common/DataResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,52 @@ | ||
package de.hsfl.budgetBinder.common | ||
|
||
import de.hsfl.budgetBinder.domain.usecase.NavigateToScreenUseCase | ||
import de.hsfl.budgetBinder.presentation.Screen | ||
import de.hsfl.budgetBinder.presentation.event.UiEvent | ||
import de.hsfl.budgetBinder.presentation.flow.RouterFlow | ||
import de.hsfl.budgetBinder.presentation.flow.UiEventSharedFlow | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
|
||
sealed class DataResponse<T>(val data: T? = null, val error: ErrorModel? = null) { | ||
class Success<T>(data: T) : DataResponse<T>(data) | ||
class Error<T>(error: ErrorModel?, data: T? = null) : DataResponse<T>(data, error) | ||
class Loading<T>(data: T? = null) : DataResponse<T>(data) | ||
class Unauthorized<T>(error: ErrorModel? = null, data: T? = null) : DataResponse<T>(data, error) | ||
} | ||
|
||
fun <T> DataResponse<T>.handleDataResponse( | ||
scope: CoroutineScope = CoroutineScope(Dispatchers.Unconfined + SupervisorJob()), | ||
routerFlow: RouterFlow = RouterFlow(NavigateToScreenUseCase(), scope), | ||
onSuccess: (T) -> Unit, | ||
onError: ((ErrorModel) -> Unit)? = null, | ||
onLoading: (() -> Unit)? = null, | ||
onUnauthorized: (() -> Unit)? = null | ||
) = scope.launch { | ||
when (this@handleDataResponse) { | ||
is DataResponse.Error -> { | ||
onError?.let { | ||
onError(this@handleDataResponse.error!!) | ||
} ?: UiEventSharedFlow.mutableEventFlow.emit(UiEvent.ShowError(this@handleDataResponse.error!!.message)) | ||
} | ||
is DataResponse.Loading -> { | ||
onLoading?.let { | ||
onLoading() | ||
} ?: UiEventSharedFlow.mutableEventFlow.emit(UiEvent.ShowLoading) | ||
} | ||
is DataResponse.Success -> { | ||
UiEventSharedFlow.mutableEventFlow.emit(UiEvent.HideSuccess) | ||
onSuccess(this@handleDataResponse.data!!) | ||
} | ||
is DataResponse.Unauthorized -> { | ||
onUnauthorized?.let { | ||
onUnauthorized() | ||
} ?: run { | ||
routerFlow.navigateTo(Screen.Login) | ||
UiEventSharedFlow.mutableEventFlow.emit(UiEvent.ShowError(this@handleDataResponse.error!!.message)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ltiplatform-app/src/commonMain/kotlin/de/hsfl/budgetBinder/domain/usecase/AuthUseCases.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.hsfl.budgetBinder.domain.usecase | ||
|
||
data class AuthUseCases( | ||
val loginUseCase: LoginUseCase, | ||
val getMyUserUseCase: GetMyUserUseCase, | ||
val registerUseCase: RegisterUseCase | ||
) |
Oops, something went wrong.