Skip to content

Instantly share code, notes, and snippets.

View LukaKordic's full-sized avatar

Luka Kordić LukaKordic

View GitHub Profile
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>()
class WeatherFragment : BaseFragment() {
private val viewModel: WeatherViewModel by viewModel()
override fun getLayout() = R.layout.fragment_weather
override fun viewReady() {
viewModel.getWeatherForLocation()
subscribeToData()
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
/**
* 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()
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>()