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

Implemented predictive back navigation for some screens #14659

Merged
Merged
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
44 changes: 25 additions & 19 deletions AnkiDroid/src/main/java/com/ichi2/anki/Info.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.ThemeUtils
import com.google.android.material.button.MaterialButton
import com.ichi2.anim.ActivityTransitionAnimation
Expand All @@ -46,7 +47,7 @@ private const val CHANGE_LOG_URL = "https://docs.ankidroid.org/changelog.html"
* Shows an about box, which is a small HTML page.
*/
class Info : AnkiActivity() {
private var mWebView: WebView? = null
private lateinit var mWebView: WebView

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -67,7 +68,7 @@ class Info : AnkiActivity() {
findViewById<MaterialButton>(R.id.info_donate).setOnClickListener { openUrl(Uri.parse(getString(R.string.link_opencollective_donate))) }
title = "$appName v$pkgVersionName"
mWebView = findViewById(R.id.info)
mWebView!!.webChromeClient = object : WebChromeClient() {
mWebView.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView, progress: Int) {
// Hide the progress indicator when the page has finished loaded
if (progress == 100) {
Expand All @@ -88,7 +89,11 @@ class Info : AnkiActivity() {
visibility = View.GONE
}
}

val onBackPressedCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
if (mWebView.canGoBack()) mWebView.goBack()
}
}
// Apply Theme colors
val typedArray = theme.obtainStyledAttributes(intArrayOf(android.R.attr.colorBackground, android.R.attr.textColor))
val backgroundColor = typedArray.getColor(0, -1)
Expand All @@ -97,9 +102,9 @@ class Info : AnkiActivity() {
val anchorTextThemeColor = ThemeUtils.getThemeAttrColor(this, android.R.attr.colorAccent)
val anchorTextColor = anchorTextThemeColor.toRGBHex()

mWebView!!.setBackgroundColor(backgroundColor)
mWebView!!.settings.allowFileAccess = true
mWebView!!.settings.allowContentAccess = true
mWebView.setBackgroundColor(backgroundColor)
mWebView.settings.allowFileAccess = true
mWebView.settings.allowContentAccess = true
setRenderWorkaround(this)
when (type) {
TYPE_NEW_VERSION -> {
Expand All @@ -108,14 +113,14 @@ class Info : AnkiActivity() {
setOnClickListener { close() }
}
val background = backgroundColor.toRGBHex()
mWebView!!.loadUrl("/android_asset/changelog.html")
mWebView!!.settings.javaScriptEnabled = true
mWebView!!.webViewClient = object : WebViewClient() {
mWebView.loadUrl("/android_asset/changelog.html")
mWebView.settings.javaScriptEnabled = true
mWebView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
/* The order of below javascript code must not change (this order works both in debug and release mode)
* or else it will break in any one mode.
*/
mWebView!!.loadUrl(
mWebView.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"" + textColor + "\");" +
"x=document.getElementsByTagName(\"a\"); for(i=0;i<x.length;i++){x[i].style.color=\"" + anchorTextColor + "\";}" +
"document.getElementsByTagName(\"h1\")[0].style.color=\"" + textColor + "\";" +
Expand Down Expand Up @@ -146,10 +151,20 @@ class Info : AnkiActivity() {
}
return true
}

override fun doUpdateVisitedHistory(
view: WebView?,
url: String?,
isReload: Boolean
) {
super.doUpdateVisitedHistory(view, url, isReload)
onBackPressedCallback.isEnabled = view != null && view.canGoBack()
}
}
}
else -> finish()
}
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
}

private fun close() {
Expand All @@ -170,15 +185,6 @@ class Info : AnkiActivity() {
finishWithAnimation(ActivityTransitionAnimation.Direction.START)
}

@Suppress("deprecation") // onBackPressed
override fun onBackPressed() {
if (mWebView!!.canGoBack()) {
mWebView!!.goBack()
} else {
super.onBackPressed()
}
}

companion object {
const val TYPE_EXTRA = "infoType"
const val TYPE_NEW_VERSION = 2
Expand Down
10 changes: 0 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,6 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
}

override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
android.R.id.home -> {
onBackPressed()
true
}
R.id.action_add_new_model -> {
addFieldDialog()
true
Expand All @@ -469,12 +465,6 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
finishWithAnimation(ActivityTransitionAnimation.Direction.END)
}

@Suppress("DEPRECATION", "Deprecated in API34+dependencies for predictive back feature")
override fun onBackPressed() {
super.onBackPressed()
closeActivity()
}

fun handleAction(contextMenuAction: ModelEditorContextMenuAction) {
when (contextMenuAction) {
ModelEditorContextMenuAction.Sort -> sortByField()
Expand Down
26 changes: 13 additions & 13 deletions AnkiDroid/src/main/java/com/ichi2/anki/Previewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Previewer : AbstractFlashcardViewer() {
disableDrawerSwipe()
startLoadingCollection()
initPreviewProgress()
setOkResult()
}

private fun initPreviewProgress() {
Expand Down Expand Up @@ -161,13 +162,8 @@ class Previewer : AbstractFlashcardViewer() {
return super.onOptionsItemSelected(item)
}

override fun onBackPressed() {
setResult(RESULT_OK, resultIntent)
super.onBackPressed()
}

override fun onNavigationPressed() {
setResult(RESULT_OK, resultIntent)
setOkResult()
super.onNavigationPressed()
}

Expand Down Expand Up @@ -214,6 +210,7 @@ class Previewer : AbstractFlashcardViewer() {

override fun performReload() {
mReloadRequired = true
setOkResult()
val newCardList = getColUnsafe.filterToValidCards(mCardList)
if (newCardList.isEmpty()) {
finish()
Expand All @@ -228,6 +225,7 @@ class Previewer : AbstractFlashcardViewer() {
override fun onEditedNoteChanged() {
super.onEditedNoteChanged()
mNoteChanged = true
setOkResult()
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Expand Down Expand Up @@ -267,13 +265,15 @@ class Previewer : AbstractFlashcardViewer() {
mProgressText.text = progress
}

private val resultIntent: Intent
get() {
val intent = Intent()
intent.putExtra("reloadRequired", mReloadRequired)
intent.putExtra("noteChanged", mNoteChanged)
return intent
}
private fun setOkResult() {
setResult(
RESULT_OK,
Intent().apply {
putExtra("reloadRequired", mReloadRequired)
putExtra("noteChanged", mNoteChanged)
}
)
}

companion object {
@CheckResult
Expand Down
Loading