OpenContacts/lib/widgets/messages.dart

369 lines
12 KiB
Dart
Raw Normal View History

2023-04-30 17:14:29 -04:00
import 'package:cached_network_image/cached_network_image.dart';
2023-04-29 15:26:12 -04:00
import 'package:contacts_plus/apis/message_api.dart';
2023-04-30 17:14:29 -04:00
import 'package:contacts_plus/aux.dart';
2023-04-30 07:39:09 -04:00
import 'package:contacts_plus/main.dart';
2023-04-29 15:26:12 -04:00
import 'package:contacts_plus/models/friend.dart';
import 'package:contacts_plus/models/message.dart';
2023-04-30 17:14:29 -04:00
import 'package:contacts_plus/widgets/generic_avatar.dart';
2023-04-29 15:26:12 -04:00
import 'package:flutter/material.dart';
2023-04-30 17:14:29 -04:00
import 'package:http/http.dart';
2023-04-30 03:01:59 -04:00
import 'package:intl/intl.dart';
2023-04-29 15:26:12 -04:00
class Messages extends StatefulWidget {
const Messages({required this.friend, super.key});
final Friend friend;
@override
State<StatefulWidget> createState() => _MessagesState();
}
class _MessagesState extends State<Messages> {
2023-04-30 17:14:29 -04:00
static const double headerItemSize = 300.0;
2023-04-29 15:26:12 -04:00
Future<Iterable<Message>>? _messagesFuture;
2023-04-30 03:01:59 -04:00
final TextEditingController _messageTextController = TextEditingController();
2023-04-30 07:39:09 -04:00
ClientHolder? _clientHolder;
2023-04-30 17:14:29 -04:00
MessageCacheHolder? _cacheHolder;
2023-04-30 03:01:59 -04:00
2023-04-30 17:14:29 -04:00
bool _headerExpanded = false;
2023-04-30 03:01:59 -04:00
bool _isSendable = false;
2023-04-29 15:26:12 -04:00
2023-04-30 17:14:29 -04:00
double get _headerHeight => _headerExpanded ? headerItemSize : 0;
double get _chevronTurns => _headerExpanded ? -1/4 : 1/4;
2023-04-29 15:26:12 -04:00
void _refreshMessages() {
2023-04-30 17:14:29 -04:00
final cache = _cacheHolder?.getCache(widget.friend.id);
if (cache?.isValid ?? false) {
_messagesFuture = Future(() => cache!.messages);
} else {
_messagesFuture = MessageApi.getUserMessages(_clientHolder!.client, userId: widget.friend.id)
..then((value) {
final list = value.toList();
_cacheHolder?.setCache(widget.friend.id, list);
return list;
});
}
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);
2023-04-30 17:14:29 -04:00
bool dirty = false;
2023-04-30 07:39:09 -04:00
if (_clientHolder != clientHolder) {
_clientHolder = clientHolder;
2023-04-30 17:14:29 -04:00
dirty = true;
2023-04-30 07:39:09 -04:00
}
2023-04-30 17:14:29 -04:00
final cacheHolder = MessageCacheHolder.of(context);
if (_cacheHolder != cacheHolder) {
_cacheHolder = cacheHolder;
dirty = true;
}
if (dirty) _refreshMessages();
2023-04-29 15:26:12 -04:00
}
@override
Widget build(BuildContext context) {
2023-04-30 07:39:09 -04:00
final apiClient = ClientHolder.of(context).client;
2023-04-30 17:14:29 -04:00
var sessions = widget.friend.userStatus.activeSessions;
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
actions: [
if(sessions.isNotEmpty) AnimatedRotation(
turns: _chevronTurns,
curve: Curves.easeOutCirc,
duration: const Duration(milliseconds: 250),
child: IconButton(
onPressed: () {
setState(() {
_headerExpanded = !_headerExpanded;
});
},
icon: const Icon(Icons.chevron_right),
),
)
],
scrolledUnderElevation: 0.0,
backgroundColor: Theme.of(context).colorScheme.surfaceVariant,
bottom: sessions.isEmpty ? null : PreferredSize(
preferredSize: Size.fromHeight(_headerHeight),
child: AnimatedContainer(
height: _headerHeight,
duration: const Duration(milliseconds: 400),
child: Column(
children: sessions.getRange(0, _headerExpanded ? sessions.length : 1).map((e) => Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: GenericAvatar(imageUri: Aux.neosDbToHttp(e.thumbnail),),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(e.name),
Text("${e.sessionUsers.length} users active"),
],
),
),
const Spacer(),
if (sessions.length > 1) TextButton(onPressed: (){
setState(() {
_headerExpanded = !_headerExpanded;
});
}, child: Text("+${sessions.length-1}"),)
],
)).toList(),
),
),
),
2023-04-29 15:26:12 -04:00
),
body: FutureBuilder(
future: _messagesFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
final data = snapshot.data as Iterable<Message>;
return ListView.builder(
2023-04-29 16:21:00 -04:00
reverse: true,
2023-04-29 15:26:12 -04:00
itemCount: data.length,
itemBuilder: (context, index) {
final entry = data.elementAt(index);
2023-04-29 16:21:00 -04:00
return entry.senderId == apiClient.userId
? MyMessageBubble(message: entry)
: OtherMessageBubble(message: entry);
2023-04-29 15:26:12 -04:00
},
);
} else if (snapshot.hasError) {
2023-04-30 17:14:29 -04:00
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 64, vertical: 128),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("Failed to load messages:", style: Theme.of(context).textTheme.titleMedium,),
const SizedBox(height: 16,),
Text("${snapshot.error}"),
const Spacer(),
TextButton.icon(
onPressed: () {
setState(() {
_refreshMessages();
});
},
style: TextButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 16),
),
icon: const Icon(Icons.refresh),
label: const Text("Retry"),
),
],
2023-04-29 15:26:12 -04:00
),
2023-04-30 17:14:29 -04:00
),
2023-04-29 15:26:12 -04:00
);
} else {
return const LinearProgressIndicator();
}
},
),
2023-04-30 03:01:59 -04:00
bottomNavigationBar: BottomAppBar(
2023-04-30 17:14:29 -04:00
padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 6),
2023-04-30 03:01:59 -04:00
child: Row(
children: [
Expanded(
child: Padding(
2023-04-30 17:14:29 -04:00
padding: const EdgeInsets.all(8),
2023-04-30 03:01:59 -04:00
child: TextField(
controller: _messageTextController,
maxLines: 4,
minLines: 1,
onChanged: (text) {
if (text.isNotEmpty && !_isSendable) {
setState(() {
_isSendable = true;
});
} else if (text.isEmpty && _isSendable) {
setState(() {
_isSendable = false;
});
}
},
decoration: InputDecoration(
isDense: true,
hintText: "Send a message to ${widget.friend.username}...",
2023-04-30 17:14:29 -04:00
contentPadding: const EdgeInsets.all(16),
2023-04-30 03:01:59 -04:00
border: OutlineInputBorder(
2023-04-30 17:14:29 -04:00
borderRadius: BorderRadius.circular(24)
)
2023-04-30 03:01:59 -04:00
),
),
),
),
Padding(
2023-04-30 17:14:29 -04:00
padding: const EdgeInsets.only(left: 8, right: 4.0),
2023-04-30 03:01:59 -04:00
child: IconButton(
splashRadius: 24,
2023-04-30 07:39:09 -04:00
onPressed: _isSendable ? () async {
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 {
await apiClient.hub.sendMessage(message);
_messageTextController.clear();
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to send message\n$e",
maxLines: null,
),
),
);
setState(() {
_isSendable = true;
});
}
} : null,
2023-04-30 03:01:59 -04:00
iconSize: 28,
icon: const Icon(Icons.send),
),
)
],
),
),
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;
if (message.type == MessageType.sessionInvite) {
content = "<Session Invite>";
} else if (message.type == MessageType.sound) {
content = "<Voice Message>";
} else if (message.type == MessageType.object) {
content = "<Asset>";
}
2023-04-29 15:26:12 -04:00
return Row(
mainAxisAlignment: MainAxisAlignment.end,
2023-04-29 16:21:00 -04:00
mainAxisSize: MainAxisSize.min,
2023-04-29 15:26:12 -04:00
children: [
2023-04-29 16:21:00 -04:00
Flexible(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme.of(context).colorScheme.primaryContainer,
margin: const EdgeInsets.only(left: 32, bottom: 16, right: 8),
child: Padding(
2023-04-30 03:01:59 -04:00
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
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),
),
],
2023-04-29 16:21:00 -04:00
),
),
),
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;
if (message.type == MessageType.sessionInvite) {
content = "<Session Invite>";
} else if (message.type == MessageType.sound) {
content = "<Voice Message>";
} else if (message.type == MessageType.object) {
content = "<Asset>";
}
2023-04-29 15:26:12 -04:00
return Row(
2023-04-29 16:21:00 -04:00
mainAxisSize: MainAxisSize.min,
2023-04-29 15:26:12 -04:00
mainAxisAlignment: MainAxisAlignment.start,
children: [
2023-04-29 16:21:00 -04:00
Flexible(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
color: Theme
.of(context)
.colorScheme
.secondaryContainer,
margin: const EdgeInsets.only(right: 32, bottom: 16, left: 8),
child: Padding(
2023-04-30 03:01:59 -04:00
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),
),
],
2023-04-29 16:21:00 -04:00
),
),
),
2023-04-29 15:26:12 -04:00
),
],
);
}
2023-04-30 03:01:59 -04:00
}
class MessageStatusIndicator extends StatelessWidget {
const MessageStatusIndicator({super.key});
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}
2023-04-29 15:26:12 -04:00
}