Replace objectbox with sembast

This commit is contained in:
Oystein Kristoffer Tveit 2021-12-04 05:13:13 +01:00
parent a0c608ccca
commit fe5c5a4cce
23 changed files with 754 additions and 782 deletions

2
.gitignore vendored
View File

@ -30,8 +30,6 @@
.pub/
/build/
objectbox.g.dart
# Web related
lib/generated_plugin_registrant.dart

View File

@ -1,6 +1,5 @@
analyzer:
exclude:
- "lib/objectbox.g.dart"
linter:
rules:

View File

@ -1,27 +0,0 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import './database_event.dart';
import './database_state.dart';
export 'package:flutter_bloc/flutter_bloc.dart';
export './database_event.dart';
export './database_not_connected_exception.dart';
export './database_state.dart';
class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {
DatabaseBloc() : super(const DatabaseDisconnected());
@override
Stream<DatabaseState> mapEventToState(DatabaseEvent event)
async* {
if (event is ConnectedToDatabase) {
yield DatabaseConnected(event.database);
} else {
yield const DatabaseDisconnected();
}
}
}

View File

@ -1,14 +0,0 @@
import 'package:objectbox/objectbox.dart';
abstract class DatabaseEvent {
const DatabaseEvent();
}
class ConnectedToDatabase extends DatabaseEvent {
final Store database;
const ConnectedToDatabase(this.database);
}
class DisconnectedFromDatabase extends DatabaseEvent {
const DisconnectedFromDatabase();
}

View File

@ -1 +0,0 @@
class DatabaseNotConnectedException implements Exception {}

View File

@ -1,14 +0,0 @@
import 'package:objectbox/objectbox.dart';
abstract class DatabaseState {
const DatabaseState();
}
class DatabaseConnected extends DatabaseState {
final Store database;
const DatabaseConnected(this.database);
}
class DatabaseDisconnected extends DatabaseState {
const DatabaseDisconnected();
}

View File

@ -1,12 +1,15 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:mdi/mdi.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
import 'bloc/database/database_bloc.dart';
import 'bloc/theme/theme_bloc.dart';
import 'models/themes/theme.dart';
import 'objectbox.g.dart';
import 'router.dart';
import 'view/components/common/splash.dart';
import 'view/screens/history.dart';
@ -14,56 +17,35 @@ import 'view/screens/search/kanji_view.dart';
import 'view/screens/search/search_view.dart';
import 'view/screens/settings.dart';
void main() => runApp(const MyApp());
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final Directory appDocDir = await getApplicationDocumentsDirectory();
DatabaseBloc _databaseBloc = DatabaseBloc();
if (!appDocDir.existsSync())
appDocDir.createSync(recursive: true);
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
final Database db = await databaseFactoryIo.openDatabase(join(appDocDir.path, 'sembast.db'));
@override
_MyAppState createState() => _MyAppState();
GetIt.instance.registerSingleton<Database>(db);
runApp(const MyApp());
}
class _MyAppState extends State<MyApp> {
late final Store _store;
bool dbConnected = false;
class MyApp extends StatelessWidget {
@override
void initState() {
super.initState();
getApplicationDocumentsDirectory().then((dir) {
_store = Store(
getObjectBoxModel(),
directory: join(dir.path, 'objectbox'),
);
_databaseBloc.add(ConnectedToDatabase(_store));
setState(() {
dbConnected = true;
});
});
}
@override
void dispose() {
_store.close();
_databaseBloc.add(const DisconnectedFromDatabase());
super.dispose();
}
const MyApp({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => _databaseBloc),
BlocProvider(create: (context) => ThemeBloc()),
],
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, themeState) {
if (!(dbConnected && themeState.prefsAreLoaded))
return const SplashScreen();
if (!themeState.prefsAreLoaded) return const SplashScreen();
return MaterialApp(
title: 'Jisho Study Tool',
@ -77,6 +59,18 @@ class _MyAppState extends State<MyApp> {
}
}
class _Page {
final Widget content;
final Widget titleBar;
final BottomNavigationBarItem item;
const _Page({
required this.content,
required this.titleBar,
required this.item,
});
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@ -126,59 +120,47 @@ class _HomeState extends State<Home> {
},
);
}
List<_Page> get pages => [
const _Page(
content: SearchView(),
titleBar: Text('Search'),
item: BottomNavigationBarItem(
label: 'Search',
icon: Icon(Icons.search),
),
),
const _Page(
content: KanjiView(),
titleBar: Text('Kanji'),
item: BottomNavigationBarItem(
label: 'Kanji',
icon: Icon(Mdi.ideogramCjk, size: 30),
),
),
const _Page(
content: HistoryView(),
titleBar: Text('History'),
item: BottomNavigationBarItem(
label: 'History',
icon: Icon(Icons.history),
),
),
_Page(
content: Container(),
titleBar: const Text('Saved'),
item: const BottomNavigationBarItem(
label: 'Saved',
icon: Icon(Icons.bookmark),
),
),
const _Page(
content: SettingsView(),
titleBar: Text('Settings'),
item: BottomNavigationBarItem(
label: 'Settings',
icon: Icon(Icons.settings),
),
),
];
}
class _Page {
final Widget content;
final Widget titleBar;
final BottomNavigationBarItem item;
const _Page({
required this.content,
required this.titleBar,
required this.item,
});
}
final List<_Page> pages = [
const _Page(
content: SearchView(),
titleBar: Text('Search'),
item: BottomNavigationBarItem(
label: 'Search',
icon: Icon(Icons.search),
),
),
const _Page(
content: KanjiView(),
titleBar: Text('Kanji'),
item: BottomNavigationBarItem(
label: 'Kanji',
icon: Icon(Mdi.ideogramCjk, size: 30),
),
),
const _Page(
content: HistoryView(),
titleBar: Text('History'),
item: BottomNavigationBarItem(
label: 'History',
icon: Icon(Icons.history),
),
),
_Page(
content: Container(),
titleBar: const Text('Saved'),
item: const BottomNavigationBarItem(
label: 'Saved',
icon: Icon(Icons.bookmark),
),
),
const _Page(
content: SettingsView(),
titleBar: Text('Settings'),
item: BottomNavigationBarItem(
label: 'Settings',
icon: Icon(Icons.settings),
),
),
];

View File

@ -1,13 +1,12 @@
import 'package:objectbox/objectbox.dart';
@Entity()
class KanjiQuery {
int id;
String kanji;
final String kanji;
KanjiQuery({
this.id = 0,
required this.kanji,
});
Map<String, Object?> toJson() => {'kanji': kanji};
factory KanjiQuery.fromJson(Map<String, dynamic> json) =>
KanjiQuery(kanji: json['kanji'] as String);
}

View File

@ -1,30 +1,43 @@
import 'package:objectbox/objectbox.dart';
import 'package:sembast/sembast.dart';
import 'package:sembast/timestamp.dart';
import './kanji_query.dart';
import './word_query.dart';
@Entity()
class Search {
int id;
final DateTime timestamp;
final WordQuery? wordQuery;
final KanjiQuery? kanjiQuery;
@Property(type: PropertyType.date)
late final DateTime timestamp;
final wordQuery = ToOne<WordQuery>();
final kanjiQuery = ToOne<KanjiQuery>();
Search({
this.id = 0,
Search.fromKanjiQuery({
required this.timestamp,
});
required KanjiQuery this.kanjiQuery,
}) : wordQuery = null;
bool isKanji() {
// // TODO: better error message
if (wordQuery.target == null && kanjiQuery.target == null)
throw Exception();
return wordQuery.target == null;
}
Search.fromWordQuery({
required this.timestamp,
required WordQuery this.wordQuery,
}) : kanjiQuery = null;
bool get isKanji => wordQuery == null;
Map<String, Object?> toJson() => {
'timestamp': timestamp.millisecondsSinceEpoch,
'wordQuery': wordQuery?.toJson(),
'kanjiQuery': kanjiQuery?.toJson(),
};
factory Search.fromJson(Map<String, dynamic> json) =>
json['wordQuery'] != null
? Search.fromWordQuery(
timestamp:
DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
wordQuery: WordQuery.fromJson(json['wordQuery']),
)
: Search.fromKanjiQuery(
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
kanjiQuery: KanjiQuery.fromJson(json['kanjiQuery']),
);
static StoreRef<int, Object?> get store => intMapStoreFactory.store('search');
}

View File

@ -1,19 +1,17 @@
import 'package:objectbox/objectbox.dart';
import './word_result.dart';
@Entity()
class WordQuery {
int id;
String query;
final String query;
// TODO: Link query with results that the user clicks onto.
@Backlink()
final chosenResults = ToMany<WordResult>();
// final List<WordResult> chosenResults;
WordQuery({
this.id = 0,
required this.query,
});
Map<String, Object?> toJson() => {'query': query};
factory WordQuery.fromJson(Map<String, dynamic> json) =>
WordQuery(query: json['query'] as String);
}

View File

@ -1,21 +1,13 @@
import 'package:objectbox/objectbox.dart';
import 'word_query.dart';
@Entity()
class WordResult {
int id;
@Property(type: PropertyType.date)
DateTime timestamp;
String word;
final searchString = ToOne<WordQuery>();
final DateTime timestamp;
final String word;
final WordQuery searchString;
WordResult({
this.id = 0,
required this.timestamp,
required this.word,
required this.searchString,
});
}

View File

@ -1,13 +1,22 @@
import 'package:objectbox/objectbox.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
// import 'package:objectbox/objectbox.dart';
// import 'package:unofficial_jisho_api/api.dart' as jisho;
@Entity()
class ExampleSentencePiece {
int id = 0;
String? lifted;
String unlifted;
// TODO: Rewrite for sembast
ExampleSentencePiece.fromJishoObject(jisho.ExampleSentencePiece object) :
lifted = object.lifted,
unlifted = object.unlifted;
}
// @Entity()
// class ExampleSentencePiece {
// int id;
// String? lifted;
// String unlifted;
// ExampleSentencePiece({
// this.id = 0,
// required this.lifted,
// required this.unlifted,
// });
// ExampleSentencePiece.fromJishoObject(jisho.ExampleSentencePiece object)
// : id = 0,
// lifted = object.lifted,
// unlifted = object.unlifted;
// }

View File

