Skip to content

Commit

Permalink
refactor: activity to use viewModel to set warning
Browse files Browse the repository at this point in the history
* added TODOs to the viewModel and activity
  • Loading branch information
criticalAY authored and lukstbit committed Jun 3, 2024
1 parent 1481492 commit dfe33b6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
* @param skipClozeCheck Indicates whether to skip the cloze field check.
* @return A [SaveNoteResult] indicating the outcome of the operation.
*/
// TODO: remove context from here
suspend fun checkAndSaveNote(
context: Context,
skipClozeCheck: Boolean = false
Expand Down Expand Up @@ -154,21 +155,64 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
}
}

/**
* Retrieves all cloze text fields from the current editor note's note type.
*
* This method accesses the `editorNote` property to fetch its associated note type
* and then retrieves all cloze text fields using the [getAllClozeTextFields] method.
*
* @return A list of strings representing the cloze text fields in the current editor note's note type.
*/
fun getClozeFields(): List<String> {
return editorNote.notetype.getAllClozeTextFields()
}

/**
* Set the warning message to be displayed in editor dialog
*/
fun setWarningMessage(message: String?) {
viewModelScope.launch {
instantEditorError.emit(message)
}
}
}

/**
* Represents the result of saving a note operation.
* Has three possible outcomes: `Success`, `Failure`, and `Warning`.
*/
sealed class SaveNoteResult {
/**
* Indicates that the save note operation was successful.
*/
data object Success : SaveNoteResult()

/**
* Indicates that the save note operation failed.
*
* @property message An optional message describing the reason for the failure.
*/
data class Failure(val message: String? = null) : SaveNoteResult() {

/**
* Retrieves the error message associated with this failure.
*
* If a message is provided, it returns that message. Otherwise, it returns a default
* error message from the context's resources.
*
* @param context The context used to retrieve the default error message string.
* @return The error message.
*/
fun getErrorMessage(context: Context) =
message ?: context.getString(R.string.something_wrong)
}

/**
* Indicates that the save note operation completed with a warning.
*
* Example, when user tries to save cloze field with no cloze
*
* @property message A message describing the warning.
*/
data class Warning(val message: String?) : SaveNoteResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
}

/** Setup the deck spinner and custom editor dialog layout **/
// TODO: subscribe to the flow of deckId to change the control value
private fun showEditorDialog() {
showDialog()
deckSpinnerSelection = DeckSpinnerSelection(
Expand Down Expand Up @@ -269,9 +270,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
lifecycleScope.launch {
viewModel.instantEditorError.emit(null)
}
viewModel.setWarningMessage(null)
}

override fun afterTextChanged(s: Editable?) {
Expand All @@ -285,14 +284,9 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
* retrieves the field content, and saves the note
*/
private fun checkAndSave() {
getFieldValues()
extractFieldValues()

lifecycleScope.launch {
val result = withProgress(resources.getString(R.string.saving_facts)) {
viewModel.checkAndSaveNote(this@InstantNoteEditorActivity)
}
handleSaveNoteResult(result)
}
saveNoteWithProgress(skipClozeCheck = false)
}

private fun handleSaveNoteResult(result: SaveNoteResult) {
Expand All @@ -311,7 +305,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect

is SaveNoteResult.Warning -> {
Timber.d("Showing warning to the user")
lifecycleScope.launch { viewModel.instantEditorError.emit(result.message) }
viewModel.setWarningMessage(result.message)
}
}
}
Expand All @@ -321,8 +315,8 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
currentClozeNumber = 0
}

/** Gets the field content from the editor **/
private fun getFieldValues() {
/** Gets the field content from the editor, and updates the Note **/
private fun extractFieldValues() {
val editTextValues = mutableListOf<String>()

editFieldsLayout?.let { layout ->
Expand Down Expand Up @@ -415,17 +409,21 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
AlertDialog.Builder(this).show {
message(text = errorMessage)
positiveButton(text = TR.actionsSave()) {
lifecycleScope.launch {
val result = withProgress(resources.getString(R.string.saving_facts)) {
viewModel.checkAndSaveNote(this@InstantNoteEditorActivity, true)
}
handleSaveNoteResult(result)
}
saveNoteWithProgress(skipClozeCheck = true)
}
negativeButton(R.string.dialog_cancel)
}
}

private fun saveNoteWithProgress(skipClozeCheck: Boolean) {
lifecycleScope.launch {
val result = withProgress(resources.getString(R.string.saving_facts)) {
viewModel.checkAndSaveNote(this@InstantNoteEditorActivity, skipClozeCheck = skipClozeCheck)
}
handleSaveNoteResult(result)
}
}

override fun onDeckSelected(deck: DeckSelectionDialog.SelectableDeck?) {
if (deck == null) {
return
Expand Down Expand Up @@ -508,6 +506,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
}

companion object {
// TODO: Should not be global
/** Allows to keep track of the current cloze number, reset to 0 when activity is destroyed **/
var currentClozeNumber: Int = 0
}
Expand Down

0 comments on commit dfe33b6

Please sign in to comment.