Last active
April 30, 2021 01:52
-
-
Save labibmuhajir/be4fbb4e59fc6d0e86a5c2b3cb0cacff to your computer and use it in GitHub Desktop.
Network Bounce Resource
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 UserRepository @Inject constructor( | |
private val webservice: Webservice, | |
private val userDao: UserDao | |
) { | |
fun getUser(userId: String) = | |
object : NetworkBoundResource<User, User>() { | |
override suspend fun saveCallResult(item: User) { | |
userDao.save(item) | |
} | |
override fun shouldFetch(data: User?): Boolean { | |
return rateLimiter.canFetch(userId) && (data == null || !isFresh(data)) | |
} | |
override suspend fun loadFromDb(): Flow<User> { | |
return userDao.load(userId) | |
} | |
override fun createCall(): Flow<ApiResponse<User>> { | |
return webservice.getUser(userId) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source https://developer.android.com/jetpack/guide