From bbc83bb2e3037778fe3e4fda47e5d11f2a6df68c Mon Sep 17 00:00:00 2001 From: lukstbit <52494258+lukstbit@users.noreply.github.com> Date: Sun, 17 Nov 2024 19:28:44 +0200 Subject: [PATCH] Remove legacy collection loading code from AnkiFragment This class was using the legacy CollectionLoader to return the already available for use, CollectionManager.getColUnsafe(). The try-catch replicates CollectionLoader's behavior which invokes the activity's onCollectionLoadError() method to go to DeckPicker when faced with any exception during loading the collection. Probably fixes #17380 where a NullPointerException is thrown for one of AnkiFragment's subclasses, NoteEditor, because the culprit property editorNote is assigned a valid note in the callback onCollectionLoaded() which may or may not be executed at the right time. --- .../main/java/com/ichi2/anki/AnkiFragment.kt | 44 ------------------- .../main/java/com/ichi2/anki/NoteEditor.kt | 10 +++-- AnkiDroid/src/main/res/layout/note_editor.xml | 2 - 3 files changed, 7 insertions(+), 49 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/AnkiFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/AnkiFragment.kt index f7901212b84e..ccebca9e59a8 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/AnkiFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/AnkiFragment.kt @@ -28,7 +28,6 @@ import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import com.ichi2.anki.android.input.ShortcutGroup -import com.ichi2.async.CollectionLoader import com.ichi2.libanki.Collection import com.ichi2.themes.Themes import com.ichi2.utils.increaseHorizontalPaddingOfOverflowMenuIcons @@ -61,13 +60,6 @@ open class AnkiFragment(@LayoutRes layout: Int) : Fragment(layout), AnkiActivity // Open function: These can be overridden to react to specific parts of the lifecycle - /** - * Callback for [startLoadingCollection], executed once the collection is available. - */ - protected open fun onCollectionLoaded(col: Collection) { - hideProgressBar() - } - @Suppress("deprecation", "API35 properly handle edge-to-edge") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { requireActivity().window.statusBarColor = Themes.getColorFromAttr(requireContext(), R.attr.appBarColor) @@ -111,16 +103,6 @@ open class AnkiFragment(@LayoutRes layout: Int) : Fragment(layout), AnkiActivity return requireView().findViewById(id) } - /** - * Hides progress bar. - */ - private fun hideProgressBar() = ankiActivity.hideProgressBar() - - /** - * Shows progress bar. - */ - private fun showProgressBar() = ankiActivity.showProgressBar() - /** * Unregisters a previously registered broadcast receiver. * @@ -163,32 +145,6 @@ open class AnkiFragment(@LayoutRes layout: Int) : Fragment(layout), AnkiActivity mainToolbar.setTitle(title) } - /** - * Starts loading the Anki collection asynchronously if it hasn't been opened yet. - * If the collection is already open, calls `onCollectionLoaded` synchronously. - * Shows a progress bar during loading. - */ - protected fun startLoadingCollection() { - Timber.d("AnkiFragment.startLoadingCollection()") - if (CollectionManager.isOpenUnsafe()) { - Timber.d("Synchronously calling onCollectionLoaded") - onCollectionLoaded(getColUnsafe) - return - } - // Open collection asynchronously if it hasn't already been opened - showProgressBar() - CollectionLoader.load( - this - ) { col: Collection? -> - if (col != null) { - Timber.d("Asynchronously calling onCollectionLoaded") - onCollectionLoaded(col) - } else { - ankiActivity.onCollectionLoadError() - } - } - } - /** * Method to show dialog fragment including adding it to back stack * diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt index be13af4e3f0f..6dc7545fcbe9 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt @@ -495,7 +495,12 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su ) setIconColor(MaterialColors.getColor(requireContext(), R.attr.toolbarIconColor, 0)) } - startLoadingCollection() + try { + setupEditor(getColUnsafe) + } catch (ex: RuntimeException) { + ankiActivity.onCollectionLoadError() + return + } // TODO this callback doesn't handle predictive back navigation! // see #14678, added to temporarily fix for a bug requireActivity().onBackPressedDispatcher.addCallback(this) { @@ -574,8 +579,7 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su get() = NoteService.getFieldsAsBundleForPreview(editFields, shouldReplaceNewlines()) // Finish initializing the fragment after the collection has been correctly loaded - override fun onCollectionLoaded(col: Collection) { - super.onCollectionLoaded(col) + private fun setupEditor(col: Collection) { val intent = requireActivity().intent Timber.d("NoteEditor() onCollectionLoaded: caller: %d", caller) ankiActivity.registerReceiver() diff --git a/AnkiDroid/src/main/res/layout/note_editor.xml b/AnkiDroid/src/main/res/layout/note_editor.xml index 5595a966bde9..fdad897f5833 100644 --- a/AnkiDroid/src/main/res/layout/note_editor.xml +++ b/AnkiDroid/src/main/res/layout/note_editor.xml @@ -148,6 +148,4 @@ android:id="@+id/editor_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content"/> - -