feet/lib/widgets/homepage_post.dart

133 lines
4.8 KiB
Dart
Raw Normal View History

2023-01-30 07:28:21 +00:00
import 'dart:convert';
2022-12-30 15:43:49 +00:00
import 'package:feet/pages/article.dart';
2023-02-05 16:05:22 +00:00
import 'package:feet/widgets/favicon.dart';
2022-12-29 23:27:35 +00:00
import 'package:flutter/material.dart';
2022-12-31 23:56:12 +00:00
import 'package:flutter/services.dart';
2022-12-29 23:27:35 +00:00
import '../api/api.dart';
// TODO use some localization library
String formatDate(DateTime date) {
return '${date.day}.${date.month}.${date.year}, ${date.hour}:${date.minute < 10 ? '0${date.minute}' : date.minute}';
}
2022-12-30 16:10:09 +00:00
class HomepagePost extends StatefulWidget {
2022-12-29 23:27:35 +00:00
final Item post;
2022-12-30 15:43:49 +00:00
const HomepagePost({super.key, required this.post});
2022-12-29 23:27:35 +00:00
2022-12-30 16:10:09 +00:00
@override
State<StatefulWidget> createState() => _HomepagePostState();
}
class _HomepagePostState extends State<HomepagePost> {
2022-12-29 23:27:35 +00:00
@override
Widget build(BuildContext context) {
2022-12-30 16:10:09 +00:00
var post = widget.post;
2023-01-30 07:28:21 +00:00
var favicon = post.api.cache.favicons.get(post.feedId);
2022-12-29 23:27:35 +00:00
return Container(
margin: const EdgeInsets.symmetric(vertical: 6.0, horizontal: 8.0),
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).colorScheme.secondaryContainer,
),
borderRadius: const BorderRadius.all(Radius.circular(15)),
color: post.isRead
2022-12-30 15:43:49 +00:00
? null
: Theme.of(context).colorScheme.secondaryContainer.withAlpha(150),
2022-12-29 23:27:35 +00:00
),
child: InkWell(
2023-01-01 01:05:19 +00:00
onTap: () async {
2022-12-30 15:43:49 +00:00
Navigator.of(context).push(
2022-12-30 16:10:09 +00:00
MaterialPageRoute(
builder: (context) => ArticlePage(article: post)),
);
2023-01-01 01:05:19 +00:00
if (!post.isRead) {
await post.api
.request()
.markItem(post, ItemMarkType.read)
.execute();
setState(() {});
}
2022-12-30 16:10:09 +00:00
},
onLongPress: () async {
var theme = Theme.of(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
2022-12-31 23:56:12 +00:00
'Marking post as ${post.isRead ? 'Unread' : 'Read'}',
style: TextStyle(color: theme.colorScheme.onBackground)),
2022-12-30 16:10:09 +00:00
duration: const Duration(seconds: 2),
backgroundColor: theme.colorScheme.secondaryContainer,
),
2022-12-30 15:43:49 +00:00
);
2022-12-30 16:10:09 +00:00
await post.api
.request()
.markItem(
post, post.isRead ? ItemMarkType.unread : ItemMarkType.read)
.execute();
setState(() {});
2022-12-30 15:43:49 +00:00
},
borderRadius: const BorderRadius.all(Radius.circular(15)),
child: Container(
2023-01-23 08:55:46 +00:00
margin: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 12.0),
2022-12-30 15:43:49 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2023-01-30 07:28:21 +00:00
Row(
children: [
favicon?.data != null
? Container(
margin: const EdgeInsets.fromLTRB(0, 0, 6, 4),
2023-02-05 16:05:22 +00:00
child: PostFavicon(favicon!.data),
2023-01-30 07:28:21 +00:00
)
: Container(),
Expanded(
child: Text(post.title,
style: Theme.of(context).textTheme.titleMedium,
overflow: TextOverflow.ellipsis),
),
],
),
2022-12-30 15:43:49 +00:00
ClipRRect(
clipBehavior: Clip.antiAlias,
child: Row(
children: [
2022-12-31 23:56:12 +00:00
GestureDetector(
onTap: () async {
HapticFeedback.lightImpact();
await post.api
.request()
.markItem(
post,
post.isSaved
? ItemMarkType.unsaved
: ItemMarkType.saved)
.execute();
setState(() {});
},
child: Icon(
post.isSaved
? Icons.bookmark
: Icons.bookmark_add_outlined,
color:
post.isSaved ? const Color(0xFFF3A13E) : null),
),
2022-12-30 15:43:49 +00:00
Text('${post.api.cache.feeds.get(post.feedId)?.title}'),
const Text(' \u2022 '),
post.author.isNotEmpty
? Text(post.author)
: const Text('Unknown author',
style: TextStyle(fontStyle: FontStyle.italic)),
const Text(' \u2022 '),
Text(formatDate(post.createdOnTime)),
],
),
2022-12-29 23:44:54 +00:00
),
2022-12-30 15:43:49 +00:00
],
),
)),
2022-12-29 23:27:35 +00:00
);
}
2022-12-30 15:43:49 +00:00
}