Skip to content

Commit

Permalink
Refreshing UI now shows automatically at start up
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsp committed Jan 9, 2014
1 parent a757d00 commit 408e8dc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
@@ -1,10 +1,8 @@
package nz.net.speakman.android.whatsshaking.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
Expand Down Expand Up @@ -32,6 +30,7 @@ public class MainActivity extends ActionBarActivity implements ActionBar.OnNavig
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

private PopupWindow mFiltersPopup;
private boolean mFirstLoad;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -57,6 +56,20 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, EarthquakeListFragment.newInstance(), FRAGMENT_TAG_EARTHQUAKE_LIST)
.commit();
mFirstLoad = true;
} else {
mFirstLoad = false;
}
}

@Override
protected void onResume() {
super.onResume();
// We only want to auto-retrieve our quakes on first load. Since child fragments
// might want to update UI, they'll need to subscribe to Earthquake.DATA_RETRIEVAL_STARTED
// broadcasts - which they can only do after their parent Activity has been created.
// Therefore, we have to start retrieval after onCreate() has returned.
if (mFirstLoad) {
retrieveNewEarthquakes();
}
}
Expand Down Expand Up @@ -154,10 +167,7 @@ public Loader<Boolean> onCreateLoader(int i, Bundle bundle) {
@Override
public void onLoadFinished(Loader<Boolean> booleanLoader, Boolean success) {
getSupportLoaderManager().destroyLoader(booleanLoader.getId());
if (success) {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Earthquake.DATA_UPDATED));
} else {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Earthquake.DATA_RETRIEVAL_FAILED));
if (!success) {
Crouton.makeText(this, R.string.toast_check_connectivity, Style.ALERT).show();
}
}
Expand Down
Expand Up @@ -33,23 +33,29 @@
public class EarthquakeListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>, OnRefreshListener {

private DBHelper mDBHelper;
private BroadcastReceiver mDataChangeBroadcastReceiver = new BroadcastReceiver() {

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
hideRefreshUI();
if (intent.getAction().equals(Earthquake.DATA_UPDATED)) {
String action = intent.getAction();
if (action.equals(Earthquake.DATA_RETRIEVAL_STARTED)) {
showRefreshUI();
}
else if (action.equals(FiltersPopup.FILTER_UPDATED_MAGNITUDE)
|| action.equals(FiltersPopup.FILTER_UPDATED_MAGNITUDE)
|| action.equals(FiltersPopup.FILTER_UPDATED_MAGNITUDE)){
// TODO Perhaps implement a delay, so we don't just hit the DB every 10ms.
updateAdapter(true);
}
else if (action.equals(Earthquake.DATA_RETRIEVAL_SUCCESSFUL)) {
hideRefreshUI();
updateAdapter(true);
} else if (action.equals(Earthquake.DATA_RETRIEVAL_FAILED)) {
hideRefreshUI();
}
}
};

private BroadcastReceiver mFilterChangeBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Perhaps implement a delay, so we don't just hit the DB every 10ms.
updateAdapter(true);
}
};
private LocalBroadcastManager mBroadcastMgr;
private SimpleCursorAdapter mAdapter;
private PullToRefreshLayout mPullToRefreshLayout;
Expand Down Expand Up @@ -91,12 +97,14 @@ public void onActivityCreated(Bundle savedInstanceState) {
mDBHelper = DBHelper.getInstance(getActivity());
mBroadcastMgr = LocalBroadcastManager.getInstance(getActivity());

mBroadcastMgr.registerReceiver(mDataChangeBroadcastReceiver, new IntentFilter(Earthquake.DATA_UPDATED));
mBroadcastMgr.registerReceiver(mDataChangeBroadcastReceiver, new IntentFilter(Earthquake.DATA_RETRIEVAL_FAILED));
mBroadcastMgr.registerReceiver(mBroadcastReceiver, new IntentFilter(Earthquake.DATA_RETRIEVAL_STARTED));

mBroadcastMgr.registerReceiver(mBroadcastReceiver, new IntentFilter(Earthquake.DATA_RETRIEVAL_SUCCESSFUL));
mBroadcastMgr.registerReceiver(mBroadcastReceiver, new IntentFilter(Earthquake.DATA_RETRIEVAL_FAILED));

mBroadcastMgr.registerReceiver(mFilterChangeBroadcastReceiver, new IntentFilter(FiltersPopup.FILTER_UPDATED_MAGNITUDE));
mBroadcastMgr.registerReceiver(mFilterChangeBroadcastReceiver, new IntentFilter(FiltersPopup.FILTER_UPDATED_MMI));
mBroadcastMgr.registerReceiver(mFilterChangeBroadcastReceiver, new IntentFilter(FiltersPopup.FILTER_UPDATED_DATE));
mBroadcastMgr.registerReceiver(mBroadcastReceiver, new IntentFilter(FiltersPopup.FILTER_UPDATED_MAGNITUDE));
mBroadcastMgr.registerReceiver(mBroadcastReceiver, new IntentFilter(FiltersPopup.FILTER_UPDATED_MMI));
mBroadcastMgr.registerReceiver(mBroadcastReceiver, new IntentFilter(FiltersPopup.FILTER_UPDATED_DATE));

updateAdapter(false);
}
Expand All @@ -105,7 +113,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
public void onDestroy() {
super.onDestroy();
if (mBroadcastMgr != null) {
mBroadcastMgr.unregisterReceiver(mDataChangeBroadcastReceiver);
mBroadcastMgr.unregisterReceiver(mBroadcastReceiver);
}
if (mDBHelper != null) {
mDBHelper = null;
Expand Down Expand Up @@ -172,6 +180,12 @@ public void onRefreshStarted(View view) {
}
}

private void showRefreshUI() {
if (mPullToRefreshLayout != null) {
mPullToRefreshLayout.setRefreshing(true);
}
}

private void hideRefreshUI() {
if (mPullToRefreshLayout != null) {
mPullToRefreshLayout.setRefreshComplete();
Expand Down
Expand Up @@ -23,8 +23,9 @@ public static Dao<Earthquake, Integer> getDao(ConnectionSource connectionSource)
public static final int LOADER_NETWORK = 0;
public static final int LOADER_DB = 1;

public static final String DATA_UPDATED = "nz.net.speakman.whatsshaking.model.Earthquake.DATA_UPDATED";
public static final String DATA_RETRIEVAL_SUCCESSFUL = "nz.net.speakman.whatsshaking.model.Earthquake.DATA_RETRIEVAL_SUCCESSFUL";
public static final String DATA_RETRIEVAL_FAILED = "nz.net.speakman.whatsshaking.model.Earthquake.DATA_RETRIEVAL_FAILED";
public static final String DATA_RETRIEVAL_STARTED = "nz.net.speakman.whatsshaking.model.Earthquake.DATA_RETRIEVAL_STARTED";

public static final int TSUNAMI_UNLIKELY = 0;
public static final int TSUNAMI_POSSIBLE = 1;
Expand Down
@@ -1,7 +1,9 @@
package nz.net.speakman.android.whatsshaking.network.earthquakeretrieval;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.LocalBroadcastManager;
import com.j256.ormlite.dao.Dao;
import nz.net.speakman.android.whatsshaking.db.DBHelper;
import nz.net.speakman.android.whatsshaking.model.Earthquake;
Expand All @@ -22,19 +24,31 @@ public EarthquakeLoader(Context context) {
super(context);
}



@Override
public Boolean loadInBackground() {
EarthquakeRetrieval earthquakeRetrieval = new UsgsRetrieval();
List<Earthquake> earthquakes = earthquakeRetrieval.getEarthquakes(getContext());
if (earthquakes == null) {
return false;
}
boolean success = storeEarthquakesInDb(earthquakes);
// If we succeeded in downloading & storing quakes, update our "last checked" date to now.
if (success) {
new Preferences(getContext()).setLastCheckedDate(new DateTime());
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getContext());
broadcastManager.sendBroadcast(new Intent(Earthquake.DATA_RETRIEVAL_STARTED));
boolean success = false;
try {
EarthquakeRetrieval earthquakeRetrieval = new UsgsRetrieval();
List<Earthquake> earthquakes = earthquakeRetrieval.getEarthquakes(getContext());
if (earthquakes != null) {
success = storeEarthquakesInDb(earthquakes);
// If we succeeded in downloading & storing quakes, update our "last checked" date to now.
if (success) {
new Preferences(getContext()).setLastCheckedDate(new DateTime());
}
}
} catch (Exception e) {
// If we get some unhandled exception here, then our data retrieval or storage probably failed.
success = false;
} finally {
Intent intent = success ? new Intent(Earthquake.DATA_RETRIEVAL_SUCCESSFUL) : new Intent(Earthquake.DATA_RETRIEVAL_FAILED);
broadcastManager.sendBroadcast(intent);
return success;
}
return success;
}

private boolean storeEarthquakesInDb(final List<Earthquake> earthquakes) {
Expand Down

0 comments on commit 408e8dc

Please sign in to comment.