Skip to content

Commit

Permalink
android: fix bugs: exception in StageShowScreen & correctAnswerIndexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
sszuev committed Jan 19, 2025
1 parent 0d05fc0 commit 1f55a84
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun StageShowScreen(
) {

if (dictionaryViewModel.selectedDictionaryIds.value.isEmpty()) {
throw IllegalStateException("no dictionaries selected")
return
}

BackHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.sszuev.flashcards.android.utils
import com.github.sszuev.flashcards.android.CELL_TEXT_LIMIT
import com.github.sszuev.flashcards.android.STAGE_WRITING_PREFIX_LENGTH
import com.github.sszuev.flashcards.android.entities.CardEntity
import kotlin.math.max

fun examplesAsString(examples: List<String>) = examples.joinToString("\n")

Expand All @@ -19,18 +20,20 @@ fun translationFromString(translation: String) = translation.split(",").map { it

fun wordAsList(word: String) = word.split(", ").map { it.trim() }

fun correctAnswerIndexOf(translation: List<String>, input: String): Int {
fun correctAnswerIndexOf(samples: List<String>, input: String): Int {
val txt = input.trim()
if (txt.length < STAGE_WRITING_PREFIX_LENGTH) {
translation.forEachIndexed { index, tr ->
samples.forEachIndexed { index, tr ->
if (tr.trim().equals(txt, true)) {
return index
}
}
return -1
}
translation.forEachIndexed { index, tr ->
if (tr.trim().startsWith(txt.substring(0, STAGE_WRITING_PREFIX_LENGTH), true)) {
samples.forEachIndexed { index, tr ->
if (tr.trim()
.startsWith(txt.substring(0, max(input.length, STAGE_WRITING_PREFIX_LENGTH)), true)
) {
return index
}
}
Expand Down

0 comments on commit 1f55a84

Please sign in to comment.