Created
August 6, 2022 12:17
-
-
Save krzdabrowski/21389d3c12ecd9f54b481825d9b311b7 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 RocketRepositoryImpl @Inject constructor( | |
private val rocketApi: RocketApi, | |
private val rocketDao: RocketDao | |
) : RocketRepository { | |
override fun getRockets(): Flow<List<Rocket>> { | |
return rocketDao | |
.getRockets() | |
.map { rocketsCached -> | |
rocketsCached.map { it.toDomainModel() } | |
} | |
.onEach { rockets -> | |
if (rockets.isEmpty()) { | |
refreshRockets() | |
} | |
} | |
} | |
override suspend fun refreshRockets() { | |
rocketApi | |
.getRockets() | |
.map { | |
it.toDomainModel().toEntityModel() | |
} | |
.also { | |
rocketDao.saveRockets(it) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment