Home Activity, use the Repository

This commit is contained in:
davide 2022-07-24 14:02:48 +02:00
parent 0859854610
commit 77fada1b02
3 changed files with 20 additions and 16 deletions

View File

@ -747,7 +747,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
fun sourcesApiCall() { fun sourcesApiCall() {
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
val response = api.sources() val response = repository.getSources()
if (response != null) { if (response != null) {
sources = response sources = response
val apiDrawerData = DrawerData(tags, sources) val apiDrawerData = DrawerData(tags, sources)
@ -766,10 +766,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = api.tags() val tags = repository.getTags()
if (response != null) {
tags = response
}
sourcesApiCall() sourcesApiCall()
} }
} }
@ -1125,9 +1122,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) {
needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) {
Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show()
// TODO: Use Dispatchers.IO
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
val status = api.update() val updatedRemote = repository.updateRemote()
if (status != null && status.isSuccess) { if (updatedRemote) {
Toast.makeText( Toast.makeText(
this@HomeActivity, this@HomeActivity,
R.string.refresh_success_response, Toast.LENGTH_LONG R.string.refresh_success_response, Toast.LENGTH_LONG
@ -1219,8 +1217,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
} }
private fun handleOfflineActions() { private fun handleOfflineActions() {
fun doAndReportOnFail(call: SelfossModel.SuccessResponse?, action: ActionEntity) { fun doAndReportOnFail(success: Boolean, action: ActionEntity) {
if (call != null && call.isSuccess) { if (success) {
thread { thread {
db.actionsDao().delete(action) db.actionsDao().delete(action)
} }
@ -1233,10 +1231,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
actions.forEach { action -> actions.forEach { action ->
when { when {
action.read -> doAndReportOnFail(api.markAsRead(action.articleId), action) action.read -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.unread -> doAndReportOnFail(api.unmarkAsRead(action.articleId), action) action.unread -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.starred -> doAndReportOnFail(api.starr(action.articleId), action) action.starred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
action.unstarred -> doAndReportOnFail(api.unstarr(action.articleId), action) action.unstarred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action)
} }
} }
} }

View File

@ -30,7 +30,7 @@ interface Repository {
tags: String, tags: String,
filter: String): Boolean filter: String): Boolean
suspend fun deleteSource(id: Int): Boolean suspend fun deleteSource(id: Int): Boolean
fun updateRemote(): Boolean suspend fun updateRemote(): Boolean
suspend fun login(): Boolean suspend fun login(): Boolean
fun refreshLoginInformation() fun refreshLoginInformation()
} }

View File

@ -185,8 +185,14 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return success return success
} }
override fun updateRemote(): Boolean { override suspend fun updateRemote(): Boolean {
TODO("Not yet implemented") // TODO: Handle connectivity issues
val response = api.update()
return if (response != null) {
response.isSuccess
} else {
false
}
} }
override suspend fun login(): Boolean { override suspend fun login(): Boolean {