From: gu.martinm@gmail.com Date: Tue, 7 Oct 2014 19:51:40 +0000 (+0200) Subject: WeatherInformation: new preferences wind and kelvin X-Git-Tag: weatherinformation-1.0~107 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=c12e3a9989d6a1c3300677c7090c89fd10e79ef2;p=AndroidWeatherInformation WeatherInformation: new preferences wind and kelvin --- diff --git a/res/layout/weather_current_fragment.xml b/res/layout/weather_current_fragment.xml index 3533424..74a56ec 100644 --- a/res/layout/weather_current_fragment.xml +++ b/res/layout/weather_current_fragment.xml @@ -114,7 +114,7 @@ android:layout_gravity="start" android:layout_margin="5dp" android:gravity="start" - android:text="FEELS LIKE" + android:text="@string/text_field_feels_like" android:textAlignment="textStart" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/weather_time_of_day_color_title" @@ -162,7 +162,7 @@ android:id="@+id/weather_current_snow" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="SNOW" + android:text="@string/text_field_snow" android:gravity="end" android:layout_gravity="end" android:textAlignment="textEnd" @@ -224,7 +224,7 @@ android:layout_gravity="start" android:layout_margin="5dp" android:gravity="start" - android:text="HUMIDITY" + android:text="@string/text_field_humidity" android:textAlignment="textStart" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/weather_time_of_day_color_title" @@ -272,7 +272,7 @@ android:id="@+id/weather_current_rain" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="RAIN" + android:text="@string/text_field_rain" android:layout_gravity="end" android:gravity="end" android:textAlignment="textEnd" @@ -331,7 +331,7 @@ android:id="@+id/weather_current_wind" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="WIND" + android:text="@string/text_field_wind" android:layout_gravity="start" android:gravity="start" android:textAlignment="textStart" @@ -382,7 +382,7 @@ android:id="@+id/weather_current_clouds" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="CLOUDS" + android:text="@string/text_field_clouds" android:layout_gravity="end" android:gravity="end" android:textAlignment="textEnd" @@ -432,7 +432,7 @@ android:id="@+id/weather_current_pressure" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="PRESSURE" + android:text="@string/text_field_pressure" android:layout_gravity="center" android:gravity="center" android:textAlignment="center" @@ -481,7 +481,7 @@ android:id="@+id/weather_current_sunrise" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="SUN RISE" + android:text="@string/text_field_sun_rise" android:layout_gravity="center" android:gravity="center" android:textAlignment="center" @@ -515,7 +515,7 @@ android:id="@+id/weather_current_sunset" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="SUN SET" + android:text="@string/text_field_sun_set" android:layout_gravity="center" android:gravity="center" android:textAlignment="center" diff --git a/res/layout/weather_specific_fragment.xml b/res/layout/weather_specific_fragment.xml index 71f22d8..fd80c01 100644 --- a/res/layout/weather_specific_fragment.xml +++ b/res/layout/weather_specific_fragment.xml @@ -110,7 +110,7 @@ android:layout_gravity="start" android:layout_margin="5dp" android:gravity="start" - android:text="HUMIDITY" + android:text="@string/text_field_humidity" android:textAlignment="textStart" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/weather_time_of_day_color_title" @@ -158,7 +158,7 @@ android:id="@+id/weather_specific_rain" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="RAIN" + android:text="@string/text_field_rain" android:layout_gravity="end" android:gravity="end" android:textAlignment="textEnd" @@ -219,7 +219,7 @@ android:id="@+id/weather_specific_wind" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="WIND" + android:text="@string/text_field_wind" android:layout_gravity="start" android:gravity="start" android:textAlignment="textStart" @@ -270,7 +270,7 @@ android:id="@+id/weather_specific_clouds" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="CLOUDS" + android:text="@string/text_field_clouds" android:layout_gravity="end" android:gravity="end" android:textAlignment="textEnd" @@ -320,7 +320,7 @@ android:id="@+id/weather_specific_pressure" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="PRESSURE" + android:text="@string/text_field_pressure" android:layout_gravity="center" android:gravity="center" android:textAlignment="center" diff --git a/res/values/arrays.xml b/res/values/arrays.xml index ef2b6d3..0404af5 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -1,12 +1,14 @@ - Celsius - Fahrenheit + ºC + ºF + K Celsius Fahrenheit + Kelvin ar @@ -82,7 +84,7 @@ half day one day - + 300000 900000 1800000 @@ -100,4 +102,12 @@ half day one day + + m/s + mph + + + meter per second + miles per hour + diff --git a/res/values/strings.xml b/res/values/strings.xml index e4153b8..3150a22 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -21,7 +21,6 @@ Settings Units weather_preferences_units - Fahrenheit Celsius Temperature Notifications @@ -36,6 +35,9 @@ Forecast days number weather_preferences_refresh_interval Refresh interval + weather_preferences_wind + Wind + Wind city not found country not found Please wait… @@ -46,8 +48,6 @@ Get Location mm 3h % - ºC - m/s hpa No data available diff --git a/res/xml/weather_preferences.xml b/res/xml/weather_preferences.xml index 04751af..ac012d9 100644 --- a/res/xml/weather_preferences.xml +++ b/res/xml/weather_preferences.xml @@ -24,7 +24,11 @@ android:entryValues="@array/weather_preferences_units_value" android:selectable="true" android:persistent="true" - android:defaultValue="Celsius" /> + android:defaultValue="@string/weather_preferences_units_celsius" /> + 0) @@ -301,7 +336,7 @@ public class CurrentFragment extends Fragment { String feelsLike = ""; if (current.getMain().getTemp() != null) { double conversion = (Double) current.getMain().getTemp(); - conversion = conversion - tempUnits; + conversion = tempUnitsConversor.doConversion(conversion); feelsLike = tempFormatter.format(conversion); } String sunRiseTime = ""; @@ -349,8 +384,7 @@ public class CurrentFragment extends Fragment { ((TextView) getActivity().findViewById(R.id.weather_current_wind)).setText( this.getActivity().getApplicationContext().getString(R.string.text_field_wind)); ((TextView) getActivity().findViewById(R.id.weather_current_wind_value)).setText(windValue); - ((TextView) getActivity().findViewById(R.id.weather_current_wind_units)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_units_ms)); + ((TextView) getActivity().findViewById(R.id.weather_current_wind_units)).setText(windSymbol); ((TextView) getActivity().findViewById(R.id.weather_current_rain)).setText( this.getActivity().getApplicationContext().getString(R.string.text_field_rain)); @@ -373,8 +407,7 @@ public class CurrentFragment extends Fragment { ((TextView) getActivity().findViewById(R.id.weather_current_feelslike)).setText( this.getActivity().getApplicationContext().getString(R.string.text_field_feels_like)); ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_value)).setText(feelsLike); - ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_units)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_units_centigrade)); + ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_units)).setText(tempSymbol); ((TextView) getActivity().findViewById(R.id.weather_current_sunrise)).setText( this.getActivity().getApplicationContext().getString(R.string.text_field_sun_rise)); @@ -406,6 +439,7 @@ public class CurrentFragment extends Fragment { private void clearUI() { + // TODO: something better than this for clearing view? ((TextView) getActivity().findViewById(R.id.weather_current_temp_max)).setText(""); ((TextView) getActivity().findViewById(R.id.weather_current_temp_min)).setText(""); diff --git a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java index f413122..c8a5bf6 100644 --- a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -198,6 +198,11 @@ public class OverviewFragment extends ListFragment { } } + private interface UnitsConversor { + + public double doConversion(final double value); + } + private void updateUI(final Forecast forecastWeatherData) { final SharedPreferences sharedPreferences = PreferenceManager @@ -205,27 +210,49 @@ public class OverviewFragment extends ListFragment { // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - boolean isFahrenheit = false; - String keyPreference = this.getResources() - .getString(R.string.weather_preferences_units_key); - final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "Celsius"); - final String celsius = this.getResources().getString( - R.string.weather_preferences_units_celsius); - if (unitsPreferenceValue.equals(celsius)) { - isFahrenheit = false; + String symbol; + UnitsConversor unitsConversor; + String keyPreference = this.getResources().getString( + R.string.weather_preferences_units_key); + final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value); + if (unitsPreferenceValue.equals(values[0])) { + symbol = values[0]; + unitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return value - 273.15; + } + + }; + } else if (unitsPreferenceValue.equals(values[1])) { + symbol = values[1]; + unitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return (value * 1.8) - 459.67; + } + + }; } else { - isFahrenheit = true; + symbol = values[2]; + unitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return value; + } + + }; } - final double tempUnits = isFahrenheit ? 0 : 273.15; - final String symbol = isFahrenheit ? "ºF" : "ºC"; // 2. Update number day forecast. - int mDayForecast; - keyPreference = this.getResources() - .getString(R.string.weather_preferences_day_forecast_key); + keyPreference = this.getResources().getString(R.string.weather_preferences_day_forecast_key); final String dayForecast = sharedPreferences.getString(keyPreference, "5"); - mDayForecast = Integer.valueOf(dayForecast); + final int mDayForecast = Integer.valueOf(dayForecast); // 3. Formatters @@ -266,13 +293,13 @@ public class OverviewFragment extends ListFragment { Double maxTemp = null; if (forecast.getTemp().getMax() != null) { maxTemp = (Double) forecast.getTemp().getMax(); - maxTemp = maxTemp - tempUnits; + maxTemp = unitsConversor.doConversion(maxTemp); } Double minTemp = null; if (forecast.getTemp().getMin() != null) { minTemp = (Double) forecast.getTemp().getMin(); - minTemp = minTemp - tempUnits; + minTemp = unitsConversor.doConversion(minTemp); } if ((maxTemp != null) && (minTemp != null)) { diff --git a/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java b/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java index 1315c88..6ad22ba 100644 --- a/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java +++ b/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java @@ -24,22 +24,33 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment this.addPreferencesFromResource(R.xml.weather_preferences); - // Units of Measurement + // Temperature units + String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value); + String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value); String keyPreference = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_units_key); Preference connectionPref = this.findPreference(keyPreference); - connectionPref.setSummary(this.getPreferenceManager() - .getSharedPreferences().getString(keyPreference, "")); + String value = this.getPreferenceManager().getSharedPreferences() + .getString(keyPreference, ""); + String humanValue = ""; + if (value.equals(values[0])) { + humanValue = humanValues[0]; + } else if (value.equals(values[1])) { + humanValue = humanValues[1]; + } else if (value.equals(values[2])) { + humanValue = humanValues[2]; + } + connectionPref.setSummary(humanValue); // Update Time Rate - String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate); - String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value); + values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value); keyPreference = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_update_time_rate_key); connectionPref = this.findPreference(keyPreference); - String value = this.getPreferenceManager().getSharedPreferences() + value = this.getPreferenceManager().getSharedPreferences() .getString(keyPreference, ""); - String humanValue = ""; + humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; } else if (value.equals(values[1])) { diff --git a/src/de/example/exampletdd/fragment/specific/SpecificFragment.java b/src/de/example/exampletdd/fragment/specific/SpecificFragment.java index 0a4cc93..f223018 100644 --- a/src/de/example/exampletdd/fragment/specific/SpecificFragment.java +++ b/src/de/example/exampletdd/fragment/specific/SpecificFragment.java @@ -103,7 +103,11 @@ public class SpecificFragment extends Fragment { } } - + private interface UnitsConversor { + + public double doConversion(final double value); + } + private void updateUI(final Forecast forecastWeatherData, final int chosenDay) { final SharedPreferences sharedPreferences = PreferenceManager @@ -111,21 +115,52 @@ public class SpecificFragment extends Fragment { // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - boolean isFahrenheit = false; - final String keyPreference = this.getResources().getString( + // 1.1 Temperature + String tempSymbol; + UnitsConversor tempUnitsConversor; + String keyPreference = this.getResources().getString( R.string.weather_preferences_units_key); final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); - final String celsius = this.getResources().getString( - R.string.weather_preferences_units_celsius); - if (unitsPreferenceValue.equals(celsius)) { - isFahrenheit = false; + String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value); + if (unitsPreferenceValue.equals(values[0])) { + tempSymbol = values[0]; + tempUnitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return value - 273.15; + } + + }; + } else if (unitsPreferenceValue.equals(values[1])) { + tempSymbol = values[1]; + tempUnitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return (value * 1.8) - 459.67; + } + + }; } else { - isFahrenheit = true; + tempSymbol = values[2]; + tempUnitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return value; + } + + }; } - final double tempUnits = isFahrenheit ? 0 : 273.15; - final String symbol = isFahrenheit ? "ºF" : "ºC"; - - + + // 1.2 Wind + keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key); + final String windSymbol = sharedPreferences.getString( + keyPreference, + this.getResources().getStringArray(R.array.weather_preferences_wind)[0]); + + // 2. Formatters final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); tempFormatter.applyPattern("#####.#####"); @@ -144,14 +179,14 @@ public class SpecificFragment extends Fragment { String tempMax = ""; if (forecast.getTemp().getMax() != null) { double conversion = (Double) forecast.getTemp().getMax(); - conversion = conversion - tempUnits; - tempMax = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMax = tempFormatter.format(conversion) + tempSymbol; } String tempMin = ""; if (forecast.getTemp().getMin() != null) { double conversion = (Double) forecast.getTemp().getMin(); - conversion = conversion - tempUnits; - tempMin = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMin = tempFormatter.format(conversion) + tempSymbol; } Bitmap picture; if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null) @@ -200,26 +235,26 @@ public class SpecificFragment extends Fragment { String tempDay = ""; if (forecast.getTemp().getDay() != null) { double conversion = (Double) forecast.getTemp().getDay(); - conversion = conversion - tempUnits; - tempDay = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempDay = tempFormatter.format(conversion) + tempSymbol; } String tempMorn = ""; if (forecast.getTemp().getMorn() != null) { double conversion = (Double) forecast.getTemp().getMorn(); - conversion = conversion - tempUnits; - tempMorn = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMorn = tempFormatter.format(conversion) + tempSymbol; } String tempEve = ""; if (forecast.getTemp().getEve() != null) { double conversion = (Double) forecast.getTemp().getEve(); - conversion = conversion - tempUnits; - tempEve = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempEve = tempFormatter.format(conversion) + tempSymbol; } String tempNight = ""; if (forecast.getTemp().getNight() != null) { double conversion = (Double) forecast.getTemp().getNight(); - conversion = conversion - tempUnits; - tempNight = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempNight = tempFormatter.format(conversion) + tempSymbol; } @@ -240,8 +275,8 @@ public class SpecificFragment extends Fragment { humidityValueView.setText(humidityValue); final TextView pressureValueView = (TextView) getActivity().findViewById(R.id.weather_specific_pressure_value); pressureValueView.setText(pressureValue); - final TextView windValueView = (TextView) getActivity().findViewById(R.id.weather_specific_wind_value); - windValueView.setText(windValue); + ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);; + ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol); final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value); rainValueView.setText(rainValue); final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);