2024-04-17 15:00:53 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2024-05-02 12:45:24 +00:00
|
|
|
import 'package:solian/models/channel.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:solian/models/message.dart';
|
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-01 16:49:38 +00:00
|
|
|
import 'package:solian/providers/chat.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:solian/utils/service_url.dart';
|
2024-05-01 17:38:45 +00:00
|
|
|
import 'package:solian/widgets/chat/channel_action.dart';
|
2024-04-18 14:33:55 +00:00
|
|
|
import 'package:solian/widgets/chat/maintainer.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:solian/widgets/chat/message.dart';
|
2024-04-18 15:39:48 +00:00
|
|
|
import 'package:solian/widgets/chat/message_action.dart';
|
2024-04-17 15:24:09 +00:00
|
|
|
import 'package:solian/widgets/chat/message_editor.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:solian/widgets/indent_wrapper.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
class ChatScreen extends StatelessWidget {
|
2024-04-17 15:00:53 +00:00
|
|
|
final String alias;
|
|
|
|
|
|
|
|
const ChatScreen({super.key, required this.alias});
|
|
|
|
|
|
|
|
@override
|
2024-05-01 16:49:38 +00:00
|
|
|
Widget build(BuildContext context) {
|
2024-05-01 17:38:45 +00:00
|
|
|
final chat = context.watch<ChatProvider>();
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
return IndentWrapper(
|
2024-05-01 17:38:45 +00:00
|
|
|
title: chat.focusChannel?.name ?? 'Loading...',
|
2024-05-01 16:49:38 +00:00
|
|
|
noSafeArea: true,
|
|
|
|
hideDrawer: true,
|
2024-05-01 17:38:45 +00:00
|
|
|
appBarActions: chat.focusChannel != null
|
|
|
|
? [
|
|
|
|
ChannelCallAction(
|
|
|
|
call: chat.ongoingCall,
|
|
|
|
channel: chat.focusChannel!,
|
|
|
|
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias),
|
|
|
|
),
|
|
|
|
ChannelManageAction(
|
|
|
|
channel: chat.focusChannel!,
|
|
|
|
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
: [],
|
2024-05-01 16:49:38 +00:00
|
|
|
child: ChatScreenWidget(
|
|
|
|
alias: alias,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-04-17 15:00:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
class ChatScreenWidget extends StatefulWidget {
|
|
|
|
final String alias;
|
2024-04-17 15:00:53 +00:00
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
const ChatScreenWidget({super.key, required this.alias});
|
2024-04-17 15:00:53 +00:00
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
@override
|
|
|
|
State<ChatScreenWidget> createState() => _ChatScreenWidgetState();
|
|
|
|
}
|
2024-04-17 15:00:53 +00:00
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
class _ChatScreenWidgetState extends State<ChatScreenWidget> {
|
|
|
|
bool _isReady = false;
|
2024-04-17 15:00:53 +00:00
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
final PagingController<int, Message> _pagingController = PagingController(firstPageKey: 0);
|
|
|
|
|
|
|
|
late final ChatProvider _chat;
|
2024-04-26 17:36:54 +00:00
|
|
|
|
2024-04-17 15:00:53 +00:00
|
|
|
Future<void> fetchMessages(int pageKey, BuildContext context) async {
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
|
|
|
final offset = pageKey;
|
2024-04-18 15:39:48 +00:00
|
|
|
const take = 10;
|
2024-04-17 15:00:53 +00:00
|
|
|
|
|
|
|
var uri = getRequestUri(
|
|
|
|
'messaging',
|
|
|
|
'/api/channels/${widget.alias}/messages?take=$take&offset=$offset',
|
|
|
|
);
|
|
|
|
|
|
|
|
var res = await auth.client!.get(uri);
|
|
|
|
if (res.statusCode == 200) {
|
2024-05-01 11:39:48 +00:00
|
|
|
final result = PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
|
|
|
|
final items = result.data?.map((x) => Message.fromJson(x)).toList() ?? List.empty();
|
2024-04-17 15:00:53 +00:00
|
|
|
final isLastPage = (result.count - pageKey) < take;
|
|
|
|
if (isLastPage || result.data == null) {
|
|
|
|
_pagingController.appendLastPage(items);
|
|
|
|
} else {
|
|
|
|
final nextPageKey = pageKey + items.length;
|
|
|
|
_pagingController.appendPage(items, nextPageKey);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_pagingController.error = utf8.decode(res.bodyBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getMessageMergeable(Message? a, Message? b) {
|
2024-05-01 09:37:34 +00:00
|
|
|
if (a?.replyTo != null) return false;
|
2024-04-17 15:00:53 +00:00
|
|
|
if (a == null || b == null) return false;
|
|
|
|
if (a.senderId != b.senderId) return false;
|
|
|
|
return a.createdAt.difference(b.createdAt).inMinutes <= 5;
|
|
|
|
}
|
|
|
|
|
2024-04-18 14:33:55 +00:00
|
|
|
void addMessage(Message item) {
|
2024-04-18 14:43:14 +00:00
|
|
|
setState(() {
|
|
|
|
_pagingController.itemList?.insert(0, item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateMessage(Message item) {
|
|
|
|
setState(() {
|
2024-05-01 11:39:48 +00:00
|
|
|
_pagingController.itemList = _pagingController.itemList?.map((x) => x.id == item.id ? item : x).toList();
|
2024-04-18 14:43:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteMessage(Message item) {
|
|
|
|
setState(() {
|
2024-05-01 11:39:48 +00:00
|
|
|
_pagingController.itemList = _pagingController.itemList?.where((x) => x.id != item.id).toList();
|
2024-04-18 14:33:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-18 15:39:48 +00:00
|
|
|
Message? _editingItem;
|
|
|
|
Message? _replyingItem;
|
|
|
|
|
|
|
|
void viewActions(Message item) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ChatMessageAction(
|
|
|
|
channel: widget.alias,
|
|
|
|
item: item,
|
|
|
|
onEdit: () => setState(() {
|
|
|
|
_editingItem = item;
|
|
|
|
}),
|
|
|
|
onReply: () => setState(() {
|
|
|
|
_replyingItem = item;
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-17 15:00:53 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2024-05-01 11:39:48 +00:00
|
|
|
_pagingController.addPageRequestListener((pageKey) => fetchMessages(pageKey, context));
|
2024-04-17 15:00:53 +00:00
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-26 17:36:54 +00:00
|
|
|
Widget chatHistoryBuilder(context, item, index) {
|
|
|
|
bool isMerged = false, hasMerged = false;
|
|
|
|
if (index > 0) {
|
2024-05-01 11:39:48 +00:00
|
|
|
hasMerged = getMessageMergeable(_pagingController.itemList?[index - 1], item);
|
2024-04-26 17:36:54 +00:00
|
|
|
}
|
|
|
|
if (index + 1 < (_pagingController.itemList?.length ?? 0)) {
|
2024-05-01 11:39:48 +00:00
|
|
|
isMerged = getMessageMergeable(item, _pagingController.itemList?[index + 1]);
|
2024-04-26 17:36:54 +00:00
|
|
|
}
|
|
|
|
return InkWell(
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
top: !isMerged ? 8 : 0,
|
|
|
|
bottom: !hasMerged ? 8 : 0,
|
|
|
|
left: 12,
|
|
|
|
right: 12,
|
|
|
|
),
|
|
|
|
child: ChatMessage(
|
|
|
|
key: Key('m${item.id}'),
|
|
|
|
item: item,
|
|
|
|
underMerged: isMerged,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onLongPress: () => viewActions(item),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
if (!_isReady) {
|
|
|
|
_isReady = true;
|
|
|
|
_chat = context.watch<ChatProvider>();
|
|
|
|
}
|
|
|
|
|
2024-04-26 17:36:54 +00:00
|
|
|
final callBanner = MaterialBanner(
|
|
|
|
padding: const EdgeInsets.only(top: 4, bottom: 4, left: 20),
|
|
|
|
leading: const Icon(Icons.call_received),
|
2024-05-01 11:39:48 +00:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.9),
|
2024-04-26 17:36:54 +00:00
|
|
|
dividerColor: const Color.fromARGB(1, 0, 0, 0),
|
|
|
|
content: Text(AppLocalizations.of(context)!.chatCallOngoing),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(AppLocalizations.of(context)!.chatCallJoin),
|
|
|
|
onPressed: () {
|
|
|
|
router.pushNamed(
|
|
|
|
'chat.channel.call',
|
2024-05-01 16:49:38 +00:00
|
|
|
extra: _chat.ongoingCall,
|
2024-04-26 17:36:54 +00:00
|
|
|
pathParameters: {'channel': widget.alias},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
return FutureBuilder(
|
2024-05-02 12:45:24 +00:00
|
|
|
future: (() async {
|
|
|
|
final res = await Future.wait([
|
|
|
|
_chat.fetchOngoingCall(widget.alias),
|
|
|
|
_chat.fetchChannel(widget.alias),
|
|
|
|
]);
|
|
|
|
return res[0] as Channel;
|
|
|
|
})(),
|
2024-05-01 16:49:38 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData || snapshot.data == null) {
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChatMaintainer(
|
|
|
|
channel: snapshot.data!,
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: PagedListView<int, Message>(
|
|
|
|
reverse: true,
|
|
|
|
pagingController: _pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<Message>(
|
|
|
|
animateTransitions: true,
|
|
|
|
transitionDuration: 500.ms,
|
|
|
|
itemBuilder: chatHistoryBuilder,
|
|
|
|
noItemsFoundIndicatorBuilder: (_) => Container(),
|
2024-04-26 17:36:54 +00:00
|
|
|
),
|
2024-04-26 15:25:56 +00:00
|
|
|
),
|
2024-05-01 16:49:38 +00:00
|
|
|
),
|
|
|
|
ChatMessageEditor(
|
|
|
|
channel: widget.alias,
|
|
|
|
editing: _editingItem,
|
|
|
|
replying: _replyingItem,
|
|
|
|
onReset: () => setState(() {
|
|
|
|
_editingItem = null;
|
|
|
|
_replyingItem = null;
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
_chat.ongoingCall != null ? callBanner.animate().slideY() : Container(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
onInsertMessage: (message) => addMessage(message),
|
|
|
|
onUpdateMessage: (message) => updateMessage(message),
|
|
|
|
onDeleteMessage: (message) => deleteMessage(message),
|
|
|
|
onCallStarted: (call) => _chat.setOngoingCall(call),
|
|
|
|
onCallEnded: () => _chat.setOngoingCall(null),
|
|
|
|
);
|
|
|
|
},
|
2024-04-17 15:00:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|