Skip to content

Commit

Permalink
Merge pull request #18 from xsoulspace/feature/dictionary-check
Browse files Browse the repository at this point in the history
Feature/dictionary check
  • Loading branch information
Arenukvern authored Nov 4, 2020
2 parents 342ebc9 + 4871810 commit e764d72
Show file tree
Hide file tree
Showing 27 changed files with 628 additions and 289 deletions.
3 changes: 3 additions & 0 deletions lib/constants/AppConstraints.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AppConstraints {
static final int mobileWidth = 350;
}
1 change: 1 addition & 0 deletions lib/constants/GameNotificationStatuses.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum GameNotificationStatuses { warn, done, error }
10 changes: 5 additions & 5 deletions lib/constants/Locales.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:flutter/cupertino.dart';

class Locales {
static Locale get en => Locale(Languages.en, 'EN');
static Locale get ru => Locale(Languages.ru, 'RU');
static const Locale en = Locale(Languages.en, 'EN');
static const Locale ru = Locale(Languages.ru, 'RU');
}

class Languages {
static String ru = 'ru';
static String en = 'en';
static final List items = [
static const String ru = 'ru';
static const String en = 'en';
static const List items = [
ru,
en,
];
Expand Down
7 changes: 5 additions & 2 deletions lib/entities/GameNotification.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:flutter/foundation.dart';
import 'package:word_by_word_game/constants/GameNotificationStatuses.dart';
import 'package:word_by_word_game/entities/LocalName.dart';

class GameNotification {
final bool status;
final GameNotificationStatuses status;
final LocalName localName;
GameNotification({@required this.status, @required this.localName});
final String newWord;
GameNotification(
{@required this.status, @required this.localName, this.newWord});
}
5 changes: 5 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,10 @@
"@players": {
"type": "text",
"placeholders": {}
},
"addToDictionary": "Add to dictionary",
"@addToDictionary": {
"type": "text",
"placeholders": {}
}
}
7 changes: 6 additions & 1 deletion lib/l10n/intl_messages.arb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@@last_modified": "2020-11-03T04:20:13.003304",
"@@last_modified": "2020-11-04T01:07:37.983370",
"finishGame": "Finish game",
"@finishGame": {
"type": "text",
Expand Down Expand Up @@ -104,5 +104,10 @@
"@players": {
"type": "text",
"placeholders": {}
},
"addToDictionary": "Add to dictionary",
"@addToDictionary": {
"type": "text",
"placeholders": {}
}
}
5 changes: 5 additions & 0 deletions lib/l10n/intl_ru.arb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,10 @@
"@players": {
"type": "text",
"placeholders": {}
},
"addToDictionary": "Добавить в словарь",
"@addToDictionary": {
"type": "text",
"placeholders": {}
}
}
1 change: 1 addition & 0 deletions lib/l10n/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => <String, Function> {
"addNewWord" : MessageLookupByLibrary.simpleMessage("add"),
"addToDictionary" : MessageLookupByLibrary.simpleMessage("Add to dictionary"),
"congratulations" : MessageLookupByLibrary.simpleMessage("Congratulations!"),
"continueGame" : MessageLookupByLibrary.simpleMessage("Continue "),
"copyright" : MessageLookupByLibrary.simpleMessage("2020 © X Soul Space "),
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/messages_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => <String, Function> {
"addNewWord" : MessageLookupByLibrary.simpleMessage("add"),
"addToDictionary" : MessageLookupByLibrary.simpleMessage("Add to dictionary"),
"congratulations" : MessageLookupByLibrary.simpleMessage("Congratulations!"),
"continueGame" : MessageLookupByLibrary.simpleMessage("Continue "),
"copyright" : MessageLookupByLibrary.simpleMessage("2020 © X Soul Space "),
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/messages_ru.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => <String, Function> {
"addNewWord" : MessageLookupByLibrary.simpleMessage("добавить"),
"addToDictionary" : MessageLookupByLibrary.simpleMessage("Добавить в словарь"),
"congratulations" : MessageLookupByLibrary.simpleMessage("Поздравляем!"),
"continueGame" : MessageLookupByLibrary.simpleMessage("Продолжить "),
"copyright" : MessageLookupByLibrary.simpleMessage("2020 © X Soul Space "),
Expand Down
4 changes: 4 additions & 0 deletions lib/localizations/MainLocalizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ class MainLocalizations {
String get players {
return Intl.message('Players: ', name: 'players');
}

String get addToDictionary {
return Intl.message('Add to dictionary', name: 'addToDictionary');
}
}

class MainLocalizationsDelegate
Expand Down
16 changes: 14 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
import 'package:word_by_word_game/constants/Locales.dart';
import 'package:word_by_word_game/entities/FirstPlayer.dart';
import 'package:word_by_word_game/entities/Player.dart';
import 'package:word_by_word_game/models/LocalDictionaryModel.dart';
import 'package:word_by_word_game/models/LocaleModel.dart';
import 'package:word_by_word_game/models/NotificationsModel.dart';
import 'package:word_by_word_game/models/PlayerColorsModel.dart';
Expand Down Expand Up @@ -44,6 +45,8 @@ class MyApp extends StatelessWidget {
if (snapshot.connectionState == ConnectionState.done) {
return MultiProvider(providers: [
ChangeNotifierProvider(create: (context) => snapshot.data),
ChangeNotifierProvider(
create: (context) => LocalDictionaryModel()),
ChangeNotifierProvider(
create: (context) => PlayersModel(
{firstPlayer.id: firstPlayer},
Expand All @@ -52,7 +55,12 @@ class MyApp extends StatelessWidget {
.map((color) =>
Player(id: color.id, playerColor: color))
.toList())),
ChangeNotifierProvider(create: (context) => WordsModel({}, {})),
ChangeNotifierProvider(create: (context) {
var localDictionaryModel =
Provider.of<LocalDictionaryModel>(context, listen: false);
return WordsModel({}, {},
localDictionaryModel: localDictionaryModel);
}),
ChangeNotifierProvider(create: (context) => PlayerColorsModel()),
ChangeNotifierProvider(create: (context) => ScoreModel()),
ChangeNotifierProvider(create: (context) => NotificationsModel()),
Expand All @@ -61,8 +69,12 @@ class MyApp extends StatelessWidget {
Provider.of<WordsModel>(context, listen: false);
var playersModel =
Provider.of<PlayersModel>(context, listen: false);
var localDictionaryModel =
Provider.of<LocalDictionaryModel>(context, listen: false);
return await StorageModel.create(
wordsModel: wordsModel, playersModel: playersModel);
wordsModel: wordsModel,
playersModel: playersModel,
localDictionaryModel: localDictionaryModel);
}),
], child: ScaffoldApp());
} else {
Expand Down
30 changes: 30 additions & 0 deletions lib/models/LocalDictionaryModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';

part 'LocalDictionaryModel.g.dart';

class LocalDictionaryModelConsts {
static String storagename = 'localdictionary';
}

@JsonSerializable(nullable: true)
class LocalDictionaryModel extends ChangeNotifier {
Set<String> _words;
LocalDictionaryModel({Set<String> words}) {
this._words = words ?? {};
}
Set<String> get words => _words;
void addWord(String value) {
_words.add(value);
notifyListeners();
}

void reloadState({@required Set<String> words}) {
_words.clear();
_words.addAll(words);
}

factory LocalDictionaryModel.fromJson(Map<String, dynamic> json) =>
_$LocalDictionaryModelFromJson(json);
Map<String, dynamic> toJson() => _$LocalDictionaryModelToJson(this);
}
19 changes: 19 additions & 0 deletions lib/models/LocalDictionaryModel.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions lib/models/LocaleModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ class LocaleModel extends ChangeNotifier with StorageMixin {
static Future<Locale> loadSavedLocale() async {
StorageUtil storage = await StorageUtil.getInstance();
String localeStr = storage.getString(LocaleModelConsts.storagename);
if (localeStr == null || localeStr == '') {
if (kIsWeb || Platform.isWindows) return Locales.en;
if (localeStr.isEmpty) {
// FIXME: strange things happend with locales on all OS!
// seems like it has new formats nn__UTF08__NN
if (kIsWeb ||
Platform.isWindows ||
Platform.isLinux ||
Platform.isAndroid ||
Platform.isIOS ||
Platform.isMacOS) return Locales.en;

Intl.defaultLocale = await findSystemLocale();
var systemLocale = await findSystemLocale();
Intl.defaultLocale = systemLocale;
return Locale(Intl.defaultLocale);
}

Expand All @@ -67,7 +75,7 @@ class LocaleModel extends ChangeNotifier with StorageMixin {

NamedLocale get currentNamedLocale =>
LocaleModelConsts.namedLocales.firstWhere((namedLocale) {
var isEqual = _locale == namedLocale.locale;
var isEqual = _locale.languageCode == namedLocale.locale.languageCode;
return isEqual;
});
}
35 changes: 25 additions & 10 deletions lib/models/StorageModel.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:word_by_word_game/models/LocalDictionaryModel.dart';
import 'package:word_by_word_game/models/PlayersModel.dart';
import 'package:word_by_word_game/models/StorageMixin.dart';
import 'package:word_by_word_game/models/WordsModel.dart';

class StorageModel extends ChangeNotifier with StorageMixin {
WordsModel _wordsModel;
set wordsModel(WordsModel wordsModel) {
if (wordsModel != null) _wordsModel = wordsModel;
}

PlayersModel _playersModel;
set playersModel(PlayersModel playersModel) {
if (playersModel != null) _playersModel = playersModel;
}
LocalDictionaryModel _localDictionaryModel;

// Private constructor
StorageModel._create(this._wordsModel, this._playersModel);
StorageModel._create(
this._wordsModel, this._playersModel, this._localDictionaryModel);

/// Public factory
static Future<StorageModel> create(
{@required WordsModel wordsModel,
@required PlayersModel playersModel}) async {
@required PlayersModel playersModel,
@required LocalDictionaryModel localDictionaryModel}) async {
// Call the private constructor
var storageModel = StorageModel._create(wordsModel, playersModel);
var storageModel =
StorageModel._create(wordsModel, playersModel, localDictionaryModel);

await storageModel.checkAndLoadStorageInstance();
await storageModel.loadPlayersModel();
await storageModel.loadWordsModel();
await storageModel.loadLocalDictionary();
// Return the fully initialized object
return storageModel;
}
Expand Down Expand Up @@ -84,4 +83,20 @@ class StorageModel extends ChangeNotifier with StorageMixin {
..notifyListeners();
notifyListeners();
}

Future<void> loadLocalDictionary() async {
var modelStr = await load(LocalDictionaryModelConsts.storagename);
if (modelStr == null) return;
var model = LocalDictionaryModel.fromJson(modelStr);
_localDictionaryModel
..reloadState(words: model.words)
..notifyListeners();
notifyListeners();
}

Future<void> saveLocalDictionary() async {
await save(
key: LocalDictionaryModelConsts.storagename,
value: _localDictionaryModel.toJson());
}
}
Loading

0 comments on commit e764d72

Please sign in to comment.