Handle more chat server events

This commit is contained in:
LittleSheep 2024-04-18 22:43:14 +08:00
parent 94f9076abb
commit 1a1a3a5155
4 changed files with 63 additions and 17 deletions

View File

@ -28,6 +28,7 @@
"takeVideo": "Record video",
"newMoment": "Record a moment",
"newComment": "Leave a comment",
"connectingServer": "Connecting to server...",
"postIdentityNotify": "You will create this post as",
"postContentPlaceholder": "What's happened?!",
"postDeleteConfirm": "Are you sure you want to delete this post? This operation cannot be revert!",

View File

@ -28,6 +28,7 @@
"takeVideo": "拍摄视频",
"newMoment": "记录时刻",
"newComment": "留下评论",
"connectingServer": "正在连接至服务器……",
"postIdentityNotify": "你将会以该身份发表本帖子",
"postContentPlaceholder": "发生什么事了?!",
"postDeleteConfirm": "你确定要删除这篇帖子吗?这意味着这个帖子将永远被我们丢弃在硬盘海中!该操作不可被反转!",

View File

@ -79,10 +79,20 @@ class _ChatScreenState extends State<ChatScreen> {
}
void addMessage(Message item) {
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_pagingController.itemList?.insert(0, item);
});
setState(() {
_pagingController.itemList?.insert(0, item);
});
}
void updateMessage(Message item) {
setState(() {
_pagingController.itemList = _pagingController.itemList?.map((x) => x.id == item.id ? item : x).toList();
});
}
void deleteMessage(Message item) {
setState(() {
_pagingController.itemList = _pagingController.itemList?.where((x) => x.id != item.id).toList();
});
}
@ -138,7 +148,9 @@ class _ChatScreenState extends State<ChatScreen> {
ChatMessageEditor(channel: widget.alias),
],
),
onNewMessage: (message) => addMessage(message),
onInsertMessage: (message) => addMessage(message),
onUpdateMessage: (message) => updateMessage(message),
onDeleteMessage: (message) => deleteMessage(message),
),
);
}

View File

@ -6,33 +6,65 @@ import 'package:solian/models/message.dart';
import 'package:solian/models/packet.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/chat.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class ChatMaintainer extends StatefulWidget {
final Widget child;
final Function(Message val) onNewMessage;
final Function(Message val) onInsertMessage;
final Function(Message val) onUpdateMessage;
final Function(Message val) onDeleteMessage;
const ChatMaintainer({super.key, required this.child, required this.onNewMessage});
const ChatMaintainer({
super.key,
required this.child,
required this.onInsertMessage,
required this.onUpdateMessage,
required this.onDeleteMessage,
});
@override
State<ChatMaintainer> createState() => _ChatMaintainerState();
}
class _ChatMaintainerState extends State<ChatMaintainer> {
@override
void initState() {
Future.delayed(Duration.zero, () {
final auth = context.read<AuthProvider>();
final chat = context.read<ChatProvider>();
void connect() {
final notify = ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.connectingServer),
duration: const Duration(days: 1),
),
);
chat.connect(auth).then((snapshot) {
snapshot!.stream.listen((event) {
final auth = context.read<AuthProvider>();
final chat = context.read<ChatProvider>();
chat.connect(auth).then((snapshot) {
snapshot!.stream.listen(
(event) {
final result = NetworkPackage.fromJson(jsonDecode(event));
switch (result.method) {
case 'messages.new':
widget.onNewMessage(Message.fromJson(result.payload!));
widget.onInsertMessage(Message.fromJson(result.payload!));
break;
case 'messages.update':
widget.onUpdateMessage(Message.fromJson(result.payload!));
break;
case 'messages.burnt':
widget.onDeleteMessage(Message.fromJson(result.payload!));
break;
}
});
});
},
onError: (_, __) => connect(),
);
notify.close();
});
}
@override
void initState() {
Future.delayed(Duration.zero, () {
connect();
});
super.initState();