Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GithubUserRepository
@Singleton
class GithubUserRepository @Inject
constructor(val githubUserDao: GithubUserDao, val service: GithubService) {
@InjectPreference lateinit var profile: Preference_UserProfile
init {
Timber.d("Injection GithubUserRepository")
PreferenceComponent_PrefAppComponent.getInstance().inject(this)
}
fun refreshUser(user: String) {
profile.putName(user)
doAsync { githubUserDao.truncateGithubUser() }
}
fun loadUser(user: String): LiveData<Resource<GithubUser>> {
return object: NetworkBoundRepository<GithubUser, GithubUser>() {
override fun saveFetchData(item: GithubUser) {
doAsync { githubUserDao.insertGithubUser(item) }
}
override fun shouldFetch(data: GithubUser?): Boolean {
return data == null
}
override fun loadFromDb(): LiveData<GithubUser> {
return githubUserDao.getGithubUser(user)
}
override fun fetchService(): LiveData<ApiResponse<GithubUser>> {
return service.fetchGithubUser(user)
}
override fun onFetchFailed(envelope: Envelope?) {
Timber.d("onFetchFailed : $envelope")
}
}.asLiveData()
}
fun getUserKeyName() = profile.nameKeyName()
fun getPreferenceMenuPosition() = profile.menuPosition
fun putPreferenceMenuPosition(position: Int) { profile.putMenuPosition(position) }
fun getUserName() = profile.name
companion object {
const val per_page = 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment