SimpleCloudNotifier/flutter/lib/pages/channel_list/channel_list_item.dart

93 lines
2.9 KiB
Dart
Raw Normal View History

2024-02-18 17:36:58 +01:00
import 'package:flutter/material.dart';
2024-05-26 19:24:19 +02:00
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
2024-05-25 22:06:43 +02:00
import 'package:simplecloudnotifier/models/channel.dart';
2024-05-26 19:24:19 +02:00
import 'package:simplecloudnotifier/models/message.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/state/app_auth.dart';
2024-05-26 19:24:19 +02:00
class ChannelListItem extends StatefulWidget {
static final _dateFormat = DateFormat('yyyy-MM-dd kk:mm');
2024-02-18 17:36:58 +01:00
const ChannelListItem({
required this.channel,
2024-05-26 19:24:19 +02:00
required this.onPressed,
2024-02-18 17:36:58 +01:00
super.key,
});
final Channel channel;
2024-05-26 19:24:19 +02:00
final Null Function() onPressed;
@override
State<ChannelListItem> createState() => _ChannelListItemState();
}
class _ChannelListItemState extends State<ChannelListItem> {
Message? lastMessage;
@override
void initState() {
super.initState();
2024-06-02 17:09:57 +02:00
final acc = Provider.of<AppAuth>(context, listen: false);
2024-05-26 19:24:19 +02:00
2024-06-02 17:09:57 +02:00
if (acc.isAuth()) {
2024-05-26 19:24:19 +02:00
() async {
2024-06-02 17:09:57 +02:00
final (_, channelMessages) = await APIClient.getMessageList(acc, '@start', pageSize: 1, channelIDs: [widget.channel.channelID]);
2024-05-26 19:24:19 +02:00
setState(() {
lastMessage = channelMessages.firstOrNull;
});
}();
}
}
2024-02-18 17:36:58 +01:00
@override
2024-05-26 19:24:19 +02:00
Widget build(BuildContext context) {
//TODO subscription status
return Card.filled(
margin: EdgeInsets.fromLTRB(0, 4, 0, 4),
shape: BeveledRectangleBorder(borderRadius: BorderRadius.circular(0)),
color: Theme.of(context).cardTheme.color,
child: InkWell(
splashColor: Theme.of(context).splashColor,
onTap: widget.onPressed,
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: Text(
widget.channel.displayName,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
Text(
(widget.channel.timestampLastSent == null) ? '' : ChannelListItem._dateFormat.format(DateTime.parse(widget.channel.timestampLastSent!).toLocal()),
style: const TextStyle(fontSize: 14),
),
],
),
SizedBox(height: 4),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: Text(
lastMessage?.title ?? '...',
style: TextStyle(color: Theme.of(context).textTheme.bodyLarge?.color?.withAlpha(160)),
),
),
Text(widget.channel.messagesSent.toString(), style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold)),
],
),
],
),
),
),
);
}
2024-02-18 17:36:58 +01:00
}