@ -1,36 +1,58 @@
import 'package:objectbox/objectbox.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
// import 'package:objectbox/objectbox.dart';
// import 'package:unofficial_jisho_api/api.dart' as jisho;
import 'common.dart';
// import 'common.dart';
@Entity()
class ExampleResultData {
String kanji;
String kana;
String english;
List<ExampleSentencePiece> pieces;
// TODO: Rewrite for sembast
ExampleResultData.fromJishoObject(jisho.ExampleResultData object)
: kanji = object.kanji,
kana = object.kana,
english = object.english,
pieces = object.pieces
.map((p) => ExampleSentencePiece.fromJishoObject(p))
.toList();
}
// @Entity()
// class ExampleResultData {
// int id;
// String kanji;
// String kana;
// String english;
// List<ExampleSentencePiece> pieces;
@Entity()
class ExampleResults {
String query;
bool found;
String uri;
List<ExampleResultData> results;
// ExampleResultData({
// this.id = 0,
// required this.kanji,
// required this.kana,
// required this.english,
// required this.pieces,
// });
ExampleResults.fromJishoObject(jisho.ExampleResults object)
: query = object.query,
found = object.found,
uri = object.uri,
results = object.results
.map((r) => ExampleResultData.fromJishoObject(r))
.toList();
}
// ExampleResultData.fromJishoObject(jisho.ExampleResultData object)
// : id = 0,
// kanji = object.kanji,
// kana = object.kana,
// english = object.english,
// pieces = object.pieces
// .map((p) => ExampleSentencePiece.fromJishoObject(p))
// .toList();
// }
// @Entity()
// class ExampleResults {
// int id;
// String query;
// bool found;
// String uri;
// List<ExampleResultData> results;
// ExampleResults({
// this.id = 0,
// required this.query,
// required this.found,
// required this.uri,
// required this.results,
// });
// ExampleResults.fromJishoObject(jisho.ExampleResults object)
// : id = 0,
// query = object.query,
// found = object.found,
// uri = object.uri,
// results = object.results
// .map((r) => ExampleResultData.fromJishoObject(r))
// .toList();
// }

View File

@ -1,86 +1,129 @@
import 'package:objectbox/objectbox.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
// import 'package:objectbox/objectbox.dart';
// import 'package:unofficial_jisho_api/api.dart' as jisho;
@Entity()
class YomiExample {
int id = 0;
String example;
String reading;
String meaning;
// TODO: Rewrite for sembast
YomiExample.fromJishoObject(jisho.YomiExample object)
: example = object.example,
reading = object.reading,
meaning = object.meaning;
}
// @Entity()
// class YomiExample {
// int id;
// String example;
// String reading;
// String meaning;
@Entity()
class Radical {
int id = 0;
String symbol;
List<String> forms;
String meaning;
// YomiExample({
// this.id = 0,
// required this.example,
// required this.reading,
// required this.meaning,
// });
Radical.fromJishoObject(jisho.Radical object)
: symbol = object.symbol,
forms = object.forms,
meaning = object.meaning;
}
// YomiExample.fromJishoObject(jisho.YomiExample object)
// : id = 0,
// example = object.example,
// reading = object.reading,
// meaning = object.meaning;
// }
@Entity()
class KanjiResult {
int id = 0;
String query;
bool found;
KanjiResultData? data;
// @Entity()
// class Radical {
// int id = 0;
// String symbol;
// List<String> forms;
// String meaning;
KanjiResult.fromJishoObject(jisho.KanjiResult object)
: query = object.query,
found = object.found,
data = (object.data == null)
? null
: KanjiResultData.fromJishoObject(object.data!);
}
// Radical({
// this.id = 0,
// required this.symbol,
// required this.forms,
// required this.meaning,
// });
@Entity()
class KanjiResultData {
int id = 0;
String? taughtIn;
String? jlptLevel;
int? newspaperFrequencyRank;
int strokeCount;
String meaning;
List<String> kunyomi;
List<String> onyomi;
List<YomiExample> kunyomiExamples;
List<YomiExample> onyomiExamples;
Radical? radical;
List<String> parts;
String strokeOrderDiagramUri;
String strokeOrderSvgUri;
String strokeOrderGifUri;
String uri;
// Radical.fromJishoObject(jisho.Radical object)
// : symbol = object.symbol,
// forms = object.forms,
// meaning = object.meaning;
// }
KanjiResultData.fromJishoObject(jisho.KanjiResultData object)
: taughtIn = object.taughtIn,
jlptLevel = object.jlptLevel,
newspaperFrequencyRank = object.newspaperFrequencyRank,
strokeCount = object.strokeCount,
meaning = object.meaning,
kunyomi = object.kunyomi,
onyomi = object.onyomi,
kunyomiExamples = object.kunyomiExamples
.map((k) => YomiExample.fromJishoObject(k))
.toList(),
onyomiExamples = object.onyomiExamples
.map((o) => YomiExample.fromJishoObject(o))
.toList(),
radical = (object.radical == null)
? null
: Radical.fromJishoObject(object.radical!),
parts = object.parts,
strokeOrderDiagramUri = object.strokeOrderDiagramUri,
strokeOrderSvgUri = object.strokeOrderSvgUri,
strokeOrderGifUri = object.strokeOrderGifUri,
uri = object.uri;
}
// @Entity()
// class KanjiResult {
// int id = 0;
// String query;
// bool found;
// KanjiResultData? data;
// KanjiResult({
// this.id = 0,
// required this.query,
// required this.found,
// required this.data,
// });
// KanjiResult.fromJishoObject(jisho.KanjiResult object)
// : query = object.query,
// found = object.found,
// data = (object.data == null)
// ? null
// : KanjiResultData.fromJishoObject(object.data!);
// }
// @Entity()
// class KanjiResultData {
// int id = 0;
// String? taughtIn;
// String? jlptLevel;
// int? newspaperFrequencyRank;
// int strokeCount;
// String meaning;
// List<String> kunyomi;
// List<String> onyomi;
// List<YomiExample> kunyomiExamples;
// List<YomiExample> onyomiExamples;
// Radical? radical;
// List<String> parts;
// String strokeOrderDiagramUri;
// String strokeOrderSvgUri;
// String strokeOrderGifUri;
// String uri;
// KanjiResultData({
// this.id = 0,
// required this.taughtIn,
// required this.jlptLevel,
// required this.newspaperFrequencyRank,
// required this.strokeCount,
// required this.meaning,
// required this.kunyomi,
// required this.onyomi,
// required this.kunyomiExamples,
// required this.onyomiExamples,
// required this.radical,
// required this.parts,
// required this.strokeOrderDiagramUri,
// required this.strokeOrderSvgUri,
// required this.strokeOrderGifUri,
// required this.uri,
// });
// KanjiResultData.fromJishoObject(jisho.KanjiResultData object)
// : taughtIn = object.taughtIn,
// jlptLevel = object.jlptLevel,
// newspaperFrequencyRank = object.newspaperFrequencyRank,
// strokeCount = object.strokeCount,
// meaning = object.meaning,
// kunyomi = object.kunyomi,
// onyomi = object.onyomi,
// kunyomiExamples = object.kunyomiExamples
// .map((k) => YomiExample.fromJishoObject(k))
// .toList(),
// onyomiExamples = object.onyomiExamples
// .map((o) => YomiExample.fromJishoObject(o))
// .toList(),
// radical = (object.radical == null)
// ? null
// : Radical.fromJishoObject(object.radical!),
// parts = object.parts,
// strokeOrderDiagramUri = object.strokeOrderDiagramUri,
// strokeOrderSvgUri = object.strokeOrderSvgUri,
// strokeOrderGifUri = object.strokeOrderGifUri,
// uri = object.uri;
// }

