From 5997d1fec5bd8bff592e5f8771541bb75e011603 Mon Sep 17 00:00:00 2001 From: SanjaySargam Date: Sat, 6 Jul 2024 15:17:06 +0530 Subject: [PATCH] loadNoteEditor when adding note This commit ensures that when adding note from cardbrowser it will load note editor on trailing side instead of launching NoteEditor on new screen --- .../main/java/com/ichi2/anki/CardBrowser.kt | 22 ++++++++++++++----- .../main/java/com/ichi2/anki/NoteEditor.kt | 5 +++++ .../java/com/ichi2/anki/CardBrowserTest.kt | 4 ++-- .../anki/browser/CardBrowserViewModelTest.kt | 2 +- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt index 97fe56169fe2..793d3eae856d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt @@ -53,6 +53,7 @@ import androidx.annotation.VisibleForTesting import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.ThemeUtils import androidx.core.content.ContextCompat +import androidx.core.view.isVisible import androidx.fragment.app.FragmentContainerView import androidx.fragment.app.commit import androidx.lifecycle.ViewModelProvider @@ -471,6 +472,10 @@ open class CardBrowser : if (!fragmented) { return } + // Show note editor frame when adding first card + if (!noteEditorFrame!!.isVisible) { + noteEditorFrame!!.isVisible = true + } val noteEditor = NoteEditor.newInstance(arguments) supportFragmentManager.commit { replace(R.id.note_editor_frame, noteEditor) @@ -1331,8 +1336,11 @@ open class CardBrowser : } @get:VisibleForTesting - val addNoteIntent: Intent - get() = createAddNoteIntent(this, viewModel) + /** + * Provides an instance of NoteEditorLauncher for adding a note + */ + val addNoteLauncher: NoteEditorLauncher + get() = createAddNoteLauncher(viewModel) /** * Provides an instance of NoteEditorLauncher for editing a note @@ -1341,7 +1349,11 @@ open class CardBrowser : get() = NoteEditorLauncher.EditCard(viewModel.currentCardId, Direction.DEFAULT) private fun addNoteFromCardBrowser() { - onAddNoteActivityResult.launch(addNoteIntent) + if (fragmented) { + loadNoteEditorFragmentIfFragmented(addNoteLauncher.toBundle()) + } else { + onAddNoteActivityResult.launch(addNoteLauncher.getIntent(this)) + } } private val reviewerCardId: CardId @@ -2295,8 +2307,8 @@ open class CardBrowser : fun clearLastDeckId() = SharedPreferencesLastDeckIdRepository.clearLastDeckId() @VisibleForTesting - fun createAddNoteIntent(context: Context, viewModel: CardBrowserViewModel): Intent { - return NoteEditorLauncher.AddNoteFromCardBrowser(viewModel).getIntent(context) + fun createAddNoteLauncher(viewModel: CardBrowserViewModel): NoteEditorLauncher { + return NoteEditorLauncher.AddNoteFromCardBrowser(viewModel) } @CheckResult diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt index 66ada35551ff..761a0dd0b56b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt @@ -1488,6 +1488,11 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su // ensure there are no orphans from possible edit previews CardTemplateNotetype.clearTempModelFiles() + // Don't close this fragment if it is in fragmented activity + if (inFragmentedActivity) { + return + } + // Set the finish animation if there is one on the intent which created the activity val animation = BundleCompat.getParcelable( requireArguments(), diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt index 5d6c2941af5f..3f3fcfd09a2d 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt @@ -489,7 +489,7 @@ class CardBrowserTest : RobolectricTest() { assertThat("The target deck should be selected", b.lastDeckId, equalTo(targetDid)) - val addIntent = b.addNoteIntent + val addIntent = b.addNoteLauncher.getIntent(targetContext) val bundle = addIntent.getBundleExtra(SingleFragmentActivity.FRAGMENT_ARGS_EXTRA) IntentAssert.hasExtra(bundle, NoteEditor.EXTRA_DID, targetDid) } @@ -503,7 +503,7 @@ class CardBrowserTest : RobolectricTest() { assertThat("The initial deck should be selected", b.lastDeckId, equalTo(initialDid)) - val addIntent = b.addNoteIntent + val addIntent = b.addNoteLauncher.getIntent(targetContext) val bundle = addIntent.getBundleExtra(SingleFragmentActivity.FRAGMENT_ARGS_EXTRA) IntentAssert.hasExtra(bundle, NoteEditor.EXTRA_DID, initialDid) } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/browser/CardBrowserViewModelTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/browser/CardBrowserViewModelTest.kt index 6df37abbb53d..af652cdbe7ec 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/browser/CardBrowserViewModelTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/browser/CardBrowserViewModelTest.kt @@ -134,7 +134,7 @@ class CardBrowserViewModelTest : JvmTest() { assertThat("All decks should be selected", hasSelectedAllDecks()) - val addIntent = CardBrowser.createAddNoteIntent(mockIt(), this) + val addIntent = CardBrowser.createAddNoteLauncher(this).getIntent(mockIt()) val bundle = addIntent.getBundleExtra(SingleFragmentActivity.FRAGMENT_ARGS_EXTRA) IntentAssert.doesNotHaveExtra(bundle, NoteEditor.EXTRA_DID) }