Skip to content

Instantly share code, notes, and snippets.

@reactivedroid
Created July 30, 2019 12:07
Show Gist options
  • Save reactivedroid/0f22f2e0392aed60640c6151ebff1450 to your computer and use it in GitHub Desktop.
Save reactivedroid/0f22f2e0392aed60640c6151ebff1450 to your computer and use it in GitHub Desktop.
@Singleton
class FavoriteShowsRepository @Inject
constructor(private val showDao: ShowDao) {
suspend fun allFavoriteShows(): List<FavoriteShow> {
return showDao.allFavouriteShows()
}
fun insertShowIntoFavorites(show: Show) {
val favoriteShow = FavoriteShow(
id = show.id,
name = show.name,
premiered = show.premiered,
imageUrl = show.image!!["original"],
summary = show.summary,
rating = show.rating!!["average"],
runtime = show.runtime!!,
isFavorite = true
)
CoroutineScope(Dispatchers.IO).launch { showDao.insert(favoriteShow) }
}
fun removeShowFromFavorites(show: Show) {
val favoriteShow = FavoriteShow(
id = show.id,
name = show.name,
premiered = show.premiered,
imageUrl = show.image!!["original"],
summary = show.summary,
rating = show.rating!!["average"],
runtime = show.runtime!!,
isFavorite = false
)
CoroutineScope(Dispatchers.IO).launch { showDao.remove(favoriteShow) }
}
fun insertIntoFavorites(favoriteShow: FavoriteShow) {
CoroutineScope(Dispatchers.IO).launch { showDao.insert(favoriteShow) }
}
fun removeFromFavorites(favoriteShow: FavoriteShow) {
CoroutineScope(Dispatchers.IO).launch { showDao.remove(favoriteShow) }
}
suspend fun allFavoriteShowIds(): List<Long> {
return showDao.getFavoriteShowIds()
}
fun clearAll() {
CoroutineScope(Dispatchers.IO).launch { showDao.clear() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment