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