5501a7f1fbc5054956723cd0564ec0511bfba10c
[JavaForFun] /
1 package de.example.exampletdd.fragment.specific;
2
3 import java.io.IOException;
4 import java.text.DecimalFormat;
5 import java.text.NumberFormat;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Locale;
9
10 import android.app.DialogFragment;
11 import android.app.ListFragment;
12 import android.content.SharedPreferences;
13 import android.os.Bundle;
14 import android.preference.PreferenceManager;
15 import android.widget.ListView;
16 import de.example.exampletdd.R;
17 import de.example.exampletdd.activityinterface.GetWeather;
18 import de.example.exampletdd.fragment.ErrorDialogFragment;
19 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
20 import de.example.exampletdd.service.WeatherServicePersistenceFile;
21
22 public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather {
23     private boolean mIsFahrenheit;
24     private int mChosenDay;
25     private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
26
27     @Override
28     public void onCreate(final Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30
31         final Bundle extras = this.getActivity().getIntent().getExtras();
32
33         if (extras != null) {
34             this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
35         } else {
36             this.mChosenDay = 0;
37         }
38
39         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
40                 this.getActivity());
41
42         // final SharedPreferences sharedPreferences = PreferenceManager
43         // .getDefaultSharedPreferences(this.getActivity());
44         // final String keyPreference = this.getResources().getString(
45         // R.string.weather_preferences_language_key);
46         // this.mLanguage = sharedPreferences.getString(
47         // keyPreference, "");
48     }
49
50     @Override
51     public void onActivityCreated(final Bundle savedInstanceState) {
52         super.onActivityCreated(savedInstanceState);
53
54         final ListView listWeatherView = this.getListView();
55         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
56
57         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
58                 R.layout.weather_data_entry_list);
59
60         this.setEmptyText("No data available");
61
62         this.setListAdapter(adapter);
63         this.setListShown(true);
64         this.setListShownNoAnimation(true);
65
66         if (savedInstanceState != null) {
67             // Restore state
68             final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
69                     .getSerializable("ForecastWeatherData");
70
71             if (forecastWeatherData != null) {
72                 try {
73                     this.mWeatherServicePersistenceFile
74                     .storeForecastWeatherData(forecastWeatherData);
75                 } catch (final IOException e) {
76                     final DialogFragment newFragment = ErrorDialogFragment
77                             .newInstance(R.string.error_dialog_generic_error);
78                     newFragment.show(this.getFragmentManager(), "errorDialog");
79                 }
80             }
81         }
82     }
83
84     @Override
85     public void onSaveInstanceState(final Bundle savedInstanceState) {
86
87         // Save state
88         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
89                 .getForecastWeatherData();
90
91         if (forecastWeatherData != null) {
92             savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
93         }
94
95         super.onSaveInstanceState(savedInstanceState);
96     }
97
98     @Override
99     public void getWeatherByDay(final int chosenDay) {
100
101         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
102                 .getForecastWeatherData();
103         if (forecastWeatherData != null) {
104             this.updateForecastWeatherData(forecastWeatherData, chosenDay);
105         }
106
107     }
108
109     @Override
110     public void getRemoteWeatherInformation() {
111         // Nothing to do.
112     }
113
114
115     public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
116             final int chosenDay) {
117         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
118                 .getDefault());
119         tempFormatter.applyPattern("#####.#####");
120         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
121
122         final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
123
124         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
125                 this.getActivity(), R.layout.weather_data_entry_list);
126
127
128         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
129                 .getList().get((chosenDay));
130
131         if (forecast.getWeather().size() > 0) {
132             entries.set(0,
133                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
134                             forecast.getWeather().get(0).getDescription()));
135         }
136
137         if (forecast.getTemp().getDay() != null) {
138             double conversion = (Double) forecast.getTemp().getDay();
139             conversion = conversion - tempUnits;
140             entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem),
141                     tempFormatter.format(conversion)));
142         }
143
144         if (forecast.getTemp().getMax() != null) {
145             double conversion = (Double) forecast.getTemp().getMax();
146             conversion = conversion - tempUnits;
147             entries.set(2, new WeatherSpecificDataEntry(
148                     this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
149         }
150
151         if (forecast.getTemp().getMin() != null) {
152             double conversion = (Double) forecast.getTemp().getMin();
153             conversion = conversion - tempUnits;
154             entries.set(3, new WeatherSpecificDataEntry(
155                     this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
156         }
157
158
159         if (forecast.getClouds() != null) {
160             final double cloudiness = (Double) forecast.getClouds();
161             entries.set(6,
162                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
163                             tempFormatter.format(cloudiness)));
164         }
165
166         adapter.addAll(entries);
167         this.setListAdapter(adapter);
168     }
169
170     @Override
171     public void onResume() {
172         super.onResume();
173
174         final SharedPreferences sharedPreferences = PreferenceManager
175                 .getDefaultSharedPreferences(this.getActivity());
176
177         // 1. Update units of measurement.
178         final String keyPreference = this.getResources().getString(
179                 R.string.weather_preferences_units_key);
180         final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
181         final String celsius = this.getResources().getString(
182                 R.string.weather_preferences_units_celsius);
183         if (unitsPreferenceValue.equals(celsius)) {
184             this.mIsFahrenheit = false;
185         } else {
186             this.mIsFahrenheit = true;
187         }
188
189
190         // 2. Update weather data on display.
191
192         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
193                 .getForecastWeatherData();
194         if (forecastWeatherData != null) {
195             this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
196         } else {
197             // 2.1 Empty list by default
198             final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
199                     this.getActivity(), R.layout.weather_data_entry_list);
200             this.setListAdapter(adapter);
201         }
202
203
204
205         // 3. If language changed, try to retrieve new data for new language
206         // (new strings with the chosen language)
207         // keyPreference = this.getResources().getString(
208         // R.string.weather_preferences_language_key);
209         // final String languagePreferenceValue = sharedPreferences.getString(
210         // keyPreference, "");
211         // if (!languagePreferenceValue.equals(this.mLanguage)) {
212         // this.mLanguage = languagePreferenceValue;
213         // this.getWeather();
214         // }
215     }
216
217     private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
218         final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
219         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
220         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
221         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
222         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
223         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
224         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
225         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
226         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
227         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
228         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
229         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));
230
231         return entries;
232     }
233 }