diff --git a/android/gradle.properties b/android/gradle.properties index 85b94f88ee157e1d1b3cec184c8948902443d36f..65eed6426393974efb5a056ec44936d42b5ef2a1 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.7 -app.versionCode=7 +app.versionName=0.0.8 +app.versionCode=8 diff --git a/lib/provider/data.dart b/lib/provider/data.dart index c272ef023b063a48376fcecf743d8631d6597492..a1f00f7cefee1388c37c10bff10a5df2ce777b5f 100644 --- a/lib/provider/data.dart +++ b/lib/provider/data.dart @@ -3,8 +3,8 @@ import 'package:flutter/foundation.dart'; class Data extends ChangeNotifier { // randomization - bool _searchingWord = false; String _word = ''; + List _otherWords = []; Map<String, String> _image = { '1': null, '2': null, @@ -12,17 +12,17 @@ class Data extends ChangeNotifier { '4': null, }; - bool get searchingWord => _searchingWord; + String get word => _word; - set searchingWord(bool value) { - _searchingWord = value; + set updateWord(String value) { + _word = value; notifyListeners(); } - String get word => _word; + List get otherWords => _otherWords; - set updateWord(String value) { - _word = value; + set updateOtherWords(List words) { + _otherWords = words; notifyListeners(); } @@ -37,6 +37,7 @@ class Data extends ChangeNotifier { void resetGame() { _word = ''; + _otherWords = []; notifyListeners(); } } diff --git a/lib/screens/game_pick_word.dart b/lib/screens/game_pick_word.dart index ffef47e896d000613c0158023f4178ad21b6dd22..ae788672c4f6635e1031c1a6ececba682e408b10 100644 --- a/lib/screens/game_pick_word.dart +++ b/lib/screens/game_pick_word.dart @@ -6,7 +6,7 @@ import '../utils/random_pick_word.dart'; import '../utils/get_image_from_word.dart'; class GamePickWordPage extends StatelessWidget { - String _randomWord = ''; + int _count = 4; Future<void> startGame(BuildContext context, Data myProvider) async { await pickWord(context, myProvider); @@ -17,19 +17,23 @@ class GamePickWordPage extends StatelessWidget { } Future<void> pickWord(BuildContext context, Data myProvider) async { - myProvider.searchingWord = true; + List words; RandomPickWord randomPickWord; int attempts = 0; do { randomPickWord = RandomPickWord(); - await randomPickWord.init(); - if (randomPickWord.word != null) { - myProvider.updateWord = randomPickWord.word; - myProvider.searchingWord = false; + await randomPickWord.init(_count); + if (randomPickWord.words != null) { + words = randomPickWord.words; break; } attempts++; } while (attempts < 3); + + if ((words != null) && (words.length == _count)) { + myProvider.updateWord = words.take(1).toList()[0]; + myProvider.updateOtherWords = words.skip(1).toList(); + } } Future<void> pickImage(BuildContext context, Data myProvider, String word, int index) async { @@ -103,9 +107,16 @@ class GamePickWordPage extends StatelessWidget { ); } - Column _buildTextItemsBlock(String word) { + Column _buildTextItemsBlock(String word, List otherWords) { Color color = Colors.white; + List words = [ + word, + otherWords.length > 0 ? otherWords[0] : null, + otherWords.length > 1 ? otherWords[1] : null, + otherWords.length > 2 ? otherWords[2] : null, + ]; + return Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, @@ -114,16 +125,16 @@ class GamePickWordPage extends StatelessWidget { mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - _buildTextContainer(word, color), - _buildTextContainer(word, color), + _buildTextContainer(words[0], color), + _buildTextContainer(words[1], color), ], ), Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - _buildTextContainer(word, color), - _buildTextContainer(word, color), + _buildTextContainer(words[2], color), + _buildTextContainer(words[3], color), ], ) ], @@ -159,7 +170,7 @@ class GamePickWordPage extends StatelessWidget { ), ), SizedBox(height: 20), - _buildTextItemsBlock(_myProvider.word), + _buildTextItemsBlock(_myProvider.word, _myProvider.otherWords), ], ), ), diff --git a/lib/utils/get_image_from_word.dart b/lib/utils/get_image_from_word.dart index 9a13de01f7b287e4305b943fd42e8dbf51f897eb..a76c8fb3b2d9b538b5ea1427eb5310a1b0d675b6 100644 --- a/lib/utils/get_image_from_word.dart +++ b/lib/utils/get_image_from_word.dart @@ -13,7 +13,7 @@ class RandomPickImage { await imageFromLocalFile(word); } - Future<void> imageFromLocalFile(word) async { + Future<void> imageFromLocalFile(String word) async { String jsonString; try { jsonString = await rootBundle.loadString('assets/assets_images.json'); diff --git a/lib/utils/random_pick_word.dart b/lib/utils/random_pick_word.dart index 34078c841c0fcf49efccf66f0263857ff82b3593..90bb1a79b823789621e1071d8e5c1a05220903ed 100644 --- a/lib/utils/random_pick_word.dart +++ b/lib/utils/random_pick_word.dart @@ -1,31 +1,39 @@ import 'dart:async'; import 'dart:convert'; import 'package:flutter/services.dart'; -import 'dart:math' show Random; class RandomPickWord { RandomPickWord(); - String _word; - final random = Random(); + List _words; - init() async { - await wordFromLocalFile(); + init(int count) async { + _words = new List(count); + await wordFromLocalFile(count); } - Future<void> wordFromLocalFile() async { - String jsonString; + Future<void> wordFromLocalFile(int count) async { + // Get global words list + List wordList = []; try { - jsonString = await rootBundle.loadString('assets/files/words-fr.json'); + String jsonString = await rootBundle.loadString('assets/files/words-fr.json'); final jsonResponse = await json.decode(jsonString); - var wordList = jsonResponse[jsonResponse.keys.toList().join()]; - int randomWordIndex = random.nextInt(wordList.length); - String word = wordList[random.nextInt(wordList.length)]; - _word = word ?? 'UNEXPECTED ERROR'; + wordList = jsonResponse['words']; } catch (e) { - _word = 'UNEXPECTED ERROR'; + print("$e"); } + + // Check we have enough words + if (wordList.length < count) { + print('Not enough words in list.'); + } + + // Randomize words list + wordList.shuffle(); + + // Pick first words from shuffled list + _words = wordList.take(count).toList(); } - String get word => _word; + List get words => _words; }