Strip direct api access from background

This commit is contained in:
davide 2022-08-16 11:13:26 +02:00
parent 1fb16bcbdd
commit 38f1dbd34d
3 changed files with 15 additions and 29 deletions

View File

@ -15,8 +15,6 @@ import bou.amine.apps.readerforselfossv2.android.MainActivity
import bou.amine.apps.readerforselfossv2.android.MyApp import bou.amine.apps.readerforselfossv2.android.MyApp
import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.R
import bou.amine.apps.readerforselfossv2.android.model.preloadImages 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.database.AppDatabase
import bou.amine.apps.readerforselfossv2.android.persistence.entities.ActionEntity import bou.amine.apps.readerforselfossv2.android.persistence.entities.ActionEntity
import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 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.Config
import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable
import bou.amine.apps.readerforselfossv2.repository.Repository 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.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.utils.ItemType
import bou.amine.apps.readerforselfossv2.service.SearchService
import bou.amine.apps.readerforselfossv2.service.SelfossService
import bou.amine.apps.readerforselfossv2.utils.DateUtils
import com.russhwolf.settings.Settings import com.russhwolf.settings.Settings
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -46,24 +39,12 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con
lateinit var db: AppDatabase lateinit var db: AppDatabase
override val di by lazy { (applicationContext as MyApp).di } override val di by lazy { (applicationContext as MyApp).di }
private val apiDetailsService : ApiDetailsService by instance()
private val repository : Repository by instance() private val repository : Repository by instance()
override fun doWork(): Result { override fun doWork(): Result {
val settings = Settings() val settings = Settings()
val periodicRefresh = settings.getBoolean("periodic_refresh", false) val periodicRefresh = settings.getBoolean("periodic_refresh", false)
if (periodicRefresh) { 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()) { if (context.isNetworkAvailable()) {
@ -95,19 +76,19 @@ override fun doWork(): Result {
actions.forEach { action -> actions.forEach { action ->
when { when {
action.read -> doAndReportOnFail( action.read -> doAndReportOnFail(
api.markAsRead(action.articleId), repository.markAsRead(action.articleId.toInt()),
action action
) )
action.unread -> doAndReportOnFail( action.unread -> doAndReportOnFail(
api.unmarkAsRead(action.articleId), repository.unmarkAsRead(action.articleId.toInt()),
action action
) )
action.starred -> doAndReportOnFail( action.starred -> doAndReportOnFail(
api.starr(action.articleId), repository.starr(action.articleId.toInt()),
action action
) )
action.unstarred -> doAndReportOnFail( action.unstarred -> doAndReportOnFail(
api.unstarr(action.articleId), repository.unstarr(action.articleId.toInt()),
action action
) )
} }
@ -116,10 +97,10 @@ override fun doWork(): Result {
if (context.isNetworkAvailable()) { if (context.isNetworkAvailable()) {
launch { launch {
try { try {
val newItems = service.allNewItems() val newItems = repository.allItems(ItemType.UNREAD)
handleNewItemsNotification(newItems, notifyNewItems, notificationManager) handleNewItemsNotification(newItems, notifyNewItems, notificationManager)
val readItems = service.allReadItems() val readItems = repository.allItems(ItemType.ALL)
val starredItems = service.allStarredItems() val starredItems = repository.allItems(ItemType.STARRED)
// TODO: save all to DB // TODO: save all to DB
} catch (e: Throwable) {} } catch (e: Throwable) {}
} }
@ -178,8 +159,9 @@ override fun doWork(): Result {
} }
} }
private fun doAndReportOnFail(result: SelfossModel.SuccessResponse?, action: ActionEntity) { private fun doAndReportOnFail(result: Boolean, action: ActionEntity) {
if (result != null && result.isSuccess) { // TODO: Failures should be reported
if (result) {
thread { thread {
db.actionsDao().delete(action) db.actionsDao().delete(action)
} }

View File

@ -28,6 +28,7 @@ interface Repository {
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> suspend fun getNewerItems(): ArrayList<SelfossModel.Item>
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> suspend fun getOlderItems(): ArrayList<SelfossModel.Item>
suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>?
suspend fun reloadBadges(): Boolean suspend fun reloadBadges(): Boolean
suspend fun getTags(): List<SelfossModel.Tag>? suspend fun getTags(): List<SelfossModel.Tag>?
suspend fun getSpouts(): Map<String, SelfossModel.Spout>? suspend fun getSpouts(): Map<String, SelfossModel.Spout>?

View File

@ -75,6 +75,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return items return items
} }
override suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? =
api.getItems(itemType.type, 200, 0, tagFilter?.tag, sourceFilter?.id?.toLong(), searchFilter)
private fun appendItems(fetchedItems: List<SelfossModel.Item>) { private fun appendItems(fetchedItems: List<SelfossModel.Item>) {
// TODO: Store in DB if enabled by user // TODO: Store in DB if enabled by user
val fetchedIDS = fetchedItems.map { it.id } val fetchedIDS = fetchedItems.map { it.id }