View File

@ -1,101 +1,155 @@
import 'package:objectbox/objectbox.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
// import 'package:objectbox/objectbox.dart';
// import 'package:unofficial_jisho_api/api.dart' as jisho;
import 'common.dart';
// import 'common.dart';
@Entity()
class PhraseScrapeSentence {
int id = 0;
String english;
String japanese;
List<ExampleSentencePiece> pieces;
// TODO: Rewrite for sembast
PhraseScrapeSentence.fromJishoObject(jisho.PhraseScrapeSentence object)
: english = object.english,
japanese = object.japanese,
pieces = object.pieces
.map((p) => ExampleSentencePiece.fromJishoObject(p))
.toList();
}
// @Entity()
// class PhraseScrapeSentence {
// int id;
// String english;
// String japanese;
// List<ExampleSentencePiece> pieces;
@Entity()
class PhraseScrapeMeaning {
int id = 0;
List<String> seeAlsoTerms;
List<PhraseScrapeSentence> sentences;
String definition;
List<String> supplemental;
String? definitionAbstract;
List<String> tags;
// PhraseScrapeSentence({
// this.id = 0,
// required this.english,
// required this.japanese,
// required this.pieces,
// });
PhraseScrapeMeaning.fromJishoObject(jisho.PhraseScrapeMeaning object)
: seeAlsoTerms = object.seeAlsoTerms,
sentences = object.sentences
.map((s) => PhraseScrapeSentence.fromJishoObject(s))
.toList(),
definition = object.definition,
supplemental = object.supplemental,
definitionAbstract = object.definitionAbstract,
tags = object.tags;
}
// PhraseScrapeSentence.fromJishoObject(jisho.PhraseScrapeSentence object)
// : id = 0,
// english = object.english,
// japanese = object.japanese,
// pieces = object.pieces
// .map((p) => ExampleSentencePiece.fromJishoObject(p))
// .toList();
// }
@Entity()
class KanjiKanaPair {
int id = 0;
String kanji;
String? kana;
// @Entity()
// class PhraseScrapeMeaning {
// int id;
// List<String> seeAlsoTerms;
// List<PhraseScrapeSentence> sentences;
// String definition;
// List<String> supplemental;
// String? definitionAbstract;
// List<String> tags;
KanjiKanaPair.fromJishoObject(jisho.KanjiKanaPair object)
: kanji = object.kanji,
kana = object.kana;
}
// PhraseScrapeMeaning({
// this.id = 0,
// required this.seeAlsoTerms,
// required this.sentences,
// required this.definition,
// required this.supplemental,
// required this.definitionAbstract,
// required this.tags,
// });
@Entity()
class PhrasePageScrapeResult {
int id = 0;
bool found;
String query;
PhrasePageScrapeResultData? data;
// PhraseScrapeMeaning.fromJishoObject(jisho.PhraseScrapeMeaning object)
// : id = 0,
// seeAlsoTerms = object.seeAlsoTerms,
// sentences = object.sentences
// .map((s) => PhraseScrapeSentence.fromJishoObject(s))
// .toList(),
// definition = object.definition,
// supplemental = object.supplemental,
// definitionAbstract = object.definitionAbstract,
// tags = object.tags;
// }
PhrasePageScrapeResult.fromJishoObject(jisho.PhrasePageScrapeResult object)
: found = object.found,
query = object.query,
data = (object.data == null)
? null
: PhrasePageScrapeResultData.fromJishoObject(object.data!);
}
// @Entity()
// class KanjiKanaPair {
// int id;
// String kanji;
// String? kana;
@Entity()
class AudioFile {
int id = 0;
String uri;
String mimetype;
// KanjiKanaPair({
// this.id = 0,
// required this.kanji,
// required this.kana,
// });
AudioFile.fromJishoObject(jisho.AudioFile object)
: uri = object.uri,
mimetype = object.mimetype;
}
// KanjiKanaPair.fromJishoObject(jisho.KanjiKanaPair object)
// : id = 0,
// kanji = object.kanji,
// kana = object.kana;
// }
@Entity()
class PhrasePageScrapeResultData {
int id = 0;
String uri;
List<String> tags;
List<PhraseScrapeMeaning> meanings;
List<KanjiKanaPair> otherForms;
List<AudioFile> audio;
List<String> notes;
// @Entity()
// class PhrasePageScrapeResult {
// int id;
// bool found;
// String query;
// PhrasePageScrapeResultData? data;
PhrasePageScrapeResultData.fromJishoObject(
jisho.PhrasePageScrapeResultData object,
) : uri = object.uri,
tags = object.tags,
meanings = object.meanings
.map((m) => PhraseScrapeMeaning.fromJishoObject(m))
.toList(),
otherForms = object.otherForms
.map((f) => KanjiKanaPair.fromJishoObject(f))
.toList(),
audio = object.audio.map((a) => AudioFile.fromJishoObject(a)).toList(),
notes = object.notes;
}
// PhrasePageScrapeResult({
// this.id = 0,
// required this.found,
// required this.query,
// required this.data,
// });
// PhrasePageScrapeResult.fromJishoObject(jisho.PhrasePageScrapeResult object)
// : id = 0,
// found = object.found,
// query = object.query,
// data = (object.data == null)
// ? null
// : PhrasePageScrapeResultData.fromJishoObject(object.data!);
// }
// @Entity()
// class AudioFile {
// int id;
// String uri;
// String mimetype;
// AudioFile({
// this.id = 0,
// required this.uri,
// required this.mimetype,
// });
// AudioFile.fromJishoObject(jisho.AudioFile object)
// : id = 0,
// uri = object.uri,
// mimetype = object.mimetype;
// }
// @Entity()
// class PhrasePageScrapeResultData {
// int id;
// String uri;
// List<String> tags;
// List<PhraseScrapeMeaning> meanings;
// List<KanjiKanaPair> otherForms;
// List<AudioFile> audio;
// List<String> notes;
// PhrasePageScrapeResultData({
// this.id = 0,
// required this.uri,
// required this.tags,
// required this.meanings,
// required this.otherForms,
// required this.audio,
// required this.notes,
// });
// PhrasePageScrapeResultData.fromJishoObject(
// jisho.PhrasePageScrapeResultData object,
// ) : id = 0,
// uri = object.uri,
// tags = object.tags,
// meanings = object.meanings
// .map((m) => PhraseScrapeMeaning.fromJishoObject(m))
// .toList(),
// otherForms = object.otherForms
// .map((f) => KanjiKanaPair.fromJishoObject(f))
// .toList(),
// audio = object.audio.map((a) => AudioFile.fromJishoObject(a)).toList(),
// notes = object.notes;
// }

