Solian/lib/widgets/channel/channel_list.dart

151 lines
4.4 KiB
Dart
Raw Normal View History

2024-05-29 15:22:24 +00:00
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
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-05-30 14:02:54 +00:00
final bool noCategory;
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,
this.noCategory = false,
});
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 List<Channel> _directMessages = List.empty(growable: true);
final Map<String, List<Channel>> _inRealms = {};
2024-05-29 15:22:24 +00:00
2024-05-30 14:02:54 +00:00
void mapChannels() {
_inRealms.clear();
_globalChannels.clear();
_directMessages.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 if (channel.type == 1) {
_directMessages.add(channel);
} else {
_globalChannels.add(channel);
}
}
}
@override
void didUpdateWidget(covariant ChannelListWidget oldWidget) {
setState(() => mapChannels());
super.didUpdateWidget(oldWidget);
}
Widget buildItem(Channel element) {
if (element.type == 1) {
final otherside = element.members!
.where((e) => e.account.externalId != widget.selfId)
.first;
return ListTile(
leading: AccountAvatar(
content: otherside.account.avatar,
bgColor: Colors.indigo,
feColor: Colors.white,
),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
title: Text(otherside.account.nick),
subtitle: Text(
'channelDirectDescription'
.trParams({'username': '@${otherside.account.name}'}),
),
onTap: () {
AppRouter.instance.pushNamed(
'channelChat',
pathParameters: {'alias': element.alias},
queryParameters: {
if (element.realmId != null) 'realm': element.realm!.alias,
2024-05-29 15:22:24 +00:00
},
);
2024-05-30 14:02:54 +00:00
},
);
}
2024-05-29 15:22:24 +00:00
2024-05-30 14:02:54 +00:00
return ListTile(
leading: const CircleAvatar(
backgroundColor: Colors.indigo,
child: FaIcon(
FontAwesomeIcons.hashtag,
color: Colors.white,
size: 16,
),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
title: Text(element.name),
subtitle: Text(element.description),
onTap: () {
AppRouter.instance.pushNamed(
'channelChat',
pathParameters: {'alias': element.alias},
queryParameters: {
if (element.realmId != null) 'realm': element.realm!.alias,
2024-05-29 15:22:24 +00:00
},
);
},
);
}
2024-05-30 14:02:54 +00:00
@override
Widget build(BuildContext context) {
if (widget.noCategory) {
return SliverList.builder(
itemCount: _globalChannels.length,
itemBuilder: (context, index) {
final element = _globalChannels[index];
return buildItem(element);
},
);
}
return SliverList.list(
children: [
..._globalChannels.map((e) => buildItem(e)),
if (_directMessages.isNotEmpty)
ExpansionTile(
tilePadding: const EdgeInsets.symmetric(horizontal: 24),
title: Text('channelCategoryDirect'.tr),
subtitle: Text('channelCategoryDirectHint'.tr),
children: _directMessages.map((e) => buildItem(e)).toList(),
),
..._inRealms.entries.map((element) {
return ExpansionTile(
tilePadding: const EdgeInsets.symmetric(horizontal: 24),
title: Text(element.value.first.realm!.name),
subtitle: Text(
element.value.first.realm!.description,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
children: element.value.map((e) => buildItem(e)).toList(),
);
}),
],
);
}
2024-05-29 15:22:24 +00:00
}