Skip to content

Commit

Permalink
#37 - set default option in the settings for language and other optio…
Browse files Browse the repository at this point in the history
…ns; fix location selection when entering main screen
  • Loading branch information
thuryn committed Mar 29, 2018
1 parent e656d0a commit 545c325
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "org.thosp.yourlocalweather"
minSdkVersion 14
targetSdkVersion 25
versionCode 38
versionName "3.0.12"
versionCode 39
versionName "3.0.13"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
playAccountConfig = playAccountConfigs.defaultAccountConfig
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/org/thosp/yourlocalweather/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ public void onResume() {
if (currentLocation == null) {
currentLocation = locationsDbHelper.getLocationByOrderId(0);
}
if (!currentLocation.isEnabled() && (locationsDbHelper.getAllRows().size() > 1)) {
currentLocation = locationsDbHelper.getLocationByOrderId(1);
}
switchToNextLocationWhenCurrentIsAutoAndIsDisabled();
if (mToolbarMenu != null) {
if ((currentLocation.getOrderId() == 0) && !currentLocation.isEnabled()) {
mToolbarMenu.findItem(R.id.main_menu_refresh).setVisible(false);
Expand Down Expand Up @@ -278,6 +276,12 @@ public void onRefresh() {
}
};

private void switchToNextLocationWhenCurrentIsAutoAndIsDisabled() {
if ((currentLocation.getOrderId() == 0) && !currentLocation.isEnabled() && (locationsDbHelper.getAllRows().size() > 1)) {
currentLocation = locationsDbHelper.getLocationByOrderId(1);
}
}

private void preLoadWeather() {
final CurrentWeatherDbHelper currentWeatherDbHelper = CurrentWeatherDbHelper.getInstance(this);

Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/org/thosp/yourlocalweather/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;

Expand Down Expand Up @@ -283,10 +284,7 @@ public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);

for (String key : SUMMARIES_TO_UPDATE) {
updateSummary(key, false);
}
updateSummaries();
}

@Override
Expand All @@ -296,6 +294,20 @@ public void onPause() {
.unregisterOnSharedPreferenceChangeListener(this);
}

private void setDefaultValues() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
if (!preferences.contains(Constants.PREF_LANGUAGE)) {
preferences.edit().putString(Constants.PREF_LANGUAGE, Locale.getDefault().getLanguage()).apply();
entrySummary(Constants.PREF_LANGUAGE);
}
}

private void updateSummaries() {
for (String key : SUMMARIES_TO_UPDATE) {
updateSummary(key, false);
}
}

private void initLocationCache() {

Preference locationCacheEnabled = findPreference(Constants.APP_SETTINGS_LOCATION_CACHE_ENABLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class LanguageUtil {
@SuppressWarnings("deprecation")
public static void setLanguage(final ContextWrapper contextWrapper, String locale) {
Locale sLocale;
if (TextUtils.isEmpty(locale)) {
if (TextUtils.isEmpty(locale) || "default".equals(locale)) {
sLocale = Locale.getDefault();
} else {
String[] localeParts = locale.split("-");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public static SharedPreferences getDefaultSharedPrefs(Context context) {
}

public static String getLanguage(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(Constants.PREF_LANGUAGE, Locale.getDefault().getLanguage());
String language = PreferenceManager.getDefaultSharedPreferences(context).getString(Constants.PREF_LANGUAGE, Locale.getDefault().getLanguage());
if ("default".equals(language)) {
language = Locale.getDefault().getLanguage();
}
return language;
}

public static Theme getTheme(Context context) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
<item name="chinese">@string/pref_language_zh_rcn</item>
</string-array>
<string-array name="language_values" translatable="false">
<item name="default"></item>
<item name="default">default</item>
<item name="arabic">ar</item>
<item name="english">en</item>
<item name="basque">eu</item>
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:key="temperature_units_pref_key"
android:title="@string/pref_title_temperature"/>
<ListPreference
android:defaultValue="ms"
android:defaultValue="m_per_second"
android:dialogTitle="@string/pref_title_wind"
android:entries="@array/pref_wind_units_entries"
android:entryValues="@array/pref_wind_units_values"
Expand All @@ -29,19 +29,21 @@
android:key="display_category_pref_key"
android:title="@string/preference_title_category_display">
<ListPreference
android:defaultValue="default"
android:key="language_pref_key"
android:dialogTitle="@string/pref_title_language"
android:title="@string/pref_title_language"
android:entries="@array/language_entries"
android:entryValues="@array/language_values"/>
<ListPreference
android:defaultValue="light"
android:key="theme_pref_key"
android:dialogTitle="@string/pref_title_theme"
android:title="@string/pref_title_theme"
android:entries="@array/theme_entries"
android:entryValues="@array/theme_values"/>
<ListPreference
android:defaultValue="weather_icon_set_merlin_the_red"
android:defaultValue="weather_icon_set_merlinTheRed"
android:dialogTitle="@string/preference_weather_icon_set_title"
android:entries="@array/weather_icon_set_entries"
android:entryValues="@array/weather_icon_set_values"
Expand Down Expand Up @@ -73,12 +75,14 @@
android:key="update_location_weather_category_pref_key"
android:title="@string/preference_title_category_location_weather">
<ListPreference
android:defaultValue="60"
android:dialogTitle="@string/preference_widget_title_update_auto_period"
android:entries="@array/location_auto_update_period_entries"
android:entryValues="@array/location_auto_update_period_values"
android:key="location_auto_update_period_pref_key"
android:title="@string/preference_widget_title_update_auto_period"/>
<ListPreference
android:defaultValue="60"
android:dialogTitle="@string/preference_widget_title_update_period"
android:entries="@array/location_update_period_entries"
android:entryValues="@array/location_update_period_values"
Expand Down

0 comments on commit 545c325

Please sign in to comment.