View File

@ -1,126 +1,195 @@
import 'package:objectbox/objectbox.dart';
import 'package:unofficial_jisho_api/api.dart' as jisho;
// import 'package:objectbox/objectbox.dart';
// import 'package:unofficial_jisho_api/api.dart' as jisho;
@Entity()
class SearchResult {
int id = 0;
final JishoResultMeta meta;
final ToMany<JishoResult> data = ToMany<JishoResult>();
// TODO: Rewrite for sembast
SearchResult.fromJishoObject(final jisho.JishoAPIResult object)
: meta = JishoResultMeta.fromJishoObject(object.meta) {
data.addAll(
object.data
?.map((r) => JishoResult.fromJishoObject(r)) ??
<JishoResult>[],
);
}
}
// @Entity()
// class SearchResult {
// int id;
// final JishoResultMeta meta;
// final ToMany<JishoResult> data;
@Entity()
class JishoResultMeta {
int id = 0;
int status;
// SearchResult({
// this.id = 0,
// required this.meta,
// required this.data,
// });
JishoResultMeta.fromJishoObject(final jisho.JishoResultMeta object)
: status = object.status;
}
// SearchResult.fromJishoObject(final jisho.JishoAPIResult object)
// : id = 0,
// meta = JishoResultMeta.fromJishoObject(object.meta),
// data = ToMany<JishoResult>()
// ..addAll(
// object.data?.map((r) => JishoResult.fromJishoObject(r)) ??
// <JishoResult>[],
// );
// }
@Entity()
class JishoResult {
int id = 0;
JishoAttribution attribution;
bool? is_common;
List<JishoJapaneseWord> japanese;
List<String> jlpt;
List<JishoWordSense> senses;
String slug;
List<String> tags;
// @Entity()
// class JishoResultMeta {
// int id;
// int status;
JishoResult.fromJishoObject(final jisho.JishoResult object)
: attribution = JishoAttribution.fromJishoObject(object.attribution),
is_common = object.isCommon,
japanese = object.japanese
.map((j) => JishoJapaneseWord.fromJishoObject(j))
.toList(),
jlpt = object.jlpt,
senses = object.senses
.map((s) => JishoWordSense.fromJishoObject(s))
.toList(),
slug = object.slug,
tags = object.tags;
}
// JishoResultMeta({
// this.id = 0,
// required this.status,
// });
@Entity()
class JishoAttribution {
int id = 0;
String? dbpedia;
bool jmdict;
bool jmnedict;
// JishoResultMeta.fromJishoObject(final jisho.JishoResultMeta object)
// : id = 0,
// status = object.status;
// }
JishoAttribution.fromJishoObject(final jisho.JishoAttribution object)
: dbpedia = object.dbpedia,
jmdict = object.jmdict,
jmnedict = object.jmnedict;
}
// @Entity()
// class JishoResult {
// int id;
// JishoAttribution attribution;
// bool? is_common;
// List<JishoJapaneseWord> japanese;
// List<String> jlpt;
// List<JishoWordSense> senses;
// String slug;
// List<String> tags;
@Entity()
class JishoJapaneseWord {
int id = 0;
String? reading;
String? word;
// JishoResult({
// this.id = 0,
// required this.attribution,
// required this.is_common,
// required this.japanese,
// required this.jlpt,
// required this.senses,
// required this.slug,
// required this.tags,
// });
JishoJapaneseWord.fromJishoObject(final jisho.JishoJapaneseWord object)
: reading = object.reading,
word = object.word;
}
// JishoResult.fromJishoObject(final jisho.JishoResult object)
// : id = 0,
// attribution = JishoAttribution.fromJishoObject(object.attribution),
// is_common = object.isCommon,
// japanese = object.japanese
// .map((j) => JishoJapaneseWord.fromJishoObject(j))
// .toList(),
// jlpt = object.jlpt,
// senses = object.senses
// .map((s) => JishoWordSense.fromJishoObject(s))
// .toList(),
// slug = object.slug,
// tags = object.tags;
// }
@Entity()
class JishoWordSense {
int id = 0;
List<String> antonyms;
List<String> english_definitions;
List<String> info;
List<JishoSenseLink> links;
List<String> parts_of_speech;
List<String> restrictions;
List<String> see_also;
List<JishoWordSource> source;
List<String> tags;
// @Entity()
// class JishoAttribution {
// int id;
// String? dbpedia;
// bool jmdict;
// bool jmnedict;
JishoWordSense.fromJishoObject(final jisho.JishoWordSense object)
: antonyms = object.antonyms,
english_definitions = object.englishDefinitions,
info = object.info,
links =
object.links.map((l) => JishoSenseLink.fromJishoObject(l)).toList(),
parts_of_speech = object.partsOfSpeech,
restrictions = object.restrictions,
see_also = object.seeAlso,
source = object.source
.map((s) => JishoWordSource.fromJishoObject(s))
.toList(),
tags = object.tags;
}
// JishoAttribution({
// this.id = 0,
// required this.dbpedia,
// required this.jmdict,
// required this.jmnedict,
// });
@Entity()
class JishoWordSource {
int id = 0;
String language;
String? word;
// JishoAttribution.fromJishoObject(final jisho.JishoAttribution object)
// : id = 0,
// dbpedia = object.dbpedia,
// jmdict = object.jmdict,
// jmnedict = object.jmnedict;
// }
JishoWordSource.fromJishoObject(final jisho.JishoWordSource object)
: language = object.language,
word = object.word;
}
// @Entity()
// class JishoJapaneseWord {
// int id;
// String? reading;
// String? word;
@Entity()
class JishoSenseLink {
int id = 0;
String text;
String url;
// JishoJapaneseWord({
// this.id = 0,
// required this.reading,
// required this.word,
// });
JishoSenseLink.fromJishoObject(final jisho.JishoSenseLink object)
: text = object.text,
url = object.url;
}
// JishoJapaneseWord.fromJishoObject(final jisho.JishoJapaneseWord object)
// : id = 0,
// reading = object.reading,
// word = object.word;
// }
// @Entity()
// class JishoWordSense {
// int id;
// List<String> antonyms;
// List<String> english_definitions;
// List<String> info;
// List<JishoSenseLink> links;
// List<String> parts_of_speech;
// List<String> restrictions;
// List<String> see_also;
// List<JishoWordSource> source;
// List<String> tags;
// JishoWordSense({
// this.id = 0,
// required this.antonyms,
// required this.english_definitions,
// required this.info,
// required this.links,
// required this.parts_of_speech,
// required this.restrictions,
// required this.see_also,
// required this.source,
// required this.tags,
// });
// JishoWordSense.fromJishoObject(final jisho.JishoWordSense object)
// : id = 0,
// antonyms = object.antonyms,
// english_definitions = object.englishDefinitions,
// info = object.info,
// links =
// object.links.map((l) => JishoSenseLink.fromJishoObject(l)).toList(),
// parts_of_speech = object.partsOfSpeech,
// restrictions = object.restrictions,
// see_also = object.seeAlso,
// source = object.source
// .map((s) => JishoWordSource.fromJishoObject(s))
// .toList(),
// tags = object.tags;
// }
// @Entity()
// class JishoWordSource {
// int id;
// String language;
// String? word;
// JishoWordSource({
// this.id = 0,
// required this.language,
// required this.word,
// });
// JishoWordSource.fromJishoObject(final jisho.JishoWordSource object)
// : id = 0,
// language = object.language,
// word = object.word;
// }
// @Entity()
// class JishoSenseLink {
// int id;
// String text;
// String url;
// JishoSenseLink({
// this.id = 0,
// required this.text,
// required this.url,
// });
// JishoSenseLink.fromJishoObject(final jisho.JishoSenseLink object)
// : id = 0,
// text = object.text,
// url = object.url;
// }

View File

@ -1,141 +0,0 @@
{
"_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
"entities": [
{
"id": "3:8314315977756262774",
"lastPropertyId": "4:7972948456299367594",
"name": "WordResult",
"properties": [
{
"id": "1:8286440150679521496",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:2698026687178480112",
"name": "timestamp",
"type": 10
},
{
"id": "3:8750782874894963158",
"name": "word",
"type": 9
},
{
"id": "4:7972948456299367594",
"name": "searchStringId",
"type": 11,
"flags": 520,
"indexId": "1:6146948198859733323",
"relationTarget": "WordQuery"
}
],
"relations": []
},
{
"id": "4:4256390943850643278",
"lastPropertyId": "3:1496429060084558178",
"name": "KanjiQuery",
"properties": [
{
"id": "1:2966275213904862677",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:3733952844232949036",
"name": "kanji",
"type": 9
}
],
"relations": []
},
{
"id": "5:3499538826755540666",
"lastPropertyId": "3:1154921921492752045",
"name": "WordQuery",
"properties": [
{
"id": "1:2582448470002735577",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:6622038022626247037",
"name": "query",
"type": 9
}
],
"relations": []
},
{
"id": "6:8118874861016646859",
"lastPropertyId": "5:818915488505962903",
"name": "Search",
"properties": [
{
"id": "1:3233720904924970047",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:7793044338609887616",
"name": "timestamp",
"type": 10
},
{
"id": "4:5737790291742758071",
"name": "wordQueryId",
"type": 11,
"flags": 520,
"indexId": "4:4174896839978600983",
"relationTarget": "WordQuery"
},
{
"id": "5:818915488505962903",
"name": "kanjiQueryId",
"type": 11,
"flags": 520,
"indexId": "5:5394995618034342416",
"relationTarget": "KanjiQuery"
}
],
"relations": []
}
],
"lastEntityId": "6:8118874861016646859",
"lastIndexId": "5:5394995618034342416",
"lastRelationId": "1:2624712325077938293",
"lastSequenceId": "0:0",
"modelVersion": 5,
"modelVersionParserMinimum": 5,
"retiredEntityUids": [
8135239166970424087,
461492167249325765
],
"retiredIndexUids": [
2344626140411525437,
1957456749938325194
],
"retiredPropertyUids": [
2681934095975267680,
4514526257378540330,
1930470268740402049,
4297905889790758495,
4157902147911002923,
7573103520245228403,
1496429060084558178,
1154921921492752045,
2254834401134912797
],
"retiredRelationUids": [
2624712325077938293
],
"version": 1
}

