forked from turbolinks/turbolinks-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
149 lines (118 loc) · 5.12 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.basecamp.turbolinks.demo;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.basecamp.turbolinks.TurbolinksAdapter;
import com.basecamp.turbolinks.TurbolinksDebugCallback;
import com.basecamp.turbolinks.TurbolinksSession;
import com.basecamp.turbolinks.TurbolinksView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements TurbolinksAdapter {
// Change the BASE_URL to an address that your VM or device can hit.
// private static final String BASE_URL = "http://10.0.1.100:9292";
//For Testing a long scrollview page
// private static final String BASE_URL = "https://stackoverflow.com/questions/27515236/how-to-determine-the-content-size-of-a-wkwebview";
//For testing page made with Turbolinks
private static final String BASE_URL = "https://cookpad.com/us";
private static final String INTENT_URL = "intentUrl";
private String location;
private TurbolinksView turbolinksView;
// -----------------------------------------------------------------------
// Activity overrides
// -----------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the custom TurbolinksView object in your layout
turbolinksView = (TurbolinksView) findViewById(R.id.turbolinks_view);
Log.d("Demo", "about to enable logging");
// For this demo app, we force debug logging on. You will only want to do
// this for debug builds of your app (it is off by default)
TurbolinksSession.setDebugLoggingEnabled(true);
Log.d("Demo", "Enabled logging");
// For this example we set a default location, unless one is passed in through an intent
location = getIntent().getStringExtra(INTENT_URL) != null ? getIntent().getStringExtra(INTENT_URL) : BASE_URL;
TurbolinksDebugCallback debugCallback = new TurbolinksDebugCallback() {
@Override
public void logEvent(int logLevel, String tag, String msg) {
Log.d("Demo", "Received callback from debug callback. Log level = "
+ logLevel + ", Tag == " + tag + ", Msg == " + msg);
}
};
// Execute the visit
TurbolinksSession.getDefault(this)
.activity(this)
.adapter(this)
.view(turbolinksView)
.debugCallback(debugCallback)
.visit(location);
Log.d("Demo", "Finished all initialization");
}
@Override
protected void onRestart() {
super.onRestart();
// Since the webView is shared between activities, we need to tell Turbolinks
// to load the location from the previous activity upon restarting
TurbolinksSession.getDefault(this)
.activity(this)
.adapter(this)
.restoreWithCachedSnapshot(true)
.view(turbolinksView)
.visit(location);
}
// -----------------------------------------------------------------------
// TurbolinksAdapter interface
// -----------------------------------------------------------------------
@Override
public void onPageFinished() {
}
@Override
public void onPageSupportsTurbolinks(boolean bool) {
}
@Override
public void reloadPageViaRefreshTriggered() {
}
@Override
public void onReceivedError(int errorCode) {
handleError(errorCode);
}
@Override
public void pageInvalidated() {
}
@Override
public void requestFailedWithStatusCode(int statusCode) {
handleError(statusCode);
}
@Override
public void visitCompleted() {
}
@Override
public void visitCompletedByWebview(String location) {
}
// The starting point for any href clicked inside a Turbolinks enabled site. In a simple case
// you can just open another activity, or in more complex cases, this would be a good spot for
// routing logic to take you to the right place within your app.
@Override
public void visitProposedToLocationWithAction(String location, String action) {
Log.d("Turbolinks Lib", "Within MainActivity, visitProposedToLocationWithAction. loc == " + location + ", action == " + action);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(INTENT_URL, location);
this.startActivity(intent);
}
// -----------------------------------------------------------------------
// Private
// -----------------------------------------------------------------------
// Simply forwards to an error page, but you could alternatively show your own native screen
// or do whatever other kind of error handling you want.
private void handleError(int code) {
if (code == 404) {
TurbolinksSession.getDefault(this)
.activity(this)
.adapter(this)
.restoreWithCachedSnapshot(false)
.view(turbolinksView)
.visit(BASE_URL + "/error");
}
}
}