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

125 lines
4.7 KiB
Dart
Raw Normal View History

2024-02-18 17:36:58 +01:00
import 'package:flutter/material.dart';
2024-06-25 12:00:34 +02:00
import 'package:font_awesome_flutter/font_awesome_flutter.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-06-15 21:29:51 +02:00
import 'package:simplecloudnotifier/models/scn_message.dart';
2024-06-25 12:00:34 +02:00
import 'package:simplecloudnotifier/models/subscription.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/state/scn_data_cache.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-06-25 12:00:34 +02:00
required this.subscription,
2024-02-18 17:36:58 +01:00
super.key,
});
final Channel channel;
2024-06-25 12:00:34 +02:00
final Subscription? subscription;
2024-05-26 19:24:19 +02:00
final Null Function() onPressed;
@override
State<ChannelListItem> createState() => _ChannelListItemState();
}
class _ChannelListItemState extends State<ChannelListItem> {
2024-06-15 21:29:51 +02:00
SCNMessage? lastMessage;
2024-05-26 19:24:19 +02:00
@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()) {
lastMessage = SCNDataCache().getMessagesSorted().where((p) => p.channelID == widget.channel.channelID).firstOrNull;
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),
2024-06-25 12:00:34 +02:00
child: Row(
2024-05-26 19:24:19 +02:00
children: [
2024-06-25 12:00:34 +02:00
_buildIcon(context),
SizedBox(width: 8),
Expanded(
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),
),
],
2024-05-26 19:24:19 +02:00
),
2024-06-25 12:00:34 +02:00
SizedBox(height: 4),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: Text(
_preformatTitle(lastMessage),
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-05-26 19:24:19 +02:00
),
2024-06-25 12:00:34 +02:00
],
),
2024-05-26 19:24:19 +02:00
),
],
),
),
),
);
}
String _preformatTitle(SCNMessage? message) {
if (message == null) return '...';
return message.title.replaceAll('\n', '').replaceAll('\r', '').replaceAll('\t', ' ');
}
2024-06-25 12:00:34 +02:00
Widget _buildIcon(BuildContext context) {
if (widget.subscription == null) {
return Icon(FontAwesomeIcons.solidSquareDashed, color: Theme.of(context).colorScheme.outline, size: 32); // not-subscribed
2024-06-25 20:49:40 +02:00
} else if (widget.subscription!.confirmed && widget.channel.ownerUserID == widget.subscription!.subscriberUserID) {
return Icon(FontAwesomeIcons.solidSquareRss, color: Theme.of(context).colorScheme.onPrimaryContainer, size: 32); // subscribed (own channel)
2024-06-25 12:00:34 +02:00
} else if (widget.subscription!.confirmed) {
2024-06-25 20:49:40 +02:00
return Icon(FontAwesomeIcons.solidSquareShareNodes, color: Theme.of(context).colorScheme.onPrimaryContainer, size: 32); // subscribed (foreign channel)
2024-06-25 12:00:34 +02:00
} else {
return Icon(FontAwesomeIcons.solidSquareEnvelope, color: Theme.of(context).colorScheme.tertiary, size: 32); // requested
}
}
2024-02-18 17:36:58 +01:00
}