Compare commits

..

2 Commits

Author SHA1 Message Date
b808c76ea3 Optimized chat messages 2024-07-06 17:12:57 +08:00
20a82da2fa Basic optimization of repainting 2024-07-05 23:37:54 +08:00
11 changed files with 379 additions and 328 deletions

View File

@@ -2,7 +2,7 @@ import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/event.dart';
import 'package:solian/platform.dart';
import 'package:solian/providers/message/helper.dart';
import 'package:solian/providers/message/adaptor.dart';
import 'package:solian/providers/message/events.dart';
class ChatEventController {
@@ -57,11 +57,13 @@ class ChatEventController {
totalEvents.value = result?.$2 ?? 0;
if (result != null) {
for (final x in result.$1.reversed) {
applyEvent(LocalEvent(x.id, x, x.channelId, x.createdAt));
final entry = LocalEvent(x.id, x, x.channelId, x.createdAt);
insertEvent(entry);
applyEvent(entry);
}
}
} else {
final result = await database.syncEvents(
final result = await database.syncRemoteEvents(
channel,
scope: scope,
);
@@ -80,14 +82,16 @@ class ChatEventController {
remainDepth: 3,
offset: currentEvents.length,
);
totalEvents.value = result?.$2 ?? 0;
if (result != null) {
totalEvents.value = result.$2;
for (final x in result.$1.reversed) {
applyEvent(LocalEvent(x.id, x, x.channelId, x.createdAt));
final entry = LocalEvent(x.id, x, x.channelId, x.createdAt);
currentEvents.add(entry);
applyEvent(entry);
}
}
} else {
final result = await database.syncEvents(
final result = await database.syncRemoteEvents(
channel,
depth: 3,
scope: scope,
@@ -102,6 +106,7 @@ class ChatEventController {
Future<bool> syncLocal(Channel channel) async {
if (PlatformInfo.isWeb) return false;
final data = await database.localEvents.findAllByChannel(channel.id);
currentEvents.replaceRange(0, currentEvents.length, data);
for (final x in data.reversed) {
applyEvent(x);
}
@@ -121,18 +126,21 @@ class ChatEventController {
entry = await database.receiveEvent(remote);
}
insertEvent(entry);
applyEvent(entry);
}
applyEvent(LocalEvent entry) {
if (entry.channelId != channel?.id) return;
insertEvent(LocalEvent entry) {
final idx = currentEvents.indexWhere((x) => x.data.uuid == entry.data.uuid);
if (idx != -1) {
currentEvents[idx] = entry;
} else {
currentEvents.insert(0, entry);
}
}
applyEvent(LocalEvent entry) {
if (entry.channelId != channel?.id) return;
switch (entry.data.type) {
case 'messages.edit':

View File

@@ -33,32 +33,39 @@ void main() async {
appRunner: () async {
WidgetsFlutterBinding.ensureInitialized();
if (!PlatformInfo.isWeb) {
await protocolHandler.register('solink');
}
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
if (PlatformInfo.isDesktop) {
await Window.initialize();
if (PlatformInfo.isMacOS) {
await Window.hideTitle();
await Window.hideCloseButton();
await Window.hideMiniaturizeButton();
await Window.hideZoomButton();
await Window.makeTitlebarTransparent();
await Window.enableFullSizeContentView();
}
}
_initializeFirebase();
_initializePlatformComponents();
runApp(const SolianApp());
},
);
}
Future<void> _initializeFirebase() async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
}
Future<void> _initializePlatformComponents() async {
if (!PlatformInfo.isWeb) {
await protocolHandler.register('solink');
}
if (PlatformInfo.isDesktop) {
await Window.initialize();
if (PlatformInfo.isMacOS) {
await Future.wait([
Window.hideTitle(),
Window.hideCloseButton(),
Window.hideMiniaturizeButton(),
Window.hideZoomButton(),
Window.makeTitlebarTransparent(),
Window.enableFullSizeContentView(),
]);
}
}
}
class SolianApp extends StatelessWidget {
const SolianApp({super.key});
@@ -75,33 +82,7 @@ class SolianApp extends StatelessWidget {
translations: SolianMessages(),
locale: Get.deviceLocale,
fallbackLocale: const Locale('en', 'US'),
onInit: () {
Get.lazyPut(() => AuthProvider());
Get.lazyPut(() => FriendProvider());
Get.lazyPut(() => PostProvider());
Get.lazyPut(() => AttachmentProvider());
Get.lazyPut(() => ChatProvider());
Get.lazyPut(() => AccountProvider());
Get.lazyPut(() => StatusProvider());
Get.lazyPut(() => ChannelProvider());
Get.lazyPut(() => RealmProvider());
Get.lazyPut(() => ChatCallProvider());
final AuthProvider auth = Get.find();
auth.isAuthorized.then((value) async {
if (value) {
Get.find<AccountProvider>().connect();
Get.find<ChatProvider>().connect();
try {
Get.find<AccountProvider>().registerPushNotifications();
} catch (err) {
context.showSnackbar('pushNotifyRegisterFailed'
.trParams({'reason': err.toString()}));
}
}
});
},
onInit: () => _initializeProviders(context),
builder: (context, child) {
return ListenerShell(
child: ScaffoldMessenger(
@@ -111,4 +92,33 @@ class SolianApp extends StatelessWidget {
},
);
}
void _initializeProviders(BuildContext context) {
Get.lazyPut(() => AuthProvider());
Get.lazyPut(() => FriendProvider());
Get.lazyPut(() => PostProvider());
Get.lazyPut(() => AttachmentProvider());
Get.lazyPut(() => ChatProvider());
Get.lazyPut(() => AccountProvider());
Get.lazyPut(() => StatusProvider());
Get.lazyPut(() => ChannelProvider());
Get.lazyPut(() => RealmProvider());
Get.lazyPut(() => ChatCallProvider());
final AuthProvider auth = Get.find();
auth.isAuthorized.then((value) async {
if (value) {
Get.find<AccountProvider>().connect();
Get.find<ChatProvider>().connect();
try {
Get.find<AccountProvider>().registerPushNotifications();
} catch (err) {
context.showSnackbar(
'pushNotifyRegisterFailed'.trParams({'reason': err.toString()}),
);
}
}
});
}
}

View File

@@ -81,7 +81,7 @@ Future<(List<Event>, int)?> getRemoteEvents(
return ([...result, ...expandResult], response.count);
}
extension MessageHistoryHelper on MessageHistoryDb {
extension MessageHistoryAdaptor on MessageHistoryDb {
Future<LocalEvent> receiveEvent(Event remote) async {
final entry = LocalEvent(
remote.id,
@@ -121,7 +121,7 @@ extension MessageHistoryHelper on MessageHistoryDb {
return await receiveEvent(remoteRecord);
}
Future<(List<Event>, int)?> syncEvents(Channel channel,
Future<(List<Event>, int)?> syncRemoteEvents(Channel channel,
{String scope = 'global', depth = 10, offset = 0}) async {
final lastOne = await localEvents.findLastByChannel(channel.id);
@@ -145,7 +145,7 @@ extension MessageHistoryHelper on MessageHistoryDb {
return data;
}
Future<List<LocalEvent>> listMessages(Channel channel) async {
Future<List<LocalEvent>> listEvents(Channel channel) async {
return await localEvents.findAllByChannel(channel.id);
}
}

View File

@@ -186,13 +186,14 @@ class _FriendScreenState extends State<FriendScreen> {
showScopedListPopup('accountFriendBlocked'.tr, 2),
),
),
SliverFriendList(
accountId: _accountId!,
items: filterWithStatus(1),
onUpdate: () {
getFriendship();
},
),
if (_accountId != null)
SliverFriendList(
accountId: _accountId!,
items: filterWithStatus(1),
onUpdate: () {
getFriendship();
},
),
const SliverToBoxAdapter(
child: Divider(thickness: 0.3, height: 0.3),
),

View File

@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/exts.dart';
import 'package:solian/router.dart';
import 'package:solian/services.dart';
class SignUpPopup extends StatefulWidget {

View File

@@ -21,7 +21,7 @@ import 'package:solian/widgets/app_bar_title.dart';
import 'package:solian/widgets/chat/call/call_prejoin.dart';
import 'package:solian/widgets/chat/call/chat_call_action.dart';
import 'package:solian/widgets/chat/chat_event.dart';
import 'package:solian/widgets/chat/chat_event_action.dart';
import 'package:solian/widgets/chat/chat_event_list.dart';
import 'package:solian/widgets/chat/chat_message_input.dart';
import 'package:solian/widgets/current_state_action.dart';
@@ -111,7 +111,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
switch (event.method) {
case 'events.new':
final payload = Event.fromJson(event.payload!);
_chatController.receiveEvent(payload);
_chatController.receiveEvent(payload);
break;
case 'calls.new':
final payload = Call.fromJson(event.payload!);
@@ -124,12 +124,6 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
});
}
bool checkMessageMergeable(Event? a, Event? b) {
if (a == null || b == null) return false;
if (a.sender.account.id != b.sender.account.id) return false;
return a.createdAt.difference(b.createdAt).inMinutes <= 3;
}
void showCallPrejoin() {
showModalBottomSheet(
useRootNavigator: true,
@@ -153,50 +147,6 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
);
}
Widget buildHistory(context, index) {
bool isMerged = false, hasMerged = false;
if (index > 0) {
hasMerged = checkMessageMergeable(
_chatController.currentEvents[index - 1].data,
_chatController.currentEvents[index].data,
);
}
if (index + 1 < _chatController.currentEvents.length) {
isMerged = checkMessageMergeable(
_chatController.currentEvents[index].data,
_chatController.currentEvents[index + 1].data,
);
}
final item = _chatController.currentEvents[index].data;
return InkWell(
child: Container(
child: buildHistoryBody(item, isMerged: isMerged).paddingOnly(
top: !isMerged ? 8 : 0,
bottom: !hasMerged ? 8 : 0,
),
),
onLongPress: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => ChatEventAction(
channel: _channel!,
realm: _channel!.realm,
item: item,
onEdit: () {
setState(() => _messageToEditing = item);
},
onReply: () {
setState(() => _messageToReplying = item);
},
),
);
},
);
}
@override
void initState() {
_chatController = ChatEventController();
@@ -283,120 +233,81 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
),
],
),
body: Stack(
body: Column(
children: [
Column(
children: [
Expanded(
child: CustomScrollView(
reverse: true,
slivers: [
Obx(() {
return SliverList.builder(
key: Key('chat-history#${_channel!.id}'),
itemCount: _chatController.currentEvents.length,
itemBuilder: buildHistory,
);
}),
Obx(() {
final amount = _chatController.totalEvents -
_chatController.currentEvents.length;
if (amount.value <= 0 ||
_chatController.isLoading.isTrue) {
return const SliverToBoxAdapter(child: SizedBox());
}
return SliverToBoxAdapter(
child: ListTile(
tileColor:
Theme.of(context).colorScheme.surfaceContainerLow,
leading: const Icon(Icons.sync_disabled),
title: Text('messageUnsync'.tr),
subtitle: Text('messageUnsyncCaption'.trParams({
'count': amount.string,
})),
onTap: () {
_chatController.loadEvents(
_channel!,
widget.realm,
);
},
),
);
}),
Obx(() {
if (_chatController.isLoading.isFalse) {
return const SliverToBoxAdapter(child: SizedBox());
}
return SliverToBoxAdapter(
child: const LinearProgressIndicator().animate().slideY(),
);
}),
],
),
),
ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 50, sigmaY: 50),
child: SafeArea(
child: ChatMessageInput(
edit: _messageToEditing,
reply: _messageToReplying,
realm: widget.realm,
placeholder: placeholder,
channel: _channel!,
onSent: (Event item) {
setState(() {
_chatController.addPendingEvent(item);
});
},
onReset: () {
setState(() {
_messageToReplying = null;
_messageToEditing = null;
});
},
),
),
),
),
],
),
if (_ongoingCall != null)
Positioned(
top: 0,
left: 0,
right: 0,
child: MaterialBanner(
padding: const EdgeInsets.only(left: 16, top: 4, bottom: 4),
leading: const Icon(Icons.call_received),
backgroundColor: Theme.of(context).colorScheme.surfaceContainer,
dividerColor: Colors.transparent,
content: Text('callOngoing'.tr),
actions: [
Obx(() {
if (call.current.value == null) {
return TextButton(
onPressed: showCallPrejoin,
child: Text('callJoin'.tr),
);
} else if (call.channel.value?.id == _channel?.id) {
return TextButton(
onPressed: () => call.gotoScreen(context),
child: Text('callResume'.tr),
);
} else {
return TextButton(
onPressed: null,
child: Text('callJoin'.tr),
);
}
})
],
MaterialBanner(
padding: const EdgeInsets.only(left: 16, top: 4, bottom: 4),
leading: const Icon(Icons.call_received),
backgroundColor: Theme.of(context).colorScheme.surfaceContainer,
dividerColor: Colors.transparent,
content: Text('callOngoing'.tr),
actions: [
Obx(() {
if (call.current.value == null) {
return TextButton(
onPressed: showCallPrejoin,
child: Text('callJoin'.tr),
);
} else if (call.channel.value?.id == _channel?.id) {
return TextButton(
onPressed: () => call.gotoScreen(context),
child: Text('callResume'.tr),
);
} else {
return TextButton(
onPressed: null,
child: Text('callJoin'.tr),
);
}
})
],
),
Expanded(
child: ChatEventList(
scope: widget.realm,
channel: _channel!,
chatController: _chatController,
onEdit: (item) {
setState(() => _messageToEditing = item);
},
onReply: (item) {
setState(() => _messageToReplying = item);
},
),
),
Obx(() {
if (_chatController.isLoading.isTrue) {
return const LinearProgressIndicator().animate().slideY();
} else {
return const SizedBox();
}
}),
ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 50, sigmaY: 50),
child: SafeArea(
child: ChatMessageInput(
edit: _messageToEditing,
reply: _messageToReplying,
realm: widget.realm,
placeholder: placeholder,
channel: _channel!,
onSent: (Event item) {
setState(() {
_chatController.addPendingEvent(item);
});
},
onReset: () {
setState(() {
_messageToReplying = null;
_messageToEditing = null;
});
},
),
),
),
),
],
),
);

View File

@@ -178,8 +178,8 @@ class SolianMessages extends Translations {
'channelNotifyLevelNone': 'Ignore all',
'channelNotifyLevelApplied':
'Your notification settings has been applied.',
'messageUnsync': 'Messages Un-synced',
'messageUnsyncCaption': '@count message(s) still in un-synced.',
'messageUnSync': 'Messages Un-synced',
'messageUnSyncCaption': '@count message(s) still in un-synced.',
'messageSending': 'Sending...',
'messageEditDesc': 'Edited message @id',
'messageDeleteDesc': 'Deleted message @id',
@@ -408,8 +408,8 @@ class SolianMessages extends Translations {
'channelNotifyLevelMentioned': '仅提及',
'channelNotifyLevelNone': '忽略一切',
'channelNotifyLevelApplied': '你的通知设置已经应用。',
'messageUnsync': '消息未同步',
'messageUnsyncCaption': '还有 @count 条消息未同步',
'messageUnSync': '消息未同步',
'messageUnSyncCaption': '还有 @count 条消息未同步',
'messageSending': '消息发送中…',
'messageEditDesc': '修改了消息 @id',
'messageDeleteDesc': '删除了消息 @id',

View File

@@ -46,7 +46,9 @@ class _AttachmentItemState extends State<AttachmentItem> {
_chewieController = ChewieController(
aspectRatio: widget.item.metadata?['ratio'] ?? 16 / 9,
videoPlayerController: _videoPlayerController!,
customControls: const MaterialControls(showPlayButton: true),
customControls: PlatformInfo.isMobile
? const MaterialControls()
: const MaterialDesktopControls(),
materialProgressColors: ChewieProgressColors(
playedColor: Theme.of(context).colorScheme.primary,
handleColor: Theme.of(context).colorScheme.primary,

View File

@@ -102,79 +102,81 @@ class _AttachmentListState extends State<AttachmentList> {
}
Widget buildEntry(Attachment element, int idx) {
return GestureDetector(
child: Container(
width: widget.width ?? MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHigh,
),
child: Stack(
fit: StackFit.expand,
children: [
AttachmentItem(
parentId: widget.parentId,
key: Key('a${element.uuid}'),
item: element,
badge: _attachmentsMeta.length > 1
? '${idx + 1}/${_attachmentsMeta.length}'
: null,
showHideButton: !element.isMature || _showMature,
onHide: () {
setState(() => _showMature = false);
},
),
if (element.isMature && !_showMature)
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 100, sigmaY: 100),
child: Container(
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.5),
),
),
),
if (element.isMature && !_showMature)
Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 280),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.visibility_off,
color: Colors.white, size: 32),
const SizedBox(height: 8),
Text(
'matureContent'.tr,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16),
),
Text(
'matureContentCaption'.tr,
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
],
),
),
),
],
),
),
onTap: () {
if (!_showMature && _attachmentsMeta.any((e) => e!.isMature)) {
setState(() => _showMature = true);
} else if (['image'].contains(element.mimetype.split('/').first)) {
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(
builder: (context) => AttachmentListFullScreen(
return RepaintBoundary(
child: GestureDetector(
child: Container(
width: widget.width ?? MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHigh,
),
child: Stack(
fit: StackFit.expand,
children: [
AttachmentItem(
parentId: widget.parentId,
attachment: element,
key: Key('a${element.uuid}'),
item: element,
badge: _attachmentsMeta.length > 1
? '${idx + 1}/${_attachmentsMeta.length}'
: null,
showHideButton: !element.isMature || _showMature,
onHide: () {
setState(() => _showMature = false);
},
),
),
);
}
},
if (element.isMature && !_showMature)
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 100, sigmaY: 100),
child: Container(
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.5),
),
),
),
if (element.isMature && !_showMature)
Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 280),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.visibility_off,
color: Colors.white, size: 32),
const SizedBox(height: 8),
Text(
'matureContent'.tr,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16),
),
Text(
'matureContentCaption'.tr,
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
],
),
),
),
],
),
),
onTap: () {
if (!_showMature && _attachmentsMeta.any((e) => e!.isMature)) {
setState(() => _showMature = true);
} else if (['image'].contains(element.mimetype.split('/').first)) {
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(
builder: (context) => AttachmentListFullScreen(
parentId: widget.parentId,
attachment: element,
),
),
);
}
},
),
);
}

