From 4e1bf80e123a3684657465e6ed566875c2b29c6d Mon Sep 17 00:00:00 2001 From: Grishka Date: Thu, 13 Jun 2024 17:08:24 +0300 Subject: [PATCH] Make WebViewFragment work with predictive back --- .../android/fragments/WebViewFragment.java | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/WebViewFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/WebViewFragment.java index 6146abc7..7cd3022a 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/WebViewFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/WebViewFragment.java @@ -1,6 +1,7 @@ package org.joinmastodon.android.fragments; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -10,14 +11,18 @@ import android.webkit.WebResourceRequest; import android.webkit.WebView; import android.webkit.WebViewClient; +import org.joinmastodon.android.BuildConfig; import org.joinmastodon.android.api.MastodonErrorResponse; import me.grishka.appkit.Nav; import me.grishka.appkit.fragments.LoaderFragment; -import me.grishka.appkit.fragments.OnBackPressedListener; -public abstract class WebViewFragment extends LoaderFragment implements OnBackPressedListener{ +public abstract class WebViewFragment extends LoaderFragment{ + private static final String TAG="WebViewFragment"; + protected WebView webView; + private Runnable backCallback=this::onGoBack; + private boolean backCallbackSet; @Override public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ @@ -31,18 +36,28 @@ public abstract class WebViewFragment extends LoaderFragment implements OnBackPr webView.setWebViewClient(new WebViewClient(){ @Override public void onPageFinished(WebView view, String url){ + if(BuildConfig.DEBUG){ + Log.d(TAG, "onPageFinished: "+url); + } dataLoaded(); + updateBackCallback(); } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){ onError(new MastodonErrorResponse(error.getDescription().toString(), -1, null)); + updateBackCallback(); } @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){ return WebViewFragment.this.shouldOverrideUrlLoading(request); } + + @Override + public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){ + updateBackCallback(); + } }); webView.getSettings().setJavaScriptEnabled(true); return webView; @@ -58,19 +73,28 @@ public abstract class WebViewFragment extends LoaderFragment implements OnBackPr webView.reload(); } - @Override - public boolean onBackPressed(){ - if(webView.canGoBack()){ - webView.goBack(); - return true; - } - return false; - } - @Override public void onToolbarNavigationClick(){ Nav.finish(this); } + private void updateBackCallback(){ + boolean canGoBack=webView.canGoBack(); + if(canGoBack!=backCallbackSet){ + if(canGoBack){ + addBackCallback(backCallback); + backCallbackSet=true; + }else{ + removeBackCallback(backCallback); + backCallbackSet=false; + } + } + } + + private void onGoBack(){ + if(webView.canGoBack()) + webView.goBack(); + } + protected abstract boolean shouldOverrideUrlLoading(WebResourceRequest req); }