2022-01-19 02:10:05 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_slidable/flutter_slidable.dart';
|
|
|
|
|
2022-06-05 02:41:11 +02:00
|
|
|
import '../../models/history/history_entry.dart';
|
2022-01-25 20:44:00 +01:00
|
|
|
import '../../routing/routes.dart';
|
|
|
|
import '../../services/datetime.dart';
|
2022-06-05 02:41:11 +02:00
|
|
|
import '../../services/snackbar.dart';
|
2022-01-23 23:56:26 +01:00
|
|
|
import '../../settings.dart';
|
2022-06-05 02:41:11 +02:00
|
|
|
import '../common/loading.dart';
|
2022-01-25 20:44:00 +01:00
|
|
|
import 'kanji_box.dart';
|
2022-01-19 02:10:05 +01:00
|
|
|
|
2022-06-05 02:41:11 +02:00
|
|
|
class HistoryEntryItem extends StatelessWidget {
|
|
|
|
final HistoryEntry entry;
|
2022-01-19 02:10:05 +01:00
|
|
|
final int objectKey;
|
|
|
|
final void Function()? onDelete;
|
|
|
|
final void Function()? onFavourite;
|
|
|
|
|
2022-06-05 02:41:11 +02:00
|
|
|
const HistoryEntryItem({
|
|
|
|
required this.entry,
|
2022-01-19 02:10:05 +01:00
|
|
|
required this.objectKey,
|
|
|
|
this.onDelete,
|
|
|
|
this.onFavourite,
|
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
2022-06-05 02:41:11 +02:00
|
|
|
Widget get _child => (entry.isKanji)
|
|
|
|
? KanjiBox(kanji: entry.kanji!)
|
|
|
|
: Text(entry.word!);
|
2022-01-25 20:44:00 +01:00
|
|
|
|
2022-06-05 02:41:11 +02:00
|
|
|
void Function() _onTap(context) => entry.isKanji
|
2022-01-25 20:44:00 +01:00
|
|
|
? () => Navigator.pushNamed(
|
|
|
|
context,
|
|
|
|
Routes.kanjiSearch,
|
2022-06-05 02:41:11 +02:00
|
|
|
arguments: entry.kanji,
|
2022-01-25 20:44:00 +01:00
|
|
|
)
|
|
|
|
: () => Navigator.pushNamed(
|
|
|
|
context,
|
|
|
|
Routes.search,
|
2022-06-05 02:41:11 +02:00
|
|
|
arguments: entry.word,
|
2022-01-25 20:44:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
MaterialPageRoute get timestamps => MaterialPageRoute(
|
|
|
|
builder: (context) => Scaffold(
|
|
|
|
appBar: AppBar(title: const Text('Last searched')),
|
2022-06-05 02:41:11 +02:00
|
|
|
body: FutureBuilder<List<DateTime>>(
|
|
|
|
future: entry.timestamps,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
// TODO: provide proper error handling
|
|
|
|
if (snapshot.hasError)
|
|
|
|
return ErrorWidget(snapshot.error!);
|
|
|
|
if (!snapshot.hasData) return const LoadingScreen();
|
|
|
|
return ListView(
|
|
|
|
children: snapshot.data!
|
|
|
|
.map(
|
|
|
|
(ts) => ListTile(
|
|
|
|
title: Text('${formatDate(ts)} ${formatTime(ts)}'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList(),
|
|
|
|
);
|
|
|
|
},
|
2022-01-25 20:44:00 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
List<SlidableAction> _actions(context) => [
|
|
|
|
SlidableAction(
|
|
|
|
backgroundColor: Colors.blue,
|
|
|
|
icon: Icons.access_time,
|
|
|
|
onPressed: (_) => Navigator.push(context, timestamps),
|
|
|
|
),
|
|
|
|
SlidableAction(
|
|
|
|
backgroundColor: Colors.yellow,
|
|
|
|
icon: Icons.star,
|
|
|
|
onPressed: (_) {
|
2022-06-05 02:41:11 +02:00
|
|
|
showSnackbar(context, 'TODO: implement favourites');
|
2022-01-25 20:44:00 +01:00
|
|
|
onFavourite?.call();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SlidableAction(
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
icon: Icons.delete,
|
2022-06-05 02:41:11 +02:00
|
|
|
onPressed: (_) async {
|
|
|
|
await entry.delete();
|
2022-01-25 20:44:00 +01:00
|
|
|
onDelete?.call();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
2022-01-19 02:10:05 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Slidable(
|
|
|
|
endActionPane: ActionPane(
|
|
|
|
motion: const ScrollMotion(),
|
2022-01-25 20:44:00 +01:00
|
|
|
children: _actions(context),
|
2022-01-19 02:10:05 +01:00
|
|
|
),
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
|
|
child: ListTile(
|
2022-01-25 20:44:00 +01:00
|
|
|
onTap: _onTap(context),
|
2022-01-19 02:10:05 +01:00
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
title: Row(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
2022-06-05 02:41:11 +02:00
|
|
|
child: Text(formatTime(entry.lastTimestamp)),
|
2022-01-19 02:10:05 +01:00
|
|
|
),
|
2022-01-23 23:56:26 +01:00
|
|
|
DefaultTextStyle.merge(
|
|
|
|
style: japaneseFont.textStyle,
|
2022-01-25 20:44:00 +01:00
|
|
|
child: _child,
|
2022-01-23 23:56:26 +01:00
|
|
|
),
|
2022-01-19 02:10:05 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|