From 042d67b261c3119ff0b9a2ffedc29828c58abc31 Mon Sep 17 00:00:00 2001 From: David Allison <62114487+david-allison@users.noreply.github.com> Date: Wed, 27 Nov 2024 18:21:07 +0000 Subject: [PATCH] improvement: use AnkiActivity where possible The only question here is whether `showDialogFragment` in CongratsPage is necessary showDialogFragment in CongratsPage should not be necessary as the lifetime of the fragment should match the activity --- .../dialogs/customstudy/CustomStudyDialog.kt | 17 +++++++---------- .../java/com/ichi2/anki/pages/CongratsPage.kt | 10 ---------- .../ichi2/anki/dialogs/CustomStudyDialogTest.kt | 5 ++--- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/customstudy/CustomStudyDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/customstudy/CustomStudyDialog.kt index e90d680d8acc..59c2bb3c2e3f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/customstudy/CustomStudyDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/customstudy/CustomStudyDialog.kt @@ -29,7 +29,6 @@ import android.widget.TextView import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AlertDialog import androidx.core.content.edit -import androidx.fragment.app.DialogFragment import com.ichi2.anki.CrashReportService import com.ichi2.anki.R import com.ichi2.anki.Reviewer @@ -47,6 +46,7 @@ import com.ichi2.anki.dialogs.tags.TagsDialogListener import com.ichi2.anki.launchCatchingTask import com.ichi2.anki.model.CardStateFilter import com.ichi2.anki.preferences.sharedPrefs +import com.ichi2.anki.requireAnkiActivity import com.ichi2.anki.showThemedToast import com.ichi2.annotations.NeedsTest import com.ichi2.libanki.Collection @@ -85,9 +85,6 @@ class CustomStudyDialog(private val collection: Collection, private val customSt interface CustomStudyListener : CreateCustomStudySessionListener.Callback { fun onExtendStudyLimits() - fun showDialogFragment(newFragment: DialogFragment) - fun dismissAllDialogFragments() - fun startActivity(intent: Intent) } fun withArguments( @@ -148,7 +145,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt checkedTags = ArrayList(), allTags = ArrayList(collection.tags.byDeck(currentDeck)) ) - customStudyListener?.showDialogFragment(dialogFragment) + requireAnkiActivity().showDialogFragment(dialogFragment) } else -> { // User asked for a standard custom study option @@ -158,7 +155,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt jumpToReviewer, listIds[index] ) - customStudyListener?.showDialogFragment(d) + requireAnkiActivity().showDialogFragment(d) } } }.create() @@ -274,7 +271,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt } } .negativeButton(R.string.dialog_cancel) { - customStudyListener?.dismissAllDialogFragments() + requireAnkiActivity().dismissAllDialogFragments() } .create() // Added .create() because we wanted to access alertDialog positive button enable state editText.addTextChangedListener(object : TextWatcher { @@ -440,16 +437,16 @@ class CustomStudyDialog(private val collection: Collection, private val customSt collection.decks.save(dyn) requireActivity().launchCatchingTask { rebuildCram(CreateCustomStudySessionListener(customStudyListener!!)) } // Hide the dialogs - customStudyListener?.dismissAllDialogFragments() + requireAnkiActivity().dismissAllDialogFragments() } private fun onLimitsExtended(jumpToReviewer: Boolean) { if (jumpToReviewer) { - customStudyListener?.startActivity(Intent(requireContext(), Reviewer::class.java)) + requireAnkiActivity().startActivity(Intent(requireContext(), Reviewer::class.java)) } else { customStudyListener?.onExtendStudyLimits() } - customStudyListener?.dismissAllDialogFragments() + requireAnkiActivity().dismissAllDialogFragments() } /** diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt b/AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt index d2204b991fb9..3dc6d6c28878 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt @@ -21,7 +21,6 @@ import android.os.Bundle import android.view.View import android.webkit.JavascriptInterface import androidx.appcompat.app.AlertDialog -import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentActivity import androidx.fragment.app.viewModels import androidx.lifecycle.ViewModel @@ -155,11 +154,6 @@ class CongratsPage : openStudyOptionsAndFinish() } - override fun showDialogFragment(newFragment: DialogFragment) { - Timber.v("CustomStudyListener::showDialogFragment()") - newFragment.show(childFragmentManager, null) - } - override fun onCreateCustomStudySession() { Timber.v("CustomStudyListener::onCreateCustomStudySession()") openStudyOptionsAndFinish() @@ -169,10 +163,6 @@ class CongratsPage : Timber.v("CustomStudyListener::showProgressBar() - not handled") } - override fun dismissAllDialogFragments() { - Timber.v("CustomStudyListener::dismissAllDialogFragments() - not handled") - } - override fun hideProgressBar() { Timber.v("CustomStudyListener::hideProgressBar() - not handled") } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/CustomStudyDialogTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/CustomStudyDialogTest.kt index 4b15cd6b0a86..b3baf928a2f7 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/CustomStudyDialogTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/CustomStudyDialogTest.kt @@ -16,7 +16,6 @@ package com.ichi2.anki.dialogs import androidx.appcompat.app.AlertDialog -import androidx.fragment.app.testing.FragmentScenario import androidx.lifecycle.Lifecycle import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.doesNotExist @@ -72,7 +71,7 @@ class CustomStudyDialogTest : RobolectricTest() { ) .arguments val factory = CustomStudyDialogFactory({ this.col }, mockListener) - AnkiFragmentScenario.launch(CustomStudyDialog::class.java, args, androidx.appcompat.R.style.Theme_AppCompat, factory).use { scenario -> + AnkiFragmentScenario.launch(CustomStudyDialog::class.java, args, factory).use { scenario -> scenario.moveToState(Lifecycle.State.RESUMED) scenario.onFragment { f: CustomStudyDialog -> val dialog = assertNotNull(f.dialog as AlertDialog?) @@ -123,7 +122,7 @@ class CustomStudyDialogTest : RobolectricTest() { whenever(mockCollection.sched).thenReturn(mockSched) whenever(mockSched.newCount()).thenReturn(0) val factory = CustomStudyDialogFactory({ mockCollection }, mockListener) - FragmentScenario.launch(CustomStudyDialog::class.java, args, androidx.appcompat.R.style.Theme_AppCompat, factory).use { scenario -> + AnkiFragmentScenario.launch(CustomStudyDialog::class.java, args, factory).use { scenario -> scenario.moveToState(Lifecycle.State.STARTED) scenario.onFragment { f: CustomStudyDialog -> val dialog = f.dialog as AlertDialog?