fa4c6a3bf09bce3d70f8a673d85c19ccb5d29814
[JavaForFun] /
1 package de.example.exampletdd.fragment.specific;
2
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.Locale;
9
10 import android.content.SharedPreferences;
11 import android.graphics.Bitmap;
12 import android.graphics.BitmapFactory;
13 import android.os.Bundle;
14 import android.preference.PreferenceManager;
15 import android.support.v4.app.Fragment;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.ImageView;
20 import android.widget.TextView;
21 import de.example.exampletdd.R;
22 import de.example.exampletdd.model.forecastweather.Forecast;
23 import de.example.exampletdd.service.IconsList;
24 import de.example.exampletdd.service.PermanentStorage;
25
26
27 public class SpecificFragment extends Fragment {
28     private int mChosenDay;
29
30     @Override
31     public void onCreate(final Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         
34         final Bundle extras = this.getActivity().getIntent().getExtras();
35
36         if (extras != null) {
37                 // handset layout
38             this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
39         } else {
40                 // tablet layout
41                 // Always 0 when tablet layout (by default shows the first day)
42             this.mChosenDay = 0;
43         }
44     }
45     
46     @Override
47     public View onCreateView(LayoutInflater inflater, ViewGroup container,
48                              Bundle savedInstanceState) {
49     
50         // Inflate the layout for this fragment
51         return inflater.inflate(R.layout.weather_specific_fragment, container, false);
52     }
53     
54     @Override
55     public void onActivityCreated(final Bundle savedInstanceState) {
56         super.onActivityCreated(savedInstanceState);
57
58         if (savedInstanceState != null) {
59                 // Restore UI state
60             final Forecast forecast = (Forecast) savedInstanceState.getSerializable("Forecast");
61
62             // TODO: Could it be better to store in global data forecast even if it is null value?
63             //       So, perhaps do not check for null value and always store in global variable.
64             if (forecast != null) {
65                 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
66                 store.saveForecast(forecast);
67             }
68
69             this.mChosenDay = savedInstanceState.getInt("mChosenDay");
70         }
71
72         this.setHasOptionsMenu(false);
73     }
74
75     @Override
76     public void onSaveInstanceState(final Bundle savedInstanceState) {
77
78         // Save UI state
79         final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
80         final Forecast forecast = store.getForecast();
81
82         // TODO: Could it be better to save forecast data even if it is null value?
83         //       So, perhaps do not check for null value.
84         if (forecast != null) {
85             savedInstanceState.putSerializable("Forecast", forecast);
86         }
87
88         savedInstanceState.putInt("mChosenDay", this.mChosenDay);
89
90         super.onSaveInstanceState(savedInstanceState);
91     }
92
93     /**
94      * This method is used by tablet layout.
95      * 
96      * @param chosenDay
97      */
98     public void updateUIByChosenDay(final int chosenDay) {
99         final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
100         final Forecast forecast = store.getForecast();
101
102         if (forecast != null) {
103             this.updateUI(forecast, chosenDay);
104         }
105     }
106
107     private interface UnitsConversor {
108         
109         public double doConversion(final double value);
110     }
111
112     private void updateUI(final Forecast forecastWeatherData, final int chosenDay) {
113
114         final SharedPreferences sharedPreferences = PreferenceManager
115                 .getDefaultSharedPreferences(this.getActivity());
116
117         // TODO: repeating the same code in Overview, Specific and Current!!!
118         // 1. Update units of measurement.
119         // 1.1 Temperature
120         String tempSymbol;
121         UnitsConversor tempUnitsConversor;
122         String keyPreference = this.getResources().getString(
123                 R.string.weather_preferences_temperature_key);
124         String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
125         String unitsPreferenceValue = sharedPreferences.getString(
126                 keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
127         if (unitsPreferenceValue.equals(values[0])) {
128                 tempSymbol = values[0];
129                 tempUnitsConversor = new UnitsConversor(){
130
131                                 @Override
132                                 public double doConversion(final double value) {
133                                         return value - 273.15;
134                                 }
135                         
136                 };
137         } else if (unitsPreferenceValue.equals(values[1])) {
138                 tempSymbol = values[1];
139                 tempUnitsConversor = new UnitsConversor(){
140
141                                 @Override
142                                 public double doConversion(final double value) {
143                                         return (value * 1.8) - 459.67;
144                                 }
145                         
146                 };
147         } else {
148                 tempSymbol = values[2];
149                 tempUnitsConversor = new UnitsConversor(){
150
151                                 @Override
152                                 public double doConversion(final double value) {
153                                         return value;
154                                 }
155                         
156                 };
157         }
158
159         // 1.2 Wind
160         String windSymbol;
161         UnitsConversor windUnitsConversor;
162         keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key);
163         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
164         unitsPreferenceValue = sharedPreferences.getString(
165                 keyPreference, this.getString(R.string.weather_preferences_wind_meters));
166         if (unitsPreferenceValue.equals(values[0])) {
167                 windSymbol = values[0];
168                 windUnitsConversor = new UnitsConversor(){
169
170                         @Override
171                         public double doConversion(double value) {
172                                 return value;
173                         }       
174                 };
175         } else {
176                 windSymbol = values[1];
177                 windUnitsConversor = new UnitsConversor(){
178
179                         @Override
180                         public double doConversion(double value) {
181                                 return value * 2.237;
182                         }       
183                 };
184         }
185
186         // 1.3 Pressure
187         String pressureSymbol;
188         UnitsConversor pressureUnitsConversor;
189         keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key);
190         values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
191         unitsPreferenceValue = sharedPreferences.getString(
192                 keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
193         if (unitsPreferenceValue.equals(values[0])) {
194                 pressureSymbol = values[0];
195                 pressureUnitsConversor = new UnitsConversor(){
196
197                         @Override
198                         public double doConversion(double value) {
199                                 return value;
200                         }       
201                 };
202         } else {
203                 pressureSymbol = values[1];
204                 pressureUnitsConversor = new UnitsConversor(){
205
206                         @Override
207                         public double doConversion(double value) {
208                                 return value / 113.25d;
209                         }       
210                 };
211         }
212
213
214         // 2. Formatters
215         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
216         tempFormatter.applyPattern("#####.#####");
217         
218
219         // 3. Prepare data for UI.
220         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
221                 .getList().get((chosenDay));
222
223         final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
224         final Calendar calendar = Calendar.getInstance();
225         final Long forecastUNIXDate = (Long) forecast.getDt();
226         calendar.setTimeInMillis(forecastUNIXDate * 1000L);
227         final Date date = calendar.getTime();     
228
229         String tempMax = "";
230         if (forecast.getTemp().getMax() != null) {
231             double conversion = (Double) forecast.getTemp().getMax();
232             conversion = tempUnitsConversor.doConversion(conversion);
233             tempMax = tempFormatter.format(conversion) + tempSymbol;
234         }        
235         String tempMin = "";
236         if (forecast.getTemp().getMin() != null) {
237             double conversion = (Double) forecast.getTemp().getMin();
238             conversion = tempUnitsConversor.doConversion(conversion);
239             tempMin = tempFormatter.format(conversion) + tempSymbol;
240         }
241         Bitmap picture;
242         if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
243                 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
244             final String icon = forecast.getWeather().get(0).getIcon();
245             picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
246                     .getResourceDrawable());
247         } else {
248             picture = BitmapFactory.decodeResource(this.getResources(),
249                     R.drawable.weather_severe_alert);
250         }
251
252         String description = this.getString(R.string.test_field_description_when_error);
253         if (forecast.getWeather().size() > 0) {
254             description = forecast.getWeather().get(0).getDescription();
255         }
256
257         String humidityValue = "";
258         if (forecast.getHumidity() != null) {
259             final double conversion = (Double) forecast.getHumidity();
260             humidityValue = tempFormatter.format(conversion);
261         }        
262         String pressureValue = "";
263         if (forecast.getPressure() != null) {
264             double conversion = (Double) forecast.getPressure();
265             conversion = pressureUnitsConversor.doConversion(conversion);
266             pressureValue = tempFormatter.format(conversion);
267         }
268         String windValue = "";
269         if (forecast.getSpeed() != null) {
270             double conversion = (Double) forecast.getSpeed();
271             conversion = windUnitsConversor.doConversion(conversion);
272             windValue = tempFormatter.format(conversion);
273         }
274         String rainValue = "";
275         if (forecast.getRain() != null) {
276             final double conversion = (Double) forecast.getRain();
277             rainValue = tempFormatter.format(conversion);
278         }
279         String cloudsValue = "";
280         if (forecast.getRain() != null) {
281             final double conversion = (Double) forecast.getClouds();
282             cloudsValue = tempFormatter.format(conversion);
283         }
284
285         String tempDay = "";
286         if (forecast.getTemp().getDay() != null) {
287             double conversion = (Double) forecast.getTemp().getDay();
288             conversion = tempUnitsConversor.doConversion(conversion);
289             tempDay = tempFormatter.format(conversion) + tempSymbol;
290         }
291         String tempMorn = "";
292         if (forecast.getTemp().getMorn() != null) {
293             double conversion = (Double) forecast.getTemp().getMorn();
294             conversion = tempUnitsConversor.doConversion(conversion);
295             tempMorn = tempFormatter.format(conversion) + tempSymbol;
296         }
297         String tempEve = "";
298         if (forecast.getTemp().getEve() != null) {
299             double conversion = (Double) forecast.getTemp().getEve();
300             conversion = tempUnitsConversor.doConversion(conversion);
301             tempEve = tempFormatter.format(conversion) + tempSymbol;
302         }   
303         String tempNight = "";
304         if (forecast.getTemp().getNight() != null) {
305             double conversion = (Double) forecast.getTemp().getNight();
306             conversion = tempUnitsConversor.doConversion(conversion);
307             tempNight = tempFormatter.format(conversion) + tempSymbol;
308         }   
309
310
311         // 4. Update UI.
312         this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
313         
314         final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_max);
315         tempMaxView.setText(tempMax);
316         final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_min);
317         tempMinView.setText(tempMin);
318         final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_specific_picture);
319         pictureView.setImageBitmap(picture);    
320         
321         final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
322         descriptionView.setText(description);
323         
324         final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value);
325         humidityValueView.setText(humidityValue);
326         ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue);
327         ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol);
328         ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);;
329         ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol);
330         final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value);
331         rainValueView.setText(rainValue);
332         final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);
333         cloudsValueView.setText(cloudsValue); 
334         
335         final TextView tempDayView = (TextView) getActivity().findViewById(R.id.weather_specific_day_temperature);
336         tempDayView.setText(tempDay);
337         final TextView tempMornView = (TextView) getActivity().findViewById(R.id.weather_specific_morn_temperature);
338         tempMornView.setText(tempMorn);
339         final TextView tempEveView = (TextView) getActivity().findViewById(R.id.weather_specific_eve_temperature);
340         tempEveView.setText(tempEve);
341         final TextView tempNightView = (TextView) getActivity().findViewById(R.id.weather_specific_night_temperature);
342         tempNightView.setText(tempNight);
343     }
344
345     @Override
346     public void onResume() {
347         super.onResume();
348
349         final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
350         final Forecast forecast = store.getForecast();
351
352         if (forecast != null) {
353             this.updateUI(forecast, this.mChosenDay);
354         }
355     }
356 }