From e6b5ea4e67c6f926716d0768895f7197490bcb35 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 12 Jan 2021 03:55:47 +0100 Subject: [PATCH] Download read and starred items (#331) * Save read and starred articles during background sync * Use getItems * Cache images of read articles * Remove unused function * Refactor functions --- .../bou/readerforselfoss/HomeActivity.kt | 52 +++++++-- .../api/selfoss/SelfossApi.kt | 9 ++ .../readerforselfoss/background/background.kt | 106 ++++++++++++------ 3 files changed, 121 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index bd2cd9e..37eff19 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -357,7 +357,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { } private fun getAndStoreAllItems() { - api.allItems().enqueue(object : Callback> { + api.allNewItems().enqueue(object : Callback> { override fun onFailure(call: Call>, t: Throwable) { } @@ -365,18 +365,48 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { call: Call>, response: Response> ) { - thread { - if (response.body() != null) { - val apiItems = (response.body() as ArrayList).filter { - maybeTagFilter != null || filter(it.tags.tags) - } as ArrayList - db.itemsDao().deleteAllItems() - db.itemsDao() - .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) - } - } + enqueueArticles(response, true) } }) + + api.allReadItems().enqueue(object : Callback> { + override fun onFailure(call: Call>, t: Throwable) { + } + + override fun onResponse( + call: Call>, + response: Response> + ) { + enqueueArticles(response, false) + } + }) + + api.allStarredItems().enqueue(object : Callback> { + override fun onFailure(call: Call>, t: Throwable) { + } + + override fun onResponse( + call: Call>, + response: Response> + ) { + enqueueArticles(response, false) + } + }) + } + + private fun enqueueArticles(response: Response>, clearDatabase: Boolean) { + thread { + if (response.body() != null) { + val apiItems = (response.body() as ArrayList).filter { + maybeTagFilter != null || filter(it.tags.tags) + } as ArrayList + if (clearDatabase) { + db.itemsDao().deleteAllItems() + } + db.itemsDao() + .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) + } + } } override fun onStop() { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index f0a28ec..47197ab 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -172,6 +172,15 @@ class SelfossApi( fun allItems(): Call> = service.allItems(userName, password) + fun allNewItems(): Call> = + getItems("unread", null, null, null, 200, 0) + + fun allReadItems(): Call> = + getItems("read", null, null, null, 200, 0) + + fun allStarredItems(): Call> = + getItems("read", null, null, null, 200, 0) + private fun getItems( type: String, tag: String?, diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt index b2b5beb..7566c08 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt @@ -66,7 +66,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con sharedPref.getString("api_timeout", "-1")!!.toLong() ) - api.allItems().enqueue(object : Callback> { + api.allNewItems().enqueue(object : Callback> { override fun onFailure(call: Call>, t: Throwable) { Timer("", false).schedule(4000) { notificationManager.cancel(1) @@ -77,42 +77,38 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con call: Call>, response: Response> ) { - thread { - if (response.body() != null) { - val apiItems = (response.body() as ArrayList) - db.itemsDao().deleteAllItems() - db.itemsDao() - .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) - - val newSize = apiItems.filter { it.unread }.size - if (notifyNewItems && newSize > 0) { - - val intent = Intent(context, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - } - val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0) - - val newItemsNotification = NotificationCompat.Builder(applicationContext, Config.newItemsChannelId) - .setContentTitle(context.getString(R.string.new_items_notification_title)) - .setContentText(context.getString(R.string.new_items_notification_text, newSize)) - .setPriority(PRIORITY_DEFAULT) - .setChannelId(Config.newItemsChannelId) - .setContentIntent(pendingIntent) - .setAutoCancel(true) - .setSmallIcon(R.drawable.ic_tab_fiber_new_black_24dp) - - Timer("", false).schedule(4000) { - notificationManager.notify(2, newItemsNotification.build()) - } - } - apiItems.map {it.preloadImages(context)} - } - Timer("", false).schedule(4000) { - notificationManager.cancel(1) - } - } + storeItems(response, true, notifyNewItems, notificationManager) } }) + api.allReadItems().enqueue(object : Callback> { + override fun onFailure(call: Call>, t: Throwable) { + Timer("", false).schedule(4000) { + notificationManager.cancel(1) + } + } + + override fun onResponse( + call: Call>, + response: Response> + ) { + storeItems(response, false, notifyNewItems, notificationManager) + } + }) + api.allStarredItems().enqueue(object : Callback> { + override fun onFailure(call: Call>, t: Throwable) { + Timer("", false).schedule(4000) { + notificationManager.cancel(1) + } + } + + override fun onResponse( + call: Call>, + response: Response> + ) { + storeItems(response, false, notifyNewItems, notificationManager) + } + }) + thread { val actions = db.actionsDao().actions() @@ -132,6 +128,46 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con return Result.success() } + private fun storeItems(response: Response>, newItems: Boolean, notifyNewItems: Boolean, notificationManager: NotificationManager) { + thread { + if (response.body() != null) { + val apiItems = (response.body() as ArrayList) + + if (newItems) { + db.itemsDao().deleteAllItems() + } + db.itemsDao() + .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) + + val newSize = apiItems.filter { it.unread }.size + if (newItems && notifyNewItems && newSize > 0) { + + val intent = Intent(context, MainActivity::class.java).apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0) + + val newItemsNotification = NotificationCompat.Builder(applicationContext, Config.newItemsChannelId) + .setContentTitle(context.getString(R.string.new_items_notification_title)) + .setContentText(context.getString(R.string.new_items_notification_text, newSize)) + .setPriority(PRIORITY_DEFAULT) + .setChannelId(Config.newItemsChannelId) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + .setSmallIcon(R.drawable.ic_tab_fiber_new_black_24dp) + + Timer("", false).schedule(4000) { + notificationManager.notify(2, newItemsNotification.build()) + } + } + apiItems.map { it.preloadImages(context) } + } + Timer("", false).schedule(4000) { + notificationManager.cancel(1) + } + } + } + private fun doAndReportOnFail(call: Call, action: ActionEntity) { call.enqueue(object : Callback { override fun onResponse(