Updated the empty list message with more contextual information---this time around server failures and errors.
This commit is contained in:
@@ -16,9 +16,11 @@
|
|||||||
package com.example.android.sunshine.app;
|
package com.example.android.sunshine.app;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.LoaderManager;
|
import android.support.v4.app.LoaderManager;
|
||||||
import android.support.v4.content.CursorLoader;
|
import android.support.v4.content.CursorLoader;
|
||||||
@@ -40,7 +42,7 @@ import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
|
|||||||
/**
|
/**
|
||||||
* Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
|
* Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
|
||||||
*/
|
*/
|
||||||
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
|
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
public static final String LOG_TAG = ForecastFragment.class.getSimpleName();
|
public static final String LOG_TAG = ForecastFragment.class.getSimpleName();
|
||||||
private ForecastAdapter mForecastAdapter;
|
private ForecastAdapter mForecastAdapter;
|
||||||
|
|
||||||
@@ -105,6 +107,20 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa
|
|||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||||
|
sp.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
super.onResume();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||||
|
sp.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
super.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
inflater.inflate(R.menu.forecastfragment, menu);
|
inflater.inflate(R.menu.forecastfragment, menu);
|
||||||
@@ -281,11 +297,29 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa
|
|||||||
if ( null != tv ) {
|
if ( null != tv ) {
|
||||||
// if cursor is empty, why? do we have an invalid location
|
// if cursor is empty, why? do we have an invalid location
|
||||||
int message = R.string.empty_forecast_list;
|
int message = R.string.empty_forecast_list;
|
||||||
if (!Utility.isNetworkAvailable(getActivity()) ) {
|
@SunshineSyncAdapter.LocationStatus int location = Utility.getLocationStatus(getActivity());
|
||||||
message = R.string.empty_forecast_list_no_network;
|
switch (location) {
|
||||||
|
case SunshineSyncAdapter.LOCATION_STATUS_SERVER_DOWN:
|
||||||
|
message = R.string.empty_forecast_list_server_down;
|
||||||
|
break;
|
||||||
|
case SunshineSyncAdapter.LOCATION_STATUS_SERVER_INVALID:
|
||||||
|
message = R.string.empty_forecast_list_server_error;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (!Utility.isNetworkAvailable(getActivity()) ) {
|
||||||
|
message = R.string.empty_forecast_list_no_network;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tv.setText(message);
|
tv.setText(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
|
if ( key.equals(getString(R.string.pref_location_status_key)) ) {
|
||||||
|
updateEmptyView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,8 @@ import android.net.NetworkInfo;
|
|||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.text.format.Time;
|
import android.text.format.Time;
|
||||||
|
|
||||||
|
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -253,7 +255,7 @@ public class Utility {
|
|||||||
* Returns true if the network is available or about to become available.
|
* Returns true if the network is available or about to become available.
|
||||||
*
|
*
|
||||||
* @param c Context used to get the ConnectivityManager
|
* @param c Context used to get the ConnectivityManager
|
||||||
* @return
|
* @return true if the network is available
|
||||||
*/
|
*/
|
||||||
static public boolean isNetworkAvailable(Context c) {
|
static public boolean isNetworkAvailable(Context c) {
|
||||||
ConnectivityManager cm =
|
ConnectivityManager cm =
|
||||||
@@ -263,4 +265,16 @@ public class Utility {
|
|||||||
return activeNetwork != null &&
|
return activeNetwork != null &&
|
||||||
activeNetwork.isConnectedOrConnecting();
|
activeNetwork.isConnectedOrConnecting();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param c Context used to get the SharedPreferences
|
||||||
|
* @return the location status integer type
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("ResourceType")
|
||||||
|
static public @SunshineSyncAdapter.LocationStatus
|
||||||
|
int getLocationStatus(Context c){
|
||||||
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
|
||||||
|
return sp.getInt(c.getString(R.string.pref_location_status_key), SunshineSyncAdapter.LOCATION_STATUS_UNKNOWN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -115,6 +115,7 @@
|
|||||||
<!-- Empty Weather Database -->
|
<!-- Empty Weather Database -->
|
||||||
<string name="empty_forecast_list">No Weather Information Available</string>
|
<string name="empty_forecast_list">No Weather Information Available</string>
|
||||||
<string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data.</string>
|
<string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data.</string>
|
||||||
|
<string name="empty_forecast_list_server_down">No weather information available. The server is not returning data.</string>
|
||||||
|
<string name="empty_forecast_list_server_error">No weather information available. The server is not returning valid data. Please check for an updated version of Sunshine.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user