Jisho-Study-Tool/lib/components/history/history_entry_tile.dart

110 lines
3.2 KiB
Dart
Raw Permalink Normal View History

2022-01-19 02:10:05 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
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';
import '../common/loading.dart';
2022-01-19 02:10:05 +01:00
2023-02-24 09:55:55 +01:00
class HistoryEntryTile extends StatelessWidget {
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({
required this.entry,
2022-01-19 02:10:05 +01:00
required this.objectKey,
this.onDelete,
this.onFavourite,
Key? key,
}) : super(key: key);
void Function() _onTap(context) => entry.isKanji
2022-01-25 20:44:00 +01:00
? () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: entry.kanji,
2022-01-25 20:44:00 +01:00
)
: () => Navigator.pushNamed(
context,
Routes.search,
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')),
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!);
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,
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),
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
],
),
),
),
);
}
}