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-01-23 23:56:26 +01:00
|
|
|
import '../../settings.dart';
|
2023-02-24 09:55:55 +01:00
|
|
|
import '../common/kanji_box.dart';
|
2022-06-05 02:41:11 +02:00
|
|
|
import '../common/loading.dart';
|
2022-01-19 02:10:05 +01:00
|
|
|
|
2023-02-24 09:55:55 +01:00
|
|
|
class HistoryEntryTile extends StatelessWidget {
|
2022-06-05 02:41:11 +02:00
|
|
|
final HistoryEntry entry;
|
2022-01-19 02:10:05 +01:00
|
|
|
final int objectKey;
|
|
|
|
final void Function()? onDelete;
|
|
|
|
final void Function()? onFavourite;
|
|
|
|
|
2023-02-24 09:55:55 +01:00
|
|
|
const HistoryEntryTile({
|
2022-06-05 02:41:11 +02:00
|
|
|
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
|
|
|
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
|
2023-02-24 09:55:55 +01:00
|
|
|
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
|
2022-06-05 02:41:11 +02:00
|
|
|
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.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,
|
2023-02-24 09:55:55 +01:00
|
|
|
child: entry.isKanji
|
|
|
|
? KanjiBox.headline4(
|
|
|
|
context: context,
|
|
|
|
kanji: entry.kanji!,
|
|
|
|
)
|
|
|
|
: Expanded(child: Text(entry.word!)),
|
2022-01-23 23:56:26 +01:00
|
|
|
),
|
2022-01-19 02:10:05 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|