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 4376c30
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 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.getLocalizedMessage(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 @@ -39,7 +38,7 @@ sealed interface 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)
fun getLocalizedMessage(defaultError: String) = localizedMessage ?: defaultError
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,12 @@ 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 defaultErrorMessage Default error message in case backend error is null.
* @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,
defaultErrorMessage: String,
skipClozeCheck: Boolean = false
): SaveNoteResult {
if (skipClozeCheck) {
Expand All @@ -152,7 +151,7 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
val note = editorNote
val result = checkNoteFieldsResponse(note)
if (result is NoteFieldsCheckResult.Failure) {
val errorMessage = result.getLocalizedMessage(context)
val errorMessage = result.getLocalizedMessage(defaultError = defaultErrorMessage)
return SaveNoteResult.Warning(errorMessage)
}
Timber.d("Note fields check successful, saving note")
Expand Down
Original file line number Diff line number Diff line change
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(getString(R.string.something_wrong), skipClozeCheck = skipClozeCheck)
}
handleSaveNoteResult(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.ichi2.anki.instanteditor

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.R
import com.ichi2.anki.RobolectricTest
import com.ichi2.anki.instantnoteeditor.InstantEditorViewModel
import com.ichi2.anki.instantnoteeditor.InstantNoteEditorActivity
Expand Down Expand Up @@ -90,7 +91,7 @@ class InstantEditorViewModelTest : RobolectricTest() {
@Test
fun testSavingNoteWithNoCloze() = runViewModelTest {
editorNote.setField(0, "Hello")
val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote(targetContext.getString(R.string.something_wrong))

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

val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote(targetContext.getString(R.string.something_wrong))

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

@Test
fun testSavingNoteWithClozeFields() = runViewModelTest {
val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote(targetContext.getString(R.string.something_wrong))

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

@Test
fun testCheckAndSaveNote_NullEditorNote_ReturnsFailure() = runViewModelTest {
val result = checkAndSaveNote(targetContext)
val result = checkAndSaveNote(targetContext.getString(R.string.something_wrong))

assertTrue(result is SaveNoteResult.Warning)
}
Expand Down

0 comments on commit 4376c30

Please sign in to comment.