Skip to content

Commit 942cb6c

Browse files
author
Bruno Barbieri
authored
enable repeated words (#552)
1 parent dccdfb7 commit 942cb6c

File tree

1 file changed

+53
-5
lines changed
  • app/components/Views/AccountBackupStep5

1 file changed

+53
-5
lines changed

app/components/Views/AccountBackupStep5/index.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,30 @@ class AccountBackupStep5 extends Component {
206206
});
207207
};
208208

209+
getIndexOfWord(word, index, words) {
210+
const confirmedWordsOcurrences = this.getNumberOfOcurrences(word, words);
211+
if (confirmedWordsOcurrences === 1) {
212+
return words.indexOf(word);
213+
}
214+
215+
let currentOccurence = 0;
216+
217+
for (let i = 0; i < words.length; i++) {
218+
if (words[i] === word) {
219+
currentOccurence++;
220+
if (i === index && currentOccurence <= confirmedWordsOcurrences) {
221+
return i;
222+
}
223+
}
224+
}
225+
return words.indexOf(word);
226+
}
227+
209228
selectWord = (word, i) => {
210229
const newConfirmedWords = this.state.confirmedWords.slice();
211230
let newIndex;
212-
if (newConfirmedWords.includes(word)) {
213-
const matchIndex = newConfirmedWords.indexOf(word);
231+
if (this.isSelectedWord(word, i)) {
232+
const matchIndex = this.getIndexOfWord(word, i, this.state.confirmedWords);
214233
newConfirmedWords[matchIndex] = undefined;
215234
newIndex = matchIndex;
216235
} else {
@@ -234,6 +253,37 @@ class AccountBackupStep5 extends Component {
234253
this.setState({ confirmedWords: newConfirmedWords, currentIndex: newIndex });
235254
};
236255

256+
getNumberOfOcurrences(word, words) {
257+
let ocurrences = 0;
258+
for (let i = 0; i < words.length; i++) {
259+
words[i] === word && ocurrences++;
260+
}
261+
return ocurrences;
262+
}
263+
264+
isSelectedWord(word, index) {
265+
if (!this.state.confirmedWords.includes(word)) {
266+
return false;
267+
}
268+
const totalOcurrences = this.getNumberOfOcurrences(word, this.words);
269+
const confirmedWordsOcurrences = this.getNumberOfOcurrences(word, this.state.confirmedWords);
270+
if (totalOcurrences === confirmedWordsOcurrences) {
271+
return true;
272+
}
273+
274+
let currentOccurence = 0;
275+
276+
for (let i = 0; i < this.words.length; i++) {
277+
if (this.words[i] === word) {
278+
currentOccurence++;
279+
if (i === index && currentOccurence <= confirmedWordsOcurrences) {
280+
return true;
281+
}
282+
}
283+
}
284+
return false;
285+
}
286+
237287
render() {
238288
return (
239289
<SafeAreaView style={styles.mainWrapper}>
@@ -290,9 +340,7 @@ class AccountBackupStep5 extends Component {
290340

291341
<View style={styles.words}>
292342
{this.words.map((word, i) => {
293-
const selected = this.state.confirmedWords.includes(word)
294-
? styles.selectedWord
295-
: null;
343+
const selected = this.isSelectedWord(word, i) ? styles.selectedWord : null;
296344
const selectedText = selected ? styles.selectedWordText : null;
297345
return (
298346
<TouchableOpacity

0 commit comments

Comments
 (0)