Changing theme is now available.

This commit is contained in:
Alex Ning
2019-08-22 11:52:27 +08:00
parent a2fe95912b
commit 0840b7144f
22 changed files with 236 additions and 192 deletions

View File

@@ -1,8 +1,11 @@
package Settings;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
@@ -12,6 +15,11 @@ import ml.docilealligator.infinityforreddit.ChangeNSFWEvent;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SharedPreferencesUtils;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
/**
* A simple {@link PreferenceFragmentCompat} subclass.
*/
@@ -21,11 +29,42 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat {
setPreferencesFromResource(R.xml.main_preferences, rootKey);
SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY);
ListPreference listPreference = findPreference(SharedPreferencesUtils.THEME_KEY);
if(nsfwSwitch != null) {
nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue));
return true;
});
}
boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
if(listPreference != null) {
if(systemDefault) {
listPreference.setEntries(R.array.settings_theme_q);
} else {
listPreference.setEntries(R.array.settings_theme);
}
listPreference.setOnPreferenceChangeListener((preference, newValue) -> {
int option = Integer.parseInt((String) newValue);
switch (option) {
case 0:
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO);
break;
case 1:
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES);
break;
case 2:
if(systemDefault) {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM);
} else {
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY);
}
}
return true;
});
}
}
}

View File

@@ -7,20 +7,14 @@ import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@@ -87,53 +81,34 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
ListenableFuture<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG);
try {
List<WorkInfo> list = workInfo.get();
if(list != null && list.size() != 0) {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
}
return true;
});
}
if(notificationIntervalListPreference != null) {
notificationIntervalListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
notificationInterval = (Long) newValue;
notificationIntervalListPreference.setOnPreferenceChangeListener((preference, newValue) -> {
notificationInterval = Long.parseLong((String) newValue);
if(enableNotification) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
if(enableNotification) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
ListenableFuture<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG);
try {
List<WorkInfo> list = workInfo.get();
if(list != null && list.size() != 0) {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
return true;
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
}
return true;
});
}
}