OpenContacts/lib/widgets/messages/messages_list.dart

580 lines
26 KiB
Dart
Raw Normal View History

2023-05-17 02:07:10 -04:00
import 'dart:convert';
import 'dart:io';
import 'package:contacts_plus_plus/apis/record_api.dart';
import 'package:contacts_plus_plus/client_holder.dart';
2023-05-17 02:07:10 -04:00
import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/message.dart';
2023-05-16 09:57:44 -04:00
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
import 'package:contacts_plus_plus/widgets/friends/friend_online_status_indicator.dart';
import 'package:contacts_plus_plus/widgets/messages/message_attachment_list.dart';
import 'package:contacts_plus_plus/widgets/messages/message_camera_view.dart';
import 'package:contacts_plus_plus/widgets/messages/message_record_button.dart';
import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart';
2023-05-17 02:07:10 -04:00
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'message_bubble.dart';
class MessagesList extends StatefulWidget {
const MessagesList({required this.friend, super.key});
final Friend friend;
@override
State<StatefulWidget> createState() => _MessagesListState();
}
class _MessagesListState extends State<MessagesList> with SingleTickerProviderStateMixin {
final TextEditingController _messageTextController = TextEditingController();
final ScrollController _sessionListScrollController = ScrollController();
final ScrollController _messageScrollController = ScrollController();
2023-05-17 17:20:56 -04:00
final List<File> _loadedFiles = [];
2023-05-17 02:07:10 -04:00
bool _hasText = false;
bool _isSending = false;
2023-05-17 17:20:56 -04:00
bool _attachmentPickerOpen = false;
double? _sendProgress;
2023-05-17 17:20:56 -04:00
bool _showBottomBarShadow = false;
2023-05-17 17:20:56 -04:00
bool _showSessionListScrollChevron = false;
double get _shevronOpacity => _showSessionListScrollChevron ? 1.0 : 0.0;
@override
void dispose() {
_messageTextController.dispose();
_sessionListScrollController.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
_sessionListScrollController.addListener(() {
if (_sessionListScrollController.position.maxScrollExtent > 0 && !_showSessionListScrollChevron) {
setState(() {
_showSessionListScrollChevron = true;
});
}
if (_sessionListScrollController.position.atEdge && _sessionListScrollController.position.pixels > 0
&& _showSessionListScrollChevron) {
setState(() {
_showSessionListScrollChevron = false;
});
}
});
_messageScrollController.addListener(() {
if (!_messageScrollController.hasClients) return;
2023-05-17 17:20:56 -04:00
if (_attachmentPickerOpen && _loadedFiles.isEmpty) {
setState(() {
_attachmentPickerOpen = false;
});
}
if (_messageScrollController.position.atEdge && _messageScrollController.position.pixels == 0 &&
_showBottomBarShadow) {
setState(() {
_showBottomBarShadow = false;
});
} else if (!_showBottomBarShadow) {
setState(() {
_showBottomBarShadow = true;
});
}
});
}
2023-05-18 04:52:32 -04:00
Future<void> sendTextMessage(ApiClient client, MessagingClient mClient, String content) async {
if (content.isEmpty) return;
2023-05-17 02:07:10 -04:00
final message = Message(
id: Message.generateId(),
recipientId: widget.friend.id,
senderId: client.userId,
type: MessageType.text,
content: content,
sendTime: DateTime.now().toUtc(),
);
2023-05-17 17:20:56 -04:00
mClient.sendMessage(message);
_messageTextController.clear();
2023-05-18 04:52:32 -04:00
_hasText = false;
2023-05-17 02:07:10 -04:00
}
Future<void> sendImageMessage(ApiClient client, MessagingClient mClient, File file, String machineId,
void Function(double progress) progressCallback) async {
2023-05-17 17:20:56 -04:00
final record = await RecordApi.uploadImage(
client,
image: file,
machineId: machineId,
2023-05-18 04:52:32 -04:00
progressCallback: progressCallback,
2023-05-17 17:20:56 -04:00
);
final message = Message(
id: record.extractMessageId() ?? Message.generateId(),
2023-05-17 17:20:56 -04:00
recipientId: widget.friend.id,
senderId: client.userId,
type: MessageType.object,
content: jsonEncode(record.toMap()),
sendTime: DateTime.now().toUtc(),
);
mClient.sendMessage(message);
_messageTextController.clear();
2023-05-18 04:52:32 -04:00
_hasText = false;
2023-05-17 02:07:10 -04:00
}
Future<void> sendVoiceMessage(ApiClient client, MessagingClient mClient, File file, String machineId,
void Function(double progress) progressCallback) async {
final record = await RecordApi.uploadVoiceClip(
client,
voiceClip: file,
machineId: machineId,
progressCallback: progressCallback,
);
final message = Message(
id: record.extractMessageId() ?? Message.generateId(),
recipientId: widget.friend.id,
senderId: client.userId,
type: MessageType.sound,
content: jsonEncode(record.toMap()),
sendTime: DateTime.now().toUtc(),
);
mClient.sendMessage(message);
_messageTextController.clear();
_hasText = false;
}
@override
Widget build(BuildContext context) {
final apiClient = ClientHolder
.of(context)
.apiClient;
var sessions = widget.friend.userStatus.activeSessions;
final appBarColor = Theme
.of(context)
.colorScheme
.surfaceVariant;
2023-05-16 09:57:44 -04:00
return Consumer<MessagingClient>(
2023-05-16 10:11:03 -04:00
builder: (context, mClient, _) {
final cache = mClient.getUserMessageCache(widget.friend.id);
return Scaffold(
appBar: AppBar(
title: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
FriendOnlineStatusIndicator(userStatus: widget.friend.userStatus),
const SizedBox(width: 8,),
Text(widget.friend.username),
if (widget.friend.isHeadless) Padding(
padding: const EdgeInsets.only(left: 12),
child: Icon(Icons.dns, size: 18, color: Theme
.of(context)
.colorScheme
.onSecondaryContainer
.withAlpha(150),),
),
],
),
scrolledUnderElevation: 0.0,
backgroundColor: appBarColor,
),
2023-05-16 10:11:03 -04:00
body: Column(
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-16 09:57:44 -04:00
),
2023-05-16 10:11:03 -04:00
height: double.infinity,
child: const Icon(Icons.chevron_right),
2023-05-16 09:57:44 -04:00
),
),
2023-05-16 10:11:03 -04:00
)
],
),
),
2023-05-16 10:11:03 -04:00
Expanded(
2023-05-17 17:20:56 -04:00
child: Stack(
children: [
Builder(
builder: (context) {
if (cache == null) {
return const Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
LinearProgressIndicator()
],
);
}
if (cache.error != null) {
return DefaultErrorWidget(
message: cache.error.toString(),
onRetry: () {
setState(() {
mClient.deleteUserMessageCache(widget.friend.id);
});
mClient.loadUserMessageCache(widget.friend.id);
},
2023-05-16 10:11:03 -04:00
);
}
2023-05-17 17:20:56 -04:00
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];
if (index == cache.messages.length - 1) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: MessageBubble(message: entry,),
);
}
return MessageBubble(message: entry,);
},
);
2023-05-16 10:11:03 -04:00
},
2023-05-17 17:20:56 -04:00
),
Align(
alignment: Alignment.bottomCenter,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
blurRadius: 8,
color: Theme
.of(context)
.shadowColor,
offset: const Offset(0, 4),
),
],
color: Theme
.of(context)
.colorScheme
.background,
),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
switchInCurve: Curves.easeOut,
switchOutCurve: Curves.easeOut,
transitionBuilder: (Widget child, animation) =>
SizeTransition(sizeFactor: animation, child: child,),
2023-05-17 17:20:56 -04:00
child: switch ((_attachmentPickerOpen, _loadedFiles)) {
(true, []) =>
Row(
key: const ValueKey("attachment-picker"),
children: [
TextButton.icon(
onPressed: _isSending ? null : () async {
final result = await FilePicker.platform.pickFiles(type: FileType.image);
if (result != null && result.files.single.path != null) {
setState(() {
_loadedFiles.add(File(result.files.single.path!));
});
}
},
icon: const Icon(Icons.image),
label: const Text("Gallery"),
),
TextButton.icon(
onPressed: _isSending ? null : () async {
final picture = await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => const MessageCameraView()));
if (picture != null) {
setState(() {
_loadedFiles.add(picture);
});
}
},
icon: const Icon(Icons.camera_alt),
label: const Text("Camera"),
),
],
2023-05-17 17:20:56 -04:00
),
(false, []) => null,
(_, _) =>
MessageAttachmentList(
disabled: _isSending,
initialFiles: _loadedFiles,
onChange: (List<File> loadedFiles) =>
setState(() {
_loadedFiles.clear();
_loadedFiles.addAll(loadedFiles);
}),
)
2023-05-17 17:20:56 -04:00
},
),
),
],
),
),
if (_isSending && _sendProgress != null)
2023-05-18 04:52:32 -04:00
Align(
alignment: Alignment.bottomCenter,
child: LinearProgressIndicator(value: _sendProgress),
),
2023-05-17 17:20:56 -04:00
],
2023-05-16 10:11:03 -04:00
),
),
AnimatedContainer(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
2023-05-17 17:20:56 -04:00
blurRadius: _showBottomBarShadow && !_attachmentPickerOpen ? 8 : 0,
2023-05-16 10:11:03 -04:00
color: Theme
.of(context)
.shadowColor,
offset: const Offset(0, 4),
2023-05-16 09:57:44 -04:00
),
2023-05-16 10:11:03 -04:00
],
color: Theme
.of(context)
.colorScheme
.background,
),
padding: const EdgeInsets.symmetric(horizontal: 4),
duration: const Duration(milliseconds: 250),
child: Row(
children: [
2023-05-17 17:20:56 -04:00
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
transitionBuilder: (Widget child, Animation<double> animation) =>
FadeTransition(
opacity: animation,
child: RotationTransition(
turns: Tween<double>(begin: 0.6, end: 1).animate(animation),
child: child,
),
),
child: !_attachmentPickerOpen ?
IconButton(
key: const ValueKey("add-attachment-icon"),
onPressed: _isSending ? null : () {
2023-05-17 02:07:10 -04:00
setState(() {
2023-05-17 17:20:56 -04:00
_attachmentPickerOpen = true;
2023-05-17 02:07:10 -04:00
});
2023-05-17 17:20:56 -04:00
},
icon: const Icon(Icons.attach_file),
) :
IconButton(
key: const ValueKey("remove-attachment-icon"),
onPressed: _isSending ? null : () async {
if (_loadedFiles.isNotEmpty) {
await showDialog(context: context, builder: (context) =>
AlertDialog(
title: const Text("Remove all attachments"),
content: const Text("This will remove all attachments, are you sure?"),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("No"),
),
TextButton(
onPressed: () {
setState(() {
_loadedFiles.clear();
_attachmentPickerOpen = false;
});
Navigator.of(context).pop();
},
child: const Text("Yes"),
)
],
));
} else {
setState(() {
_attachmentPickerOpen = false;
});
}
2023-05-17 17:20:56 -04:00
},
icon: const Icon(Icons.close),
),
),
2023-05-16 10:11:03 -04:00
Expanded(
child: Padding(
padding: const EdgeInsets.all(8),
child: TextField(
2023-05-18 04:52:32 -04:00
enabled: cache != null && cache.error == null && !_isSending,
2023-05-16 10:11:03 -04:00
autocorrect: true,
controller: _messageTextController,
maxLines: 4,
minLines: 1,
onChanged: (text) {
2023-05-17 02:07:10 -04:00
if (text.isNotEmpty && !_hasText) {
2023-05-16 09:57:44 -04:00
setState(() {
2023-05-17 02:07:10 -04:00
_hasText = true;
2023-05-16 09:57:44 -04:00
});
2023-05-17 02:07:10 -04:00
} else if (text.isEmpty && _hasText) {
2023-05-16 10:11:03 -04:00
setState(() {
2023-05-17 02:07:10 -04:00
_hasText = false;
2023-05-16 10:11:03 -04:00
});
2023-05-16 09:57:44 -04:00
}
2023-05-16 10:11:03 -04:00
},
decoration: InputDecoration(
isDense: true,
2023-05-17 17:20:56 -04:00
hintText: "Message ${widget.friend
.username}...",
2023-05-16 10:11:03 -04:00
hintMaxLines: 1,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24)
)
),
),
),
2023-05-16 09:57:44 -04:00
),
2023-05-16 10:11:03 -04:00
Padding(
padding: const EdgeInsets.only(left: 8, right: 4.0),
2023-05-17 17:20:56 -04:00
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
transitionBuilder: (Widget child, Animation<double> animation) =>
FadeTransition(opacity: animation, child: RotationTransition(
turns: Tween<double>(begin: 0.5, end: 1).animate(animation), child: child,),),
child: _hasText || _loadedFiles.isNotEmpty ? IconButton(
key: const ValueKey("send-button"),
splashRadius: 24,
onPressed: _isSending ? null : () async {
final sMsgnr = ScaffoldMessenger.of(context);
final toSend = List.from(_loadedFiles);
2023-05-17 17:20:56 -04:00
setState(() {
_isSending = true;
2023-05-18 04:52:32 -04:00
_sendProgress = 0;
_attachmentPickerOpen = false;
_loadedFiles.clear();
2023-05-17 17:20:56 -04:00
});
try {
for (int i = 0; i < toSend.length; i++) {
final totalProgress = i / toSend.length;
final file = toSend[i];
2023-05-17 17:20:56 -04:00
await sendImageMessage(apiClient, mClient, file, ClientHolder
.of(context)
.settingsClient
.currentSettings
.machineId
2023-05-18 04:52:32 -04:00
.valueOrDefault,
(progress) =>
setState(() {
_sendProgress = totalProgress + progress * 1 / toSend.length;
2023-05-18 04:52:32 -04:00
}),
);
2023-05-17 17:20:56 -04:00
}
2023-05-18 04:52:32 -04:00
setState(() {
_sendProgress = null;
2023-05-18 04:52:32 -04:00
});
2023-05-17 17:20:56 -04:00
if (_hasText) {
await sendTextMessage(apiClient, mClient, _messageTextController.text);
}
_messageTextController.clear();
_loadedFiles.clear();
_attachmentPickerOpen = false;
} catch (e, s) {
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
sMsgnr.showSnackBar(SnackBar(content: Text("Failed to send a message: $e")));
}
setState(() {
_isSending = false;
_sendProgress = null;
2023-05-17 17:20:56 -04:00
});
},
iconSize: 28,
icon: const Icon(Icons.send),
) : MessageRecordButton(
2023-05-17 17:20:56 -04:00
key: const ValueKey("mic-button"),
disabled: _isSending,
onRecordEnd: (File? file) async {
if (file == null) return;
setState(() {
_isSending = true;
_sendProgress = 0;
});
await sendVoiceMessage(
apiClient,
mClient,
file,
ClientHolder
.of(context)
.settingsClient
.currentSettings
.machineId
.valueOrDefault, (progress) {
setState(() {
_sendProgress = progress;
});
}
);
setState(() {
_isSending = false;
_sendProgress = null;
});
},
2023-05-17 17:20:56 -04:00
),
2023-05-16 10:11:03 -04:00
),
),
],
),
2023-05-16 09:57:44 -04:00
),
2023-05-16 10:11:03 -04:00
],
),
);
}
);
}
}