Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/data/PreferencesHolder.java
| Show All 14 Lines | BusTO - Data components | ||||
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
| */ | */ | ||||
| package it.reyboz.bustorino.data; | package it.reyboz.bustorino.data; | ||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
| import androidx.appcompat.app.AppCompatDelegate; | |||||
| import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
| import static android.content.Context.MODE_PRIVATE; | import static android.content.Context.MODE_PRIVATE; | ||||
| import androidx.preference.PreferenceManager; | import androidx.preference.PreferenceManager; | ||||
| import it.reyboz.bustorino.backend.KotlinUtils; | |||||
| import it.reyboz.bustorino.fragments.SettingsFragment; | import it.reyboz.bustorino.fragments.SettingsFragment; | ||||
| import it.reyboz.bustorino.map.MapLibreUtils; | import it.reyboz.bustorino.map.MapLibreUtils; | ||||
| import java.util.HashSet; | import java.util.HashSet; | ||||
| import java.util.Set; | import java.util.Set; | ||||
| /** | /** | ||||
| * Static class for commonly used SharedPreference operations | * Static class for commonly used SharedPreference operations | ||||
| */ | */ | ||||
| public abstract class PreferencesHolder { | public abstract class PreferencesHolder { | ||||
| public static final String PREF_GTFS_DB_VERSION = "gtfs_db_version"; | public static final String PREF_GTFS_DB_VERSION = "gtfs_db_version"; | ||||
| public static final String PREF_INTRO_ACTIVITY_RUN ="pref_intro_activity_run"; | public static final String PREF_INTRO_ACTIVITY_RUN ="pref_intro_activity_run"; | ||||
| public static final String DB_GTT_VERSION_KEY = "NextGenDB.GTTVersion"; | public static final String DB_GTT_VERSION_KEY = "NextGenDB.GTTVersion"; | ||||
| public static final String DB_LAST_UPDATE_KEY = "NextGenDB.LastDBUpdate"; | public static final String DB_LAST_UPDATE_KEY = "NextGenDB.LastDBUpdate"; | ||||
| public static final String PREF_FAVORITE_LINES = "pref_favorite_lines"; | public static final String PREF_FAVORITE_LINES = "pref_favorite_lines"; | ||||
| // match the one in preferences.xml | |||||
| public static final String PREF_THEME_DAY_NIGHT = "app_theme_day_night"; | |||||
| public static final Set<String> KEYS_MERGE_SET = Set.of(PREF_FAVORITE_LINES); | public static final Set<String> KEYS_MERGE_SET = Set.of(PREF_FAVORITE_LINES); | ||||
| public static final Set<String> IGNORE_KEYS_LOAD_MAIN = Set.of(PREF_GTFS_DB_VERSION, PREF_INTRO_ACTIVITY_RUN, DB_GTT_VERSION_KEY, DB_LAST_UPDATE_KEY); | public static final Set<String> IGNORE_KEYS_LOAD_MAIN = Set.of(PREF_GTFS_DB_VERSION, PREF_INTRO_ACTIVITY_RUN, DB_GTT_VERSION_KEY, DB_LAST_UPDATE_KEY); | ||||
| public static SharedPreferences getMainSharedPreferences(Context context){ | public static SharedPreferences getMainSharedPreferences(Context context){ | ||||
| return context.getSharedPreferences(context.getString(R.string.mainSharedPreferences), MODE_PRIVATE); | return context.getSharedPreferences(context.getString(R.string.mainSharedPreferences), MODE_PRIVATE); | ||||
| } | } | ||||
| public static SharedPreferences getAppPreferences(Context con){ | public static SharedPreferences getAppPreferences(Context con){ | ||||
| Show All 40 Lines | public abstract class PreferencesHolder { | ||||
| public static HashSet<String> getFavoritesLinesGtfsIDs(@NonNull Context con){ | public static HashSet<String> getFavoritesLinesGtfsIDs(@NonNull Context con){ | ||||
| final SharedPreferences pref = getMainSharedPreferences(con); | final SharedPreferences pref = getMainSharedPreferences(con); | ||||
| return new HashSet<>(pref.getStringSet(PREF_FAVORITE_LINES, new HashSet<>())); | return new HashSet<>(pref.getStringSet(PREF_FAVORITE_LINES, new HashSet<>())); | ||||
| } | } | ||||
| public static String getMapLibreStyleFile(Context con){ | public static String getMapLibreStyleFile(Context con){ | ||||
| final SharedPreferences pref = getAppPreferences(con); | final SharedPreferences pref = getAppPreferences(con); | ||||
| final String mapStyle_val = pref.getString(SettingsFragment.LIBREMAP_STYLE_PREF_KEY, ""); | final String mapStyle_val = pref.getString(SettingsFragment.LIBREMAP_STYLE_PREF_KEY, ""); | ||||
| return switch (mapStyle_val) { | final boolean isNightMode = KotlinUtils.isDarkTheme(con); | ||||
| String styleFile; | |||||
| switch (mapStyle_val) { | |||||
| //MUST MATCH IN keys.xml -> map_style_pref_values | //MUST MATCH IN keys.xml -> map_style_pref_values | ||||
| case "versatiles_c" -> MapLibreUtils.STYLE_VERSATILES_COLORFUL_JSON; | case "osm_legacy": | ||||
| case "osm_legacy" -> MapLibreUtils.STYLE_OSM_RASTER; | styleFile = MapLibreUtils.STYLE_OSM_RASTER; | ||||
| default -> MapLibreUtils.getDefaultStyleJson(); | break; | ||||
| case "versatiles_c": | |||||
| default: | |||||
| styleFile = (isNightMode) ? MapLibreUtils.STYLE_VERSATILES_ECLIPSE_JSON : MapLibreUtils.STYLE_VERSATILES_COLORFUL_JSON; | |||||
| } | |||||
| return styleFile; | |||||
| } | |||||
| public static Integer getAppThemeDayNight(Context context){ | |||||
| final SharedPreferences pref = getAppPreferences(context); | |||||
| final var value = pref.getString(PREF_THEME_DAY_NIGHT, ""); | |||||
| return getAppThemeDayNight(value); | |||||
| } | |||||
| public static Integer getAppThemeDayNight(String prefValue){ | |||||
| return switch (prefValue){ | |||||
| case "day" -> AppCompatDelegate.MODE_NIGHT_NO; | |||||
| case "night" -> AppCompatDelegate.MODE_NIGHT_YES; | |||||
| case "system" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; | |||||
| default -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; | |||||
| }; | }; | ||||
| } | } | ||||
| } | } | ||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register