From 17f0816fd39d6d3bae89bd862b00895f539f1b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr> Date: Thu, 7 Apr 2022 21:23:00 +0200 Subject: [PATCH] Fix get word after parameter change --- android/gradle.properties | 4 ++-- lib/utils/random_pick_word.dart | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index 4bb5439..6bf54a6 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,5 +1,5 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true -app.versionName=0.0.9 -app.versionCode=9 +app.versionName=0.0.10 +app.versionCode=10 diff --git a/lib/utils/random_pick_word.dart b/lib/utils/random_pick_word.dart index 56bcdb8..d8c573f 100644 --- a/lib/utils/random_pick_word.dart +++ b/lib/utils/random_pick_word.dart @@ -7,6 +7,8 @@ import 'package:flutter/services.dart'; class RandomPickWord { static Set<String> wordList = <String>{}; static Set<String> dictionary = <String>{}; + + static String _lang = ''; static int _length = 5; static String _level = ''; @@ -16,22 +18,18 @@ class RandomPickWord { String get word => _word; init(String lang, int length, String level) async { - _length = length; - _level = level; - _word = ''; - await wordFromLocalFile(lang, length, level); - } - - Future<void> wordFromLocalFile(String lang, int length, String level) async { - if (length != _length || level != _level || wordList.isEmpty || dictionary.isEmpty) { + if (lang != _lang || length != _length || level != _level || wordList.isEmpty || dictionary.isEmpty) { + _lang = lang; _length = length; _level = level; + + dictionary.clear(); wordList.clear(); String wordBaseFilename = 'words-' + length.toString() + '-' + lang; // Get full dictionary (eligible words) - print('Reload dictionary'); + print('Reload dictionary / ' + lang + ' / ' + length.toString()); try { String wordsFile = wordBaseFilename + '-' + 'dictionary'; var data = await rootBundle.loadString('assets/files/' + wordsFile + '.txt'); @@ -45,7 +43,7 @@ class RandomPickWord { } // Get guessable words list (will pick random word from) - print('Reload words list (' + level + ')'); + print('Reload words list / ' + lang + ' / ' + length.toString() + ' / ' + level); try { String wordsFile = wordBaseFilename + '-' + level; var data = await rootBundle.loadString('assets/files/' + wordsFile + '.txt'); @@ -57,12 +55,16 @@ class RandomPickWord { } catch (e) { throw "Failed loading words database"; } - } print('Words in dictionary: ' + dictionary.length.toString()); print('Words in words list: ' + wordList.length.toString()); + _word = ''; + await wordFromLocalFile(); + } + + Future<void> wordFromLocalFile() async { // Check we have enough words if (wordList.length < 1) { print('Not enough words in list.'); -- GitLab