Display notification when sync worker is running

This commit is contained in:
Shinokuni 2020-02-05 11:00:28 +01:00
parent 6d3f517790
commit 287a20baa9
3 changed files with 24 additions and 0 deletions

View File

@ -16,6 +16,7 @@ public class ReadropsApp extends Application {
public static final String FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel";
public static final String OPML_EXPORT_CHANNEL_ID = "opmlExportChannel";
public static final String SYNC_CHANNEL_ID = "syncChannel";
@Override
public void onCreate() {
@ -44,10 +45,14 @@ public class ReadropsApp extends Application {
getString(R.string.opml_export), NotificationManager.IMPORTANCE_DEFAULT);
opmlExportChannel.setDescription(getString(R.string.opml_export_description));
NotificationChannel syncChannel = new NotificationChannel(SYNC_CHANNEL_ID,
getString(R.string.auto_synchro), NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(feedsColorsChannel);
manager.createNotificationChannel(opmlExportChannel);
manager.createNotificationChannel(syncChannel);
}
}
}

View File

@ -70,6 +70,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
if (interval != null) {
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(SyncWorker.class, interval.first, interval.second)
.addTag(SyncWorker.Companion.getTAG())
.setInitialDelay(15, TimeUnit.MINUTES)
.build();
workManager.enqueueUniquePeriodicWork(SyncWorker.Companion.getTAG(), ExistingPeriodicWorkPolicy.REPLACE, request);

View File

@ -1,8 +1,12 @@
package com.readrops.app.utils
import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.readrops.app.R
import com.readrops.app.ReadropsApp
import com.readrops.app.repositories.ARepository
import com.readrops.readropsdb.Database
import io.reactivex.disposables.Disposable
@ -16,7 +20,18 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
val accounts = database.accountDao().selectAll()
var result = Result.success()
val notificationBuilder = NotificationCompat.Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID)
.setContentTitle(applicationContext.getString(R.string.auto_synchro))
.setProgress(0, 0, true)
.setSmallIcon(R.drawable.ic_notif)
.setOnlyAlertOnce(true)
val notificationManager = NotificationManagerCompat.from(applicationContext)
accounts.forEach {
notificationBuilder.setContentText(it.accountName)
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build())
val repository = ARepository.repositoryFactory(it, applicationContext)
disposable = repository.sync(null)
@ -24,6 +39,8 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
.subscribe()
}
notificationManager.cancel(NOTIFICATION_ID)
return result
}
@ -34,5 +51,6 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
companion object {
val TAG = SyncWorker::class.java.simpleName
private const val NOTIFICATION_ID = 2
}
}