Skip to content

Commit

Permalink
Make the dark mode override 'stick' (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
millanp authored and aaronbrethorst committed Jan 18, 2024
1 parent 015b3fa commit 791370a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import edu.usf.cutr.open311client.models.Open311Option;

import static com.google.android.gms.location.LocationServices.getFusedLocationProviderClient;
import static org.onebusaway.android.util.UIUtils.setAppTheme;

public class Application extends MultiDexApplication {

Expand Down Expand Up @@ -436,6 +437,7 @@ private void initOba() {
}

checkArrivalStylePreferenceDefault();
checkDarkMode();

// Get the current app version.
PackageManager pm = getPackageManager();
Expand Down Expand Up @@ -481,6 +483,15 @@ private void checkArrivalStylePreferenceDefault() {
}
}

private void checkDarkMode() {
String appThemePrefKey = getResources()
.getString(R.string.preference_key_app_theme);
String appThemePref = mPrefs.getString(appThemePrefKey, null);
if (appThemePref != null) {
setAppTheme(appThemePref);
}
}

private void initObaRegion() {
// Read the region preference, look it up in the DB, then set the region.
long id = mPrefs.getLong(getString(R.string.preference_key_region), -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.onebusaway.android.util.PermissionUtils.RESTORE_BACKUP_PERMISSION_REQUEST;
import static org.onebusaway.android.util.PermissionUtils.SAVE_BACKUP_PERMISSION_REQUEST;
import static org.onebusaway.android.util.PermissionUtils.STORAGE_PERMISSIONS;
import static org.onebusaway.android.util.UIUtils.setAppTheme;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand Down Expand Up @@ -666,16 +667,4 @@ public void onRegionTaskFinished(boolean currentRegionChanged) {
NavHelp.goHome(this, false);
}
}

private void setAppTheme(String themeValue) {
if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_system_default))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_dark))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_light))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.core.content.pm.ShortcutInfoCompat;
Expand Down Expand Up @@ -2010,4 +2011,16 @@ public static void openBatteryIgnoreIntent(Activity activity) {
intent.setData(Uri.parse("package:" + activity.getPackageName()));
activity.startActivity(intent);
}

public static void setAppTheme(String themeValue) {
if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_system_default))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_dark))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
if (themeValue.equalsIgnoreCase(Application.get().getString(R.string.preferences_app_theme_option_light))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}

0 comments on commit 791370a

Please sign in to comment.