Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
fix: Discovery WebViews Issues (#1828)
Browse files Browse the repository at this point in the history
- Horizontal scrolling conflicted with vertical scrolling, causing
 pull-to-refresh issues.
- WebViews were not functioning properly with data.
- Back navigation did not work as expected in the webview.

Fixes: LEARNER-9636 | LEARNER-9637
  • Loading branch information
HamzaIsrar12 authored Sep 28, 2023
1 parent 29ecb08 commit bcf1d6d
Show file tree
Hide file tree
Showing 3 changed files with 484 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.webkit.URLUtil;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -39,6 +39,20 @@ public class WebViewDiscoverFragment extends BaseWebViewFragment {
protected FragmentWebviewDiscoveryBinding binding;
private ViewTreeObserver.OnScrollChangedListener onScrollChangedListener;

private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
if (binding.webview.canGoBack()) {
binding.webview.goBack();
} else {
// Disable the current callback to enable triggering the callback on the
// MainTabsDashboardFragment
onBackPressedCallback.setEnabled(false);
requireActivity().onBackPressed();
}
}
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -51,7 +65,9 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initTitle();
setWebViewActionListener();
setWebViewBackPressListener();
requireActivity().getOnBackPressedDispatcher().addCallback(
getViewLifecycleOwner(), onBackPressedCallback
);

// Check for search query in extras
String searchQueryExtra = null;
Expand Down Expand Up @@ -118,18 +134,6 @@ public void onUserNotLoggedIn(@NonNull String courseId, boolean emailOptIn) {
}));
}

private void setWebViewBackPressListener() {
binding.webview.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK && binding.webview.canGoBack()) {
binding.webview.goBack();
return true;
}
}
return false;
});
}

@Override
public void onSaveInstanceState(Bundle outState) {
if (URLUtil.isValidUrl(binding.webview.getUrl())) {
Expand Down Expand Up @@ -191,6 +195,18 @@ public void onStart() {
});
}

@Override
public void onResume() {
super.onResume();
onBackPressedCallback.setEnabled(true);
}

@Override
public void onPause() {
super.onPause();
onBackPressedCallback.setEnabled(false);
}

@Override
public void onStop() {
super.onStop();
Expand Down
Loading

0 comments on commit bcf1d6d

Please sign in to comment.