Skip to content

Commit

Permalink
Fix logic to get local super heroes before remote if present
Browse files Browse the repository at this point in the history
  • Loading branch information
Serchinastico committed Jan 29, 2019
1 parent 36b3c67 commit f6956d8
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class SuperHeroRepository(
private val local: LocalSuperHeroDataSource,
private val remote: RemoteSuperHeroDataSource
) {
suspend fun getAllSuperHeroes(): List<SuperHero> {
return remote.getAllSuperHeroes()
.also { local.saveAll(it) }
}
suspend fun getAllSuperHeroes(): List<SuperHero> =
local.getAllSuperHeroes().ifEmpty {
remote.getAllSuperHeroes()
.also { local.saveAll(it) }
}

suspend fun get(id: String): SuperHero? {
return local.get(id)
suspend fun get(id: String): SuperHero? =
local.get(id)
?: remote.get(id)?.also { local.save(it) }
}

suspend fun save(superHero: SuperHero): SuperHero = coroutineScope {
listOf(
Expand Down

0 comments on commit f6956d8

Please sign in to comment.