Skip to content

Commit

Permalink
loadNoteEditor when adding note
Browse files Browse the repository at this point in the history
This commit ensures that when adding note from cardbrowser it will load
note editor on trailing side instead of launching NoteEditor on new
screen
  • Loading branch information
SanjaySargam committed Jul 20, 2024
1 parent 3b922fe commit a49e64c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
22 changes: 17 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1330,9 +1335,12 @@ open class CardBrowser :
showDialogFragment(dialog)
}

/**
* Provides an instance of NoteEditorLauncher for adding a note
*/
@get:VisibleForTesting
val addNoteIntent: Intent
get() = createAddNoteIntent(this, viewModel)
val addNoteLauncher: NoteEditorLauncher
get() = createAddNoteLauncher(viewModel)

/**
* Provides an instance of NoteEditorLauncher for editing a note
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/test/java/com/ichi2/anki/CardBrowserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a49e64c

Please sign in to comment.