mirror of https://github.com/readrops/Readrops.git
Fix account not having its credentials when syncing with the worker and catch all exceptions to avoid zombie notification
This commit is contained in:
parent
1e8e387d1b
commit
7314440534
|
@ -9,6 +9,7 @@ import androidx.core.app.NotificationCompat
|
|||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import com.readrops.api.services.SyncResult
|
||||
import com.readrops.app.R
|
||||
import com.readrops.app.ReadropsApp
|
||||
import com.readrops.app.activities.MainActivity
|
||||
|
@ -16,20 +17,22 @@ import com.readrops.app.repositories.ARepository
|
|||
import com.readrops.db.Database
|
||||
import com.readrops.db.entities.Item
|
||||
import com.readrops.db.entities.account.Account
|
||||
import com.readrops.api.services.SyncResult
|
||||
import io.reactivex.disposables.Disposable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) {
|
||||
|
||||
private lateinit var disposable: Disposable
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
private val notificationManager = NotificationManagerCompat.from(applicationContext)
|
||||
private val database = Database.getInstance(applicationContext)
|
||||
|
||||
override fun doWork(): Result {
|
||||
val accounts = database.accountDao().selectAll()
|
||||
var result = Result.success()
|
||||
val syncResults = mutableMapOf<Account, SyncResult>()
|
||||
|
||||
try {
|
||||
val accounts = database.accountDao().selectAll()
|
||||
|
||||
val notificationBuilder = NotificationCompat.Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID)
|
||||
.setContentTitle(applicationContext.getString(R.string.auto_synchro))
|
||||
|
@ -37,11 +40,13 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
|
|||
.setSmallIcon(R.drawable.ic_notif)
|
||||
.setOnlyAlertOnce(true)
|
||||
|
||||
val syncResults = mutableMapOf<Account, SyncResult>()
|
||||
accounts.forEach {
|
||||
notificationBuilder.setContentText(it.accountName)
|
||||
notificationManager.notify(SYNC_NOTIFICATION_ID, notificationBuilder.build())
|
||||
|
||||
it.login = SharedPreferencesManager.readString(applicationContext, it.loginKey)
|
||||
it.password = SharedPreferencesManager.readString(applicationContext, it.passwordKey)
|
||||
|
||||
val repository = ARepository.repositoryFactory(it, applicationContext)
|
||||
|
||||
disposable = repository.sync(null)
|
||||
|
@ -53,22 +58,27 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
|
|||
|
||||
if (repository.syncResult != null) syncResults[it] = repository.syncResult
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, e.message!!)
|
||||
result = Result.failure()
|
||||
} finally {
|
||||
notificationManager.cancel(SYNC_NOTIFICATION_ID)
|
||||
displaySyncResultNotif(syncResults)
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStopped() {
|
||||
super.onStopped()
|
||||
|
||||
disposable.dispose()
|
||||
disposable?.dispose()
|
||||
notificationManager.cancel(SYNC_NOTIFICATION_ID)
|
||||
}
|
||||
|
||||
private fun displaySyncResultNotif(syncResults: Map<Account, SyncResult>) {
|
||||
val notifContent = SyncResultAnalyser(applicationContext, syncResults, database).getSyncNotifContent()
|
||||
val notifContent = SyncResultAnalyser(applicationContext, syncResults, database)
|
||||
.getSyncNotifContent()
|
||||
|
||||
if (notifContent.title != null) {
|
||||
val intent = Intent(applicationContext, MainActivity::class.java).apply {
|
||||
|
@ -85,7 +95,8 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
|
|||
.setContentText(notifContent.content)
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(notifContent.content))
|
||||
.setSmallIcon(R.drawable.ic_notif)
|
||||
.setContentIntent(PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
|
||||
.setContentIntent(PendingIntent.getActivity(applicationContext, 0,
|
||||
intent, PendingIntent.FLAG_UPDATE_CURRENT))
|
||||
.setAutoCancel(true)
|
||||
|
||||
notifContent.item?.let {
|
||||
|
@ -100,8 +111,7 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
|
|||
notificationBuilder.setLargeIcon(it)
|
||||
}
|
||||
|
||||
notificationManager.notify(SYNC_RESULT_NOTIFICATION_ID,
|
||||
notificationBuilder.build())
|
||||
notificationManager.notify(SYNC_RESULT_NOTIFICATION_ID, notificationBuilder.build())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue