Skip to content

Instantly share code, notes, and snippets.

@leocruzdev
Created July 11, 2019 18:08
Show Gist options
  • Save leocruzdev/92f660ede513a4e4fdc037d0a3a5f6f7 to your computer and use it in GitHub Desktop.
Save leocruzdev/92f660ede513a4e4fdc037d0a3a5f6f7 to your computer and use it in GitHub Desktop.
repository
package com.dacruzl2.gitmvpkoin.data.source
import android.util.Log
import com.dacruzl2.gitmvpkoin.data.Root
import com.dacruzl2.gitmvpkoin.data.source.local.GithubDao
import com.dacruzl2.gitmvpkoin.data.source.remote.services.GithubService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class GithubRepository(
private val remoteDataSource: GithubService,
private val localDataSource: GithubDao
) : GithubDataSource {
override suspend fun getIssues(onSuccess: (issues: List<Root>) -> Unit) {
withContext(Dispatchers.IO) {
try {
val request = remoteDataSource.getIssues().await()
if (request.isSuccessful) {
withContext(Dispatchers.Main) {
val issuesList = request.body()!!
onSuccess(issuesList)
withContext(Dispatchers.IO) {
Log.d("DB", "initSaveDB")
localDataSource.saveIssues(issuesList)
Log.d("DB", "DBsaved: $issuesList")
}
}
} else {
}
} catch (t: Exception) {
}
}
}
override suspend fun saveIssues(issues: List<Root>) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment