From fb572dbb272e4e1d85aed3e31271bd1fa0b997e5 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Wed, 24 Aug 2022 13:34:05 +0200 Subject: [PATCH] Closes #36 --- .../android/HomeActivity.kt | 23 ++----------- .../android/background/background.kt | 31 +----------------- .../repository/RepositoryImpl.kt | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 50 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 722a8e4..87b5b67 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 @@ -318,7 +318,9 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar handleRecurringTask() - handleOfflineActions() + CoroutineScope(Dispatchers.Main).launch { + repository.handleDBActions() + } getElementsAccordingToTab() } @@ -1082,26 +1084,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } private fun handleOfflineActions() { - fun doAndReportOnFail(success: Boolean, action: ACTION) { - if (success) { - thread { - repository.deleteDBAction(action) - } - } - } - CoroutineScope(Dispatchers.Main).launch { - val actions = repository.getDBActions() - - actions.forEach { action -> - when { - action.read -> doAndReportOnFail(repository.markAsReadById(action.articleid.toInt()), action) - action.unread -> doAndReportOnFail(repository.unmarkAsReadById(action.articleid.toInt()), action) - action.starred -> doAndReportOnFail(repository.starrById(action.articleid.toInt()), action) - action.unstarred -> doAndReportOnFail(repository.unstarrById(action.articleid.toInt()), action) - } - } - } } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt index 1cbba10..907338d 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt @@ -56,28 +56,7 @@ override fun doWork(): Result { val notifyNewItems = settings.getBoolean("notify_new_items", false) - val actions: List = repository.getDBActions() - - actions.forEach { action -> - when { - action.read -> doAndReportOnFail( - repository.markAsReadById(action.articleid.toInt()), - action - ) - action.unread -> doAndReportOnFail( - repository.unmarkAsReadById(action.articleid.toInt()), - action - ) - action.starred -> doAndReportOnFail( - repository.starrById(action.articleid.toInt()), - action - ) - action.unstarred -> doAndReportOnFail( - repository.unstarrById(action.articleid.toInt()), - action - ) - } - } + repository.handleDBActions() launch { handleNewItemsNotification(repository.tryToCacheItemsAndGetNewOnes(), notifyNewItems, notificationManager) @@ -134,12 +113,4 @@ override fun doWork(): Result { } } } - - private fun doAndReportOnFail(result: Boolean, action: ACTION) { - if (result) { - thread { - repository.deleteDBAction(action) - } - } - } } \ 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 2344d20..6b7c824 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 @@ -434,4 +434,36 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } catch (e: Throwable) {} return emptyList() } + + suspend fun handleDBActions() { + + val actions: List = getDBActions() + + actions.forEach { action -> + when { + action.read -> doAndReportOnFail( + markAsReadById(action.articleid.toInt()), + action + ) + action.unread -> doAndReportOnFail( + unmarkAsReadById(action.articleid.toInt()), + action + ) + action.starred -> doAndReportOnFail( + starrById(action.articleid.toInt()), + action + ) + action.unstarred -> doAndReportOnFail( + unstarrById(action.articleid.toInt()), + action + ) + } + } + } + + private fun doAndReportOnFail(result: Boolean, action: ACTION) { + if (result) { + deleteDBAction(action) + } + } } \ No newline at end of file