From 9005119247536350e4c26fa5afdc273cddde4baa Mon Sep 17 00:00:00 2001 From: gliechtenstein Date: Thu, 30 Nov 2017 20:04:55 -0800 Subject: [PATCH] Tab contents shouldn't reload every time, like how iOS works --- .../seed/Core/JasonViewActivity.java | 56 +++++++++++++++++-- .../com/jasonette/seed/Launcher/Launcher.java | 22 ++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java b/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java index 227430c6..44bb102d 100644 --- a/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java +++ b/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java @@ -83,6 +83,7 @@ public class JasonViewActivity extends AppCompatActivity { public JasonModel model; public JSONObject preload; private ProgressBar loading; + public Integer depth; private ArrayList listViewOnItemTouchListeners; @@ -221,6 +222,7 @@ protected void onCreate(Bundle savedInstanceState) { } else { url = getString(R.string.url); } + depth = intent.getIntExtra("depth", 0); preload = null; if (intent.hasExtra("preload")) { try { @@ -307,7 +309,33 @@ private void onRefresh() { } - + private void onSwitchTab(String newUrl, String newParams, Intent intent) { + // if tab transition, restore from stored tab using this.build() + try { + // remove all touch listeners before replacing + // Use case : Tab bar + removeListViewOnItemTouchListeners(); + // Store the current model + ((Launcher)getApplicationContext()).setTabModel(model.url+model.params, model); + // Retrieve the new view's model + + JasonModel m = ((Launcher)getApplicationContext()).getTabModel(newUrl + newParams); + + if (m == null) { + // refresh + removeListViewOnItemTouchListeners(); + model = new JasonModel(newUrl, intent, this); + onRefresh(); + } else { + // build + model = m; + setup_body(m.rendered); + } + } catch (Exception e) { + + } + } + @Override protected void onPause() { // Unregister since the activity is paused. @@ -330,6 +358,7 @@ protected void onPause() { if (model.params != null) temp_model.put("params", model.params); if (model.session != null) temp_model.put("session", model.session); if (model.action != null) temp_model.put("action", model.action); + temp_model.put("depth", depth); if (model.url!= null){ editor.putString(model.url, temp_model.toString()); editor.commit(); @@ -363,7 +392,9 @@ protected void onResume() { if(temp_model.has("state")) model.state = temp_model.getJSONObject("state"); if(temp_model.has("var")) model.var = temp_model.getJSONObject("var"); if(temp_model.has("cache")) model.cache = temp_model.getJSONObject("cache"); - if(temp_model.has("params")) model.params = temp_model.getJSONObject("params"); + if (temp_model.getInt("depth") == depth) { + if (temp_model.has("params")) model.params = temp_model.getJSONObject("params"); + } if(temp_model.has("session")) model.session = temp_model.getJSONObject("session"); if(temp_model.has("action")) model.action = temp_model.getJSONObject("action"); @@ -1299,7 +1330,20 @@ public void href(final JSONObject action, JSONObject data, JSONObject event, Con editor.remove(url); editor.commit(); - if(transition.equalsIgnoreCase("replace")){ + if(transition.equalsIgnoreCase("switchtab")) { + if (action.getJSONObject("options").has("preload")) { + preload = action.getJSONObject("options").getJSONObject("preload"); + } + Intent intent = new Intent(this, JasonViewActivity.class); + intent.putExtra("depth", depth); + if(params!=null) { + intent.putExtra("params", params); + onSwitchTab(url, params, intent); + } else { + params = "{}"; + onSwitchTab(url, params, intent); + } + } else if(transition.equalsIgnoreCase("replace")){ // remove all touch listeners before replacing // Use case : Tab bar removeListViewOnItemTouchListeners(); @@ -1308,6 +1352,7 @@ public void href(final JSONObject action, JSONObject data, JSONObject event, Con if(params!=null) { intent.putExtra("params", params); } + intent.putExtra("depth", depth); model = new JasonModel(url, intent, this); if (action.getJSONObject("options").has("preload")) { preload = action.getJSONObject("options").getJSONObject("preload"); @@ -1322,6 +1367,7 @@ public void href(final JSONObject action, JSONObject data, JSONObject event, Con if (action.getJSONObject("options").has("preload")) { intent.putExtra("preload", action.getJSONObject("options").getJSONObject("preload").toString()); } + intent.putExtra("depth", depth+1); startActivity(intent); } } @@ -2126,7 +2172,7 @@ public boolean onTabSelected(int position, boolean wasSelected) { if (href.has("transition")) { // nothing } else { - href.put("transition", "replace"); + href.put("transition", "switchtab"); } action.put("options", href); href(action, new JSONObject(), new JSONObject(), JasonViewActivity.this); @@ -2138,7 +2184,7 @@ public boolean onTabSelected(int position, boolean wasSelected) { JSONObject action = new JSONObject(); JSONObject options = new JSONObject(); options.put("url", url); - options.put("transition", "replace"); + options.put("transition", "switchtab"); if (item.has("preload")) { options.put("preload", item.getJSONObject("preload")); } diff --git a/app/src/main/java/com/jasonette/seed/Launcher/Launcher.java b/app/src/main/java/com/jasonette/seed/Launcher/Launcher.java index b6d130be..78f529f2 100644 --- a/app/src/main/java/com/jasonette/seed/Launcher/Launcher.java +++ b/app/src/main/java/com/jasonette/seed/Launcher/Launcher.java @@ -11,6 +11,7 @@ import android.util.Log; import com.bumptech.glide.request.target.ViewTarget; +import com.jasonette.seed.Core.JasonModel; import com.jasonette.seed.Core.JasonViewActivity; import com.jasonette.seed.Helper.JasonHelper; import com.jasonette.seed.R; @@ -36,6 +37,7 @@ public class Launcher extends Application { private JSONObject handlers; private JSONObject global; private JSONObject env; + private JSONObject models; private JSONObject services; private static Context currentContext; @@ -58,6 +60,25 @@ public static void setCurrentContext(Context context) { } + public void setTabModel(String url, JasonModel model) { + try { + models.put(url, model); + } catch (Exception e) { + + } + } + public JasonModel getTabModel(String url) { + try { + if (models.has(url)) { + return (JasonModel)models.get(url); + } else { + return null; + } + } catch (Exception e) { + return null; + } + } + public JSONObject getEnv(){ return this.env; } @@ -150,6 +171,7 @@ public void onCreate() { } this.env = new JSONObject(); + this.models = new JSONObject(); // device info JSONObject device = new JSONObject();