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 -> {
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();

View File

@ -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;

View File

@ -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;