From dc9235c154a1fcc52a33554f87512ed703646e01 Mon Sep 17 00:00:00 2001 From: Samidh Talsania Date: Mon, 23 Feb 2015 00:29:59 +0530 Subject: [PATCH] Listview state now maintained on configuration change or when navigating back from a different activity. --- .../bluealeaf/dota2ticker/MainActivity.java | 56 ++++++++++++++----- .../dota2ticker/async/RestClient.java | 4 +- .../dota2ticker/events/GetMatchesEvent.java | 2 + 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java b/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java index 9b72823..8237778 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/MainActivity.java @@ -41,18 +41,25 @@ public class MainActivity extends ActionBarActivity { private MatchListAdapter adapter; private SwipeRefreshLayout swipeRefreshLayout; - private List matches ; + private ArrayList matches ; private boolean isSwiped = false; private Context mContext; private static final String tag = MainActivity.class.getName(); + private static final String INDEX = "INDEX"; + private static final String TOP = "TOP"; private AdView mAdView; + private int top; + private int index; + private boolean isInstanceSaved; + @Override protected void onCreate(Bundle savedInstanceState) { + //TODO: Make app snappier. Make db calls in background worker thread. // StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() // .detectDiskReads() // .detectDiskWrites() @@ -67,6 +74,7 @@ protected void onCreate(Bundle savedInstanceState) { // .build()); super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); @@ -81,9 +89,17 @@ public void onAdLoaded() { mContext = this; - } + if(savedInstanceState != null){ + isInstanceSaved = true; + index = savedInstanceState.getInt(INDEX); + top = savedInstanceState.getInt(TOP); + } + else{ + isInstanceSaved = false; + } + } @Override protected void onResume() { @@ -93,7 +109,7 @@ protected void onResume() { matches = new ArrayList(); adapter = new MatchListAdapter(this,matches); listView.setAdapter(adapter); - Log.d(tag,String.valueOf(matches.size())); + swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe); swipeRefreshLayout.setColorSchemeResources( @@ -116,7 +132,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) Match match = matches.get(position); Intent intent = new Intent(mContext,MatchDetailsActivity.class); intent.putExtra("MATCH_ID", match); - Log.d(tag,"Clicked"); + startActivity(intent); } @@ -135,12 +151,11 @@ protected void onPause() { super.onPause(); mAdView.pause(); - - //Unregister subscribed event BusProvider.getBusInstance().unregister(this); } + @Override public void onDestroy() { // Destroy the AdView. mAdView.destroy(); @@ -148,11 +163,26 @@ public void onDestroy() { super.onDestroy(); } + @Override + public void onSaveInstanceState(Bundle bundle){ + super.onSaveInstanceState(bundle); +// bundle.putParcelableArrayList("Matches",matches); + int index = listView.getFirstVisiblePosition(); + View v = listView.getChildAt(0); + int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop()); + bundle.putInt(INDEX,index); + bundle.putInt(TOP,top); + } + @Subscribe public void OnListReceivedFromDb(PassMatchListFromDBEvent event){ updateMatches(event.getMatchList()); adapter.notifyDataSetChanged(); + if(isInstanceSaved){ + listView.setSelectionFromTop(index, top); + Log.d(tag,"called"); + } } @Subscribe @@ -176,11 +206,6 @@ public int compare(Match lhs, Match rhs) { }); adapter.notifyDataSetChanged(); } - - - - Log.d(tag,"OnListReceived"); - } @Subscribe @@ -190,8 +215,11 @@ public void OnConnectionError(ConnectionErrorEvent event){ isSwiped = false; } Toast.makeText(this,event.getMessage(),Toast.LENGTH_LONG).show(); + //TODO + //why is this used when No extra match data is coming. maybe because if not written it will show an empty list. + // Need to check this adapter.notifyDataSetChanged(); - Log.d(tag,"OnConnectionError"); + } @Subscribe @@ -200,7 +228,9 @@ public void OnNoNewMatchesReceived(NoNewMatchesEvent event){ swipeRefreshLayout.setRefreshing(false); isSwiped = false; } - + //TODO + //why is this used when No extra match data is coming. maybe because if not written it will show an empty list. + // Need to check this adapter.notifyDataSetChanged(); } diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java b/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java index f89117c..6207f95 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/async/RestClient.java @@ -25,14 +25,14 @@ public class RestClient { private static final String tag = RestClient.class.getName(); public static void getMatchesList(long id){ - Log.d(tag, "getMatchesList"); +// Log.d(tag, "getMatchesList"); Gson gson = new GsonBuilder() .create(); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(Endpoints.GET_MATCHES_ENDPOINT) - .setLogLevel(RestAdapter.LogLevel.FULL) + .setLogLevel(RestAdapter.LogLevel.NONE) .setConverter(new GsonConverter(gson)) .setClient(new OkClient(BusProvider.getClientInstance())) .build(); diff --git a/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java b/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java index b856f92..472aab2 100644 --- a/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java +++ b/app/src/main/java/com/bluealeaf/dota2ticker/events/GetMatchesEvent.java @@ -61,6 +61,8 @@ public void OnReceiveNewMatches(PassMatchListFromNetEvent event) { //Send another event to main ui to update it with the new matches BusProvider.getBusInstance().post(new UpdateMatchesEvent(dbMatchList)); } + // Can handle this better. Remove this else. Check for network connection before making network call(line 44 of this file) + else{ if(event.getMessage().equalsIgnoreCase("Updated")){ BusProvider.getBusInstance().post(new NoNewMatchesEvent());