WeatherInformation Android: no time for comments
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 7 Sep 2014 17:35:33 +0000 (19:35 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 7 Sep 2014 17:35:33 +0000 (19:35 +0200)
libs/jackson-core-2.3.3.jar [new file with mode: 0644]
res/layout/weather_map.xml
src/de/example/exampletdd/MapActivity.java
src/de/example/exampletdd/fragment/current/CurrentFragment.java
src/de/example/exampletdd/fragment/overview/OverviewFragment.java
src/de/example/exampletdd/model/WeatherLocationDbQueries.java

diff --git a/libs/jackson-core-2.3.3.jar b/libs/jackson-core-2.3.3.jar
new file mode 100644 (file)
index 0000000..8312650
Binary files /dev/null and b/libs/jackson-core-2.3.3.jar differ
index 0a71302..a50f806 100644 (file)
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
-        android:layout_toEndOf="@+id/weather_map_city"
+        android:layout_alignParentEnd="true"
         android:paddingStart="5dp"
         android:paddingEnd="5dp"
         android:text="country"
-        android:textAlignment="textStart"
+        android:textAlignment="textEnd"
         android:textAppearance="?android:attr/textAppearanceMedium"
         android:textIsSelectable="false"
         android:textStyle="bold|normal" />
index c489ebd..6f6e00e 100644 (file)
@@ -162,19 +162,26 @@ public class MapActivity extends FragmentActivity implements
     }
     
     public void onClickSaveLocation(final View v) {
-       if (this.mMarker == null) {
+       if (this.mMarker != null) {
                final LatLng position = this.mMarker.getPosition();
                
+               final TextView city = (TextView) this.findViewById(R.id.weather_map_city);
+            final TextView country = (TextView) this.findViewById(R.id.weather_map_country);
+            final String cityString = city.getText().toString();
+            final String countryString = country.getText().toString();
+            
                final DatabaseQueries query = new DatabaseQueries(this);
                final WeatherLocation weatherLocation = query.queryDataBase();
             if (weatherLocation != null) {
+               weatherLocation
+               .setCity(cityString)
+               .setCountry(countryString)
+               .setLatitude(position.latitude)
+               .setLongitude(position.longitude)
+               .setLastCurrentUIUpdate(null)
+               .setLastForecastUIUpdate(null);
                query.updateDataBase(weatherLocation);
             } else {
-                final TextView city = (TextView) this.findViewById(R.id.weather_map_city);
-                final TextView country = (TextView) this.findViewById(R.id.weather_map_country);
-                final String cityString = city.getText().toString();
-                final String countryString = country.getText().toString();
-                
                final WeatherLocation location = new WeatherLocation()
                        .setCity(cityString)
                        .setCountry(countryString)
@@ -213,13 +220,11 @@ public class MapActivity extends FragmentActivity implements
         country.setText(weatherLocation.getCountry());
 
         final LatLng point = new LatLng(weatherLocation.getLatitude(), weatherLocation.getLongitude());
-        this.mMap.clear();
-        if (this.mMarker == null) {
-               this.mMarker = this.mMap.addMarker(new MarkerOptions().position(point).draggable(true));
-        } else {
+        if (this.mMarker != null) {
                // Just one marker on map
-               this.mMarker.setPosition(point);
+               this.mMarker.remove();
         }
+        this.mMarker = this.mMap.addMarker(new MarkerOptions().position(point).draggable(true));
         this.mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 5));
         this.mMap.animateCamera(CameraUpdateFactory.zoomIn());
         this.mMap.animateCamera(CameraUpdateFactory.zoomTo(8), 2000, null);
@@ -287,7 +292,7 @@ public class MapActivity extends FragmentActivity implements
             // Default values
             String city = this.localContext.getString(R.string.city_not_found);
             String country = this.localContext.getString(R.string.country_not_found); 
-            if (addresses == null || addresses.size() <= 0) {
+            if (addresses != null && addresses.size() > 0) {
                if (addresses.get(0).getLocality() != null) {
                        city = addresses.get(0).getLocality();
                }
index 3f41ba0..b605248 100644 (file)
@@ -245,7 +245,7 @@ public class CurrentFragment extends Fragment {
         final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_current_picture);
         pictureView.setImageBitmap(picture);    
         
-        final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
+        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);
@@ -274,9 +274,8 @@ public class CurrentFragment extends Fragment {
                return false;
        }
        
-       final Calendar calendar = Calendar.getInstance();
-       final Date currentTime = calendar.getTime();
-       if (((currentTime.getTime() - lastUpdate.getTime()) / 1000) < 30) {
+       final Date currentTime = new Date();
+       if (((currentTime.getTime() - lastUpdate.getTime())) < 120000L) {
                return true;
        }
        
index f112dfa..8379d84 100644 (file)
@@ -103,7 +103,7 @@ public class OverviewFragment extends ListFragment {
                        (WeatherInformationApplication) getActivity().getApplication();
         final Forecast forecast = application.getForecast();
 
-        if (forecast != null && this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) {
+        if (forecast != null && this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) {
             this.updateUI(forecast);
         } else {
             // Load remote data (aynchronous)
@@ -262,9 +262,8 @@ public class OverviewFragment extends ListFragment {
                return false;
        }
        
-       final Calendar calendar = Calendar.getInstance();
-       final Date currentTime = calendar.getTime();
-       if (((currentTime.getTime() - lastUpdate.getTime()) / 1000) < 30) {
+       final Date currentTime = new Date();
+       if (((currentTime.getTime() - lastUpdate.getTime())) < 120000L) {
                return true;
        }
        
index 9f2e3e8..fa97ca2 100644 (file)
@@ -38,9 +38,7 @@ public class WeatherLocationDbQueries {
         final WeatherLocationDbQueries.DoQuery doQuery = new WeatherLocationDbQueries.DoQuery() {
 
                @Override
-               public WeatherLocation doQuery(final Cursor cursor) {
-                       final Calendar calendar = Calendar.getInstance();
-                       
+               public WeatherLocation doQuery(final Cursor cursor) {                   
                        final int id = cursor.getInt(cursor.getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation._ID));
                        final String city = cursor.getString(cursor.
                                        getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY));
@@ -51,18 +49,16 @@ public class WeatherLocationDbQueries {
                        Date lastCurrentUIUpdate = null;
                        if (!cursor.isNull(cursor
                                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE))) {
-                       final int UNIXTime = cursor.getInt(cursor
+                       final long javaTime = cursor.getLong(cursor
                                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE));
-                       calendar.setTimeInMillis((long)UNIXTime * 1000L);
-                       lastCurrentUIUpdate = calendar.getTime();
+                       lastCurrentUIUpdate = new Date(javaTime);
                        }
                        Date lasForecastUIUpdate = null;
                        if (!cursor.isNull(cursor
                                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE))) {
-                       final int UNIXTime = cursor.getInt(cursor
+                       final long javaTime = cursor.getLong(cursor
                                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE));
-                       calendar.setTimeInMillis((long)UNIXTime * 1000L);
-                       lastCurrentUIUpdate = calendar.getTime();
+                       lasForecastUIUpdate = new Date(javaTime);
                        }
                        final double latitude = cursor.getDouble(cursor.
                                        getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE));
@@ -95,13 +91,11 @@ public class WeatherLocationDbQueries {
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
                Date javaTime = weatherLocation.getLastCurrentUIUpdate();
                if (javaTime != null) {
-                       final int UNIXTime = (int) javaTime.getTime() / 1000;
-                       values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, UNIXTime);
+                       values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
                }
                javaTime = weatherLocation.getLastForecastUIUpdate();
                if (javaTime != null) {
-                       final int UNIXTime = (int) javaTime.getTime() / 1000;
-                       values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, UNIXTime);
+                       values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
                }
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
@@ -117,8 +111,18 @@ public class WeatherLocationDbQueries {
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_CITY, weatherLocation.getCity());
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY, weatherLocation.getCountry());
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED, weatherLocation.getIsSelected());
-               values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
-               values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
+               Date javaTime = weatherLocation.getLastCurrentUIUpdate();
+               if (javaTime != null) {
+                       values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE, javaTime.getTime());
+               } else {
+                       values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE);
+               }
+               javaTime = weatherLocation.getLastForecastUIUpdate();
+               if (javaTime != null) {
+                       values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE, javaTime.getTime());
+               } else {
+                       values.putNull(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE);
+               }
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
                
@@ -161,7 +165,7 @@ public class WeatherLocationDbQueries {
        // TODO: May I perform another query after this method (after closing almost everything but mDbHelper)
        private long updateDataBase(final String table, final String[] selectionArgs,
                        final String selection, final ContentValues values) {
-        final SQLiteDatabase db = this.mDbHelper.getReadableDatabase();
+        final SQLiteDatabase db = this.mDbHelper.getWritableDatabase();
         try {
                return db.update(table, values, selection, selectionArgs);
         } finally {