SimpleCloudNotifier/flutter/lib/pages/message_view/message_view.dart

207 lines
7.0 KiB
Dart
Raw Normal View History

2024-05-21 23:20:34 +02:00
import 'package:flutter/material.dart';
2024-06-07 23:44:32 +02:00
import 'package:flutter/widgets.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
2024-05-21 23:20:34 +02:00
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
2024-06-07 23:44:32 +02:00
import 'package:simplecloudnotifier/api/api_exception.dart';
2024-05-21 23:20:34 +02:00
import 'package:simplecloudnotifier/components/layout/scaffold.dart';
2024-06-07 23:44:32 +02:00
import 'package:simplecloudnotifier/models/api_error.dart';
import 'package:simplecloudnotifier/models/channel.dart';
import 'package:simplecloudnotifier/models/keytoken.dart';
2024-05-21 23:20:34 +02:00
import 'package:simplecloudnotifier/models/message.dart';
2024-06-07 23:44:32 +02:00
import 'package:simplecloudnotifier/models/user.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/state/app_auth.dart';
2024-06-08 12:55:58 +02:00
import 'package:simplecloudnotifier/utils/ui.dart';
2024-05-21 23:20:34 +02:00
class MessageViewPage extends StatefulWidget {
2024-05-23 17:41:51 +02:00
const MessageViewPage({super.key, required this.message});
2024-05-21 23:20:34 +02:00
2024-05-23 17:41:51 +02:00
final Message message; // Potentially trimmed
2024-05-21 23:20:34 +02:00
@override
State<MessageViewPage> createState() => _MessageViewPageState();
}
class _MessageViewPageState extends State<MessageViewPage> {
2024-06-07 23:44:32 +02:00
late Future<(Message, ChannelWithSubscription?, KeyToken?)>? mainFuture;
static final _dateFormat = DateFormat('yyyy-MM-dd kk:mm');
2024-05-21 23:20:34 +02:00
@override
void initState() {
super.initState();
2024-06-07 23:44:32 +02:00
mainFuture = fetchData();
2024-05-21 23:20:34 +02:00
}
2024-06-07 23:44:32 +02:00
Future<(Message, ChannelWithSubscription?, KeyToken?)> fetchData() async {
2024-06-02 17:09:57 +02:00
final acc = Provider.of<AppAuth>(context, listen: false);
2024-05-21 23:20:34 +02:00
2024-06-07 23:44:32 +02:00
final msg = await APIClient.getMessage(acc, widget.message.messageID);
ChannelWithSubscription? chn = null;
try {
chn = await APIClient.getChannel(acc, msg.channelID);
} on APIException catch (e) {
if (e.error == APIError.USER_AUTH_FAILED) {
chn = null;
} else {
rethrow;
}
}
KeyToken? tok = null;
try {
tok = await APIClient.getKeyToken(acc, msg.usedKeyID);
} on APIException catch (e) {
if (e.error == APIError.USER_AUTH_FAILED) {
tok = null;
} else {
rethrow;
}
}
return (msg, chn, tok);
2024-05-21 23:20:34 +02:00
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return SCNScaffold(
title: 'Message',
2024-05-23 17:41:51 +02:00
showSearch: false,
2024-06-07 23:44:32 +02:00
//TODO showShare: true
child: FutureBuilder<(Message, ChannelWithSubscription?, KeyToken?)>(
future: mainFuture,
2024-05-21 23:20:34 +02:00
builder: (context, snapshot) {
if (snapshot.hasData) {
2024-06-07 23:44:32 +02:00
final msg = snapshot.data!.$1;
final chn = snapshot.data!.$2;
final tok = snapshot.data!.$3;
return _buildMessageView(context, msg, chn, tok);
2024-05-21 23:20:34 +02:00
} else if (snapshot.hasError) {
return Center(child: Text('${snapshot.error}')); //TODO nice error page
2024-05-23 17:41:51 +02:00
} else if (!widget.message.trimmed) {
2024-06-07 23:44:32 +02:00
return _buildLoadingView(context, widget.message);
2024-05-23 17:41:51 +02:00
} else {
return const Center(child: CircularProgressIndicator());
2024-05-21 23:20:34 +02:00
}
},
),
);
}
2024-05-23 17:41:51 +02:00
2024-06-07 23:44:32 +02:00
Widget _buildMessageView(BuildContext context, Message message, ChannelWithSubscription? channel, KeyToken? token) {
2024-05-23 20:05:55 +02:00
//TODO loading true/false indicator
2024-06-07 23:44:32 +02:00
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(24, 16, 24, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
..._buildMessageHeader(context, message, channel, token),
SizedBox(height: 8),
if (message.content != null) ..._buildMessageContent(context, message, channel, token),
SizedBox(height: 8),
if (message.senderName != null) _buildMetaCard(context, FontAwesomeIcons.solidSignature, 'Sender', [message.senderName!], () => {/*TODO*/}),
if (token != null) _buildMetaCard(context, FontAwesomeIcons.solidGearCode, 'KeyToken', [token.keytokenID, token.name], () => {/*TODO*/}),
_buildMetaCard(context, FontAwesomeIcons.solidIdCardClip, 'MessageID', [message.messageID, if (message.userMessageID != null) message.userMessageID!], null),
if (channel != null) _buildMetaCard(context, FontAwesomeIcons.solidSnake, 'Channel', [message.channelID, channel.channel.displayName], () => {/*TODO*/}),
_buildMetaCard(context, FontAwesomeIcons.solidTimer, 'Timestamp', [message.timestamp], null),
],
),
),
);
}
Widget _buildLoadingView(BuildContext context, Message message) {
//TODO loading / skeleton use limitdata
return SizedBox();
}
String _resolveChannelName(ChannelWithSubscription? channel, Message message) {
return channel?.channel.displayName ?? message.channelInternalName;
}
List<Widget> _buildMessageHeader(BuildContext context, Message message, ChannelWithSubscription? channel, KeyToken? token) {
return [
Row(
children: [
2024-06-08 12:55:58 +02:00
UI.channelChip(
context: context,
text: _resolveChannelName(channel, message),
2024-06-07 23:44:32 +02:00
margin: const EdgeInsets.fromLTRB(0, 0, 4, 0),
2024-06-08 12:55:58 +02:00
fontSize: 16,
2024-06-07 23:44:32 +02:00
),
Expanded(child: SizedBox()),
Text(_dateFormat.format(DateTime.parse(message.timestamp)), style: const TextStyle(fontSize: 14)),
],
),
SizedBox(height: 8),
Text(message.title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
];
}
List<Widget> _buildMessageContent(BuildContext context, Message message, ChannelWithSubscription? channel, KeyToken? token) {
return [
Row(
children: [
Expanded(child: SizedBox()),
2024-06-08 12:55:58 +02:00
UI.buttonIconOnly(
2024-06-07 23:44:32 +02:00
onPressed: () {/*TODO*/},
2024-06-08 12:55:58 +02:00
icon: FontAwesomeIcons.copy,
2024-06-07 23:44:32 +02:00
),
2024-06-08 12:55:58 +02:00
UI.buttonIconOnly(
icon: FontAwesomeIcons.lineColumns,
2024-06-07 23:44:32 +02:00
onPressed: () {/*TODO*/},
),
],
),
2024-06-08 12:55:58 +02:00
UI.box(
context: context,
2024-06-07 23:44:32 +02:00
padding: const EdgeInsets.all(4),
child: Text(message.content ?? ''),
),
];
}
Widget _buildMetaCard(BuildContext context, IconData icn, String title, List<String> values, void Function()? action) {
2024-06-08 12:55:58 +02:00
final container = UI.box(
context: context,
2024-06-07 23:44:32 +02:00
padding: EdgeInsets.fromLTRB(16, 2, 4, 2),
child: Row(
2024-05-23 17:41:51 +02:00
children: [
2024-06-07 23:44:32 +02:00
FaIcon(icn, size: 18),
SizedBox(width: 16),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
for (final val in values) Text(val, style: const TextStyle(fontSize: 14)),
],
),
2024-05-23 17:41:51 +02:00
],
),
);
2024-06-07 23:44:32 +02:00
if (action == null) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 0),
child: container,
);
} else {
return Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 0),
child: InkWell(
splashColor: Theme.of(context).splashColor,
onTap: action,
child: container,
),
);
}
2024-05-23 17:41:51 +02:00
}
2024-05-21 23:20:34 +02:00
}