Fix notification interval not properly set for 15 and 30 mins.

This commit is contained in:
Alex Ning 2020-07-06 10:51:41 +08:00
parent 845e016f0a
commit 5e02a43ba7
3 changed files with 17 additions and 16 deletions

View File

@ -322,7 +322,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
boolean enableNotification = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); boolean enableNotification = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true);
long notificationInterval = Long.parseLong(mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); 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); WorkManager workManager = WorkManager.getInstance(this);
@ -354,10 +354,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else { } else {
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
} }
bindView(); bindView();
@ -380,10 +380,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else { } else {
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
} }
bindView(); bindView();
@ -410,10 +410,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else { } else {
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
} }
bindView(); bindView();

View File

@ -42,7 +42,7 @@ import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class PullNotificationWorker extends Worker { public class PullNotificationWorker extends Worker {
public static final String WORKER_TAG = "PNWT"; public static final String UNIQUE_WORKER_NAME = "PNWT";
@Inject @Inject
@Named("oauth_without_authenticator") @Named("oauth_without_authenticator")
Retrofit mOauthWithoutAuthenticatorRetrofit; Retrofit mOauthWithoutAuthenticatorRetrofit;

View File

@ -53,6 +53,7 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
enableNotification = sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); enableNotification = sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true);
notificationInterval = Long.parseLong(sharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); notificationInterval = Long.parseLong(sharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1"));
TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS;
if (enableNotification) { if (enableNotification) {
if (notificationIntervalListPreference != null) { if (notificationIntervalListPreference != null) {
@ -74,15 +75,15 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
PeriodicWorkRequest pullNotificationRequest = PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, TimeUnit.HOURS) notificationInterval, timeUnit)
.setConstraints(constraints) .setConstraints(constraints)
.setInitialDelay(notificationInterval, TimeUnit.HOURS) .setInitialDelay(notificationInterval, timeUnit)
.build(); .build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
} else { } else {
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
} }
return true; return true;
}); });
@ -99,15 +100,15 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
PeriodicWorkRequest pullNotificationRequest = PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class, new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, TimeUnit.HOURS) notificationInterval, timeUnit)
.setConstraints(constraints) .setConstraints(constraints)
.setInitialDelay(notificationInterval, TimeUnit.HOURS) .setInitialDelay(notificationInterval, timeUnit)
.build(); .build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
} else { } else {
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
} }
return true; return true;