OpenContacts/lib/widgets/messages_list.dart

695 lines
25 KiB
Dart
Raw Normal View History

import 'dart:async';
2023-05-01 16:00:56 -04:00
import 'package:cached_network_image/cached_network_image.dart';
2023-05-05 06:45:00 -04:00
import 'package:contacts_plus_plus/client_holder.dart';
2023-05-01 16:00:56 -04:00
import 'package:contacts_plus_plus/auxiliary.dart';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/message.dart';
2023-05-01 16:00:56 -04:00
import 'package:contacts_plus_plus/models/session.dart';
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
2023-05-02 16:50:35 -04:00
import 'package:contacts_plus_plus/widgets/message_audio_player.dart';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
2023-05-02 16:50:35 -04:00
import 'package:contacts_plus_plus/widgets/message_session_invite.dart';
2023-04-29 15:26:12 -04:00
import 'package:flutter/material.dart';
2023-04-30 03:01:59 -04:00
import 'package:intl/intl.dart';
2023-04-29 15:26:12 -04:00
class MessagesList extends StatefulWidget {
const MessagesList({required this.friend, super.key});
2023-04-29 15:26:12 -04:00
final Friend friend;
@override
State<StatefulWidget> createState() => _MessagesListState();
2023-04-29 15:26:12 -04:00
}
class _MessagesListState extends State<MessagesList> {
2023-05-02 04:04:54 -04:00
Future<MessageCache>? _messageCacheFuture;
2023-04-30 03:01:59 -04:00
final TextEditingController _messageTextController = TextEditingController();
2023-05-01 16:00:56 -04:00
final ScrollController _sessionListScrollController = ScrollController();
final ScrollController _messageScrollController = ScrollController();
2023-04-30 07:39:09 -04:00
ClientHolder? _clientHolder;
2023-04-30 03:01:59 -04:00
bool _isSendable = false;
2023-05-01 16:00:56 -04:00
bool _showSessionListChevron = false;
bool _messageCacheFutureComplete = false;
2023-05-01 16:00:56 -04:00
double get _shevronOpacity => _showSessionListChevron ? 1.0 : 0.0;
2023-04-30 17:14:29 -04:00
2023-04-29 15:26:12 -04:00
@override
2023-04-30 07:39:09 -04:00
void didChangeDependencies() {
super.didChangeDependencies();
final clientHolder = ClientHolder.of(context);
if (_clientHolder != clientHolder) {
_clientHolder = clientHolder;
2023-04-30 17:14:29 -04:00
}
2023-05-02 04:04:54 -04:00
_loadMessages();
}
void _loadMessages() {
_messageCacheFutureComplete = false;
_messageCacheFuture = _clientHolder?.messagingClient.getMessageCache(widget.friend.id)
.whenComplete(() => _messageCacheFutureComplete = true);
2023-05-05 05:29:54 -04:00
final mClient = _clientHolder?.messagingClient;
final id = widget.friend.id;
mClient?.registerMessageListener(id, () {
2023-05-05 05:29:54 -04:00
if (context.mounted) {
setState(() {});
} else {
mClient.unregisterMessageListener(id);
2023-05-05 05:29:54 -04:00
}
});
}
@override
void dispose() {
_clientHolder?.messagingClient.unregisterMessageListener(widget.friend.id);
2023-05-01 16:00:56 -04:00
_messageTextController.dispose();
_sessionListScrollController.dispose();
super.dispose();
2023-04-29 15:26:12 -04:00
}
2023-05-01 16:00:56 -04:00
@override
void initState() {
super.initState();
_sessionListScrollController.addListener(() {
if (_sessionListScrollController.position.maxScrollExtent > 0 && !_showSessionListChevron) {
setState(() {
_showSessionListChevron = true;
});
}
if (_sessionListScrollController.position.atEdge && _sessionListScrollController.position.pixels > 0
&& _showSessionListChevron) {
setState(() {
_showSessionListChevron = false;
});
}
});
_messageScrollController.addListener(() {
if (_messageScrollController.position.atEdge && _messageScrollController.position.pixels > 0 &&
_messageScrollController.position.maxScrollExtent > 0 && _messageCacheFutureComplete) {
setState(() {
_messageCacheFutureComplete = false;
_messageCacheFuture = _clientHolder?.messagingClient.getMessageCache(widget.friend.id)
.then((value) => value.loadOlderMessages()).whenComplete(() => _messageCacheFutureComplete = true);
});
}
});
2023-05-01 16:00:56 -04:00
}
2023-04-29 15:26:12 -04:00
@override
Widget build(BuildContext context) {
2023-05-01 16:00:56 -04:00
final apiClient = ClientHolder
.of(context)
.apiClient;
2023-04-30 17:14:29 -04:00
var sessions = widget.friend.userStatus.activeSessions;
2023-05-01 16:00:56 -04:00
final appBarColor = Theme
.of(context)
.colorScheme
.surfaceVariant;
2023-04-29 15:26:12 -04:00
return Scaffold(
appBar: AppBar(
title: Text(widget.friend.username),
2023-04-30 17:14:29 -04:00
scrolledUnderElevation: 0.0,
2023-05-01 16:00:56 -04:00
backgroundColor: appBarColor,
2023-04-29 15:26:12 -04:00
),
body: Column(
2023-05-01 16:00:56 -04:00
children: [
if (sessions.isNotEmpty) Container(
constraints: const BoxConstraints(maxHeight: 64),
decoration: BoxDecoration(
color: appBarColor,
border: const Border(top: BorderSide(width: 1, color: Colors.black26),)
),
child: Stack(
children: [
ListView.builder(
controller: _sessionListScrollController,
scrollDirection: Axis.horizontal,
itemCount: sessions.length,
itemBuilder: (context, index) => SessionTile(session: sessions[index]),
),
AnimatedOpacity(
opacity: _shevronOpacity,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 200),
child: Align(
alignment: Alignment.centerRight,
child: Container(
padding: const EdgeInsets.only(left: 16, right: 4, top: 1, bottom: 1),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
appBarColor.withOpacity(0),
appBarColor,
appBarColor,
],
2023-05-01 16:00:56 -04:00
),
),
height: double.infinity,
child: const Icon(Icons.chevron_right),
2023-05-01 16:00:56 -04:00
),
),
)
],
),
2023-05-01 16:00:56 -04:00
),
Expanded(
child: FutureBuilder(
future: _messageCacheFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
final cache = snapshot.data as MessageCache;
if (cache.messages.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.message_outlined),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
"There are no messages here\nWhy not say hello?",
textAlign: TextAlign.center,
style: Theme
.of(context)
.textTheme
.titleMedium,
),
)
],
),
);
}
return ListView.builder(
controller: _messageScrollController,
reverse: true,
itemCount: cache.messages.length,
itemBuilder: (context, index) {
final entry = cache.messages[index];
final widget = entry.senderId == apiClient.userId
? MyMessageBubble(message: entry)
: OtherMessageBubble(message: entry);
if (index == cache.messages.length-1) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: widget,
);
}
return widget;
},
);
} else if (snapshot.hasError) {
return DefaultErrorWidget(
message: "${snapshot.error}",
onRetry: () {
setState(() {
_loadMessages();
});
},
);
} else {
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: const [
LinearProgressIndicator(),
],
);
}
},
2023-05-01 16:00:56 -04:00
),
),
],
),
bottomNavigationBar: Padding(
padding: MediaQuery
.of(context)
.viewInsets,
child: BottomAppBar(
2023-05-02 02:43:35 -04:00
elevation: 0.0,
2023-05-01 16:00:56 -04:00
padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 6),
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8),
child: TextField(
2023-05-02 02:43:35 -04:00
autocorrect: true,
2023-05-01 16:00:56 -04:00
controller: _messageTextController,
maxLines: 4,
minLines: 1,
onChanged: (text) {
if (text.isNotEmpty && !_isSendable) {
2023-04-30 17:14:29 -04:00
setState(() {
2023-05-01 16:00:56 -04:00
_isSendable = true;
2023-04-30 17:14:29 -04:00
});
2023-05-01 16:00:56 -04:00
} else if (text.isEmpty && _isSendable) {
setState(() {
_isSendable = false;
});
}
},
decoration: InputDecoration(
isDense: true,
hintText: "Send a message to ${widget.friend
.username}...",
contentPadding: const EdgeInsets.all(16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24)
)
2023-04-30 17:14:29 -04:00
),
2023-05-01 16:00:56 -04:00
),
2023-04-29 15:26:12 -04:00
),
2023-04-30 17:14:29 -04:00
),
2023-05-01 16:00:56 -04:00
Padding(
padding: const EdgeInsets.only(left: 8, right: 4.0),
child: IconButton(
splashRadius: 24,
2023-05-02 04:04:54 -04:00
onPressed: _isSendable && _clientHolder != null ? () async {
2023-05-01 16:00:56 -04:00
setState(() {
_isSendable = false;
});
final message = Message(
id: Message.generateId(),
recipientId: widget.friend.id,
senderId: apiClient.userId,
type: MessageType.text,
content: _messageTextController.text,
sendTime: DateTime.now().toUtc(),
);
try {
_clientHolder!.messagingClient.sendMessage(message);
2023-05-01 16:00:56 -04:00
_messageTextController.clear();
2023-05-02 02:43:35 -04:00
setState(() {});
2023-05-01 16:00:56 -04:00
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to send message\n$e",
maxLines: null,
),
),
);
2023-04-30 03:01:59 -04:00
setState(() {
_isSendable = true;
});
}
2023-05-01 16:00:56 -04:00
} : null,
iconSize: 28,
icon: const Icon(Icons.send),
2023-04-30 03:01:59 -04:00
),
2023-05-01 16:00:56 -04:00
)
],
),
2023-04-30 03:01:59 -04:00
),
),
2023-04-29 15:26:12 -04:00
);
}
}
class MyMessageBubble extends StatelessWidget {
2023-04-30 03:01:59 -04:00
MyMessageBubble({required this.message, super.key});
2023-04-29 15:26:12 -04:00
final Message message;
2023-04-30 03:01:59 -04:00
final DateFormat _dateFormat = DateFormat.Hm();
2023-04-29 15:26:12 -04:00
@override
Widget build(BuildContext context) {
2023-04-29 16:21:00 -04:00
var content = message.content;
2023-05-02 15:34:36 -04:00
switch (message.type) {
case MessageType.sessionInvite:
2023-05-02 16:50:35 -04:00
return Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.primaryContainer,
margin: const EdgeInsets.only(left: 32, bottom: 16, right: 12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 12),
child: MessageSessionInvite(message: message,),
),
),
],
);
2023-05-02 15:34:36 -04:00
case MessageType.object:
content = "[Asset]";
continue rawText;
case MessageType.unknown:
rawText:
case MessageType.text:
return Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.primaryContainer,
margin: const EdgeInsets.only(left: 32, bottom: 16, right: 12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
2023-05-02 15:34:36 -04:00
content,
softWrap: true,
maxLines: null,
style: Theme
.of(context)
.textTheme
.bodyLarge,
),
const SizedBox(height: 6,),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: Text(
_dateFormat.format(message.sendTime),
style: Theme
.of(context)
.textTheme
.labelMedium
?.copyWith(color: Colors.white54),
),
),
MessageStateIndicator(messageState: message.state),
],
),
],
2023-04-30 03:01:59 -04:00
),
2023-05-02 15:34:36 -04:00
),
2023-04-29 16:21:00 -04:00
),
),
2023-05-02 15:34:36 -04:00
],
);
case MessageType.sound:
return Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.primaryContainer,
margin: const EdgeInsets.only(left: 32, bottom: 16, right: 12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 12),
child: MessageAudioPlayer(message: message,),
),
),
],
);
}
2023-04-29 15:26:12 -04:00
}
}
class OtherMessageBubble extends StatelessWidget {
2023-04-30 03:01:59 -04:00
OtherMessageBubble({required this.message, super.key});
2023-04-29 15:26:12 -04:00
final Message message;
2023-04-30 03:01:59 -04:00
final DateFormat _dateFormat = DateFormat.Hm();
2023-04-29 15:26:12 -04:00
@override
Widget build(BuildContext context) {
2023-04-29 16:21:00 -04:00
var content = message.content;
2023-05-02 15:34:36 -04:00
switch (message.type) {
case MessageType.sessionInvite:
2023-05-02 16:50:35 -04:00
return Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.secondaryContainer,
margin: const EdgeInsets.only(right: 32, bottom: 16, left: 12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: MessageSessionInvite(message: message,),
),
),
],
);
2023-05-02 15:34:36 -04:00
case MessageType.object:
content = "[Asset]";
continue rawText;
case MessageType.unknown:
rawText:
case MessageType.text:
return Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Flexible(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.secondaryContainer,
margin: const EdgeInsets.only(right: 32, bottom: 16, left: 12),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
content,
softWrap: true,
maxLines: null,
style: Theme
.of(context)
.textTheme
.bodyLarge,
),
const SizedBox(height: 6,),
Text(
_dateFormat.format(message.sendTime),
style: Theme
.of(context)
.textTheme
.labelMedium
?.copyWith(color: Colors.white54),
),
],
),
),
),
),
],
);
case MessageType.sound:
return Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
2023-05-02 16:50:35 -04:00
children: [
Card(
2023-04-29 16:21:00 -04:00
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.secondaryContainer,
margin: const EdgeInsets.only(right: 32, bottom: 16, left: 12),
2023-04-29 16:21:00 -04:00
child: Padding(
2023-04-30 03:01:59 -04:00
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
2023-05-02 15:34:36 -04:00
child: MessageAudioPlayer(message: message,),
2023-04-29 16:21:00 -04:00
),
),
2023-05-02 15:34:36 -04:00
],
);
}
2023-04-29 15:26:12 -04:00
}
2023-04-30 03:01:59 -04:00
}
class MessageStateIndicator extends StatelessWidget {
const MessageStateIndicator({required this.messageState, super.key});
final MessageState messageState;
2023-04-30 03:01:59 -04:00
@override
Widget build(BuildContext context) {
late final IconData icon;
switch (messageState) {
case MessageState.local:
icon = Icons.alarm;
break;
case MessageState.sent:
icon = Icons.done;
break;
case MessageState.read:
icon = Icons.done_all;
break;
}
return Icon(
icon,
size: 12,
color: messageState == MessageState.read ? Theme.of(context).colorScheme.primary : null,
);
2023-04-30 03:01:59 -04:00
}
2023-05-01 16:00:56 -04:00
}
2023-04-30 03:01:59 -04:00
2023-05-02 16:50:35 -04:00
class SessionPopup extends StatelessWidget {
const SessionPopup({required this.session, super.key});
2023-05-01 16:00:56 -04:00
final Session session;
@override
Widget build(BuildContext context) {
2023-05-02 16:50:35 -04:00
final ScrollController userListScrollController = ScrollController();
final thumbnailUri = Aux.neosDbToHttp(session.thumbnail);
return Dialog(
insetPadding: const EdgeInsets.all(32),
child: Container(
constraints: const BoxConstraints(maxHeight: 400),
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Row(
2023-05-01 16:00:56 -04:00
mainAxisSize: MainAxisSize.min,
2023-05-02 16:50:35 -04:00
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2023-05-01 16:00:56 -04:00
children: [
Expanded(
2023-05-02 16:50:35 -04:00
child: ListView(
2023-05-01 16:00:56 -04:00
children: [
2023-05-02 16:50:35 -04:00
Text(session.name, style: Theme.of(context).textTheme.titleMedium),
Text(session.description.isEmpty ? "No description." : session.description, style: Theme.of(context).textTheme.labelMedium),
Text("Tags: ${session.tags.isEmpty ? "None" : session.tags.join(", ")}",
style: Theme.of(context).textTheme.labelMedium,
softWrap: true,
2023-05-01 16:00:56 -04:00
),
Text("Access: ${session.accessLevel.toReadableString()}"),
2023-05-02 16:50:35 -04:00
Text("Users: ${session.sessionUsers.length}", style: Theme.of(context).textTheme.labelMedium),
Text("Maximum users: ${session.maxUsers}", style: Theme.of(context).textTheme.labelMedium),
Text("Headless: ${session.headlessHost ? "Yes" : "No"}", style: Theme.of(context).textTheme.labelMedium),
2023-05-01 16:00:56 -04:00
],
),
),
2023-05-02 16:50:35 -04:00
if (session.sessionUsers.isNotEmpty) Expanded(
child: Scrollbar(
trackVisibility: true,
controller: userListScrollController,
thumbVisibility: true,
child: ListView.builder(
controller: userListScrollController,
shrinkWrap: true,
itemCount: session.sessionUsers.length,
itemBuilder: (context, index) {
final user = session.sessionUsers[index];
return ListTile(
dense: true,
title: Text(user.username, textAlign: TextAlign.end,),
subtitle: Text(user.isPresent ? "Active" : "Inactive", textAlign: TextAlign.end,),
);
2023-05-01 16:00:56 -04:00
},
),
),
2023-05-02 16:50:35 -04:00
) else Expanded(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(Icons.person_remove_alt_1_rounded),
Padding(
padding: EdgeInsets.all(16.0),
child: Text("No one is currently playing.", textAlign: TextAlign.center,),
)
],
),
),
),
2023-05-01 16:00:56 -04:00
],
),
),
2023-05-02 16:50:35 -04:00
Expanded(
child: Center(
child: CachedNetworkImage(
imageUrl: thumbnailUri,
placeholder: (context, url) {
return const CircularProgressIndicator();
},
errorWidget: (context, error, what) => Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.no_photography),
Padding(
padding: EdgeInsets.all(16.0),
child: Text("Failed to load Image"),
)
],
),
),
),
)
],
),
),
);
}
}
class SessionTile extends StatelessWidget {
const SessionTile({required this.session, super.key});
final Session session;
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
showDialog(context: context, builder: (context) => SessionPopup(session: session));
2023-05-01 16:00:56 -04:00
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GenericAvatar(imageUri: Aux.neosDbToHttp(session.thumbnail), placeholderIcon: Icons.no_photography),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(session.name),
Text("${session.sessionUsers.length}/${session.maxUsers} active users")
],
),
)
],
),
);
}
2023-04-29 15:26:12 -04:00
}