Skip to content

Commit

Permalink
improvement: use AnkiActivity where possible
Browse files Browse the repository at this point in the history
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
  • Loading branch information
david-allison committed Nov 29, 2024
1 parent 7880454 commit 042d67b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -158,7 +155,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
jumpToReviewer,
listIds[index]
)
customStudyListener?.showDialogFragment(d)
requireAnkiActivity().showDialogFragment(d)
}
}
}.create()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

/**
Expand Down
10 changes: 0 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/CongratsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?)
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 042d67b

Please sign in to comment.