From f49256c72f6a6fc22f863688a5d3335ed8c2f7b8 Mon Sep 17 00:00:00 2001 From: Amine Date: Sun, 4 Nov 2018 14:33:41 +0100 Subject: [PATCH] Manual sync for read/unread/star/unstar. --- .../readerforselfoss/background/background.kt | 123 ++++++++++++------ 1 file changed, 82 insertions(+), 41 deletions(-) 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 d96235e..e63edb1 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 @@ -13,10 +13,14 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase +import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_1_2 import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_2_3 import apps.amine.bou.readerforselfoss.utils.Config +import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException +import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible import apps.amine.bou.readerforselfoss.utils.persistence.toEntity +import org.acra.ACRA import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -27,58 +31,95 @@ import kotlin.concurrent.thread class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params) { override fun doWork(): Result { - val notificationManager = - applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + if (context.isNetworkAccessible(null)) { - val notification = NotificationCompat.Builder(applicationContext, Config.syncChannelId) - .setContentTitle(context.getString(R.string.loading_notification_title)) - .setContentText(context.getString(R.string.loading_notification_text)) - .setOngoing(true) - .setPriority(PRIORITY_LOW) - .setChannelId(Config.syncChannelId) - .setSmallIcon(R.drawable.ic_cloud_download) + val notificationManager = + applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - notificationManager.notify(1, notification.build()) + val notification = NotificationCompat.Builder(applicationContext, Config.syncChannelId) + .setContentTitle(context.getString(R.string.loading_notification_title)) + .setContentText(context.getString(R.string.loading_notification_text)) + .setOngoing(true) + .setPriority(PRIORITY_LOW) + .setChannelId(Config.syncChannelId) + .setSmallIcon(R.drawable.ic_cloud_download) - val settings = this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) - val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context) - val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false) + notificationManager.notify(1, notification.build()) - val db = Room.databaseBuilder( - applicationContext, - AppDatabase::class.java, "selfoss-database" - ).addMigrations(MIGRATION_1_2).addMigrations(MIGRATION_2_3).build() + val settings = + this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) + val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context) + val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false) - val api = SelfossApi( - this.context, - null, - settings.getBoolean("isSelfSignedCert", false), - shouldLogEverything - ) - api.allItems().enqueue(object : Callback> { - override fun onFailure(call: Call>, t: Throwable) { - Timer("", false).schedule(4000) { - notificationManager.cancel(1) - } - } + val db = Room.databaseBuilder( + applicationContext, + AppDatabase::class.java, "selfoss-database" + ).addMigrations(MIGRATION_1_2).addMigrations(MIGRATION_2_3).build() - override fun onResponse( - 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 api = SelfossApi( + this.context, + null, + settings.getBoolean("isSelfSignedCert", false), + shouldLogEverything + ) + + api.allItems().enqueue(object : Callback> { + override fun onFailure(call: Call>, t: Throwable) { Timer("", false).schedule(4000) { notificationManager.cancel(1) } } + + override fun onResponse( + 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()) + } + Timer("", false).schedule(4000) { + notificationManager.cancel(1) + } + } + } + }) + thread { + val actions = db.actionsDao().actions() + + actions.forEach { action -> + when { + action.read -> doAndReportOnFail(api.markItem(action.articleId), action) + action.unread -> doAndReportOnFail(api.unmarkItem(action.articleId), action) + action.starred -> doAndReportOnFail(api.starrItem(action.articleId), action) + action.unstarred -> doAndReportOnFail( + api.unstarrItem(action.articleId), + action + ) + } + } } - }) + } return Result.SUCCESS } + + private fun doAndReportOnFail(call: Call, action: ActionEntity) { + call.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + thread { + db.actionsDao().delete(action) + } + } + + override fun onFailure(call: Call, t: Throwable) { + ACRA.getErrorReporter().maybeHandleSilentException(t, context) + } + }) + } } \ No newline at end of file