From 5e02a43ba7abacd484daf07e50ad26973fb83240 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 6 Jul 2020 10:51:41 +0800 Subject: Fix notification interval not properly set for 15 and 30 mins. --- .../infinityforreddit/Activity/MainActivity.java | 14 +++++++------- .../infinityforreddit/PullNotificationWorker.java | 2 +- .../Settings/NotificationPreferenceFragment.java | 17 +++++++++-------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index 824a686d..1c840c63 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -322,7 +322,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { boolean enableNotification = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); long notificationInterval = Long.parseLong(mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); - TimeUnit timeUnit = notificationInterval == 15 || notificationInterval == 30 ? TimeUnit.MINUTES : TimeUnit.HOURS; + TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS; WorkManager workManager = WorkManager.getInstance(this); @@ -354,10 +354,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb .setConstraints(constraints) .build(); - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } else { - workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); } bindView(); @@ -380,10 +380,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb .setConstraints(constraints) .build(); - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } else { - workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); } bindView(); @@ -410,10 +410,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb .setConstraints(constraints) .build(); - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } else { - workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); } bindView(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 246f9119..ebabe07c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -42,7 +42,7 @@ import retrofit2.Response; import retrofit2.Retrofit; public class PullNotificationWorker extends Worker { - public static final String WORKER_TAG = "PNWT"; + public static final String UNIQUE_WORKER_NAME = "PNWT"; @Inject @Named("oauth_without_authenticator") Retrofit mOauthWithoutAuthenticatorRetrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java index f6644468..3060be41 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java @@ -53,6 +53,7 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat { enableNotification = sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); notificationInterval = Long.parseLong(sharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); + TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS; if (enableNotification) { if (notificationIntervalListPreference != null) { @@ -74,15 +75,15 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat { PeriodicWorkRequest pullNotificationRequest = new PeriodicWorkRequest.Builder(PullNotificationWorker.class, - notificationInterval, TimeUnit.HOURS) + notificationInterval, timeUnit) .setConstraints(constraints) - .setInitialDelay(notificationInterval, TimeUnit.HOURS) + .setInitialDelay(notificationInterval, timeUnit) .build(); - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); } else { - workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); } return true; }); @@ -99,15 +100,15 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat { PeriodicWorkRequest pullNotificationRequest = new PeriodicWorkRequest.Builder(PullNotificationWorker.class, - notificationInterval, TimeUnit.HOURS) + notificationInterval, timeUnit) .setConstraints(constraints) - .setInitialDelay(notificationInterval, TimeUnit.HOURS) + .setInitialDelay(notificationInterval, timeUnit) .build(); - workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME, ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); } else { - workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME); } return true; -- cgit v1.2.3