WeatherInformation: current with progress and error
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 23 Sep 2014 19:41:10 +0000 (21:41 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 23 Sep 2014 19:41:10 +0000 (21:41 +0200)
res/layout/weather_current_fragment.xml
res/values/strings.xml
src/de/example/exampletdd/fragment/current/CurrentFragment.java
src/de/example/exampletdd/fragment/overview/OverviewFragment.java

index 9e2b192..d03debb 100644 (file)
@@ -4,6 +4,10 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
 
+    <FrameLayout 
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+        
     <!-- TODO: align start/end feels like-snow humidity-rain wind-clouds -->
     <LinearLayout
         android:layout_width="match_parent"
                        android:textStyle="normal" />
         </LinearLayout>
        </LinearLayout>
+       
+    <ProgressBar
+        android:id="@+id/weather_current_progressbar"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:indeterminateBehavior="repeat"
+        android:indeterminateDuration="3500"
+        android:indeterminateOnly="true"
+        android:visibility="gone" />
+    
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:padding="25dp"
+        android:layout_gravity="center"
+        android:orientation="horizontal" >
+
+        <TextView
+            android:id="@+id/weather_current_error_message"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:text="No data available"
+            android:textAppearance="?android:attr/textAppearanceSmall"
+            android:visibility="gone" />    
+    </LinearLayout>
+       </FrameLayout>
 </ScrollView>
index 970c490..4df58a2 100644 (file)
     <string name="uri_api_weather_forecast">http://api.openweathermap.org/data/{0}/forecast/daily?lat={1}&amp;lon={2}&amp;cnt={3}&amp;mode=json</string>
     <string name="uri_api_geocoding">https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&amp;sensor=true</string>
     <string name="api_version">2.5</string>
-    <string name="text_default_city">London,uk</string>
-    <string name="text_field_city">City name</string>
-    <string name="text_field_latitude">Latitude</string>
-    <string name="text_field_longitude">Longitude</string>
-    <string name="text_field_sun_rise">Sun rise time:</string>
-    <string name="text_field_sun_set">Sun set time:</string>
-    <string name="text_field_description">Weather description:</string>
-    <string name="text_field_tem">Temperature:</string>
-    <string name="text_field_tem_min">Min temperature:</string>
-    <string name="text_field_tem_max">Max temperature:</string>
-    <string name="text_field_cloudiness">Cloudiness in %:</string>
-    <string name="text_field_rain_time">Rain time:</string>
-    <string name="text_field_rain_amount">Rain amount:</string>
-    <string name="text_field_wind_speed">Wind speed:</string>
-    <string name="text_field_humidity">Humidity in %:</string>
+    <string name="text_field_sun_rise">SUN RISE</string>
+    <string name="text_field_sun_set">SUN SET</string>
+    <string name="text_field_feels_like">FEELS LIKE</string>
+    <string name="text_field_clouds">CLOUDS</string>
+    <string name="text_field_rain">RAIN</string>
+    <string name="text_field_wind">WIND</string>
+    <string name="text_field_snow">SNOW</string>
+    <string name="text_field_pressure">PRESSURE</string>
+    <string name="text_field_humidity">HUMIDITY</string>
     <string name="button_ok">OK</string>
     <string name="error_dialog_connection_tiemout">Connection error timeout</string>
     <string name="error_dialog_generic_error">Impossible to receive weather data.</string>
@@ -52,4 +46,9 @@
     <string name="title_section3">Section 3</string>
     <string name="weather_map_button_savelocation">Save Location</string>
        <string name="weather_map_button_getlocation">Get Location</string>
+       <string name="text_units_mm3h">mm 3h</string>
+       <string name="text_units_percent">%</string>
+       <string name="text_units_centigrade">ÂșC</string>
+       <string name="text_units_ms">m/s</string>
+       <string name="text_units_hpa">hpa</string>
 </resources>
index 502d95d..7c4dbc3 100644 (file)
@@ -31,6 +31,7 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import com.fasterxml.jackson.core.JsonParseException;
@@ -77,13 +78,19 @@ public class CurrentFragment extends Fragment {
                                (WeatherInformationApplication) getActivity().getApplication();
                 application.setCurrent(current);
             }
-        }
+        }     
+        
+        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);
     }
 
     @Override
     public void onResume() {
         super.onResume();
 
+        
         this.mReceiver = new BroadcastReceiver() {
 
                        @Override
@@ -103,6 +110,13 @@ public class CurrentFragment extends Fragment {
                                    final WeatherLocation weatherLocation = query.queryDataBase();
                                    weatherLocation.setLastCurrentUIUpdate(new Date());
                                    query.updateDataBase(weatherLocation);
+                                       } 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);
                                        }
                                }
                        }
