Last active
October 18, 2018 18:12
-
-
Save vferreirati/ad28a3ed921c5d1633249c3b4863edba to your computer and use it in GitHub Desktop.
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 BeerListViewModel (private val beerRepository: BeerRepository): ViewModel(), CoroutineScope { | |
private var job = Job() | |
override val coroutineContext: CoroutineContext | |
get() = Dispatchers.IO + job | |
fun getBeers(): LiveData<List<BeerResponse>> { | |
val beers = MutableLiveData<List<BeerResponse>>() | |
launch { | |
beers.postValue(beerRepository.getBeers()) | |
} | |
return beers | |
} | |
fun onDestroy() { | |
job.cancel() | |
} | |
} |
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 BeerRepository @Inject constructor(private val beerDao: BeerDao, | |
private val punkService: PunkService) { | |
suspend fun getBeers(): List<BeerResponse>? { | |
val response = punkService.getBeers().await() | |
return response.body() | |
} | |
suspend fun getBeerById(beerId: Int): BeerResponse? { | |
val response = punkService.getBeerById(beerId).await() | |
return response.body()?.get(0) | |
} |
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 DetailViewModel (private val beerRepository: BeerRepository): ViewModel(), CoroutineScope { | |
private var job = Job() | |
override val coroutineContext: CoroutineContext | |
get() = Dispatchers.IO + job | |
fun getBeer(beerId: Int): LiveData<BeerResponse> { | |
val beer = MutableLiveData<BeerResponse>() | |
launch { | |
beer.postValue(beerRepository.getBeerById(beerId)) | |
} | |
return beer | |
} | |
fun onDestroy() { | |
job.cancel() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment