Manual sync for read/unread/star/unstar.

This commit is contained in:
Amine 2018-11-04 14:33:41 +01:00
parent d02b28b81f
commit f49256c72f

View File

@ -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.Item
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi
import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase 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_1_2
import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_2_3 import apps.amine.bou.readerforselfoss.persistence.migrations.MIGRATION_2_3
import apps.amine.bou.readerforselfoss.utils.Config 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 apps.amine.bou.readerforselfoss.utils.persistence.toEntity
import org.acra.ACRA
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
@ -27,58 +31,95 @@ import kotlin.concurrent.thread
class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params) { class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result { override fun doWork(): Result {
val notificationManager = if (context.isNetworkAccessible(null)) {
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationCompat.Builder(applicationContext, Config.syncChannelId) val notificationManager =
.setContentTitle(context.getString(R.string.loading_notification_title)) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
.setContentText(context.getString(R.string.loading_notification_text))
.setOngoing(true)
.setPriority(PRIORITY_LOW)
.setChannelId(Config.syncChannelId)
.setSmallIcon(R.drawable.ic_cloud_download)
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) notificationManager.notify(1, notification.build())
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context)
val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false)
val db = Room.databaseBuilder( val settings =
applicationContext, this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE)
AppDatabase::class.java, "selfoss-database" val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context)
).addMigrations(MIGRATION_1_2).addMigrations(MIGRATION_2_3).build() val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false)
val api = SelfossApi( val db = Room.databaseBuilder(
this.context, applicationContext,
null, AppDatabase::class.java, "selfoss-database"
settings.getBoolean("isSelfSignedCert", false), ).addMigrations(MIGRATION_1_2).addMigrations(MIGRATION_2_3).build()
shouldLogEverything
)
api.allItems().enqueue(object : Callback<List<Item>> {
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
Timer("", false).schedule(4000) {
notificationManager.cancel(1)
}
}
override fun onResponse( val api = SelfossApi(
call: Call<List<Item>>, this.context,
response: Response<List<Item>> null,
) { settings.getBoolean("isSelfSignedCert", false),
thread { shouldLogEverything
if (response.body() != null) { )
val apiItems = (response.body() as ArrayList<Item>)
db.itemsDao().deleteAllItems() api.allItems().enqueue(object : Callback<List<Item>> {
db.itemsDao() override fun onFailure(call: Call<List<Item>>, t: Throwable) {
.insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray())
}
Timer("", false).schedule(4000) { Timer("", false).schedule(4000) {
notificationManager.cancel(1) notificationManager.cancel(1)
} }
} }
override fun onResponse(
call: Call<List<Item>>,
response: Response<List<Item>>
) {
thread {
if (response.body() != null) {
val apiItems = (response.body() as ArrayList<Item>)
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 return Result.SUCCESS
} }
private fun <T> doAndReportOnFail(call: Call<T>, action: ActionEntity) {
call.enqueue(object : Callback<T> {
override fun onResponse(
call: Call<T>,
response: Response<T>
) {
thread {
db.actionsDao().delete(action)
}
}
override fun onFailure(call: Call<T>, t: Throwable) {
ACRA.getErrorReporter().maybeHandleSilentException(t, context)
}
})
}
} }