DM message last preview

This commit is contained in:
LittleSheep 2024-08-02 00:54:19 +08:00
parent c6b2ef8459
commit a0a3a8d182
2 changed files with 54 additions and 22 deletions

View File

@ -16,7 +16,7 @@ class ChatEventController {
Channel? channel; Channel? channel;
String? scope; String? scope;
initialize() async { Future<void> initialize() async {
if (!PlatformInfo.isWeb) { if (!PlatformInfo.isWeb) {
database = await createHistoryDb(); database = await createHistoryDb();
} }

View File

@ -1,7 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/controllers/chat_events_controller.dart';
import 'package:solian/models/channel.dart'; import 'package:solian/models/channel.dart';
import 'package:solian/platform.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:solian/widgets/account/account_avatar.dart'; import 'package:solian/widgets/account/account_avatar.dart';
@ -31,7 +33,9 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
final List<Channel> _globalChannels = List.empty(growable: true); final List<Channel> _globalChannels = List.empty(growable: true);
final Map<String, List<Channel>> _inRealms = {}; final Map<String, List<Channel>> _inRealms = {};
void mapChannels() { final ChatEventController _eventController = ChatEventController();
void _mapChannels() {
_inRealms.clear(); _inRealms.clear();
_globalChannels.clear(); _globalChannels.clear();
@ -55,16 +59,17 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
@override @override
void didUpdateWidget(covariant ChannelListWidget oldWidget) { void didUpdateWidget(covariant ChannelListWidget oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
setState(() => mapChannels()); setState(() => _mapChannels());
} }
@override @override
void initState() { void initState() {
super.initState(); super.initState();
mapChannels(); _mapChannels();
_eventController.initialize();
} }
void gotoChannel(Channel item) { void _gotoChannel(Channel item) {
if (widget.useReplace) { if (widget.useReplace) {
AppRouter.instance.pushReplacementNamed( AppRouter.instance.pushReplacementNamed(
'channelChat', 'channelChat',
@ -88,7 +93,35 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
} }
} }
Widget buildItem(Channel item) { Widget _buildDirectMessageDescription(Channel item, ChannelMember otherside) {
if (PlatformInfo.isWeb) {
return Text('channelDirectDescription'.trParams(
{'username': '@${otherside.account.name}'},
));
}
return FutureBuilder(
future: Future.delayed(
const Duration(milliseconds: 500),
() => _eventController.database.localEvents.findLastByChannel(item.id),
),
builder: (context, snapshot) {
if (!snapshot.hasData && snapshot.data == null) {
return Text('channelDirectDescription'.trParams(
{'username': '@${otherside.account.name}'},
));
}
return Text(
'${snapshot.data!.data.sender.account.nick}: ${snapshot.data!.data.body['text'] ?? 'Unsupported message to preview'}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
);
},
);
}
Widget _buildEntry(Channel item) {
final padding = widget.isDense final padding = widget.isDense
? const EdgeInsets.symmetric(horizontal: 20) ? const EdgeInsets.symmetric(horizontal: 20)
: const EdgeInsets.symmetric(horizontal: 16); : const EdgeInsets.symmetric(horizontal: 16);
@ -102,37 +135,36 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
leading: AccountAvatar( leading: AccountAvatar(
content: otherside.account.avatar, content: otherside.account.avatar,
radius: widget.isDense ? 12 : 20, radius: widget.isDense ? 12 : 20,
bgColor: Colors.indigo, bgColor: Theme.of(context).colorScheme.primary,
feColor: Colors.white, feColor: Theme.of(context).colorScheme.onPrimary,
), ),
contentPadding: padding, contentPadding: padding,
title: Text(otherside.account.nick), title: Text(otherside.account.nick),
subtitle: !widget.isDense subtitle: !widget.isDense
? Text( ? _buildDirectMessageDescription(item, otherside)
'channelDirectDescription'.trParams(
{'username': '@${otherside.account.name}'},
),
)
: null, : null,
onTap: () => gotoChannel(item), onTap: () => _gotoChannel(item),
); );
} else { } else {
return ListTile( return ListTile(
minTileHeight: widget.isDense ? 48 : null, minTileHeight: widget.isDense ? 48 : null,
leading: CircleAvatar( leading: CircleAvatar(
backgroundColor: backgroundColor: item.realmId == null
item.realmId == null ? Colors.indigo : Colors.transparent, ? Theme.of(context).colorScheme.primary
: Colors.transparent,
radius: widget.isDense ? 12 : 20, radius: widget.isDense ? 12 : 20,
child: FaIcon( child: FaIcon(
FontAwesomeIcons.hashtag, FontAwesomeIcons.hashtag,
color: item.realmId == null ? Colors.white : Colors.indigo, color: item.realmId == null
? Theme.of(context).colorScheme.onPrimary
: Theme.of(context).colorScheme.primary,
size: widget.isDense ? 12 : 16, size: widget.isDense ? 12 : 16,
), ),
), ),
contentPadding: padding, contentPadding: padding,
title: Text(item.name), title: Text(item.name),
subtitle: !widget.isDense ? Text(item.description) : null, subtitle: !widget.isDense ? Text(item.description) : null,
onTap: () => gotoChannel(item), onTap: () => _gotoChannel(item),
); );
} }
} }
@ -146,7 +178,7 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
itemCount: _globalChannels.length, itemCount: _globalChannels.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final element = _globalChannels[index]; final element = _globalChannels[index];
return buildItem(element); return _buildEntry(element);
}, },
), ),
], ],
@ -159,13 +191,13 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
itemCount: _globalChannels.length, itemCount: _globalChannels.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final element = _globalChannels[index]; final element = _globalChannels[index];
return buildItem(element); return _buildEntry(element);
}, },
), ),
SliverList.list( SliverList.list(
children: _inRealms.entries.map((element) { children: _inRealms.entries.map((element) {
return ExpansionTile( return ExpansionTile(
tilePadding: const EdgeInsets.symmetric(horizontal: 20), tilePadding: const EdgeInsets.only(left: 20, right: 24),
minTileHeight: 48, minTileHeight: 48,
title: Text(element.value.first.realm!.name), title: Text(element.value.first.realm!.name),
leading: CircleAvatar( leading: CircleAvatar(
@ -177,7 +209,7 @@ class _ChannelListWidgetState extends State<ChannelListWidget> {
size: widget.isDense ? 12 : 16, size: widget.isDense ? 12 : 16,
), ),
), ),
children: element.value.map((x) => buildItem(x)).toList(), children: element.value.map((x) => _buildEntry(x)).toList(),
); );
}).toList(), }).toList(),
), ),