Skip to content
Snippets Groups Projects
Commit ad7413d3 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Improve get random words and images

parent 1e507b03
No related branches found
No related tags found
1 merge request!42Resolve "Improve code to get random words and images"
Pipeline #1116 passed
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.1.8
app.versionCode=32
app.versionName=0.1.9
app.versionCode=33
......@@ -2,11 +2,11 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../provider/data.dart';
import '../utils/random_pick_word.dart';
import '../utils/random_pick_image.dart';
import '../utils/random_pick_data.dart';
class GamePickWordPage extends StatelessWidget {
int _count = 4;
int _countWords = 4;
int _countImages = 4;
Future<void> startGame(Data myProvider, String lang) async {
await resetGame(myProvider);
......@@ -31,32 +31,26 @@ class GamePickWordPage extends StatelessWidget {
Future<void> pickData(Data myProvider) async {
List words;
List images;
RandomPickWord randomPickWord;
RandomPickImage randomPickImage;
RandomPickData randomPickData;
Map word;
int attempts = 0;
do {
randomPickWord = RandomPickWord();
await randomPickWord.init(myProvider.lang, _count);
randomPickData = RandomPickData();
await randomPickData.init(myProvider.lang, _countWords, _countImages);
if (randomPickWord.words != null) {
words = randomPickWord.words;
if (randomPickData.words != null) {
words = randomPickData.words;
word = words.take(1).toList()[0];
randomPickImage = RandomPickImage();
await randomPickImage.init(word['key'], _count);
if (randomPickImage.images != null) {
images = randomPickImage.images;
break;
}
images = word['images'];
}
attempts++;
} while (attempts < 3);
if (
((words != null) && (words.length == _count))
((words != null) && (words.length == _countWords))
&&
((images != null) && (images.length == _count))
((images != null) && (images.length == _countImages))
) {
myProvider.updateWord = word;
myProvider.updateOtherWords = words.skip(1).toList();
......@@ -157,7 +151,7 @@ class GamePickWordPage extends StatelessWidget {
}
Column _buildImageItemsBlock(List images) {
if ((images == null) || (images.length != _count)) {
if ((images == null) || (images.length != _countImages)) {
return Column();
}
......@@ -207,7 +201,7 @@ class GamePickWordPage extends StatelessWidget {
Map word = myProvider.word;
List otherWords = myProvider.otherWords;
if ((word == null) || (otherWords.length != (_count - 1))) {
if ((word == null) || (otherWords.length != (_countWords - 1))) {
return Column();
}
......
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'random_pick_word.dart';
import 'random_pick_image.dart';
class RandomPickData {
RandomPickData();
List _words;
init(String lang, int wordsCount, int imagesPerWordCount) async {
_words = new List(wordsCount);
await getWordsAndImages(lang, wordsCount, imagesPerWordCount);
}
Future<void> getWordsAndImages(String lang, int wordsCount, int imagesPerWordCount) async {
RandomPickWord randomPickWord;
RandomPickImage randomPickImage;
List pickedWords;
randomPickWord = RandomPickWord();
await randomPickWord.init(lang, wordsCount);
if (randomPickWord.words != null) {
pickedWords = randomPickWord.words;
for (var i = 0; i < pickedWords.length; i++) {
Map word = pickedWords[i];
randomPickImage = RandomPickImage();
await randomPickImage.init(word['key'], imagesPerWordCount);
if (randomPickImage.images != null) {
pickedWords[i]['images'] = randomPickImage.images;
}
}
}
_words = pickedWords;
}
List get words => _words;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment