42
42
import butterknife .Bind ;
43
43
import butterknife .ButterKnife ;
44
44
import butterknife .OnClick ;
45
- import timber .log .Timber ;
46
45
47
46
import static android .os .Looper .getMainLooper ;
48
47
import static java .lang .Math .round ;
@@ -60,6 +59,9 @@ public class InformationFragment extends BaseFragment {
60
59
@ Bind (R .id .conditionImage )
61
60
ImageView conditionImage ;
62
61
62
+ @ Bind (R .id .timeText )
63
+ TextView timeText ;
64
+
63
65
@ Bind (R .id .dateText )
64
66
TextView dateText ;
65
67
@@ -75,6 +77,8 @@ void weatherLayoutClicked() {
75
77
76
78
private WeatherModule weatherModule ;
77
79
private Daily extendedDaily ;
80
+ private Handler weatherHandler ;
81
+ private Handler timeHandler ;
78
82
79
83
public InformationFragment () {
80
84
// Required empty public constructor
@@ -102,25 +106,9 @@ public void onCreate(Bundle savedInstanceState) {
102
106
public void onViewCreated (View view , Bundle savedInstanceState ) {
103
107
104
108
super .onViewCreated (view , savedInstanceState );
105
-
106
- // start the clock
107
- final Handler someHandler = new Handler (getMainLooper ());
108
- someHandler .postDelayed (new Runnable () {
109
- @ Override
110
- public void run () {
111
- String currentDateString = DateFormat .getDateInstance (DateFormat .LONG , Locale .getDefault ()).format (new Date ());
112
- dateText .setText (currentDateString );
113
- someHandler .postDelayed (this , DATE_INTERVAL );
114
- }
115
- }, 10 );
116
-
117
- // start the weather module
118
- if (getConfiguration ().showWeatherModule () && getConfiguration ().getDarkSkyKey () != null
119
- && getConfiguration ().getLatitude () != null && getConfiguration ().getLongitude () != null ) {
120
- connectWeatherModule ();
121
- } else {
122
- weatherLayout .setVisibility (View .GONE );
123
- }
109
+ weatherHandler = new Handler (getMainLooper ());
110
+ timeHandler = new Handler (getMainLooper ());
111
+ timeHandler .postDelayed (timeRunnable , 1000 );
124
112
}
125
113
126
114
@ Override
@@ -136,6 +124,9 @@ public void onResume() {
136
124
// this picks up changes made in the settings and connects weather if needed
137
125
if (getConfiguration ().showWeatherModule () && getConfiguration ().getDarkSkyKey () != null
138
126
&& getConfiguration ().getLatitude () != null && getConfiguration ().getLongitude () != null ) {
127
+ if (weatherHandler != null ) {
128
+ weatherHandler .removeCallbacks (weatherRunnable );
129
+ }
139
130
connectWeatherModule ();
140
131
} else {
141
132
disconnectWeatherModule ();
@@ -147,9 +138,35 @@ && getConfiguration().getLatitude() != null && getConfiguration().getLongitude()
147
138
public void onDetach () {
148
139
super .onDetach ();
149
140
disconnectWeatherModule ();
141
+ if (timeHandler != null ) {
142
+ timeHandler .removeCallbacks (timeRunnable );
143
+ }
150
144
}
145
+
146
+ private Runnable timeRunnable = new Runnable () {
147
+ @ Override
148
+ public void run () {
149
+ String currentDateString = DateFormat .getDateInstance (DateFormat .LONG , Locale .getDefault ()).format (new Date ());
150
+ String currentTimeString = DateFormat .getTimeInstance (DateFormat .DEFAULT , Locale .getDefault ()).format (new Date ());
151
+ dateText .setText (currentDateString );
152
+ timeText .setText (currentTimeString );
153
+ if (timeHandler != null ) {
154
+ timeHandler .postDelayed (timeRunnable , 1000 );
155
+ }
156
+ }
157
+ };
158
+
159
+ private Runnable weatherRunnable = new Runnable () {
160
+ @ Override
161
+ public void run () {
162
+ weatherHandler .postDelayed (weatherRunnable , DATE_INTERVAL );
163
+ }
164
+ };
151
165
152
166
private void disconnectWeatherModule () {
167
+ if (weatherHandler != null ) {
168
+ weatherHandler .removeCallbacks (weatherRunnable );
169
+ }
153
170
if (weatherModule != null ) {
154
171
weatherModule .cancelDarkSkyHourlyForecast ();
155
172
}
@@ -162,7 +179,6 @@ private void connectWeatherModule() {
162
179
163
180
final String apiKey = getConfiguration ().getDarkSkyKey ();
164
181
final String units = getConfiguration ().getWeatherUnits ();
165
- Timber .d ("units: " + units );
166
182
final String lat = getConfiguration ().getLatitude ();
167
183
final String lon = getConfiguration ().getLongitude ();
168
184
weatherModule .getDarkSkyHourlyForecast (apiKey , units , lat , lon , new WeatherModule .ForecastListener () {
@@ -173,6 +189,11 @@ public void onWeatherToday(String icon, double temperature, String summary) {
173
189
String displayUnits = (units .equals ( DarkSkyRequest .UNITS_US )? getString (R .string .text_f ): getString (R .string .text_c ));
174
190
temperatureText .setText (getString (R .string .text_temperature , String .valueOf (round (temperature )), displayUnits ));
175
191
conditionImage .setImageDrawable (ResourcesCompat .getDrawable (getResources (), WeatherUtils .getIconForWeatherCondition (icon ), getActivity ().getTheme ()));
192
+
193
+ // start the clock
194
+ if (weatherHandler != null ) {
195
+ weatherHandler .postDelayed (weatherRunnable , DATE_INTERVAL );
196
+ }
176
197
}
177
198
178
199
@ Override
0 commit comments