Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #11 from turbolinks/bridge-injection
Browse files Browse the repository at this point in the history
Re-inject the Turbolinks bridge on any full page load
  • Loading branch information
Dan Kim committed Mar 31, 2016
2 parents 3e65225 + 4aa4efb commit 2eeb916
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta5'
classpath 'com.android.tools.build:gradle:2.0.0-beta7'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion turbolinks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ext {

libraryName = 'Turbolinks Android'
libraryDescription = 'Turbolinks for Android'
libraryVersion = '1.0.0'
libraryVersion = '1.0.1-beta'

siteUrl = 'https://github.com/basecamp/turbolinks-android'
gitUrl = 'https://github.com/basecamp/turbolinks-android.git'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.MutableContextWrapper;
import android.os.Handler;
import android.util.Base64;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
Expand Down Expand Up @@ -83,17 +82,8 @@ static String getContentFromAssetFile(Context context, String filePath) throws I
*/
static void injectTurbolinksBridge(final TurbolinksSession turbolinksSession, Context context, WebView webView) {
try {
String script = TurbolinksHelper.getContentFromAssetFile(context, "js/turbolinks_bridge.js");
String jsCall = String.format(scriptInjectionFormat, script);

webView.evaluateJavascript(jsCall, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
if (turbolinksSession != null) {
turbolinksSession.turbolinksBridgeInjected = Boolean.parseBoolean(s);
}
}
});
String jsCall = String.format(scriptInjectionFormat, TurbolinksHelper.getContentFromAssetFile(context, "js/turbolinks_bridge.js"));
runJavascriptRaw(context, webView, jsCall);
} catch (IOException e) {
TurbolinksLog.e("Error injecting script file into webview: " + e.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
Expand All @@ -30,7 +31,7 @@ public class TurbolinksSession {
// Package public vars (allows for greater flexibility and access for testing)
// ---------------------------------------------------

boolean turbolinksBridgeInjected; // Script injected into DOM
boolean bridgeInjectionInProgress; // Ensures the bridge is only injected once
boolean coldBootInProgress;
boolean restoreWithCachedSnapshot;
boolean turbolinksIsReady; // Script finished and TL fully instantiated
Expand Down Expand Up @@ -84,13 +85,20 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
}

@Override
public void onPageFinished(WebView view, String location) {
if (!turbolinksBridgeInjected) {
TurbolinksHelper.injectTurbolinksBridge(TurbolinksSession.this, applicationContext, webView);
turbolinksAdapter.onPageFinished();

TurbolinksLog.d("Page finished: " + location);
}
public void onPageFinished(WebView view, final String location) {
String jsCall = "window.webView == null";
webView.evaluateJavascript(jsCall, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
if (Boolean.parseBoolean(s) && !bridgeInjectionInProgress) {
bridgeInjectionInProgress = true;
TurbolinksHelper.injectTurbolinksBridge(TurbolinksSession.this, applicationContext, webView);
TurbolinksLog.d("Bridge injected");

turbolinksAdapter.onPageFinished();
}
}
});
}

/**
Expand Down Expand Up @@ -520,6 +528,8 @@ public void setTurbolinksIsReady(boolean turbolinksIsReady) {
this.turbolinksIsReady = turbolinksIsReady;

if (turbolinksIsReady) {
bridgeInjectionInProgress = false;

TurbolinksHelper.runOnMainThread(applicationContext, new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -604,7 +614,7 @@ public WebView getWebView() {
* on the next Turbolinks visit.</p>
*/
public void resetToColdBoot() {
turbolinksBridgeInjected = false;
bridgeInjectionInProgress = false;
turbolinksIsReady = false;
coldBootInProgress = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ public void webViewIsNullIfNotInitialized() {
public void resetToColdBoot() {
turbolinksSession.activity(activity)
.adapter(adapter);
turbolinksSession.turbolinksBridgeInjected = true;
turbolinksSession.bridgeInjectionInProgress = true;
turbolinksSession.turbolinksIsReady = true;
turbolinksSession.coldBootInProgress = false;
turbolinksSession.resetToColdBoot();

assertThat(turbolinksSession.turbolinksBridgeInjected).isFalse();
assertThat(turbolinksSession.bridgeInjectionInProgress).isFalse();
assertThat(turbolinksSession.turbolinksIsReady).isFalse();
assertThat(turbolinksSession.coldBootInProgress).isFalse();
}
Expand Down

0 comments on commit 2eeb916

Please sign in to comment.