✨ Handle more chat server events
This commit is contained in:
parent
94f9076abb
commit
1a1a3a5155
@ -28,6 +28,7 @@
|
|||||||
"takeVideo": "Record video",
|
"takeVideo": "Record video",
|
||||||
"newMoment": "Record a moment",
|
"newMoment": "Record a moment",
|
||||||
"newComment": "Leave a comment",
|
"newComment": "Leave a comment",
|
||||||
|
"connectingServer": "Connecting to server...",
|
||||||
"postIdentityNotify": "You will create this post as",
|
"postIdentityNotify": "You will create this post as",
|
||||||
"postContentPlaceholder": "What's happened?!",
|
"postContentPlaceholder": "What's happened?!",
|
||||||
"postDeleteConfirm": "Are you sure you want to delete this post? This operation cannot be revert!",
|
"postDeleteConfirm": "Are you sure you want to delete this post? This operation cannot be revert!",
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
"takeVideo": "拍摄视频",
|
"takeVideo": "拍摄视频",
|
||||||
"newMoment": "记录时刻",
|
"newMoment": "记录时刻",
|
||||||
"newComment": "留下评论",
|
"newComment": "留下评论",
|
||||||
|
"connectingServer": "正在连接至服务器……",
|
||||||
"postIdentityNotify": "你将会以该身份发表本帖子",
|
"postIdentityNotify": "你将会以该身份发表本帖子",
|
||||||
"postContentPlaceholder": "发生什么事了?!",
|
"postContentPlaceholder": "发生什么事了?!",
|
||||||
"postDeleteConfirm": "你确定要删除这篇帖子吗?这意味着这个帖子将永远被我们丢弃在硬盘海中!该操作不可被反转!",
|
"postDeleteConfirm": "你确定要删除这篇帖子吗?这意味着这个帖子将永远被我们丢弃在硬盘海中!该操作不可被反转!",
|
||||||
|
@ -79,10 +79,20 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addMessage(Message item) {
|
void addMessage(Message item) {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_pagingController.itemList?.insert(0, item);
|
_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),
|
ChatMessageEditor(channel: widget.alias),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
onNewMessage: (message) => addMessage(message),
|
onInsertMessage: (message) => addMessage(message),
|
||||||
|
onUpdateMessage: (message) => updateMessage(message),
|
||||||
|
onDeleteMessage: (message) => deleteMessage(message),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -6,33 +6,65 @@ import 'package:solian/models/message.dart';
|
|||||||
import 'package:solian/models/packet.dart';
|
import 'package:solian/models/packet.dart';
|
||||||
import 'package:solian/providers/auth.dart';
|
import 'package:solian/providers/auth.dart';
|
||||||
import 'package:solian/providers/chat.dart';
|
import 'package:solian/providers/chat.dart';
|
||||||
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||||
|
|
||||||
class ChatMaintainer extends StatefulWidget {
|
class ChatMaintainer extends StatefulWidget {
|
||||||
final Widget child;
|
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
|
@override
|
||||||
State<ChatMaintainer> createState() => _ChatMaintainerState();
|
State<ChatMaintainer> createState() => _ChatMaintainerState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ChatMaintainerState extends State<ChatMaintainer> {
|
class _ChatMaintainerState extends State<ChatMaintainer> {
|
||||||
@override
|
void connect() {
|
||||||
void initState() {
|
final notify = ScaffoldMessenger.of(context).showSnackBar(
|
||||||
Future.delayed(Duration.zero, () {
|
SnackBar(
|
||||||
|
content: Text(AppLocalizations.of(context)!.connectingServer),
|
||||||
|
duration: const Duration(days: 1),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
final auth = context.read<AuthProvider>();
|
final auth = context.read<AuthProvider>();
|
||||||
final chat = context.read<ChatProvider>();
|
final chat = context.read<ChatProvider>();
|
||||||
|
|
||||||
chat.connect(auth).then((snapshot) {
|
chat.connect(auth).then((snapshot) {
|
||||||
snapshot!.stream.listen((event) {
|
snapshot!.stream.listen(
|
||||||
|
(event) {
|
||||||
final result = NetworkPackage.fromJson(jsonDecode(event));
|
final result = NetworkPackage.fromJson(jsonDecode(event));
|
||||||
switch (result.method) {
|
switch (result.method) {
|
||||||
case 'messages.new':
|
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();
|
super.initState();
|
||||||
|
Loading…
Reference in New Issue
Block a user