import 'package:get_it/get_it.dart'; import 'package:sembast/sembast.dart'; import './kanji_query.dart'; import './word_query.dart'; export 'package:get_it/get_it.dart'; export 'package:sembast/sembast.dart'; class Search { final WordQuery? wordQuery; final KanjiQuery? kanjiQuery; final List timestamps; Search.fromKanjiQuery({ required KanjiQuery this.kanjiQuery, List? timestamps, }) : wordQuery = null, timestamps = timestamps ?? [DateTime.now()]; Search.fromWordQuery({ required WordQuery this.wordQuery, List? timestamps, }) : kanjiQuery = null, timestamps = timestamps ?? [DateTime.now()]; bool get isKanji => wordQuery == null; DateTime get timestamp => timestamps.last; Map toJson() => { 'timestamps': [for (final ts in timestamps) ts.millisecondsSinceEpoch], 'lastTimestamp': timestamps.last.millisecondsSinceEpoch, 'wordQuery': wordQuery?.toJson(), 'kanjiQuery': kanjiQuery?.toJson(), }; factory Search.fromJson(Map json) { final List timestamps = [ for (final ts in json['timestamps'] as List) 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, ); } static StoreRef get store => intMapStoreFactory.store('search'); } Future addSearchToDatabase({ required String searchTerm, required bool isKanji, }) async { final DateTime now = DateTime.now(); final db = GetIt.instance.get(); final Filter filter = Filter.equals( isKanji ? 'kanjiQuery.kanji' : 'wordQuery.query', searchTerm, ); final RecordSnapshot? previousSearch = await Search.store.findFirst(db, finder: Finder(filter: filter)); if (previousSearch != null) { final search = Search.fromJson(previousSearch.value! as Map); 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(), ); }