Skip to content

Commit

Permalink
feat: load dictionaries at startup, various improvements & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen-meijer committed Jul 20, 2020
1 parent 77ac966 commit 696b3b6
Show file tree
Hide file tree
Showing 26 changed files with 699 additions and 110 deletions.
43 changes: 43 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
**/.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch app",
"name": "Debug app",
"request": "launch",
"type": "dart",
"program": "app/lib/main.dart"
},
{
"name": "Run dict parser (example.xml)",
"name": "Debug dict parser (example.xml)",
"program": "bin/dict_parser.dart",
"request": "launch",
"type": "dart",
Expand Down
1 change: 1 addition & 0 deletions app/assets/dicts/dict_ja.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions app/lib/app/app_root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:shiritori/assets/assets.dart';
import 'package:shiritori/intl/intl.dart';
import 'package:shiritori/theme/theme.dart';
import 'package:shiritori/ui/routes/routes.dart';
Expand All @@ -11,14 +12,19 @@ import 'package:shiritori/ui/screens/home/home.dart';
class AppRoot extends StatelessWidget {
const AppRoot({
@required this.backgroundImage,
@required this.dictionaries,
});

final ui.Image backgroundImage;
final Dictionaries dictionaries;

@override
Widget build(BuildContext context) {
return Provider<ui.Image>.value(
value: backgroundImage,
return MultiProvider(
providers: [
Provider<ui.Image>.value(value: backgroundImage),
Provider<Dictionaries>.value(value: dictionaries),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
theme: AppTheme.themeDataLight,
Expand Down
1 change: 1 addition & 0 deletions app/lib/assets/assets.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'dictionaries.dart';
export 'fonts.dart';
export 'images.dart';
52 changes: 52 additions & 0 deletions app/lib/assets/dictionaries.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:provider/provider.dart';
import 'package:shared_models/shared_models.dart';

@immutable
class Dictionaries {
const Dictionaries({
@required this.japanese,
});

final Dictionary japanese;

List<Dictionary> get all => [japanese];

List<Language> get supportedLanguages {
return all.map((dict) => dict.language).toList();
}

static Future<Dictionary> _loadDictionaryFromDisk(Language language) async {
final dictText =
await rootBundle.loadString('assets/dicts/dict_${language.code}.json');
final dictJson = json.decode(dictText) as Map<String, dynamic>;

final dict = Dictionary.fromJson(dictJson);

return dict;
}

static Future<Dictionaries> loadFromDisk() async {
final japanese = await _loadDictionaryFromDisk(Language.japanese);

return Dictionaries(
japanese: japanese,
);
}
}

extension DictionariesX on Dictionaries {
static Dictionaries of(BuildContext context) {
return Provider.of<Dictionaries>(context, listen: false);
}
}

extension DictionaryX on Dictionary {
static Dictionary of(BuildContext context) {
return Provider.of<Dictionary>(context, listen: false);
}
}
19 changes: 16 additions & 3 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ import 'package:shiritori/app/app.dart';
import 'assets/assets.dart';

void main() async {
log('Initializing binding...');
log('Initializing bindings...');

final initStopwatch = Stopwatch()..start();
WidgetsFlutterBinding.ensureInitialized();
log('Loading background image...');
final backgroundImage = await Images.loadBackground();
log('Initialization done.');
runApp(AppRoot(backgroundImage: backgroundImage));
log('Loading dictionaries...');
final dictionaries = await Dictionaries.loadFromDisk();
initStopwatch.stop();

final seconds = (initStopwatch.elapsedMilliseconds / 1000).toStringAsFixed(3);
log('Initialization done. (took $seconds seconds)');

runApp(
AppRoot(
backgroundImage: backgroundImage,
dictionaries: dictionaries,
),
);
}
18 changes: 16 additions & 2 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.2"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
matcher:
dependency: transitive
description:
Expand All @@ -185,7 +192,7 @@ packages:
source: hosted
version: "0.12.6"
meta:
dependency: transitive
dependency: "direct main"
description:
name: meta
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -268,6 +275,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.4"
shared_models:
dependency: "direct main"
description:
path: "../shared_models"
relative: true
source: path
version: "1.0.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -351,5 +365,5 @@ packages:
source: hosted
version: "2.2.1"
sdks:
dart: ">=2.7.0 <3.0.0"
dart: ">=2.8.1 <3.0.0"
flutter: "1.19.0-4.3.pre"
4 changes: 4 additions & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ dependencies:
sdk: flutter
flutter_localizations:
sdk: flutter
shared_models:
path: ../shared_models
animations: ^1.1.1
equatable: 1.2.2
flutter_svg: 0.17.4
font_awesome_flutter: 8.8.1
intl: 0.16.1
meta: 1.1.8
provider: ^4.3.1

dev_dependencies:
Expand All @@ -32,6 +35,7 @@ flutter:
assets:
- assets/images/png/
- assets/images/svg/
- assets/dicts/
fonts:
- family: HurmeGeometricSans
fonts:
Expand Down
1 change: 1 addition & 0 deletions dict_parser/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build/

# Directory created by dartdoc
doc/api/
dict_parser
4 changes: 3 additions & 1 deletion dict_parser/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
A simple command-line application.
# dict_parser

A parser for the Shiritori Flutter app that converts an XML dictionary file to an easily parsable dictionary for the Shiritori client app.

Created from templates made available by Stagehand under a BSD-style
[license](https://github.com/dart-lang/stagehand/blob/master/LICENSE).
48 changes: 26 additions & 22 deletions dict_parser/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:dict_parser/utils.dart';
import 'package:shared_models/shared_models.dart';
import 'package:xml/xml.dart';

const _wordCountLimit = 20000;

// TODO: Move to config wrapper class for better compatibilty with other dicts.
const _nounTag = '&n;';
const _spellingElementPath = ['k_ele', 'keb'];
Expand Down Expand Up @@ -43,26 +45,29 @@ Future<void> main(List<String> args) async {
});

_time('Parsing nouns');
final nouns = xmlNouns.map((noun) {
final spellings = noun
.findAllElementsDeep(_spellingElementPath)
.mapEachToText()
.toList(growable: false);
final phoneticSpellings = noun
.findAllElementsDeep(_phoneticSpellingElementPath)
.mapEachToText()
.toList(growable: false);
final definitions = noun
.findAllElementsDeep(_definitionsElementPath)
.mapEachToText()
.toList(growable: false);

return WordEntry(
spellings: spellings,
phoneticSpellings: phoneticSpellings,
definitions: definitions,
);
}).toList(growable: false);
final nouns = xmlNouns
.map((noun) {
final spellings = noun
.findAllElementsDeep(_spellingElementPath)
.mapEachToText()
.toList(growable: false);
final phoneticSpellings = noun
.findAllElementsDeep(_phoneticSpellingElementPath)
.mapEachToText()
.toList(growable: false);
final definitions = noun
.findAllElementsDeep(_definitionsElementPath)
.mapEachToText()
.toList(growable: false);

return WordEntry(
spellings: spellings,
phoneticSpellings: phoneticSpellings,
definitions: definitions,
);
})
.take(_wordCountLimit)
.toList(growable: false);
_stopTime();

_time('Indexing into dictionary...');
Expand All @@ -79,8 +84,7 @@ Future<void> main(List<String> args) async {
_stopTime();

final exportFile = File(
'${xmlFileName.split('.').first}_'
'${DateTime.now().millisecondsSinceEpoch}.json',
'dict_${dictionary.language.code}.json',
);

_time('Exporting to file (${exportFile.path})');
Expand Down
13 changes: 10 additions & 3 deletions dict_parser/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
meta:
dependency: "direct main"
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.2"
version: "1.1.8"
pedantic:
dependency: "direct dev"
description:
Expand All @@ -63,7 +70,7 @@ packages:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
version: "3.0.4"
shared_models:
dependency: "direct main"
description:
Expand Down Expand Up @@ -91,6 +98,6 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.0"
version: "4.2.0"
sdks:
dart: ">=2.8.1 <3.0.0"
4 changes: 2 additions & 2 deletions dict_parser/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ environment:
dependencies:
shared_models:
path: ../shared_models
meta: 1.2.2
meta: 1.1.8
sqlite3: 0.1.4
xml: 4.3.0
xml: 4.2.0

dev_dependencies:
effective_dart: 1.2.3
Expand Down
Loading

0 comments on commit 696b3b6

Please sign in to comment.