Skip to content

Commit

Permalink
show save dialog if there are unsaved changes
Browse files Browse the repository at this point in the history
Prompt users to save or discard changes dialog if there is unsaved changes in NoteEditor before moving to another note, preventing accidental data loss.
  • Loading branch information
SanjaySargam committed Sep 1, 2024
1 parent 9cccf86 commit a027b8c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
39 changes: 34 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import com.ichi2.anki.dialogs.DeckSelectionDialog
import com.ichi2.anki.dialogs.DeckSelectionDialog.Companion.newInstance
import com.ichi2.anki.dialogs.DeckSelectionDialog.DeckSelectionListener
import com.ichi2.anki.dialogs.DeckSelectionDialog.SelectableDeck
import com.ichi2.anki.dialogs.DiscardChangesDialog
import com.ichi2.anki.dialogs.IntegerDialog
import com.ichi2.anki.dialogs.SimpleMessageDialog
import com.ichi2.anki.dialogs.tags.TagsDialog
Expand Down Expand Up @@ -468,6 +469,33 @@ open class CardBrowser :
setupFlows()
}

private fun showSaveChangessDialog(launcher: NoteEditorLauncher) {
DiscardChangesDialog.showDialog(
context = this,
positiveButtonText = this.getString(R.string.save),
negativeButtonText = this.getString(R.string.discard),
message = this.getString(R.string.save_changes_message),
positiveMethod = {
launchCatchingTask {
fragment?.saveNote()
loadNoteEditorFragment(launcher)
}
},
negativeMethod = {
loadNoteEditorFragment(launcher)
}
)
}

private fun loadNoteEditorFragment(launcher: NoteEditorLauncher) {
val noteEditor = NoteEditor.newInstance(launcher)
supportFragmentManager.commit {
replace(R.id.note_editor_frame, noteEditor)
}
// Invalidate options menu so that note editor menu will show
invalidateOptionsMenu()
}

/**
* Retrieves the `NoteEditor` fragment if it is present in the fragment container
*/
Expand All @@ -494,12 +522,13 @@ open class CardBrowser :
if (!noteEditorFrame!!.isVisible) {
noteEditorFrame!!.isVisible = true
}
val noteEditor = NoteEditor.newInstance(launcher)
supportFragmentManager.commit {
replace(R.id.note_editor_frame, noteEditor)

// If there are unsaved changes in NoteEditor then show dialog for confirmation
if (fragment?.hasUnsavedChanges() == true) {
showSaveChangessDialog(launcher)
} else {
loadNoteEditorFragment(launcher)
}
// invalidate options menu so that note editor menu will show
invalidateOptionsMenu()
}

@Suppress("UNUSED_PARAMETER")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ object DiscardChangesDialog {
positiveButtonText: String = context.getString(R.string.discard),
negativeButtonText: String = CollectionManager.TR.addingKeepEditing(),
message: String = CollectionManager.TR.addingDiscardCurrentInput(),
negativeMethod: () -> Unit = {},
positiveMethod: () -> Unit
) = AlertDialog.Builder(context).show {
Timber.i("showing 'discard changes' dialog")
message(text = message)
positiveButton(text = positiveButtonText) { positiveMethod() }
negativeButton(text = negativeButtonText)
negativeButton(text = negativeButtonText) { negativeMethod() }
}
}
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/res/values/07-cardbrowser.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@
<item quantity="one">%d card deleted</item>
<item quantity="other">%d cards deleted</item>
</plurals>

<string name="save_changes_message">Do you want to save changes?</string>
</resources>

0 comments on commit a027b8c

Please sign in to comment.