Skip to content

Commit

Permalink
Add launch extension on ViewModel class
Browse files Browse the repository at this point in the history
Now we can just call launch{} to launch new coroutine inside the viewModelScope with IO dispatcher as default
  • Loading branch information
LukaKordic committed Aug 30, 2019
1 parent acbdfcd commit 5074bb0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.*
import com.cobeisfresh.template.common.coroutine.CoroutineContextProvider
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

inline fun <T> LiveData<T>.subscribe(owner: LifecycleOwner, crossinline onDataReceived: (T) -> Unit) =
observe(owner, Observer { onDataReceived(it) })
Expand Down Expand Up @@ -48,4 +51,10 @@ fun FragmentActivity.showFragment(fragment: Fragment, @IdRes container: Int, add

fun FragmentActivity.goBack() {
supportFragmentManager.popBackStack()
}

inline fun ViewModel.launch(
coroutineContext: CoroutineContext = CoroutineContextProvider().io,
crossinline block: suspend CoroutineScope.() -> Unit): Job {
return viewModelScope.launch(coroutineContext) { block() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@ package com.cobeisfresh.template.ui.weather.presentation
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cobeisfresh.template.common.DEFAULT_CITY_NAME
import com.cobeisfresh.template.common.coroutine.CoroutineContextProvider
import com.cobeisfresh.template.common.extensions.launch
import com.cobeisfresh.template.ui.base.ViewState
import com.example.domain.interaction.weather.GetWeatherUseCase
import com.example.domain.model.WeatherInfo
import com.example.domain.model.onFailure
import com.example.domain.model.onSuccess
import kotlinx.coroutines.launch
import org.koin.core.KoinComponent

class WeatherViewModel(private val getWeather: GetWeatherUseCase,
private val coroutineContextProvider: CoroutineContextProvider) : ViewModel() {
class WeatherViewModel(private val getWeather: GetWeatherUseCase) : ViewModel() {

// we make this private and provide only immutable live data to observers so they can't change anything
private val _weatherLiveData = MutableLiveData<ViewState<WeatherInfo>>()
val weatherLiveData: LiveData<ViewState<WeatherInfo>>
get() = _weatherLiveData

fun getWeatherForLocation(location: String = DEFAULT_CITY_NAME) =
viewModelScope.launch(coroutineContextProvider.main) {
launch {
_weatherLiveData.value = ViewState.loading()
getWeather(location)
.onSuccess { _weatherLiveData.value = ViewState.success(it) }
.onFailure { _weatherLiveData.value = ViewState.error(it.throwable) }
.onSuccess { _weatherLiveData.postValue(ViewState.success(it)) }
.onFailure { _weatherLiveData.postValue(ViewState.error(it.throwable)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.example.domain.model.WeatherInfo

@Entity(tableName = WEATHER_TABLE_NAME)
data class WeatherEntity(@PrimaryKey val id: Int? = 0,
val weather: ArrayList<Weather>?,
val weather: List<Weather>?,
@Embedded
val main: MainInfo?,
val name: String? = "") : DomainMapper<WeatherInfo> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.example.data.database.model.WeatherEntity
import com.example.data.networking.base.RoomMapper

data class WeatherInfoResponse(val id: Int? = 0,
val weather: ArrayList<Weather>?,
val weather: List<Weather>?,
val main: MainInfo?,
val name: String? = "") : RoomMapper<WeatherEntity> {
override fun mapToRoomEntity() = WeatherEntity(id ?: 0, weather ?: arrayListOf(), main ?: MainInfo(), name)
override fun mapToRoomEntity() = WeatherEntity(id ?: 0, weather ?: emptyList(), main ?: MainInfo(), name)
}

data class MainInfo(val temp: Double? = 0.0,
Expand Down

0 comments on commit 5074bb0

Please sign in to comment.