From 38f1dbd34dd497a8293a57628766cc64152858a3 Mon Sep 17 00:00:00 2001 From: davide Date: Tue, 16 Aug 2022 11:13:26 +0200 Subject: [PATCH] Strip direct api access from background --- .../android/background/background.kt | 40 +++++-------------- .../repository/Repository.kt | 1 + .../repository/RepositoryImpl.kt | 3 ++ 3 files changed, 15 insertions(+), 29 deletions(-) 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 e853ee6..860d2c1 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 @@ -15,8 +15,6 @@ import bou.amine.apps.readerforselfossv2.android.MainActivity import bou.amine.apps.readerforselfossv2.android.MyApp import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.model.preloadImages -import bou.amine.apps.readerforselfossv2.android.persistence.AndroidDeviceDatabase -import bou.amine.apps.readerforselfossv2.android.persistence.AndroidDeviceDatabaseService import bou.amine.apps.readerforselfossv2.android.persistence.database.AppDatabase import bou.amine.apps.readerforselfossv2.android.persistence.entities.ActionEntity import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 @@ -25,13 +23,8 @@ import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATIO import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.repository.Repository - -import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.rest.SelfossModel -import bou.amine.apps.readerforselfossv2.service.ApiDetailsService -import bou.amine.apps.readerforselfossv2.service.SearchService -import bou.amine.apps.readerforselfossv2.service.SelfossService -import bou.amine.apps.readerforselfossv2.utils.DateUtils +import bou.amine.apps.readerforselfossv2.utils.ItemType import com.russhwolf.settings.Settings import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -46,24 +39,12 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con lateinit var db: AppDatabase override val di by lazy { (applicationContext as MyApp).di } - private val apiDetailsService : ApiDetailsService by instance() private val repository : Repository by instance() override fun doWork(): Result { val settings = Settings() val periodicRefresh = settings.getBoolean("periodic_refresh", false) if (periodicRefresh) { - val api = SelfossApiImpl( -// this.context, -// null, -// settings.getBoolean("isSelfSignedCert", false), -// sharedPref.getString("api_timeout", "-1")!!.toLong() - apiDetailsService - ) - - val dateUtils = DateUtils(repository.apiMajorVersion) - val searchService = SearchService(dateUtils) - val service = SelfossService(api, AndroidDeviceDatabaseService(AndroidDeviceDatabase(applicationContext), searchService), searchService) if (context.isNetworkAvailable()) { @@ -95,19 +76,19 @@ override fun doWork(): Result { actions.forEach { action -> when { action.read -> doAndReportOnFail( - api.markAsRead(action.articleId), + repository.markAsRead(action.articleId.toInt()), action ) action.unread -> doAndReportOnFail( - api.unmarkAsRead(action.articleId), + repository.unmarkAsRead(action.articleId.toInt()), action ) action.starred -> doAndReportOnFail( - api.starr(action.articleId), + repository.starr(action.articleId.toInt()), action ) action.unstarred -> doAndReportOnFail( - api.unstarr(action.articleId), + repository.unstarr(action.articleId.toInt()), action ) } @@ -116,10 +97,10 @@ override fun doWork(): Result { if (context.isNetworkAvailable()) { launch { try { - val newItems = service.allNewItems() + val newItems = repository.allItems(ItemType.UNREAD) handleNewItemsNotification(newItems, notifyNewItems, notificationManager) - val readItems = service.allReadItems() - val starredItems = service.allStarredItems() + val readItems = repository.allItems(ItemType.ALL) + val starredItems = repository.allItems(ItemType.STARRED) // TODO: save all to DB } catch (e: Throwable) {} } @@ -178,8 +159,9 @@ override fun doWork(): Result { } } - private fun doAndReportOnFail(result: SelfossModel.SuccessResponse?, action: ActionEntity) { - if (result != null && result.isSuccess) { + private fun doAndReportOnFail(result: Boolean, action: ActionEntity) { + // TODO: Failures should be reported + if (result) { thread { db.actionsDao().delete(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 a64393e..e926a0b 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 @@ -28,6 +28,7 @@ interface Repository { suspend fun getNewerItems(): ArrayList suspend fun getOlderItems(): ArrayList + suspend fun allItems(itemType: ItemType): List? suspend fun reloadBadges(): Boolean suspend fun getTags(): List? suspend fun getSpouts(): Map? 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 2208fb6..f053b42 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 @@ -75,6 +75,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet return items } + override suspend fun allItems(itemType: ItemType): List? = + api.getItems(itemType.type, 200, 0, tagFilter?.tag, sourceFilter?.id?.toLong(), searchFilter) + private fun appendItems(fetchedItems: List) { // TODO: Store in DB if enabled by user val fetchedIDS = fetchedItems.map { it.id }