@@ -114,10 +128,18 @@ public class CurrentFragment extends Fragment {
         LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext())
                                                        .registerReceiver(this.mReceiver, filter);
 
+        // Empty UI
+        this.clearUI();
+        
         final DatabaseQueries query = new DatabaseQueries(this.getActivity().getApplicationContext());
         final WeatherLocation weatherLocation = query.queryDataBase();
         if (weatherLocation == null) {
             // Nothing to do.
+               // Show error message
+               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);
             return;
         }
         
@@ -130,6 +152,10 @@ 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);
             final CurrentTask task = new CurrentTask(
                        this.getActivity().getApplicationContext(),
                     new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")),
@@ -281,6 +307,11 @@ 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);
@@ -291,25 +322,55 @@ public class CurrentFragment extends Fragment {
         final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_current_description);
         descriptionView.setText(description);
         
-        final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_current_humidity_value);
-        humidityValueView.setText(humidityValue);
-        final TextView pressureValueView = (TextView) getActivity().findViewById(R.id.weather_current_pressure_value);
-        pressureValueView.setText(pressureValue);
-        final TextView windValueView = (TextView) getActivity().findViewById(R.id.weather_current_wind_value);
-        windValueView.setText(windValue);
-        final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_current_rain_value);
-        rainValueView.setText(rainValue);
-        final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_current_clouds_value);
-        cloudsValueView.setText(cloudsValue);
-        final TextView snowValueView = (TextView) getActivity().findViewById(R.id.weather_current_snow_value);
-        snowValueView.setText(snowValue);
-        final TextView feelsLikeView = (TextView) getActivity().findViewById(R.id.weather_current_feelslike_value);
-        feelsLikeView.setText(feelsLike);
+        ((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(
+                       this.getActivity().getApplicationContext().getString(R.string.text_units_ms));
         
-        final TextView sunRiseTimeView = (TextView) getActivity().findViewById(R.id.weather_current_sunrise_value);
-        sunRiseTimeView.setText(sunRiseTime);
-        final TextView sunSetTimeView = (TextView) getActivity().findViewById(R.id.weather_current_sunset_value);
-        sunSetTimeView.setText(sunSetTime);
+        ((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(
+                       this.getActivity().getApplicationContext().getString(R.string.text_units_centigrade));
+        
+        ((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);
     }
     
     private boolean isDataFresh(final Date lastUpdate) {
@@ -318,7 +379,7 @@ public class CurrentFragment extends Fragment {
        }
        
        final Date currentTime = new Date();
-       // TODO: static resource instead of 120000L
+       // TODO: user settings instead of 120000L
        if (((currentTime.getTime() - lastUpdate.getTime())) < 120000L) {
                return true;
        }
@@ -326,6 +387,53 @@ public class CurrentFragment extends Fragment {
        return false;
     }
     
+    private void clearUI() {
+
+        ((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
@@ -345,7 +453,6 @@ public class CurrentFragment extends Fragment {
 
         @Override
         protected Current doInBackground(final Object... params) {
-               Log.i(TAG, "CurrentTask doInBackground");
                final double latitude = (Double) params[0];
             final double longitude = (Double) params[1];
   
@@ -389,11 +496,6 @@ public class CurrentFragment extends Fragment {
         protected void onPostExecute(final Current current) {
                // TODO: Is AsyncTask calling this method even when RunTimeException in doInBackground method?
                // I hope so, otherwise I must catch(Throwable) in doInBackground method :(
-               if (current == null) {
-                       // Nothing to do
-                       // TODO: Should I show some error message? I am not doing it on WP8 Should I do it on WP8?
-                       return;
-               }
 
             // Call updateUI on the UI thread.
             final Intent currentData = new Intent("de.example.exampletdd.UPDATECURRENT");
index edc0d7a..c5e5e53 100644 (file)
@@ -295,7 +295,7 @@ public class OverviewFragment extends ListFragment {
        }
        
        final Date currentTime = new Date();
-       // TODO: static resource instead of 120000L
+       // TODO: user settings instead of 120000L
        if (((currentTime.getTime() - lastUpdate.getTime())) < 120000L) {
                return true;
        }
@@ -322,7 +322,6 @@ public class OverviewFragment extends ListFragment {
         
         @Override
         protected Forecast doInBackground(final Object... params) {
-            Log.i(TAG, "OverviewFragment doInBackground");
             final double latitude = (Double) params[0];
             final double longitude = (Double) params[1];