Updated on 2026-08-14

This commit is contained in:
Tangem 2023-04-19 16:20:34 +03:00
parent 3a20cd143c
commit 9b6100c09c

View file

@ -4,6 +4,7 @@ import com.tangem.common.core.TangemSdkError
import com.tangem.crypto.bip39.Mnemonic
import com.tangem.crypto.bip39.MnemonicErrorResult
import com.tangem.feature.onboarding.data.MnemonicRepository
import com.tangem.utils.extensions.isNotWhitespace
/**
[REDACTED_AUTHOR]
@ -13,6 +14,7 @@ internal class DefaultSeedPhraseInteractor constructor(
) : SeedPhraseInteractor {
private var currentMnemonic: Mnemonic? = null
private val partWordFinder: PartWordFinder = PartWordFinder()
override suspend fun generateMnemonic(): Result<Mnemonic> {
return runCatching {
@ -61,8 +63,15 @@ internal class DefaultSeedPhraseInteractor constructor(
hasSelection: Boolean,
cursorPosition: Int,
): List<String> {
// TODO: todo
return emptyList()
if (text.isEmpty() || cursorPosition == 0 || hasSelection) return emptyList()
val word = partWordFinder.getLeadPartOfWord(text, cursorPosition)
?: return emptyList()
val suggestions = repository.getWordsDictionary()
.filter { it.startsWith(word, ignoreCase = false) && it != word }
.toList()
return suggestions
}
override suspend fun insertSuggestionWord(
@ -70,8 +79,23 @@ internal class DefaultSeedPhraseInteractor constructor(
suggestion: String,
cursorPosition: Int,
): InsertSuggestionResult {
// TODO: todo
return InsertSuggestionResult("", 0)
val textPartLead = text.substring(0, cursorPosition)
val textPartLast = text.substring(cursorPosition, text.length)
val wordPartLast = partWordFinder.getLastPartOfWord(text, suggestion, cursorPosition)
val textBuilder = StringBuilder()
.append(textPartLead)
.append(wordPartLast)
if (textPartLast.isEmpty() || textPartLast[0].isNotWhitespace()) {
textBuilder.append(SeedPhraseInteractor.MNEMONIC_DELIMITER)
}
textBuilder.append(textPartLast)
val textWithSuggestion = textBuilder.toString()
val newCursorPosition = cursorPosition + wordPartLast.length + SeedPhraseInteractor.MNEMONIC_DELIMITER.length
return InsertSuggestionResult(textWithSuggestion, newCursorPosition)
}
}