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
sealed class ViewState<out T : Any> | |
class Success<out T : Any>(val data: T) : ViewState<T>() | |
class Error<out T : Any>(val error: Throwable) : ViewState<T>() | |
class Loading<out T : Any> : ViewState<T>() | |
class NoInternetState<T : Any> : ViewState<T>() |
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
class WeatherFragment : BaseFragment() { | |
private val viewModel: WeatherViewModel by viewModel() | |
override fun getLayout() = R.layout.fragment_weather | |
override fun viewReady() { | |
viewModel.getWeatherForLocation() | |
subscribeToData() | |
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
abstract class BaseRepository<T : Any, R : DomainMapper<T>> : KoinComponent { | |
private val connectivity: Connectivity by inject() | |
private val contextProvider: CoroutineContextProvider by inject() | |
/** | |
* Use this if you need to cache data after fetching it from the api, or retrieve something from cache | |
*/ | |
protected suspend fun fetchData( | |
apiDataProvider: suspend () -> Result<T>, | |
dbDataProvider: suspend () -> R |
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
/** | |
* Use this if you need to cache data after fetching it from the api, or retrieve something from cache | |
*/ | |
inline fun <T : RoomMapper<R>, R : DomainMapper<U>, U : Any> Response<T>.getData( | |
cacheAction: (R) -> Unit, | |
fetchFromCacheAction: () -> R): Result<U> { | |
try { | |
onSuccess { | |
val databaseEntity = it.mapToRoomEntity() |
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
abstract class BaseViewModel<T : Any, E> : ViewModel(), KoinComponent { | |
protected val coroutineContext: CoroutineContextProvider by inject() | |
private val connectivity: Connectivity by inject() | |
protected val _viewState = MutableLiveData<ViewState<T>>() | |
val viewState: LiveData<ViewState<T>> | |
get() = _viewState | |
protected val _viewEffects = MutableLiveData<E>() |
OlderNewer