Skip to content

Commit

Permalink
Add preference for Theme, enabling Dark Mode #1022 (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
k7lim authored Oct 22, 2023
1 parent 780d2f6 commit 3803264
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
Expand All @@ -52,6 +53,7 @@
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment;

Expand All @@ -65,6 +67,7 @@
import com.amazon.geo.mapsv2.model.CameraPosition;
import com.amazon.geo.mapsv2.model.LatLng;
import com.amazon.geo.mapsv2.model.LatLngBounds;
import com.amazon.geo.mapsv2.model.MapStyleOptions;
import com.amazon.geo.mapsv2.model.Marker;
import com.amazon.geo.mapsv2.model.Polyline;
import com.amazon.geo.mapsv2.model.PolylineOptions;
Expand Down Expand Up @@ -333,6 +336,15 @@ public void onMapReady(com.amazon.geo.mapsv2.AmazonMap map) {
mMap.setOnMarkerClickListener(mapClickListeners);
mMap.setOnMapClickListener(mapClickListeners);

if (
AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES || (
AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_NO &&
(getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
)
) {
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.dark_map));
}

initMap(mLastSavedInstanceState);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
Expand All @@ -41,6 +42,7 @@
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment;

Expand All @@ -54,6 +56,7 @@
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
Expand Down Expand Up @@ -322,6 +325,15 @@ public void onMapReady(com.google.android.gms.maps.GoogleMap map) {
mMap.setOnMarkerClickListener(mapClickListeners);
mMap.setOnMapClickListener(mapClickListeners);

if (
AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES || (
AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_NO &&
(getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
)
) {
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.dark_map));
}

initMap(mLastSavedInstanceState);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;

Expand Down Expand Up @@ -117,6 +118,8 @@ public class PreferencesActivity extends PreferenceActivity

ListPreference preferredUnits;

ListPreference mThemePref;

private FirebaseAnalytics mFirebaseAnalytics;

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -188,6 +191,10 @@ public void onCreate(Bundle savedInstanceState) {
preferredUnits = (ListPreference) findPreference(
getString(R.string.preference_key_preferred_units));

mThemePref = (ListPreference) findPreference(
getString(R.string.preference_key_app_theme));
mThemePref.setOnPreferenceChangeListener(this);

settings.registerOnSharedPreferenceChangeListener(this);

PreferenceScreen preferenceScreen = getPreferenceScreen();
Expand Down Expand Up @@ -236,6 +243,7 @@ protected void onResume() {

changePreferenceSummary(getString(R.string.preference_key_region));
changePreferenceSummary(getString(R.string.preference_key_preferred_units));
changePreferenceSummary(getString(R.string.preference_key_app_theme));
changePreferenceSummary(getString(R.string.preference_key_otp_api_url));

// Remove preferences for notifications if no trip planning
Expand Down Expand Up @@ -300,6 +308,9 @@ private void changePreferenceSummary(String preferenceKey) {
} else if (preferenceKey
.equalsIgnoreCase(getString(R.string.preference_key_preferred_units))) {
preferredUnits.setSummary(preferredUnits.getValue());
} else if (preferenceKey
.equalsIgnoreCase(getString(R.string.preference_key_app_theme))) {
mThemePref.setSummary(mThemePref.getValue());
} else if (preferenceKey
.equalsIgnoreCase(getString(R.string.preference_key_otp_api_url))) {
String customOtpApiUrl = Application.get().getCustomOtpApiUrl();
Expand Down Expand Up @@ -567,6 +578,11 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
} else if (key.equalsIgnoreCase(getString(R.string.preference_key_preferred_units))) {
// Change the preferred units description
changePreferenceSummary(key);
} else if (key.equalsIgnoreCase(getString(R.string.preference_key_app_theme))) {
// Change the app theme preference description
changePreferenceSummary(key);
// Update the app theme
setAppTheme(settings.getString(key, getString(R.string.preferences_app_theme_option_system_default)));
} else if (key.equalsIgnoreCase(getString(R.string.preference_key_auto_select_region))) {
//Analytics
boolean autoSelect = settings
Expand Down Expand Up @@ -650,4 +666,16 @@ 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);
}
}
}
191 changes: 191 additions & 0 deletions onebusaway-android/src/main/res/raw/dark_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
[
{
"featureType": "all",
"elementType": "geometry",
"stylers": [
{
"color": "#242f3e"
}
]
},
{
"featureType": "all",
"elementType": "labels.text.stroke",
"stylers": [
{
"lightness": -80
}
]
},
{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#746855"
}
]
},
{
"featureType": "administrative.locality",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#d59563"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#d59563"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#263c3f"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#6b9a76"
}
]
},
{
"featureType": "road",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#2b3544"
}
]
},
{
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9ca5b3"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#38414e"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#212a37"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#746855"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#1f2835"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#f3d19c"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#38414e"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#212a37"
}
]
},
{
"featureType": "transit",
"elementType": "geometry",
"stylers": [
{
"color": "#2f3948"
}
]
},
{
"featureType": "transit.station",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#d59563"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#17263c"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#515c6d"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.stroke",
"stylers": [
{
"lightness": -20
}
]
}
]
5 changes: 5 additions & 0 deletions onebusaway-android/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
<item>@string/preferences_preferred_units_option_metric</item>
<item>@string/preferences_preferred_units_option_imperial</item>
</string-array>
<string-array name="app_theme_options">
<item>@string/preferences_app_theme_option_system_default</item>
<item>@string/preferences_app_theme_option_light</item>
<item>@string/preferences_app_theme_option_dark</item>
</string-array>
<string-array name="arrival_info_style_options">
<item>@string/preferences_arrival_info_style_options_a</item>
<item>@string/preferences_arrival_info_style_options_b</item>
Expand Down
1 change: 1 addition & 0 deletions onebusaway-android/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<string name="preferences_key_powered_by_oba">preference_powered_by_oba</string>
<string name="preferences_key_about">preference_about</string>
<string name="preference_key_preferred_units">preference_preferred_units</string>
<string name="preference_key_app_theme">preference_app_theme</string>
<string name="preference_key_arrival_info_style">preference_arrival_info_style</string>
<string name="preference_key_show_negative_arrivals">preference_show_negative_arrivals</string>
<string name="preference_key_show_zoom_controls">preference_show_zoom_controls</string>
Expand Down
5 changes: 5 additions & 0 deletions onebusaway-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,11 @@
<string name="preferences_preferred_units_option_metric">Metric (km, m)</string>
<string name="preferences_preferred_units_option_imperial">Imperial (mi, ft)</string>

<string name="preferences_app_theme_title">App Theme</string>
<string name="preferences_app_theme_option_system_default">System Default</string>
<string name="preferences_app_theme_option_light">Light Theme</string>
<string name="preferences_app_theme_option_dark">Dark Theme</string>

<string name="preferences_show_negative_arrivals_title">Show departed buses</string>
<string name="preferences_show_negative_arrivals_summary">Include buses that just left as
negative arrival times
Expand Down
2 changes: 2 additions & 0 deletions onebusaway-android/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
<item name="colorAccent">@color/theme_accent</item>
<!-- Change the default cursor color so its visible in the action bar -->
<item name="autoCompleteTextViewStyle">@style/cursorColor</item>
<!-- Allows us to force Dark Theme throughout the app -->
<item name="android:forceDarkAllowed">true</item>
</style>
</resources>
Loading

0 comments on commit 3803264

Please sign in to comment.