View File

@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import '../../bloc/database/database_bloc.dart';
import '../../models/history/search.dart';
import '../../objectbox.g.dart';
import '../components/history/date_divider.dart';
import '../components/history/kanji_search_item.dart';
import '../components/history/phrase_search_item.dart';
@ -11,60 +11,63 @@ import '../components/opaque_box.dart';
class HistoryView extends StatelessWidget {
const HistoryView({Key? key}) : super(key: key);
Database get _db => GetIt.instance.get<Database>();
Stream<List<Search>> get searchStream => Search.store
.query(
finder: Finder(
sortOrders: [SortOrder('timestamp', false)],
),
)
.onSnapshots(_db)
.map((snapshot) {
return snapshot
.map<Search?>(
(snap) => (snap.value != null)
? Search.fromJson(snap.value! as Map<String, Object?>)
: null,
)
.where((s) => s != null)
.map<Search>((s) => s!)
.toList();
});
@override
Widget build(BuildContext context) {
return BlocBuilder<DatabaseBloc, DatabaseState>(
builder: (context, state) {
if (state is DatabaseDisconnected) {
throw DatabaseNotConnectedException();
}
return StreamBuilder<List<Search>>(
stream: getAsyncStream(state),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container();
}
final List<Search> data = snapshot.data!;
return OpaqueBox(
child: ListView.separated(
itemCount: data.length + 1,
itemBuilder: historyEntryWithData(data),
separatorBuilder: historyEntrySeparatorWithData(data),
),
);
},
return StreamBuilder<List<Search>>(
stream: searchStream,
builder: (context, snapshot) {
if (!snapshot.hasData)
return const Center(
child: Text('The history is empty.\nTry searching for something!'),
);
final List<Search> data = snapshot.data!;
return OpaqueBox(
child: ListView.separated(
itemCount: data.length + 1,
itemBuilder: historyEntryWithData(data),
separatorBuilder: historyEntrySeparatorWithData(data),
),
);
},
);
}
Stream<List<Search>> getAsyncStream(DatabaseState state) =>
((state as DatabaseConnected).database.box<Search>().query()
..order(Search_.timestamp, flags: Order.descending))
.watch(triggerImmediately: true)
.map((query) => query.find());
Widget Function(BuildContext, int) historyEntryWithData(List<Search> data) =>
(context, index) {
if (index == 0) {
return Container();
}
if (index == 0) return Container();
final Search search = data[index - 1];
if (search.isKanji()) {
return KanjiSearchItem(
result: search.kanjiQuery.target!,
timestamp: search.timestamp,
);
} else {
return PhraseSearchItem(
search: search.wordQuery.target!,
timestamp: search.timestamp,
);
}
return (search.isKanji)
? KanjiSearchItem(
result: search.kanjiQuery!,
timestamp: search.timestamp,
)
: PhraseSearchItem(
search: search.wordQuery!,
timestamp: search.timestamp,
);
};
DateTime roundToDay(DateTime date) =>

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import '../../../bloc/database/database_bloc.dart';
import '../../../models/history/kanji_query.dart';
import '../../../models/history/search.dart';
import '../../../services/jisho_api/kanji_search.dart';
@ -19,18 +20,18 @@ class KanjiResultPage extends StatelessWidget {
appBar: AppBar(),
body: FutureBuilder<KanjiResult>(
future: fetchKanji(kanjiSearchTerm),
builder: ( context, snapshot) {
if (!snapshot.hasData) return LoadingScreen();
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
if (!addedToDatabase) {
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
.database
.box<Search>()
.put(Search(timestamp: DateTime.now())
..kanjiQuery.target = KanjiQuery(
kanji: kanjiSearchTerm,
),);
Search.store.add(
GetIt.instance.get<Database>(),
Search.fromKanjiQuery(
timestamp: DateTime.now(),
kanjiQuery: KanjiQuery(kanji: kanjiSearchTerm),
).toJson(),
);
addedToDatabase = true;
}

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import '../../../bloc/database/database_bloc.dart';
import '../../../models/history/search.dart';
import '../../../models/history/word_query.dart';
import '../../../services/jisho_api/jisho_search.dart';
@ -23,20 +24,18 @@ class SearchResultsPage extends StatelessWidget {
body: FutureBuilder<JishoAPIResult>(
future: results,
builder: (context, snapshot) {
if (!snapshot.hasData) return LoadingScreen();
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError || snapshot.data!.data == null)
return ErrorWidget(snapshot.error!);
if (!addedToDatabase) {
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
.database
.box<Search>()
.put(
Search(timestamp: DateTime.now())
..wordQuery.target = WordQuery(
query: searchTerm,
),
);
Search.store.add(
GetIt.instance.get<Database>(),
Search.fromWordQuery(
timestamp: DateTime.now(),
wordQuery: WordQuery(query: searchTerm),
).toJson(),
);
addedToDatabase = true;
}

View File

@ -1,13 +1,13 @@
import 'package:confirm_dialog/confirm_dialog.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import 'package:settings_ui/settings_ui.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../bloc/database/database_bloc.dart';
import '../../bloc/theme/theme_bloc.dart';
import '../../models/history/search.dart';
import '../../models/themes/theme.dart';
import '../../objectbox.g.dart';
class SettingsView extends StatefulWidget {
const SettingsView({Key? key}) : super(key: key);
@ -41,11 +41,20 @@ class _SettingsViewState extends State<SettingsView> {
}
/// Update stored preferences with values from setting page state
void _updatePrefs() {
Future<void> _updatePrefs() async {
prefs.setBool('darkThemeEnabled', darkThemeEnabled);
prefs.setBool('autoThemeEnabled', autoThemeEnabled);
}
Future<void> clearHistory(context) async {
final bool userIsSure = await confirm(context);
if (userIsSure) {
final Database db = GetIt.instance.get<Database>();
await Search.store.delete(db);
}
}
@override
Widget build(BuildContext context) {
final TextStyle _titleTextStyle = TextStyle(
@ -133,12 +142,11 @@ class _SettingsViewState extends State<SettingsView> {
title: 'Export Data',
enabled: false,
),
const SettingsTile(
leading: Icon(Icons.delete),
SettingsTile(
leading: const Icon(Icons.delete),
title: 'Clear History',
onPressed: _clearHistory,
titleTextStyle: TextStyle(color: Colors.red),
enabled: false,
onPressed: clearHistory,
titleTextStyle: const TextStyle(color: Colors.red),
),
SettingsTile(
leading: const Icon(Icons.delete),
@ -153,15 +161,3 @@ class _SettingsViewState extends State<SettingsView> {
);
}
}
void _clearHistory(context) {
confirm(context).then((userIsSure) {
if (userIsSure) {
final Store db =
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
.database;
// db.box<Search>().query().build().find()
db.box<Search>().removeAll();
}
});
}

View File

@ -42,7 +42,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.8.1"
bloc:
dependency: transitive
description:
@ -119,7 +119,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.1.0"
charcode:
dependency: transitive
description:
@ -282,6 +282,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
get_it:
dependency: "direct main"
description:
name: get_it
url: "https://pub.dartlang.org"
source: hosted
version: "7.2.0"
glob:
dependency: transitive
description:
@ -372,7 +379,7 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.10"
mdi:
dependency: "direct main"
description:
@ -401,27 +408,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
objectbox:
dependency: "direct main"
description:
name: objectbox
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
objectbox_flutter_libs:
dependency: "direct main"
description:
name: objectbox_flutter_libs
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
objectbox_generator:
dependency: "direct dev"
description:
name: objectbox_generator
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
package_config:
dependency: transitive
description:
@ -430,7 +416,7 @@ packages:
source: hosted
version: "2.0.0"
path:
dependency: transitive
dependency: "direct main"
description:
name: path
url: "https://pub.dartlang.org"
@ -534,6 +520,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
sembast:
dependency: "direct main"
description:
name: sembast
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.1"
settings_ui:
dependency: "direct main"
description:
@ -602,13 +595,6 @@ packages:
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
source_span:
dependency: transitive
description:
@ -644,6 +630,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
synchronized:
dependency: transitive
description:
name: synchronized
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
term_glyph:
dependency: transitive
description:
@ -657,7 +650,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.3"
version: "0.4.2"
timing:
dependency: transitive
description:
@ -778,5 +771,5 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.13.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=2.0.0"

View File

@ -13,21 +13,20 @@ dependencies:
sdk: flutter
flutter_bloc: ^7.0.1
flutter_slidable: ^0.6.0
get_it: ^7.2.0
mdi: ^5.0.0-nullsafety.0
objectbox: ^1.1.1
objectbox_flutter_libs: ^1.1.1
path: ^1.8.0
path_provider: ^2.0.2
sembast: ^3.1.1
settings_ui: ^1.0.0
shared_preferences: ^2.0.6
unofficial_jisho_api: ^2.0.2
url_launcher: ^6.0.9
settings_ui: ^1.0.0
dev_dependencies:
build_runner: ^2.0.6
flutter_test:
sdk: flutter
objectbox_generator: ^1.1.1
flutter_native_splash: ^1.2.0
flutter_launcher_icons: "^0.9.1"