1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-22 22:07:29 +01:00
Jisho-Study-Tool/lib/models/history/search.dart

44 lines
1.3 KiB
Dart

import 'package:sembast/sembast.dart';
import 'package:sembast/timestamp.dart';
import './kanji_query.dart';
import './word_query.dart';
class Search {
final DateTime timestamp;
final WordQuery? wordQuery;
final KanjiQuery? kanjiQuery;
Search.fromKanjiQuery({
required this.timestamp,
required KanjiQuery this.kanjiQuery,
}) : wordQuery = 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');
}