Jisho-Study-Tool/lib/view/screens/history.dart

114 lines
3.5 KiB
Dart
Raw Normal View History

2020-07-09 22:17:10 +02:00
import 'package:flutter/material.dart';
2021-12-04 05:13:13 +01:00
import 'package:get_it/get_it.dart';
2021-12-04 05:31:27 +01:00
import 'package:jisho_study_tool/view/components/common/loading.dart';
2021-12-04 05:13:13 +01:00
import 'package:sembast/sembast.dart';
2021-08-03 22:02:42 +02:00
2021-12-01 23:09:53 +01:00
import '../../models/history/search.dart';
import '../components/history/date_divider.dart';
import '../components/history/kanji_search_item.dart';
import '../components/history/phrase_search_item.dart';
import '../components/opaque_box.dart';
2020-07-09 22:17:10 +02:00
2020-07-13 23:41:31 +02:00
class HistoryView extends StatelessWidget {
2021-12-01 23:09:53 +01:00
const HistoryView({Key? key}) : super(key: key);
2021-12-04 05:13:13 +01:00
Database get _db => GetIt.instance.get<Database>();
2021-12-01 23:09:53 +01:00
2021-12-04 05:13:13 +01:00
Stream<List<Search>> get searchStream => Search.store
.query(
finder: Finder(
sortOrders: [SortOrder('timestamp', false)],
),
)
.onSnapshots(_db)
.map((snapshot) {
return snapshot
.map<Search?>(
(snap) => (snap.value != null)
? Search.fromJson(snap.value! as Map<String, Object?>)
: null,
)
.where((s) => s != null)
.map<Search>((s) => s!)
.toList();
});
2021-08-03 22:02:42 +02:00
2021-12-04 05:13:13 +01:00
@override
Widget build(BuildContext context) {
return StreamBuilder<List<Search>>(
stream: searchStream,
builder: (context, snapshot) {
2021-12-04 05:31:27 +01:00
if (!snapshot.hasData) return const LoadingScreen();
final List<Search> data = snapshot.data!;
if (data.isEmpty)
2021-12-04 05:13:13 +01:00
return const Center(
child: Text('The history is empty.\nTry searching for something!'),
);
2021-12-04 05:31:27 +01:00
2021-12-04 05:13:13 +01:00
return OpaqueBox(
child: ListView.separated(
itemCount: data.length + 1,
itemBuilder: historyEntryWithData(data),
separatorBuilder: historyEntrySeparatorWithData(data),
),
2021-07-17 16:11:17 +02:00
);
},
2020-07-09 22:17:10 +02:00
);
}
2021-12-01 23:09:53 +01:00
Widget Function(BuildContext, int) historyEntryWithData(List<Search> data) =>
(context, index) {
2021-12-04 05:13:13 +01:00
if (index == 0) return Container();
2021-12-01 23:09:53 +01:00
final Search search = data[index - 1];
2021-12-04 05:13:13 +01:00
return (search.isKanji)
? KanjiSearchItem(
result: search.kanjiQuery!,
timestamp: search.timestamp,
)
: PhraseSearchItem(
search: search.wordQuery!,
timestamp: search.timestamp,
);
2021-12-01 23:09:53 +01:00
};
DateTime roundToDay(DateTime date) =>
DateTime(date.year, date.month, date.day);
bool dateChangedFromLastSearch(Search prevSearch, DateTime searchDate) {
final DateTime prevSearchDate = roundToDay(prevSearch.timestamp);
return prevSearchDate != searchDate;
}
DateTime get today => roundToDay(DateTime.now());
DateTime get yesterday =>
roundToDay(DateTime.now().subtract(const Duration(days: 1)));
Widget Function(BuildContext, int) historyEntrySeparatorWithData(
List<Search> data,
) =>
(context, index) {
final Search search = data[index];
final DateTime searchDate = roundToDay(search.timestamp);
EdgeInsets? margin;
if (index != 0) {
margin = const EdgeInsets.only(bottom: 10);
}
if (index == 0 ||
dateChangedFromLastSearch(data[index - 1], searchDate)) {
if (searchDate == today)
return DateDivider(text: 'Today', margin: margin);
else if (searchDate == yesterday)
return DateDivider(text: 'Yesterday', margin: margin);
else
return DateDivider(date: searchDate, margin: margin);
}
return const Divider();
};
2020-07-09 22:17:10 +02:00
}