Skip to content

Commit

Permalink
refactor: remove context from InstantEditorViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY committed Oct 22, 2024
1 parent 28885f7 commit 45de4ee
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
lifecycleScope.launch {
val noteFieldsCheck = checkNoteFieldsResponse(editorNote!!)
if (noteFieldsCheck is NoteFieldsCheckResult.Failure) {
addNoteErrorMessage = noteFieldsCheck.getLocalizedMessage(requireContext())
addNoteErrorMessage = noteFieldsCheck.localizedMessage ?: getString(R.string.something_wrong)
displayErrorSavingNote()
return@launch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.ichi2.anki

import android.content.Context
import anki.notes.NoteFieldsCheckResponse
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
Expand All @@ -38,9 +37,7 @@ sealed interface NoteFieldsCheckResult {
data object Success : NoteFieldsCheckResult

/** @property localizedMessage user-readable error message */
data class Failure(private val localizedMessage: String?) : NoteFieldsCheckResult {
fun getLocalizedMessage(context: Context) = localizedMessage ?: context.getString(R.string.something_wrong)
}
data class Failure(val localizedMessage: String?) : NoteFieldsCheckResult
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,10 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
* Checks the note fields and calls [saveNote] if all fields are valid.
* If [skipClozeCheck] is set to true, the cloze field check is skipped.
*
* @param context The context used to retrieve localized error messages.
* @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
): SaveNoteResult {
if (skipClozeCheck) {
Expand All @@ -152,8 +149,7 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
val note = editorNote
val result = checkNoteFieldsResponse(note)
if (result is NoteFieldsCheckResult.Failure) {
val errorMessage = result.getLocalizedMessage(context)
return SaveNoteResult.Warning(errorMessage)
return SaveNoteResult.Warning(result.localizedMessage)
}
Timber.d("Note fields check successful, saving note")
instantEditorError.emit(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
when (result) {
is SaveNoteResult.Failure -> {
Timber.d("Failed to save note")
savingErrorDialog(result.getErrorMessage(this))
savingErrorDialog(result.message ?: getString(R.string.something_wrong))
}

SaveNoteResult.Success -> {
Expand All @@ -384,7 +384,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect

is SaveNoteResult.Warning -> {
Timber.d("Showing warning to the user")
viewModel.setWarningMessage(result.message)
viewModel.setWarningMessage(result.message ?: getString(R.string.something_wrong))
}
}
}
Expand Down Expand Up @@ -497,7 +497,7 @@ class InstantNoteEditorActivity : AnkiActivity(), DeckSelectionDialog.DeckSelect
private fun saveNoteWithProgress(skipClozeCheck: Boolean) {
lifecycleScope.launch {
val result = withProgress(resources.getString(R.string.saving_facts)) {
viewModel.checkAndSaveNote(this@InstantNoteEditorActivity, skipClozeCheck = skipClozeCheck)
viewModel.checkAndSaveNote(skipClozeCheck = skipClozeCheck)
}
handleSaveNoteResult(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class InstantEditorViewModelTest : RobolectricTest() {
@Test
fun testSavingNoteWithNoCloze() = runViewModelTest {
editorNote.setField(0, "Hello")
val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote()

assertEquals(CollectionManager.TR.addingYouHaveAClozeDeletionNote(), saveNoteResult(result))
}
Expand All @@ -99,21 +99,21 @@ class InstantEditorViewModelTest : RobolectricTest() {
fun testSavingNoteWithEmptyFields() = runViewModelTest {
editorNote.setField(0, "{{c1::Hello}}")

val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote()

assertEquals("Success", saveNoteResult(result))
}

@Test
fun testSavingNoteWithClozeFields() = runViewModelTest {
val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote()

assertEquals(CollectionManager.TR.addingTheFirstFieldIsEmpty(), saveNoteResult(result))
}

@Test
fun testCheckAndSaveNote_NullEditorNote_ReturnsFailure() = runViewModelTest {
val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote()

assertTrue(result is SaveNoteResult.Warning)
}
Expand Down

0 comments on commit 45de4ee

Please sign in to comment.