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

46 lines
1.3 KiB
Dart
Raw 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';
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 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
}