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

120 lines
3.2 KiB
Dart
Raw Normal View History

2022-01-25 20:44:00 +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:44:00 +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:44:00 +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:44:00 +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:44:00 +01:00
DateTime get timestamp => timestamps.last;
2021-12-04 05:13:13 +01:00
Map<String, Object?> toJson() => {
2022-01-25 20:44:00 +01:00
'timestamps': [for (final ts in timestamps) ts.millisecondsSinceEpoch],
2024-11-13 22:43:44 +01:00
'word': wordQuery?.toJson(),
'kanji': kanjiQuery?.toJson(),
2021-12-04 05:13:13 +01:00
};
2022-01-25 20:44:00 +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)
];
2024-11-13 22:43:44 +01:00
return json['word'] != null
2022-01-25 20:44:00 +01:00
? Search.fromWordQuery(
2024-11-13 22:43:44 +01:00
wordQuery: WordQuery.fromJson(json['word']),
2022-01-25 20:44:00 +01:00
timestamps: timestamps,
)
: Search.fromKanjiQuery(
2024-11-13 22:43:44 +01:00
kanjiQuery: KanjiQuery.fromJson(json['kanji']),
2022-01-25 20:44:00 +01:00
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:44:00 +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(),
);
}
2022-05-04 23:01:53 +02:00
List<Search> mergeSearches(List<Search> a, List<Search> b) {
final List<Search> result = [...a];
for (final Search search in b) {
late final Iterable<Search> matchingEntry;
if (search.isKanji) {
matchingEntry =
result.where((e) => e.kanjiQuery?.kanji == search.kanjiQuery!.kanji);
} else {
matchingEntry =
result.where((e) => e.wordQuery?.query == search.wordQuery!.query);
}
if (matchingEntry.isEmpty) {
result.add(search);
continue;
}
final timestamps = [...matchingEntry.first.timestamps];
matchingEntry.first.timestamps.clear();
matchingEntry.first.timestamps.addAll(
(timestamps
..addAll(search.timestamps)
..sort())
.toSet()
.toList(),
);
}
return result;
}