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

fix: attach custom study dialog fragment factory in SinglePageActivity to avoid crash #17508

Merged
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
48 changes: 47 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.commit
import com.ichi2.anki.android.input.ShortcutGroup
import com.ichi2.anki.android.input.ShortcutGroupProvider
import com.ichi2.anki.dialogs.customstudy.CustomStudyDialog
import com.ichi2.anki.dialogs.customstudy.CustomStudyDialogFactory
import com.ichi2.utils.ExtendedFragmentFactory
import com.ichi2.utils.getInstanceFromClassName
import timber.log.Timber
import kotlin.reflect.KClass
Expand All @@ -38,14 +42,21 @@ import kotlin.reflect.jvm.jvmName
*
* [getIntent] can be used as an easy way to build a [SingleFragmentActivity]
*/
open class SingleFragmentActivity : AnkiActivity() {
open class SingleFragmentActivity : AnkiActivity(), CustomStudyDialog.CustomStudyListener {
/** The displayed fragment. */
lateinit var fragment: Fragment

override fun onCreate(savedInstanceState: Bundle?) {
if (showedActivityFailedScreen(savedInstanceState)) {
return
}

// This page *may* host the CustomStudyDialog (CongratsPage)
// CustomStudyDialog requires a custom factory install during lifecycle or it can
// crash during lifecycle resume after background kill
val customStudyDialogFactory = CustomStudyDialogFactory({ this.getColUnsafe }, this)
customStudyDialogFactory.attachToActivity<ExtendedFragmentFactory>(this)

super.onCreate(savedInstanceState)
if (!ensureStoragePermissions()) {
return
Expand Down Expand Up @@ -101,6 +112,41 @@ open class SingleFragmentActivity : AnkiActivity() {

override val shortcuts: ShortcutGroup?
get() = (fragment as? ShortcutGroupProvider)?.shortcuts

// Begin - implementation of CustomStudyListener methods here for crash fix
// TODO - refactor https://github.com/ankidroid/Anki-Android/pull/17508#pullrequestreview-2465561993
private fun openStudyOptionsAndFinish() {
val intent = Intent(this, StudyOptionsActivity::class.java).apply {
putExtra("withDeckOptions", false)
}
startActivity(intent, null)
this.finish()
}

override fun onExtendStudyLimits() {
Timber.v("CustomStudyListener::hideProgressBar() - not handled")
david-allison marked this conversation as resolved.
Show resolved Hide resolved
openStudyOptionsAndFinish()
}

override fun showDialogFragment(newFragment: DialogFragment) {
Timber.v("CustomStudyListener::showDialogFragment()")
newFragment.show(supportFragmentManager, null)
}

override fun startActivity(intent: Intent) {
david-allison marked this conversation as resolved.
Show resolved Hide resolved
Timber.v("CustomStudyListener::startActivity() - not handled")
}

override fun onCreateCustomStudySession() {
Timber.v("CustomStudyListener::onCreateCustomStudySession()")
openStudyOptionsAndFinish()
}

override fun hideProgressBar() {
Timber.v("CustomStudyListener::hideProgressBar() - not handled")
}

// END CustomStudyListener temporary implementation - should refactor out
}

interface DispatchKeyEventListener {
Expand Down