Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Last active May 2, 2019 02:50
Show Gist options
  • Save cdmunoz/8db036128c85d49ba79572126a8fe959 to your computer and use it in GitHub Desktop.
Save cdmunoz/8db036128c85d49ba79572126a8fe959 to your computer and use it in GitHub Desktop.
class AuctionsCreationViewModel : ViewModel(), CoroutineScope {
private var auction: MutableLiveData<Auction> = MutableLiveData()
private var auctionCreationRepository = AuctionCreationRepository()
private var onClickSaveTrigger = SingleLiveEvent<Unit>()
private val job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
fun getAuction() = auction
fun queryAuctionById(auctionId: Long) {
val auctionQueried = auctionCreationRepository.getAuctionById(auctionId)
//other logic regarding auction object
auction.value = auctionQueried
}
fun saveAuction(auction: Auction) {
launch(Dispatchers.Main) {
withContext(Dispatchers.IO) { auctionCreationRepository.save(mutableListOf(auction)) }
}
}
override fun onCleared() {
job.cancel()
}
fun getOnClickSaveTrigger(): SingleLiveEvent<Unit> {
return onClickSaveTrigger
}
fun onClickSaveEvent() {
onClickSaveTrigger.call()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment