Jisho-Study-Tool/lib/models/history/search.dart

43 lines
1.2 KiB
Dart
Raw Permalink Normal View History

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';
class Search {
2021-12-04 05:13:13 +01:00
final DateTime timestamp;
final WordQuery? wordQuery;
final KanjiQuery? kanjiQuery;
2021-08-03 22:02:42 +02:00
2021-12-04 05:13:13 +01:00
Search.fromKanjiQuery({
2021-12-01 23:09:53 +01:00
required this.timestamp,
2021-12-04 05:13:13 +01:00
required KanjiQuery this.kanjiQuery,
}) : wordQuery = null;
2021-08-03 22:02:42 +02:00
2021-12-04 05:13:13 +01:00
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');
2021-12-01 23:09:53 +01:00
}