From 3bf49fa3b7168ff02c393e18513f0278fbf2a9fa Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 8 Oct 2014 03:48:01 +0200 Subject: [PATCH] WeatherInformation: settings improvements --- res/layout/weather_current_fragment.xml | 7 +- res/values/arrays.xml | 6 +- res/values/strings.xml | 3 +- res/xml/weather_preferences.xml | 12 +- .../exampletdd/NotificationIntentService.java | 63 ++++++--- src/de/example/exampletdd/WidgetIntentService.java | 63 ++++++--- .../fragment/current/CurrentFragment.java | 144 +++++++-------------- .../WeatherInformationPreferencesFragment.java | 57 +++++++- .../fragment/specific/SpecificFragment.java | 48 +++++-- 9 files changed, 244 insertions(+), 159 deletions(-) diff --git a/res/layout/weather_current_fragment.xml b/res/layout/weather_current_fragment.xml index 74a56ec..e6618b6 100644 --- a/res/layout/weather_current_fragment.xml +++ b/res/layout/weather_current_fragment.xml @@ -4,12 +4,13 @@ android:layout_width="match_parent" android:layout_height="match_parent" > - + android:layout_height="wrap_content" + android:layout_gravity="center"> - K - Celsius - Fahrenheit - Kelvin + celsius + fahrenheit + kelvin ar diff --git a/res/values/strings.xml b/res/values/strings.xml index 3150a22..4214cd3 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -21,7 +21,6 @@ Settings Units weather_preferences_units - Celsius Temperature Notifications Disabled @@ -37,7 +36,7 @@ Refresh interval weather_preferences_wind Wind - Wind + meter per second city not found country not found Please wait… diff --git a/res/xml/weather_preferences.xml b/res/xml/weather_preferences.xml index ac012d9..a2754f8 100644 --- a/res/xml/weather_preferences.xml +++ b/res/xml/weather_preferences.xml @@ -19,15 +19,19 @@ + android:defaultValue="ºC" /> diff --git a/src/de/example/exampletdd/NotificationIntentService.java b/src/de/example/exampletdd/NotificationIntentService.java index d544cb9..608c4d7 100644 --- a/src/de/example/exampletdd/NotificationIntentService.java +++ b/src/de/example/exampletdd/NotificationIntentService.java @@ -99,25 +99,54 @@ public class NotificationIntentService extends IntentService { return current; } + private interface UnitsConversor { + + public double doConversion(final double value); + } + private void showNotification(final Current current, final WeatherLocation weatherLocation) { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); - // TODO: repeating the same code in Overview, Specific and Current!!! - // 1. Update units of measurement. - boolean mIsFahrenheit = false; - final 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)) { - mIsFahrenheit = false; + // TODO: repeating the same code in Overview, Specific and Current!!! + // 1. Update units of measurement. + // 1.1 Temperature + String tempSymbol; + UnitsConversor tempUnitsConversor; + String keyPreference = this.getResources().getString(R.string.weather_preferences_units_key); + String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + 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 { - mIsFahrenheit = true; + tempSymbol = values[2]; + tempUnitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return value; + } + + }; } - final double tempUnits = mIsFahrenheit ? 0 : 273.15; - final String symbol = mIsFahrenheit ? "ºF" : "ºC"; // 2. Formatters @@ -129,14 +158,14 @@ public class NotificationIntentService extends IntentService { String tempMax = ""; if (current.getMain().getTemp_max() != null) { double conversion = (Double) current.getMain().getTemp_max(); - conversion = conversion - tempUnits; - tempMax = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMax = tempFormatter.format(conversion) + tempSymbol; } String tempMin = ""; if (current.getMain().getTemp_min() != null) { double conversion = (Double) current.getMain().getTemp_min(); - conversion = conversion - tempUnits; - tempMin = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMin = tempFormatter.format(conversion) + tempSymbol; } Bitmap picture; if ((current.getWeather().size() > 0) diff --git a/src/de/example/exampletdd/WidgetIntentService.java b/src/de/example/exampletdd/WidgetIntentService.java index 4319637..ae164ba 100644 --- a/src/de/example/exampletdd/WidgetIntentService.java +++ b/src/de/example/exampletdd/WidgetIntentService.java @@ -143,25 +143,54 @@ public class WidgetIntentService extends IntentService { return current; } + private interface UnitsConversor { + + public double doConversion(final double value); + } + private RemoteViews makeView(final Current current, final WeatherLocation weatherLocation) { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - boolean mIsFahrenheit = false; - final 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)) { - mIsFahrenheit = false; - } else { - mIsFahrenheit = true; - } - final double tempUnits = mIsFahrenheit ? 0 : 273.15; - final String symbol = mIsFahrenheit ? "ºF" : "ºC"; + // 1.1 Temperature + String tempSymbol; + UnitsConversor tempUnitsConversor; + String keyPreference = this.getResources().getString(R.string.weather_preferences_units_key); + String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + 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 { + tempSymbol = values[2]; + tempUnitsConversor = new UnitsConversor(){ + + @Override + public double doConversion(final double value) { + return value; + } + + }; + } // 2. Formatters @@ -173,14 +202,14 @@ public class WidgetIntentService extends IntentService { String tempMax = ""; if (current.getMain().getTemp_max() != null) { double conversion = (Double) current.getMain().getTemp_max(); - conversion = conversion - tempUnits; - tempMax = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMax = tempFormatter.format(conversion) + tempSymbol; } String tempMin = ""; if (current.getMain().getTemp_min() != null) { double conversion = (Double) current.getMain().getTemp_min(); - conversion = conversion - tempUnits; - tempMin = tempFormatter.format(conversion) + symbol; + conversion = tempUnitsConversor.doConversion(conversion); + tempMin = tempFormatter.format(conversion) + tempSymbol; } Bitmap picture; if ((current.getWeather().size() > 0) diff --git a/src/de/example/exampletdd/fragment/current/CurrentFragment.java b/src/de/example/exampletdd/fragment/current/CurrentFragment.java index 7552c35..901ecfa 100644 --- a/src/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/src/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -82,10 +82,9 @@ public class CurrentFragment extends Fragment { this.setHasOptionsMenu(false); - final ProgressBar progress = (ProgressBar) getActivity().findViewById(R.id.weather_current_progressbar); - progress.setVisibility(View.VISIBLE); - final TextView errorMessage = (TextView) getActivity().findViewById(R.id.weather_current_error_message); - errorMessage.setVisibility(View.GONE); + this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE); + this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.VISIBLE); + this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.GONE); } @Override @@ -126,11 +125,9 @@ public class CurrentFragment extends Fragment { } else { // Empty UI and show error message - CurrentFragment.this.clearUI(); - final ProgressBar progress = (ProgressBar) getActivity().findViewById(R.id.weather_current_progressbar); - progress.setVisibility(View.GONE); - final TextView errorMessage = (TextView) getActivity().findViewById(R.id.weather_current_error_message); - errorMessage.setVisibility(View.VISIBLE); + CurrentFragment.this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE); + CurrentFragment.this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.GONE); + CurrentFragment.this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.VISIBLE); } } } @@ -143,7 +140,7 @@ public class CurrentFragment extends Fragment { .registerReceiver(this.mReceiver, filter); // Empty UI - this.clearUI(); + this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE); final DatabaseQueries query = new DatabaseQueries(this.getActivity().getApplicationContext()); final WeatherLocation weatherLocation = query.queryDataBase(); @@ -165,10 +162,8 @@ public class CurrentFragment extends Fragment { } else { // Load remote data (aynchronous) // Gets the data from the web. - final ProgressBar progress = (ProgressBar) getActivity().findViewById(R.id.weather_current_progressbar); - progress.setVisibility(View.VISIBLE); - final TextView errorMessage = (TextView) getActivity().findViewById(R.id.weather_current_error_message); - errorMessage.setVisibility(View.GONE); + this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.VISIBLE); + this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.GONE); final CurrentTask task = new CurrentTask( this.getActivity().getApplicationContext(), new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")), @@ -202,11 +197,16 @@ public class CurrentFragment extends Fragment { super.onPause(); } - private interface UnitsConversor { + private interface TempUnitsConversor { public double doConversion(final double value); } - + + private interface WindUnitsConversor { + + public double doConversion(final double value); + } + private void updateUI(final Current current) { final SharedPreferences sharedPreferences = PreferenceManager @@ -216,13 +216,13 @@ public class CurrentFragment extends Fragment { // 1. Update units of measurement. // 1.1 Temperature String tempSymbol; - UnitsConversor tempUnitsConversor; + TempUnitsConversor tempUnitsConversor; String keyPreference = this.getResources().getString(R.string.weather_preferences_units_key); String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value); if (unitsPreferenceValue.equals(values[0])) { tempSymbol = values[0]; - tempUnitsConversor = new UnitsConversor(){ + tempUnitsConversor = new TempUnitsConversor(){ @Override public double doConversion(final double value) { @@ -232,7 +232,7 @@ public class CurrentFragment extends Fragment { }; } else if (unitsPreferenceValue.equals(values[1])) { tempSymbol = values[1]; - tempUnitsConversor = new UnitsConversor(){ + tempUnitsConversor = new TempUnitsConversor(){ @Override public double doConversion(final double value) { @@ -242,7 +242,7 @@ public class CurrentFragment extends Fragment { }; } else { tempSymbol = values[2]; - tempUnitsConversor = new UnitsConversor(){ + tempUnitsConversor = new TempUnitsConversor(){ @Override public double doConversion(final double value) { @@ -253,10 +253,30 @@ public class CurrentFragment extends Fragment { } // 1.2 Wind + String windSymbol; + WindUnitsConversor windUnitsConversor; 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]); + unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + values = this.getResources().getStringArray(R.array.weather_preferences_wind); + if (unitsPreferenceValue.equals(values[0])) { + windSymbol = values[0]; + windUnitsConversor = new WindUnitsConversor(){ + + @Override + public double doConversion(double value) { + return value; + } + }; + } else { + windSymbol = values[1]; + windUnitsConversor = new WindUnitsConversor(){ + + @Override + public double doConversion(double value) { + return value * 2.237; + } + }; + } // 2. Formatters @@ -312,7 +332,8 @@ public class CurrentFragment extends Fragment { String windValue = ""; if ((current.getWind() != null) && (current.getWind().getSpeed() != null)) { - final double conversion = (Double) current.getWind().getSpeed(); + double conversion = (Double) current.getWind().getSpeed(); + conversion = windUnitsConversor.doConversion(conversion); windValue = tempFormatter.format(conversion); } String rainValue = ""; @@ -354,11 +375,6 @@ public class CurrentFragment extends Fragment { // 4. Update UI. - ProgressBar progress = (ProgressBar) getActivity().findViewById(R.id.weather_current_progressbar); - progress.setVisibility(View.GONE); - TextView errorMessage = (TextView) getActivity().findViewById(R.id.weather_current_error_message); - errorMessage.setVisibility(View.GONE); - final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_current_temp_max); tempMaxView.setText(tempMax); final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_current_temp_min); @@ -369,53 +385,39 @@ public class CurrentFragment extends Fragment { final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_current_description); descriptionView.setText(description); - ((TextView) getActivity().findViewById(R.id.weather_current_humidity)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_humidity)); ((TextView) getActivity().findViewById(R.id.weather_current_humidity_value)).setText(humidityValue); ((TextView) getActivity().findViewById(R.id.weather_current_humidity_units)).setText( this.getActivity().getApplicationContext().getString(R.string.text_units_percent)); - ((TextView) getActivity().findViewById(R.id.weather_current_pressure)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_pressure)); ((TextView) getActivity().findViewById(R.id.weather_current_pressure_value)).setText(pressureValue); ((TextView) getActivity().findViewById(R.id.weather_current_pressure_units)).setText( this.getActivity().getApplicationContext().getString(R.string.text_units_hpa)); - ((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(windSymbol); - ((TextView) getActivity().findViewById(R.id.weather_current_rain)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_rain)); ((TextView) getActivity().findViewById(R.id.weather_current_rain_value)).setText(rainValue); ((TextView) getActivity().findViewById(R.id.weather_current_rain_units)).setText( this.getActivity().getApplicationContext().getString(R.string.text_units_mm3h)); - ((TextView) getActivity().findViewById(R.id.weather_current_clouds)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_clouds)); ((TextView) getActivity().findViewById(R.id.weather_current_clouds_value)).setText(cloudsValue); ((TextView) getActivity().findViewById(R.id.weather_current_clouds_units)).setText( this.getActivity().getApplicationContext().getString(R.string.text_units_percent)); - ((TextView) getActivity().findViewById(R.id.weather_current_snow)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_snow)); ((TextView) getActivity().findViewById(R.id.weather_current_snow_value)).setText(snowValue); ((TextView) getActivity().findViewById(R.id.weather_current_snow_units)).setText( this.getActivity().getApplicationContext().getString(R.string.text_units_mm3h)); - ((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(tempSymbol); - ((TextView) getActivity().findViewById(R.id.weather_current_sunrise)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_sun_rise)); ((TextView) getActivity().findViewById(R.id.weather_current_sunrise_value)).setText(sunRiseTime); - ((TextView) getActivity().findViewById(R.id.weather_current_sunset)).setText( - this.getActivity().getApplicationContext().getString(R.string.text_field_sun_set)); ((TextView) getActivity().findViewById(R.id.weather_current_sunset_value)).setText(sunSetTime); + + this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.VISIBLE); + this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.GONE); + this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.GONE); } private boolean isDataFresh(final Date lastUpdate) { @@ -437,54 +439,6 @@ public class CurrentFragment extends Fragment { return false; } - 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(""); - - ((ImageView) getActivity().findViewById(R.id.weather_current_picture)).setImageBitmap(null); - - - ((TextView) getActivity().findViewById(R.id.weather_current_description)).setText(""); - - - ((TextView) getActivity().findViewById(R.id.weather_current_humidity)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_humidity_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_humidity_units)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_pressure)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_pressure_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_pressure_units)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_wind)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_wind_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_wind_units)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_rain)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_rain_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_rain_units)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_clouds)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_clouds_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_clouds_units)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_snow)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_snow_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_snow_units)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_feelslike)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_value)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_units)).setText(""); - - - ((TextView) getActivity().findViewById(R.id.weather_current_sunrise)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_sunrise_value)).setText(""); - - ((TextView) getActivity().findViewById(R.id.weather_current_sunset)).setText(""); - ((TextView) getActivity().findViewById(R.id.weather_current_sunset_value)).setText(""); - } - // TODO: How could I show just one progress dialog when I have two fragments in tabs // activity doing the same in background? // I mean, if OverviewTask shows one progress dialog and CurrentTask does the same I will have diff --git a/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java b/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java index 6ad22ba..b730ea1 100644 --- a/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java +++ b/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java @@ -64,6 +64,20 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment } connectionPref.setSummary(humanValue); + // Wind + values = this.getResources().getStringArray(R.array.weather_preferences_wind); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value); + keyPreference = this.getString(R.string.weather_preferences_wind_key); + connectionPref = this.findPreference(keyPreference); + value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); + humanValue = ""; + if (value.equals(values[0])) { + humanValue = humanValues[0]; + } else if (value.equals(values[1])) { + humanValue = humanValues[1]; + } + connectionPref.setSummary(humanValue); + // Forecast days number values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast); humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value); @@ -126,19 +140,30 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment public void onSharedPreferenceChanged( final SharedPreferences sharedPreferences, final String key) { - // 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 keyValue = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_units_key); - if (key.equals(keyValue)) { - final Preference connectionPref = this.findPreference(key); - connectionPref.setSummary(sharedPreferences.getString(key, "")); - return; + final Preference connectionPref = this.findPreference(key); + final String value = sharedPreferences.getString(key, ""); + 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); + return; } // 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); keyValue = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_update_time_rate_key); if (key.equals(keyValue)) { @@ -162,6 +187,24 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment return; } + // Wind + values = this.getResources().getStringArray(R.array.weather_preferences_wind); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value); + keyValue = this.getString(R.string.weather_preferences_wind_key); + if (key.equals(keyValue)) { + final Preference connectionPref = this.findPreference(key); + final String value = sharedPreferences.getString(key, ""); + String humanValue = ""; + if (value.equals(values[0])) { + humanValue = humanValues[0]; + } else if (value.equals(values[1])) { + humanValue = humanValues[1]; + } + + connectionPref.setSummary(humanValue); + return; + } + // Forecast days number values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast); humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value); diff --git a/src/de/example/exampletdd/fragment/specific/SpecificFragment.java b/src/de/example/exampletdd/fragment/specific/SpecificFragment.java index f223018..40f11d9 100644 --- a/src/de/example/exampletdd/fragment/specific/SpecificFragment.java +++ b/src/de/example/exampletdd/fragment/specific/SpecificFragment.java @@ -103,11 +103,16 @@ public class SpecificFragment extends Fragment { } } - private interface UnitsConversor { + private interface TempUnitsConversor { public double doConversion(final double value); } - + + private interface WindUnitsConversor { + + public double doConversion(final double value); + } + private void updateUI(final Forecast forecastWeatherData, final int chosenDay) { final SharedPreferences sharedPreferences = PreferenceManager @@ -117,14 +122,14 @@ public class SpecificFragment extends Fragment { // 1. Update units of measurement. // 1.1 Temperature String tempSymbol; - UnitsConversor tempUnitsConversor; + TempUnitsConversor tempUnitsConversor; String keyPreference = this.getResources().getString( R.string.weather_preferences_units_key); - final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value); if (unitsPreferenceValue.equals(values[0])) { tempSymbol = values[0]; - tempUnitsConversor = new UnitsConversor(){ + tempUnitsConversor = new TempUnitsConversor(){ @Override public double doConversion(final double value) { @@ -134,7 +139,7 @@ public class SpecificFragment extends Fragment { }; } else if (unitsPreferenceValue.equals(values[1])) { tempSymbol = values[1]; - tempUnitsConversor = new UnitsConversor(){ + tempUnitsConversor = new TempUnitsConversor(){ @Override public double doConversion(final double value) { @@ -144,7 +149,7 @@ public class SpecificFragment extends Fragment { }; } else { tempSymbol = values[2]; - tempUnitsConversor = new UnitsConversor(){ + tempUnitsConversor = new TempUnitsConversor(){ @Override public double doConversion(final double value) { @@ -155,10 +160,30 @@ public class SpecificFragment extends Fragment { } // 1.2 Wind + String windSymbol; + WindUnitsConversor windUnitsConversor; 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]); + unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + values = this.getResources().getStringArray(R.array.weather_preferences_wind); + if (unitsPreferenceValue.equals(values[0])) { + windSymbol = values[0]; + windUnitsConversor = new WindUnitsConversor(){ + + @Override + public double doConversion(double value) { + return value; + } + }; + } else { + windSymbol = values[1]; + windUnitsConversor = new WindUnitsConversor(){ + + @Override + public double doConversion(double value) { + return value * 2.237; + } + }; + } // 2. Formatters @@ -218,7 +243,8 @@ public class SpecificFragment extends Fragment { } String windValue = ""; if (forecast.getSpeed() != null) { - final double conversion = (Double) forecast.getSpeed(); + double conversion = (Double) forecast.getSpeed(); + conversion = windUnitsConversor.doConversion(conversion); windValue = tempFormatter.format(conversion); } String rainValue = ""; -- 2.1.4