Cleanup. Remove .validate, .invalid and .valid states. Figure word chips out of the state.

This commit is contained in:
Francisco Gindre 2021-12-21 10:21:19 -03:00
parent e94021b4be
commit cde059b9e7
5 changed files with 29 additions and 55 deletions

View File

@ -68,7 +68,7 @@ extension RecoveryPhraseValidationState {
return WordChipDropDelegate { chipKind in
viewStore.send(.drag(wordChip: chipKind, intoGroup: group))
}
case .complete, .valid, .invalid:
case .complete:
return NullDelegate()
}
}

View File

@ -27,8 +27,6 @@ struct RecoveryPhraseValidationState: Equatable {
case initial
case incomplete
case complete
case valid
case invalid
}
static let wordGroupSize = 6
@ -184,7 +182,6 @@ extension RecoveryPhrase.Chunk {
enum RecoveryPhraseValidationAction: Equatable {
case reset
case drag(wordChip: PhraseChip.Kind, intoGroup: Int)
case validate
case succeed
case fail
}
@ -202,9 +199,6 @@ extension RecoveryPhraseValidationReducer {
state = state.apply(chip: wordChip, into: group)
return .none
case .validate:
return .none
case .succeed:
return .none

View File

@ -28,14 +28,18 @@ struct RecoveryPhraseBackupValidationView: View {
let state = viewStore.state
let chunks = state.phrase.toChunks()
ForEach(Array(zip(chunks.indices, chunks)), id: \.0) { index, chunk in
WordChipGrid(chips: state.wordChips(for: index, groupSize: RecoveryPhraseValidationState.wordGroupSize, from: chunk))
.background(Asset.Colors.BackgroundColors.phraseGridDarkGray.color)
.whenIsDroppable(!state.groupCompleted(index: index), dropDelegate: state.dropDelegate(for: viewStore, group: index))
WordChipGrid(
state: state,
group: index,
chunk: chunk,
misingIndex: index
)
.background(Asset.Colors.BackgroundColors.phraseGridDarkGray.color)
.whenIsDroppable(!state.groupCompleted(index: index), dropDelegate: state.dropDelegate(for: viewStore, group: index))
}
}
.padding()
.background(Asset.Colors.BackgroundColors.phraseGridDarkGray.color)
}
.applyScreenBackground()
}
@ -62,14 +66,14 @@ private extension RecoveryPhraseValidationState {
}
}
extension RecoveryPhraseValidationState{
func wordsChips(for group: Int, groupSize: Int, from chunk: RecoveryPhrase.Chunk, with missingIndex: Int, completing completions: [RecoveryPhraseStepCompletion]) -> [PhraseChip.Kind] {
let completion = completions.first(where: { $0.groupIndex == group })
extension RecoveryPhraseValidationState {
func wordsChips(for group: Int, groupSize: Int, from chunk: RecoveryPhrase.Chunk) -> [PhraseChip.Kind] {
let wordCompletion = completion.first(where: { $0.groupIndex == group })
var chips: [PhraseChip.Kind] = []
for (i, word) in chunk.words.enumerated() {
if i == missingIndex {
if let completedWord = completion?.word {
if i == missingIndices[group] {
if let completedWord = wordCompletion?.word {
chips.append(.unassigned(word: completedWord))
} else {
chips.append(.empty)
@ -80,29 +84,6 @@ extension RecoveryPhraseValidationState{
}
return chips
}
func wordsChips(for group: Int, groupSize: Int, from chunk: RecoveryPhrase.Chunk, completions: [RecoveryPhraseStepCompletion]) -> [PhraseChip.Kind] {
let completion = completions.first(where: { $0.groupIndex == group })
precondition(completion != nil, "there is no completion for group \(group). This is probably a programming error")
var chips: [PhraseChip.Kind] = []
for (i, word) in chunk.words.enumerated() {
if let completedWord = completion?.word {
chips.append(.unassigned(word: completedWord))
} else {
chips.append(.ordered(position: (groupSize * group) + i + 1, word: word))
}
}
return chips
}
func wordChips(for group: Int, groupSize: Int, from chunk: RecoveryPhrase.Chunk) -> [PhraseChip.Kind] {
switch self.step {
case .initial, .incomplete, .complete:
return wordsChips(for: group, groupSize: groupSize, from: chunk, with: missingIndices[group], completing: completion)
case .valid, .invalid:
return wordsChips(for: group, groupSize: groupSize, from: chunk, completions: completion)
}
}
}
extension RecoveryPhraseValidationState {
@ -119,6 +100,18 @@ extension RecoveryPhraseValidationStore {
)
}
private extension WordChipGrid {
init(
state: RecoveryPhraseValidationState,
group: Int,
chunk: RecoveryPhrase.Chunk,
misingIndex: Int
) {
let chips = state.wordsChips(for: group, groupSize: RecoveryPhraseValidationState.wordGroupSize, from: chunk)
self.init(chips: chips)
}
}
struct RecoveryPhraseBackupView_Previews: PreviewProvider {
static var previews: some View {
RecoveryPhraseBackupValidationView(store: RecoveryPhraseValidationStore.demo)

View File

@ -39,8 +39,7 @@ struct WordChipGrid: View {
}
}
}
init(chips: [PhraseChip.Kind]) {
init(chips: [PhraseChip.Kind]) {
self.chips = chips
}

View File

@ -356,7 +356,6 @@ class RecoveryPhraseValidationTests: XCTestCase {
PhraseChip.Kind.unassigned(word: "pizza")
],
completion: [
RecoveryPhraseStepCompletion(groupIndex: 0, word: "salute"),
RecoveryPhraseStepCompletion(groupIndex: 1, word: "boil"),
RecoveryPhraseStepCompletion(groupIndex: 2, word: "cancel")
]
@ -365,12 +364,7 @@ class RecoveryPhraseValidationTests: XCTestCase {
let result = currentStep.wordsChips(
for: 0,
groupSize: 6,
from: phrase.toChunks()[0],
with: 1,
completing: [
RecoveryPhraseStepCompletion(groupIndex: 1, word: "boil"),
RecoveryPhraseStepCompletion(groupIndex: 2, word: "cancel")
]
from: phrase.toChunks()[0]
)
let expected = [
@ -423,13 +417,7 @@ class RecoveryPhraseValidationTests: XCTestCase {
let result = currentStep.wordsChips(
for: 0,
groupSize: 6,
from: phrase.toChunks()[0],
with: 1,
completing: [
RecoveryPhraseStepCompletion(groupIndex: 0, word: "salute"),
RecoveryPhraseStepCompletion(groupIndex: 1, word: "boil"),
RecoveryPhraseStepCompletion(groupIndex: 2, word: "cancel")
]
from: phrase.toChunks()[0]
)
let expected = [