Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab contents shouldn't reload every time, like how iOS works #200

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class JasonViewActivity extends AppCompatActivity {
public JasonModel model;
public JSONObject preload;
private ProgressBar loading;
public Integer depth;

private ArrayList<RecyclerView.OnItemTouchListener> listViewOnItemTouchListeners;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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"));
}
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/jasonette/seed/Launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -150,6 +171,7 @@ public void onCreate() {
}

this.env = new JSONObject();
this.models = new JSONObject();

// device info
JSONObject device = new JSONObject();
Expand Down