2022-01-25 20:41:53 +01:00
|
|
|
import 'package:get_it/get_it.dart';
|
2021-12-04 05:13:13 +01:00
|
|
|
import 'package:sembast/sembast.dart';
|
2021-08-03 22:02:42 +02:00
|
|
|
|
|
|
|
import './kanji_query.dart';
|
|
|
|
import './word_query.dart';
|
|
|
|
|
2022-01-19 02:10:05 +01:00
|
|
|
export 'package:get_it/get_it.dart';
|
|
|
|
export 'package:sembast/sembast.dart';
|
|
|
|
|
2021-08-03 22:02:42 +02:00
|
|
|
class Search {
|
2021-12-04 05:13:13 +01:00
|
|
|
final WordQuery? wordQuery;
|
|
|
|
final KanjiQuery? kanjiQuery;
|
2022-01-25 20:41:53 +01:00
|
|
|
final List<DateTime> timestamps;
|
2021-08-03 22:02:42 +02:00
|
|
|
|
2021-12-04 05:13:13 +01:00
|
|
|
Search.fromKanjiQuery({
|
|
|
|
required KanjiQuery this.kanjiQuery,
|
2022-01-25 20:41:53 +01:00
|
|
|
List<DateTime>? timestamps,
|
|
|
|
}) : wordQuery = null,
|
|
|
|
timestamps = timestamps ?? [DateTime.now()];
|
2021-08-03 22:02:42 +02:00
|
|
|
|
2021-12-04 05:13:13 +01:00
|
|
|
Search.fromWordQuery({
|
|
|
|
required WordQuery this.wordQuery,
|
2022-01-25 20:41:53 +01:00
|
|
|
List<DateTime>? timestamps,
|
|
|
|
}) : kanjiQuery = null,
|
|
|
|
timestamps = timestamps ?? [DateTime.now()];
|
2021-12-04 05:13:13 +01:00
|
|
|
|
|
|
|
bool get isKanji => wordQuery == null;
|
|
|
|
|
2022-01-25 20:41:53 +01:00
|
|
|
DateTime get timestamp => timestamps.last;
|
|
|
|
|
2021-12-04 05:13:13 +01:00
|
|
|
Map<String, Object?> toJson() => {
|
2022-01-25 20:41:53 +01:00
|
|
|
'timestamps': [for (final ts in timestamps) ts.millisecondsSinceEpoch],
|
|
|
|
'lastTimestamp': timestamps.last.millisecondsSinceEpoch,
|
2021-12-04 05:13:13 +01:00
|
|
|
'wordQuery': wordQuery?.toJson(),
|
|
|
|
'kanjiQuery': kanjiQuery?.toJson(),
|
|
|
|
};
|
|
|
|
|
2022-01-25 20:41:53 +01:00
|
|
|
factory Search.fromJson(Map<String, dynamic> json) {
|
|
|
|
final List<DateTime> timestamps = [
|
|
|
|
for (final ts in json['timestamps'] as List<dynamic>)
|
|
|
|
DateTime.fromMillisecondsSinceEpoch(ts as int)
|
|
|
|
];
|
|
|
|
|
|
|
|
return json['wordQuery'] != null
|
|
|
|
? Search.fromWordQuery(
|
|
|
|
wordQuery: WordQuery.fromJson(json['wordQuery']),
|
|
|
|
timestamps: timestamps,
|
|
|
|
)
|
|
|
|
: Search.fromKanjiQuery(
|
|
|
|
kanjiQuery: KanjiQuery.fromJson(json['kanjiQuery']),
|
|
|
|
timestamps: timestamps,
|
|
|
|
);
|
|
|
|
}
|
2021-12-04 05:13:13 +01:00
|
|
|
|
|
|
|
static StoreRef<int, Object?> get store => intMapStoreFactory.store('search');
|
2021-12-01 23:09:53 +01:00
|
|
|
}
|
2022-01-25 20:41:53 +01:00
|
|
|
|
|
|
|
Future<void> addSearchToDatabase({
|
|
|
|
required String searchTerm,
|
|
|
|
required bool isKanji,
|
|
|
|
}) async {
|
|
|
|
final DateTime now = DateTime.now();
|
|
|
|
final db = GetIt.instance.get<Database>();
|
|
|
|
final Filter filter = Filter.equals(
|
|
|
|
isKanji ? 'kanjiQuery.kanji' : 'wordQuery.query',
|
|
|
|
searchTerm,
|
|
|
|
);
|
|
|
|
|
|
|
|
final RecordSnapshot<int, Object?>? previousSearch =
|
|
|
|
await Search.store.findFirst(db, finder: Finder(filter: filter));
|
|
|
|
|
|
|
|
if (previousSearch != null) {
|
|
|
|
final search =
|
|
|
|
Search.fromJson(previousSearch.value! as Map<String, Object?>);
|
|
|
|
search.timestamps.add(now);
|
|
|
|
Search.store.record(previousSearch.key).put(db, search.toJson());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Search.store.add(
|
|
|
|
db,
|
|
|
|
isKanji
|
|
|
|
? Search.fromKanjiQuery(kanjiQuery: KanjiQuery(kanji: searchTerm))
|
|
|
|
.toJson()
|
|
|
|
: Search.fromWordQuery(wordQuery: WordQuery(query: searchTerm))
|
|
|
|
.toJson(),
|
|
|
|
);
|
|
|
|
}
|