Setup Presentation Module

This commit is contained in:
2021-09-04 15:36:05 +02:00
parent e532ae454c
commit c00c3db51c
39 changed files with 602 additions and 68 deletions
@@ -0,0 +1,17 @@
package com.ericampire.android.androidstudycase.util.mvi
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.ContainerHost
abstract class BaseViewModel<S: Any, E: Any, A> : ContainerHost<S, E>, ViewModel() {
protected val pendingAction = MutableSharedFlow<A>()
protected fun submitAction(action: A) {
viewModelScope.launch {
pendingAction.emit(action)
}
}
}
@@ -0,0 +1,18 @@
package com.ericampire.android.androidstudycase.util.room
import androidx.room.TypeConverter
import java.util.*
object DateConverter {
@TypeConverter
fun fromTimestamp(value: Long): Date {
return Date(value)
}
@TypeConverter
fun dateToTimestamp(date: Date): Long {
return date.time
}
}
@@ -5,6 +5,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onStart
/**
* Executes business logic in its execute method and keep posting updates to the result as
@@ -13,6 +14,7 @@ import kotlinx.coroutines.flow.flowOn
*/
abstract class FlowUseCase<in P, R>(private val coroutineDispatcher: CoroutineDispatcher) {
operator fun invoke(parameters: P): Flow<Result<R>> = execute(parameters)
.onStart { emit(Result.Loading) }
.catch { e -> emit(Result.Error(Exception(e))) }
.flowOn(coroutineDispatcher)