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

Merge branch '75-put-words-list-in-code-instead-of-assets' into 'master'

Resolve "Put words list in code instead of assets"

Closes #75

See merge request !76
parents cb4b367f 215ce8de
No related branches found
No related tags found
1 merge request!76Resolve "Put words list in code instead of assets"
Pipeline #5661 passed
...@@ -37,7 +37,7 @@ if (keystorePropertiesFile.exists()) { ...@@ -37,7 +37,7 @@ if (keystorePropertiesFile.exists()) {
} }
android { android {
compileSdkVersion 33 compileSdkVersion 34
namespace "org.benoitharrault.wordguessing" namespace "org.benoitharrault.wordguessing"
defaultConfig { defaultConfig {
......
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
app.versionName=0.1.39 app.versionName=0.1.40
app.versionCode=63 app.versionCode=64
Put words list in code instead of asset file. Update dependencies.
Liste des mots dans le code plutôt qu'en fichier ressource. Mise à jour des dépendances.
import 'package:wordguessing/data/game_data.dart';
import 'package:wordguessing/models/word.dart';
import 'package:wordguessing/utils/tools.dart';
class FetchDataHelper {
FetchDataHelper();
List<Word> _words = [];
String _lang = '';
void init(String lang) {
_words = [];
_lang = lang;
try {
const gameData = GameData.data;
final Map<String, dynamic> rawWords = gameData['words'] as Map<String, dynamic>;
rawWords.forEach((String key, rawWord) {
final String text = rawWord[_lang] as String;
final List<String> images = [];
for (var rawImage in rawWord['images'] as List<dynamic>) {
images.add(rawImage.toString());
}
images.shuffle();
if (images.isNotEmpty) {
_words.add(Word(
key: key,
text: text,
images: images,
));
}
});
} catch (e) {
printlog("$e");
}
}
List<Word> getWords(String lang, int count) {
if (_words.isEmpty || lang != _lang) {
init(lang);
}
List<Word> words = _words;
words.shuffle();
return words.take(count).toList();
}
}
This diff is collapsed.
...@@ -8,19 +8,19 @@ class GameAbstract extends StatelessWidget { ...@@ -8,19 +8,19 @@ class GameAbstract extends StatelessWidget {
final int countWords = 4; final int countWords = 4;
Future<void> startGame(Data myProvider, String lang) async { void startGame(Data myProvider, String lang) {
myProvider.updateLang(lang); myProvider.updateLang(lang);
await nextWord(myProvider); nextWord(myProvider);
} }
Future<void> nextWord(Data myProvider) async { void nextWord(Data myProvider) {
await pickData(myProvider); pickData(myProvider);
myProvider.updateQuestionsCount(myProvider.questionsCount + 1); myProvider.updateQuestionsCount(myProvider.questionsCount + 1);
} }
Future<void> pickData(Data myProvider) async {} void pickData(Data myProvider) {}
Future<void> checkWord(Data myProvider, word) async { void checkWord(Data myProvider, word) {
if (myProvider.word?.key == word.key) { if (myProvider.word?.key == word.key) {
myProvider.updateGoodAnswers(myProvider.goodAnswers + 1); myProvider.updateGoodAnswers(myProvider.goodAnswers + 1);
nextWord(myProvider); nextWord(myProvider);
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:wordguessing/data/fetch_data_helper.dart';
import 'package:wordguessing/models/word.dart'; import 'package:wordguessing/models/word.dart';
import 'package:wordguessing/provider/data.dart'; import 'package:wordguessing/provider/data.dart';
import 'package:wordguessing/ui/games/abstract_game.dart'; import 'package:wordguessing/ui/games/abstract_game.dart';
import 'package:wordguessing/utils/random_pick_words.dart';
class GamePickImagePage extends GameAbstract { class GamePickImagePage extends GameAbstract {
const GamePickImagePage({super.key}); const GamePickImagePage({super.key});
@override @override
Future<void> pickData(Data myProvider) async { pickData(Data myProvider) {
final RandomPickWords randomPickWords = RandomPickWords();
Word word; Word word;
int attempts = 0; int attempts = 0;
do { do {
await randomPickWords.init(myProvider.lang, countWords); final List<Word> words = FetchDataHelper().getWords(myProvider.lang, countWords);
final List<Word> words = randomPickWords.words;
word = words.take(1).toList()[0]; word = words.take(1).toList()[0];
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:wordguessing/data/fetch_data_helper.dart';
import 'package:wordguessing/models/word.dart'; import 'package:wordguessing/models/word.dart';
import 'package:wordguessing/provider/data.dart'; import 'package:wordguessing/provider/data.dart';
import 'package:wordguessing/ui/games/abstract_game.dart'; import 'package:wordguessing/ui/games/abstract_game.dart';
import 'package:wordguessing/utils/random_pick_words.dart';
class GamePickWordPage extends GameAbstract { class GamePickWordPage extends GameAbstract {
const GamePickWordPage({super.key}); const GamePickWordPage({super.key});
@override @override
Future<void> pickData(Data myProvider) async { void pickData(Data myProvider) {
final RandomPickWords randomPickWords = RandomPickWords();
Word word; Word word;
int attempts = 0; int attempts = 0;
do { do {
await randomPickWords.init(myProvider.lang, countWords); final List<Word> words = FetchDataHelper().getWords(myProvider.lang, countWords);
final List<Word> words = randomPickWords.words;
word = words.take(1).toList()[0]; word = words.take(1).toList()[0];
......
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:wordguessing/models/word.dart';
import 'package:wordguessing/utils/tools.dart';
class RandomPickWords {
RandomPickWords();
List<Word> _words = [];
init(String lang, int count) async {
await wordsFromLocalFile(lang, count);
}
Future<void> wordsFromLocalFile(String lang, int count) async {
List<Word> wordList = [];
try {
final String jsonString = await rootBundle.loadString('assets/words.json');
final jsonResponse = await json.decode(jsonString);
final Map<String, dynamic> wordsMap = jsonResponse['words'] as Map<String, dynamic>;
wordsMap.forEach((key, data) {
final List<String> images = [];
for (var image in (data['images'] as List<dynamic>)) {
images.add(image as String);
}
images.shuffle();
if (images.isNotEmpty) {
wordList.add(Word(
key: key,
text: data[lang] ?? '',
images: images,
));
}
});
} catch (e) {
printlog("$e");
}
// Check we have enough words
if (wordList.length < count) {
printlog('Not enough words in list.');
}
// Remove empty words
wordList.removeWhere((word) => (word.text == ''));
// Randomize words list
wordList.shuffle();
// Pick first words from shuffled list
_words = wordList.take(count).toList();
}
List<Word> get words => _words;
}
...@@ -5,10 +5,10 @@ packages: ...@@ -5,10 +5,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: args name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596 sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.4.2" version: "2.5.0"
async: async:
dependency: transitive dependency: transitive
description: description:
...@@ -21,10 +21,10 @@ packages: ...@@ -21,10 +21,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: bloc name: bloc
sha256: f53a110e3b48dcd78136c10daa5d51512443cea5e1348c9d80a320095fa2db9e sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "8.1.3" version: "8.1.4"
characters: characters:
dependency: transitive dependency: transitive
description: description:
...@@ -61,10 +61,10 @@ packages: ...@@ -61,10 +61,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: easy_localization name: easy_localization
sha256: c145aeb6584aedc7c862ab8c737c3277788f47488bfdf9bae0fe112bd0a4789c sha256: "432698c31a488dd64c56d4759f20d04844baba5e9e4f2cb1abb9676257918b17"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.0.5" version: "3.0.6"
easy_logger: easy_logger:
dependency: transitive dependency: transitive
description: description:
...@@ -106,18 +106,18 @@ packages: ...@@ -106,18 +106,18 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: flutter_bloc name: flutter_bloc
sha256: "87325da1ac757fcc4813e6b34ed5dd61169973871fdf181d6c2109dd6935ece1" sha256: f0ecf6e6eb955193ca60af2d5ca39565a86b8a142452c5b24d96fb477428f4d2
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "8.1.4" version: "8.1.5"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:
name: flutter_lints name: flutter_lints
sha256: e2a421b7e59244faef694ba7b30562e489c2b489866e505074eb005cd7060db7 sha256: "9e8c3858111da373efc5aa341de011d9bd23e2c5c5e0c62bccf32438e192d7b1"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.0.1" version: "3.0.2"
flutter_localizations: flutter_localizations:
dependency: transitive dependency: transitive
description: flutter description: flutter
...@@ -148,10 +148,10 @@ packages: ...@@ -148,10 +148,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: http name: http
sha256: a2bbf9d017fcced29139daa8ed2bba4ece450ab222871df93ca9eec6f80c34ba sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.2.0" version: "1.2.1"
http_parser: http_parser:
dependency: transitive dependency: transitive
description: description:
...@@ -164,10 +164,10 @@ packages: ...@@ -164,10 +164,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: hydrated_bloc name: hydrated_bloc
sha256: "00a2099680162e74b5a836b8a7f446e478520a9cae9f6032e028ad8129f4432d" sha256: af35b357739fe41728df10bec03aad422cdc725a1e702e03af9d2a41ea05160c
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "9.1.4" version: "9.1.5"
intl: intl:
dependency: transitive dependency: transitive
description: description:
...@@ -212,18 +212,18 @@ packages: ...@@ -212,18 +212,18 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: package_info_plus name: package_info_plus
sha256: "88bc797f44a94814f2213db1c9bd5badebafdfb8290ca9f78d4b9ee2a3db4d79" sha256: b93d8b4d624b4ea19b0a5a208b2d6eff06004bc3ce74c06040b120eeadd00ce0
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.0.1" version: "8.0.0"
package_info_plus_platform_interface: package_info_plus_platform_interface:
dependency: transitive dependency: transitive
description: description:
name: package_info_plus_platform_interface name: package_info_plus_platform_interface
sha256: "9bc8ba46813a4cc42c66ab781470711781940780fd8beddd0c3da62506d3a6c6" sha256: f49918f3433a3146047372f9d4f1f847511f2acd5cd030e1f44fe5a50036b70e
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.1" version: "3.0.0"
path: path:
dependency: "direct main" dependency: "direct main"
description: description:
...@@ -236,18 +236,18 @@ packages: ...@@ -236,18 +236,18 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: path_provider name: path_provider
sha256: b27217933eeeba8ff24845c34003b003b2b22151de3c908d0e679e8fe1aa078b sha256: c9e7d3a4cd1410877472158bee69963a4579f78b68c65a2b7d40d1a7a88bb161
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.2" version: "2.1.3"
path_provider_android: path_provider_android:
dependency: transitive dependency: transitive
description: description:
name: path_provider_android name: path_provider_android
sha256: "477184d672607c0a3bf68fbbf601805f92ef79c82b64b4d6eb318cbca4c48668" sha256: a248d8146ee5983446bf03ed5ea8f6533129a12b11f12057ad1b4a67a2b3b41d
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.2.2" version: "2.2.4"
path_provider_foundation: path_provider_foundation:
dependency: transitive dependency: transitive
description: description:
...@@ -308,18 +308,18 @@ packages: ...@@ -308,18 +308,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: shared_preferences name: shared_preferences
sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02" sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.2.2" version: "2.2.3"
shared_preferences_android: shared_preferences_android:
dependency: transitive dependency: transitive
description: description:
name: shared_preferences_android name: shared_preferences_android
sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06" sha256: "1ee8bf911094a1b592de7ab29add6f826a7331fb854273d55918693d5364a1f2"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.2.1" version: "2.2.2"
shared_preferences_foundation: shared_preferences_foundation:
dependency: transitive dependency: transitive
description: description:
...@@ -348,10 +348,10 @@ packages: ...@@ -348,10 +348,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: shared_preferences_web name: shared_preferences_web
sha256: "7b15ffb9387ea3e237bb7a66b8a23d2147663d391cafc5c8f37b2e7b4bde5d21" sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.2.2" version: "2.3.0"
shared_preferences_windows: shared_preferences_windows:
dependency: transitive dependency: transitive
description: description:
...@@ -425,18 +425,18 @@ packages: ...@@ -425,18 +425,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: web name: web
sha256: "4188706108906f002b3a293509234588823c8c979dc83304e229ff400c996b05" sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.4.2" version: "0.5.1"
win32: win32:
dependency: transitive dependency: transitive
description: description:
name: win32 name: win32
sha256: "8cb58b45c47dcb42ab3651533626161d6b67a2921917d8d429791f76972b3480" sha256: "0eaf06e3446824099858367950a813472af675116bf63f008a4c2a75ae13e9cb"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.3.0" version: "5.5.0"
xdg_directories: xdg_directories:
dependency: transitive dependency: transitive
description: description:
...@@ -447,4 +447,4 @@ packages: ...@@ -447,4 +447,4 @@ packages:
version: "1.0.4" version: "1.0.4"
sdks: sdks:
dart: ">=3.3.0 <4.0.0" dart: ">=3.3.0 <4.0.0"
flutter: ">=3.16.0" flutter: ">=3.19.0"
name: wordguessing name: wordguessing
description: A wordguessing game application. description: A wordguessing game application.
publish_to: 'none' publish_to: "none"
version: 0.1.39+63 version: 0.1.40+64
environment: environment:
sdk: '^3.0.0' sdk: "^3.0.0"
dependencies: dependencies:
flutter: flutter:
...@@ -17,7 +17,7 @@ dependencies: ...@@ -17,7 +17,7 @@ dependencies:
flutter_swipe: ^1.0.1 flutter_swipe: ^1.0.1
hive: ^2.2.3 hive: ^2.2.3
hydrated_bloc: ^9.0.0 hydrated_bloc: ^9.0.0
package_info_plus: ^5.0.1 package_info_plus: ^8.0.0
path: ^1.9.0 path: ^1.9.0
path_provider: ^2.0.11 path_provider: ^2.0.11
provider: ^6.0.5 provider: ^6.0.5
...@@ -29,7 +29,6 @@ dev_dependencies: ...@@ -29,7 +29,6 @@ dev_dependencies:
flutter: flutter:
uses-material-design: true uses-material-design: true
assets: assets:
- assets/words.json
- assets/images/ - assets/images/
- assets/menu/ - assets/menu/
- assets/placeholder.png - assets/placeholder.png
...@@ -46,4 +45,3 @@ flutter: ...@@ -46,4 +45,3 @@ flutter:
weight: 400 weight: 400
- asset: assets/fonts/Nunito-Light.ttf - asset: assets/fonts/Nunito-Light.ttf
weight: 300 weight: 300
...@@ -10,7 +10,7 @@ SOURCE_CSV_FILE="${CURRENT_DIR}/words.csv" ...@@ -10,7 +10,7 @@ SOURCE_CSV_FILE="${CURRENT_DIR}/words.csv"
ASSETS_BASE_FOLDER="${BASE_DIR}/assets" ASSETS_BASE_FOLDER="${BASE_DIR}/assets"
OUTPUT_WORDS_LIST="${ASSETS_BASE_FOLDER}/words.json" OUTPUT_WORDS_LIST="${CURRENT_DIR}/words.json"
touch "${OUTPUT_WORDS_LIST}" touch "${OUTPUT_WORDS_LIST}"
TARGET_IMAGES_ASSETS_FOLDER="${ASSETS_BASE_FOLDER}/images" TARGET_IMAGES_ASSETS_FOLDER="${ASSETS_BASE_FOLDER}/images"
...@@ -83,4 +83,12 @@ echo "}" >> "${OUTPUT_WORDS_LIST_TMP}" ...@@ -83,4 +83,12 @@ echo "}" >> "${OUTPUT_WORDS_LIST_TMP}"
cat "${OUTPUT_WORDS_LIST_TMP}" | jq > "${OUTPUT_WORDS_LIST}" cat "${OUTPUT_WORDS_LIST_TMP}" | jq > "${OUTPUT_WORDS_LIST}"
rm "${OUTPUT_WORDS_LIST_TMP}" rm "${OUTPUT_WORDS_LIST_TMP}"
# inject json file in app code
GAME_DATA_DART_FILE="${BASE_DIR}/lib/data/game_data.dart"
echo "class GameData {" >"${GAME_DATA_DART_FILE}"
echo " static const Map<String, dynamic> data = $(cat "${OUTPUT_WORDS_LIST}");" >>"${GAME_DATA_DART_FILE}"
echo "}" >>"${GAME_DATA_DART_FILE}"
dart format "${GAME_DATA_DART_FILE}"
echo "done." echo "done."
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment