From 77fada1b026e89f74c8fd1b0cf8e17b7a661dd9e Mon Sep 17 00:00:00 2001 From: davide Date: Sun, 24 Jul 2022 14:02:48 +0200 Subject: [PATCH] Home Activity, use the Repository --- .../android/HomeActivity.kt | 24 +++++++++---------- .../repository/Repository.kt | 2 +- .../repository/RepositoryImpl.kt | 10 ++++++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index 485d834..84e1360 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -747,7 +747,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar fun sourcesApiCall() { if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { CoroutineScope(Dispatchers.Main).launch { - val response = api.sources() + val response = repository.getSources() if (response != null) { sources = response val apiDrawerData = DrawerData(tags, sources) @@ -766,10 +766,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut) && updateSources) { CoroutineScope(Dispatchers.IO).launch { - val response = api.tags() - if (response != null) { - tags = response - } + val tags = repository.getTags() sourcesApiCall() } } @@ -1125,9 +1122,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar if (this@HomeActivity.isNetworkAvailable(null, offlineShortcut)) { needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() + // TODO: Use Dispatchers.IO CoroutineScope(Dispatchers.Main).launch { - val status = api.update() - if (status != null && status.isSuccess) { + val updatedRemote = repository.updateRemote() + if (updatedRemote) { Toast.makeText( this@HomeActivity, R.string.refresh_success_response, Toast.LENGTH_LONG @@ -1219,8 +1217,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } private fun handleOfflineActions() { - fun doAndReportOnFail(call: SelfossModel.SuccessResponse?, action: ActionEntity) { - if (call != null && call.isSuccess) { + fun doAndReportOnFail(success: Boolean, action: ActionEntity) { + if (success) { thread { db.actionsDao().delete(action) } @@ -1233,10 +1231,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar actions.forEach { action -> when { - action.read -> doAndReportOnFail(api.markAsRead(action.articleId), action) - action.unread -> doAndReportOnFail(api.unmarkAsRead(action.articleId), action) - action.starred -> doAndReportOnFail(api.starr(action.articleId), action) - action.unstarred -> doAndReportOnFail(api.unstarr(action.articleId), action) + action.read -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) + action.unread -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) + action.starred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) + action.unstarred -> doAndReportOnFail(repository.markAsRead(action.articleId.toInt()), action) } } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt index a1cb199..ad780c8 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt @@ -30,7 +30,7 @@ interface Repository { tags: String, filter: String): Boolean suspend fun deleteSource(id: Int): Boolean - fun updateRemote(): Boolean + suspend fun updateRemote(): Boolean suspend fun login(): Boolean fun refreshLoginInformation() } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index 0cbe107..e5f38f1 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -185,8 +185,14 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return success } - override fun updateRemote(): Boolean { - TODO("Not yet implemented") + override suspend fun updateRemote(): Boolean { + // TODO: Handle connectivity issues + val response = api.update() + return if (response != null) { + response.isSuccess + } else { + false + } } override suspend fun login(): Boolean {