View File

@@ -0,0 +1,116 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/controllers/chat_events_controller.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/event.dart';
import 'package:solian/widgets/chat/chat_event.dart';
import 'package:solian/widgets/chat/chat_event_action.dart';
class ChatEventList extends StatelessWidget {
final String scope;
final Channel channel;
final ChatEventController chatController;
final Function(Event) onEdit;
final Function(Event) onReply;
const ChatEventList({
super.key,
this.scope = 'global',
required this.channel,
required this.chatController,
required this.onEdit,
required this.onReply,
});
bool checkMessageMergeable(Event? a, Event? b) {
if (a == null || b == null) return false;
if (a.sender.account.id != b.sender.account.id) return false;
return a.createdAt.difference(b.createdAt).inMinutes <= 3;
}
@override
Widget build(BuildContext context) {
return CustomScrollView(
reverse: true,
slivers: [
Obx(() {
return SliverList.builder(
key: Key('chat-history#${channel.id}'),
itemCount: chatController.currentEvents.length,
itemBuilder: (context, index) {
bool isMerged = false, hasMerged = false;
if (index > 0) {
hasMerged = checkMessageMergeable(
chatController.currentEvents[index - 1].data,
chatController.currentEvents[index].data,
);
}
if (index + 1 < chatController.currentEvents.length) {
isMerged = checkMessageMergeable(
chatController.currentEvents[index].data,
chatController.currentEvents[index + 1].data,
);
}
final item = chatController.currentEvents[index].data;
return InkWell(
child: RepaintBoundary(
child: ChatEvent(
key: Key('m${item.uuid}'),
item: item,
isMerged: isMerged,
chatController: chatController,
).paddingOnly(
top: !isMerged ? 8 : 0,
bottom: !hasMerged ? 8 : 0,
),
),
onLongPress: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => ChatEventAction(
channel: channel,
realm: channel.realm,
item: item,
onEdit: () {
onEdit(item);
},
onReply: () {
onReply(item);
},
),
);
},
);
},
);
}),
Obx(() {
final amount =
chatController.totalEvents - chatController.currentEvents.length;
if (amount.value <= 0 || chatController.isLoading.isTrue) {
return const SliverToBoxAdapter(child: SizedBox());
}
return SliverToBoxAdapter(
child: ListTile(
tileColor: Theme.of(context).colorScheme.surfaceContainerLow,
leading: const Icon(Icons.sync_disabled),
title: Text('messageUnSync'.tr),
subtitle: Text('messageUnSyncCaption'.trParams({
'count': amount.string,
})),
onTap: () {
chatController.loadEvents(channel, scope);
},
),
);
}),
],
);
}
}

View File

@@ -26,29 +26,31 @@ class PostListWidget extends StatelessWidget {
pagingController: controller,
builderDelegate: PagedChildBuilderDelegate<Post>(
itemBuilder: (context, item, index) {
return GestureDetector(
child: PostItem(
key: Key('p${item.alias}'),
item: item,
isShowEmbed: isShowEmbed,
isClickable: isNestedClickable,
).paddingSymmetric(vertical: 8),
onTap: () {
if (!isClickable) return;
AppRouter.instance.pushNamed(
'postDetail',
pathParameters: {'alias': item.alias},
);
},
onLongPress: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => PostAction(item: item),
).then((value) {
if (value != null) controller.refresh();
});
},
return RepaintBoundary(
child: GestureDetector(
child: PostItem(
key: Key('p${item.alias}'),
item: item,
isShowEmbed: isShowEmbed,
isClickable: isNestedClickable,
).paddingSymmetric(vertical: 8),
onTap: () {
if (!isClickable) return;
AppRouter.instance.pushNamed(
'postDetail',
pathParameters: {'alias': item.alias},
);
},
onLongPress: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => PostAction(item: item),
).then((value) {
if (value != null) controller.refresh();
});
},
),
);
},
),