2024-09-16 13:09:19 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2024-05-29 15:22:24 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
2024-09-16 13:09:19 +00:00
|
|
|
import 'package:gap/gap.dart';
|
2024-05-29 15:22:24 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-01 16:54:19 +00:00
|
|
|
import 'package:solian/controllers/chat_events_controller.dart';
|
2024-05-29 15:22:24 +00:00
|
|
|
import 'package:solian/models/channel.dart';
|
2024-08-01 16:54:19 +00:00
|
|
|
import 'package:solian/platform.dart';
|
2024-05-29 15:22:24 +00:00
|
|
|
import 'package:solian/router.dart';
|
|
|
|
import 'package:solian/widgets/account/account_avatar.dart';
|
|
|
|
|
2024-05-30 14:02:54 +00:00
|
|
|
class ChannelListWidget extends StatefulWidget {
|
2024-05-29 15:22:24 +00:00
|
|
|
final List<Channel> channels;
|
|
|
|
final int selfId;
|
2024-07-12 13:59:16 +00:00
|
|
|
final bool isDense;
|
2024-09-13 12:22:10 +00:00
|
|
|
final bool isCollapsed;
|
2024-05-30 14:02:54 +00:00
|
|
|
final bool noCategory;
|
2024-07-12 13:59:16 +00:00
|
|
|
final bool useReplace;
|
2024-07-12 14:31:45 +00:00
|
|
|
final Function(Channel)? onSelected;
|
2024-05-29 15:22:24 +00:00
|
|
|
|
2024-05-30 14:02:54 +00:00
|
|
|
const ChannelListWidget({
|
|
|
|
super.key,
|
|
|
|
required this.channels,
|
|
|
|
required this.selfId,
|
2024-07-12 13:59:16 +00:00
|
|
|
this.isDense = false,
|
2024-09-13 12:22:10 +00:00
|
|
|
this.isCollapsed = false,
|
2024-05-30 14:02:54 +00:00
|
|
|
this.noCategory = false,
|
2024-07-12 13:59:16 +00:00
|
|
|
this.useReplace = false,
|
2024-07-12 14:31:45 +00:00
|
|
|
this.onSelected,
|
2024-05-30 14:02:54 +00:00
|
|
|
});
|
2024-05-29 15:22:24 +00:00
|
|
|
|
|
|
|
@override
|
2024-05-30 14:02:54 +00:00
|
|
|
State<ChannelListWidget> createState() => _ChannelListWidgetState();
|
|
|
|
}
|
2024-05-29 15:22:24 +00:00
|
|
|
|
2024-05-30 14:02:54 +00:00
|
|
|
class _ChannelListWidgetState extends State<ChannelListWidget> {
|
|
|
|
final List<Channel> _globalChannels = List.empty(growable: true);
|
|
|
|
final Map<String, List<Channel>> _inRealms = {};
|
2024-05-29 15:22:24 +00:00
|
|
|
|
2024-08-01 16:54:19 +00:00
|
|
|
final ChatEventController _eventController = ChatEventController();
|
|
|
|
|
|
|
|
void _mapChannels() {
|
2024-05-30 14:02:54 +00:00
|
|
|
_inRealms.clear();
|
|
|
|
_globalChannels.clear();
|
|
|
|
|
|
|
|
if (widget.noCategory) {
|
|
|
|
_globalChannels.addAll(widget.channels);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final channel in widget.channels) {
|
|
|
|
if (channel.realmId != null) {
|
2024-06-01 16:39:50 +00:00
|
|
|
if (_inRealms[channel.realm!.alias] == null) {
|
|
|
|
_inRealms[channel.realm!.alias] = List.empty(growable: true);
|
2024-05-30 14:02:54 +00:00
|
|
|
}
|
2024-06-01 16:39:50 +00:00
|
|
|
_inRealms[channel.realm!.alias]!.add(channel);
|
2024-05-30 14:02:54 +00:00
|
|
|
} else {
|
|
|
|
_globalChannels.add(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(covariant ChannelListWidget oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2024-08-01 16:54:19 +00:00
|
|
|
setState(() => _mapChannels());
|
2024-05-30 14:02:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 18:25:45 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-08-01 16:54:19 +00:00
|
|
|
_mapChannels();
|
|
|
|
_eventController.initialize();
|
2024-06-22 18:25:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 16:54:19 +00:00
|
|
|
void _gotoChannel(Channel item) {
|
2024-07-12 13:59:16 +00:00
|
|
|
if (widget.useReplace) {
|
|
|
|
AppRouter.instance.pushReplacementNamed(
|
|
|
|
'channelChat',
|
|
|
|
pathParameters: {'alias': item.alias},
|
|
|
|
queryParameters: {
|
|
|
|
if (item.realmId != null) 'realm': item.realm!.alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'channelChat',
|
|
|
|
pathParameters: {'alias': item.alias},
|
|
|
|
queryParameters: {
|
|
|
|
if (item.realmId != null) 'realm': item.realm!.alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2024-07-12 14:31:45 +00:00
|
|
|
|
|
|
|
if (widget.onSelected != null) {
|
|
|
|
widget.onSelected!(item);
|
|
|
|
}
|
2024-07-12 13:59:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 16:54:19 +00:00
|
|
|
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),
|
2024-09-13 16:30:33 +00:00
|
|
|
() => _eventController.src.getLastInChannel(item),
|
2024-08-01 16:54:19 +00:00
|
|
|
),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData && snapshot.data == null) {
|
|
|
|
return Text('channelDirectDescription'.trParams(
|
|
|
|
{'username': '@${otherside.account.name}'},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-09-13 16:30:33 +00:00
|
|
|
final data = snapshot.data!.data!;
|
2024-08-01 16:54:19 +00:00
|
|
|
return Text(
|
2024-09-13 16:30:33 +00:00
|
|
|
'${data.sender.account.nick}: ${data.body['text'] ?? 'Unsupported message to preview'}',
|
2024-08-01 16:54:19 +00:00
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildEntry(Channel item) {
|
2024-07-12 13:59:16 +00:00
|
|
|
final padding = widget.isDense
|
|
|
|
? const EdgeInsets.symmetric(horizontal: 20)
|
2024-07-12 14:31:45 +00:00
|
|
|
: const EdgeInsets.symmetric(horizontal: 16);
|
2024-07-12 13:59:16 +00:00
|
|
|
|
2024-10-04 19:21:53 +00:00
|
|
|
final otherside =
|
|
|
|
item.members!.where((e) => e.account.id != widget.selfId).firstOrNull;
|
2024-05-30 14:02:54 +00:00
|
|
|
|
2024-10-04 19:21:53 +00:00
|
|
|
if (item.type == 1 && otherside != null) {
|
2024-09-13 12:22:10 +00:00
|
|
|
final avatar = AccountAvatar(
|
|
|
|
content: otherside.account.avatar,
|
|
|
|
radius: widget.isDense ? 12 : 20,
|
|
|
|
bgColor: Theme.of(context).colorScheme.primary,
|
|
|
|
feColor: Theme.of(context).colorScheme.onPrimary,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (widget.isCollapsed) {
|
|
|
|
return Tooltip(
|
|
|
|
message: otherside.account.nick,
|
|
|
|
child: InkWell(
|
|
|
|
child: avatar.paddingSymmetric(vertical: 12),
|
|
|
|
onTap: () => _gotoChannel(item),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:02:54 +00:00
|
|
|
return ListTile(
|
2024-09-13 12:22:10 +00:00
|
|
|
leading: avatar,
|
2024-07-12 13:59:16 +00:00
|
|
|
contentPadding: padding,
|
2024-05-30 14:02:54 +00:00
|
|
|
title: Text(otherside.account.nick),
|
2024-07-12 13:59:16 +00:00
|
|
|
subtitle: !widget.isDense
|
2024-08-01 16:54:19 +00:00
|
|
|
? _buildDirectMessageDescription(item, otherside)
|
2024-07-12 13:59:16 +00:00
|
|
|
: null,
|
2024-08-01 16:54:19 +00:00
|
|
|
onTap: () => _gotoChannel(item),
|
2024-07-12 13:59:16 +00:00
|
|
|
);
|
|
|
|
} else {
|
2024-09-13 12:22:10 +00:00
|
|
|
final avatar = CircleAvatar(
|
|
|
|
backgroundColor: item.realmId == null
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Colors.transparent,
|
|
|
|
radius: widget.isDense ? 12 : 20,
|
|
|
|
child: FaIcon(
|
|
|
|
FontAwesomeIcons.hashtag,
|
|
|
|
color: item.realmId == null
|
|
|
|
? Theme.of(context).colorScheme.onPrimary
|
|
|
|
: Theme.of(context).colorScheme.primary,
|
|
|
|
size: widget.isDense ? 12 : 16,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (widget.isCollapsed) {
|
|
|
|
return Tooltip(
|
|
|
|
message: item.name,
|
|
|
|
child: InkWell(
|
|
|
|
child: avatar.paddingSymmetric(vertical: 12),
|
|
|
|
onTap: () => _gotoChannel(item),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-12 13:59:16 +00:00
|
|
|
return ListTile(
|
2024-07-16 11:46:53 +00:00
|
|
|
minTileHeight: widget.isDense ? 48 : null,
|
2024-09-13 12:22:10 +00:00
|
|
|
leading: avatar,
|
2024-07-12 13:59:16 +00:00
|
|
|
contentPadding: padding,
|
|
|
|
title: Text(item.name),
|
2024-09-12 15:55:31 +00:00
|
|
|
subtitle: !widget.isDense
|
|
|
|
? Text(
|
|
|
|
item.description,
|
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
)
|
|
|
|
: null,
|
2024-08-01 16:54:19 +00:00
|
|
|
onTap: () => _gotoChannel(item),
|
2024-05-30 14:02:54 +00:00
|
|
|
);
|
|
|
|
}
|
2024-05-29 15:22:24 +00:00
|
|
|
}
|
2024-05-30 14:02:54 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (widget.noCategory) {
|
2024-07-06 10:17:54 +00:00
|
|
|
return CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
SliverList.builder(
|
|
|
|
itemCount: _globalChannels.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final element = _globalChannels[index];
|
2024-08-01 16:54:19 +00:00
|
|
|
return _buildEntry(element);
|
2024-07-06 10:17:54 +00:00
|
|
|
},
|
|
|
|
),
|
2024-09-16 13:09:19 +00:00
|
|
|
SliverGap(max(16, MediaQuery.of(context).padding.bottom)),
|
2024-07-06 10:17:54 +00:00
|
|
|
],
|
2024-05-30 14:02:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-06 10:17:54 +00:00
|
|
|
return CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
SliverList.builder(
|
|
|
|
itemCount: _globalChannels.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final element = _globalChannels[index];
|
2024-08-01 16:54:19 +00:00
|
|
|
return _buildEntry(element);
|
2024-07-06 10:17:54 +00:00
|
|
|
},
|
|
|
|
),
|
2024-07-12 13:59:16 +00:00
|
|
|
SliverList.list(
|
|
|
|
children: _inRealms.entries.map((element) {
|
|
|
|
return ExpansionTile(
|
2024-08-01 16:54:19 +00:00
|
|
|
tilePadding: const EdgeInsets.only(left: 20, right: 24),
|
2024-07-12 13:59:16 +00:00
|
|
|
minTileHeight: 48,
|
|
|
|
title: Text(element.value.first.realm!.name),
|
2024-07-12 14:31:45 +00:00
|
|
|
leading: CircleAvatar(
|
|
|
|
backgroundColor: Colors.teal,
|
|
|
|
radius: widget.isDense ? 12 : 24,
|
|
|
|
child: Icon(
|
|
|
|
Icons.workspaces,
|
|
|
|
color: Colors.white,
|
|
|
|
size: widget.isDense ? 12 : 16,
|
|
|
|
),
|
|
|
|
),
|
2024-08-01 16:54:19 +00:00
|
|
|
children: element.value.map((x) => _buildEntry(x)).toList(),
|
2024-07-12 13:59:16 +00:00
|
|
|
);
|
|
|
|
}).toList(),
|
|
|
|
),
|
2024-05-30 14:02:54 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2024-05-29 15:22:24 +00:00
|
|
|
}
|