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 Sep 1, 2024
1 parent df88953 commit 06d2403
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
26 changes: 19 additions & 7 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 @@ -164,6 +165,13 @@ open class CardBrowser :
ChangeManager.Subscriber,
ExportDialogsFactoryProvider {

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

/**
* Provides an instance of NoteEditorLauncher for editing a note
*/
Expand Down Expand Up @@ -482,6 +490,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(launcher)
supportFragmentManager.commit {
replace(R.id.note_editor_frame, noteEditor)
Expand Down Expand Up @@ -1527,12 +1539,12 @@ open class CardBrowser :
showDialogFragment(dialog)
}

@get:VisibleForTesting
val addNoteIntent: Intent
get() = createAddNoteIntent(this, viewModel)

private fun addNoteFromCardBrowser() {
onAddNoteActivityResult.launch(addNoteIntent)
if (fragmented) {
loadNoteEditorFragmentIfFragmented(addNoteLauncher)
} else {
onAddNoteActivityResult.launch(addNoteLauncher.getIntent(this))
}
}

private val reviewerCardId: CardId
Expand Down Expand Up @@ -2488,8 +2500,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, inFragmentedActivity: Boolean = false): NoteEditorLauncher {
return NoteEditorLauncher.AddNoteFromCardBrowser(viewModel, inFragmentedActivity)
}

@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 @@ -1538,6 +1538,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
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ sealed interface NoteEditorLauncher {
* Represents adding a note to the NoteEditor from the card browser.
* @property viewModel The view model containing data from the card browser.
*/
data class AddNoteFromCardBrowser(val viewModel: CardBrowserViewModel) :
data class AddNoteFromCardBrowser(val viewModel: CardBrowserViewModel, val inFragmentedActivity: Boolean = false) :
NoteEditorLauncher {
override fun toBundle(): Bundle {
val bundle = bundleOf(
NoteEditor.EXTRA_CALLER to NoteEditor.CALLER_CARDBROWSER_ADD,
NoteEditor.EXTRA_TEXT_FROM_SEARCH_VIEW to viewModel.searchTerms
NoteEditor.EXTRA_TEXT_FROM_SEARCH_VIEW to viewModel.searchTerms,
NoteEditor.IN_FRAGMENTED_ACTIVITY to inFragmentedActivity
)
if (viewModel.lastDeckId?.let { id -> id > 0 } == true) {
bundle.putLong(NoteEditor.EXTRA_DID, viewModel.lastDeckId!!)
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 06d2403

Please sign in to comment.