Solian/lib/screens/chat.dart

330 lines
11 KiB
Dart
Raw Normal View History

2024-05-23 13:12:47 +00:00
import 'package:flutter/material.dart';
2024-05-25 16:11:00 +00:00
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2024-10-05 15:12:23 +00:00
import 'package:gap/gap.dart';
2024-05-25 16:11:00 +00:00
import 'package:get/get.dart';
2024-10-05 15:12:23 +00:00
import 'package:solian/controllers/chat_events_controller.dart';
2024-07-12 13:59:16 +00:00
import 'package:solian/exts.dart';
2024-10-05 15:12:23 +00:00
import 'package:solian/models/channel.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/content/channel.dart';
2024-10-05 15:12:23 +00:00
import 'package:solian/providers/content/realm.dart';
import 'package:solian/providers/database/database.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/router.dart';
import 'package:solian/screens/account/notification.dart';
import 'package:solian/theme.dart';
2024-10-05 15:12:23 +00:00
import 'package:solian/widgets/account/account_avatar.dart';
2024-05-28 12:13:36 +00:00
import 'package:solian/widgets/account/signin_required_overlay.dart';
import 'package:solian/widgets/app_bar_leading.dart';
2024-06-22 15:59:11 +00:00
import 'package:solian/widgets/app_bar_title.dart';
2024-05-29 15:22:24 +00:00
import 'package:solian/widgets/channel/channel_list.dart';
2024-06-01 12:18:25 +00:00
import 'package:solian/widgets/chat/call/chat_call_indicator.dart';
2024-06-06 12:23:50 +00:00
import 'package:solian/widgets/current_state_action.dart';
import 'package:solian/widgets/loading_indicator.dart';
2024-10-06 09:37:07 +00:00
import 'package:solian/widgets/root_container.dart';
import 'package:solian/widgets/sidebar/empty_placeholder.dart';
2024-05-23 13:12:47 +00:00
class ChatScreen extends StatelessWidget {
const ChatScreen({super.key});
2024-05-23 13:12:47 +00:00
2024-05-25 16:11:00 +00:00
@override
Widget build(BuildContext context) {
2024-10-06 17:47:34 +00:00
return const ResponsiveRootContainer(
2024-10-06 09:37:07 +00:00
child: ChatList(),
);
}
}
class ChatListShell extends StatelessWidget {
final Widget? child;
const ChatListShell({super.key, required this.child});
@override
Widget build(BuildContext context) {
2024-10-06 09:37:07 +00:00
return RootContainer(
child: Row(
children: [
const SizedBox(
width: 360,
child: ChatList(),
),
const VerticalDivider(thickness: 0.3, width: 0.3),
Expanded(child: child ?? const EmptyPagePlaceholder()),
],
),
);
}
}
class ChatList extends StatefulWidget {
const ChatList({super.key});
@override
State<ChatList> createState() => _ChatListState();
2024-05-25 16:11:00 +00:00
}
class _ChatListState extends State<ChatList> {
2024-10-05 15:12:23 +00:00
List<Channel> _normalChannels = List.empty();
List<Channel> _directChannels = List.empty();
final Map<String, List<Channel>> _realmChannels = {};
late final ChannelProvider _channels = Get.find();
bool _isBusy = true;
2024-10-05 15:12:23 +00:00
List<Channel> _sortChannels(List<Channel> channels) {
channels.sort(
(a, b) =>
_lastMessages?[b.id]?.createdAt.compareTo(
_lastMessages?[a.id]?.createdAt ??
DateTime.fromMillisecondsSinceEpoch(0),
) ??
0,
);
return channels;
}
Future<void> _loadNormalChannels() async {
final resp = await _channels.listAvailableChannel(isDirect: false);
setState(() {
_normalChannels = _sortChannels(resp);
});
}
Future<void> _loadDirectChannels() async {
final resp = await _channels.listAvailableChannel(isDirect: true);
setState(() {
_directChannels = _sortChannels(resp);
});
}
Future<void> _loadRealmChannels(String realm) async {
final resp = await _channels.listAvailableChannel(scope: realm);
setState(() {
_realmChannels[realm] = _sortChannels(List.from(resp));
});
}
Future<void> _loadAllChannels() async {
final RealmProvider realms = Get.find();
Future.wait([
_loadNormalChannels(),
_loadDirectChannels(),
...realms.availableRealms.map((x) => _loadRealmChannels(x.alias)),
]);
}
Map<int, LocalMessageEventTableData>? _lastMessages;
Future<void> _loadLastMessages() async {
final ctrl = ChatEventController();
await ctrl.initialize();
final messages = await ctrl.src.getLastInAllChannels();
2024-10-07 15:34:58 +00:00
if (mounted) {
setState(() {
_lastMessages = messages
.map((k, v) => MapEntry(k, v.firstOrNull))
.cast<int, LocalMessageEventTableData>();
});
}
2024-10-05 15:12:23 +00:00
}
2024-05-25 16:11:00 +00:00
@override
void initState() {
super.initState();
2024-10-05 15:12:23 +00:00
_loadLastMessages().then((_) {
if (!mounted) return;
_loadAllChannels().then((_) {
if (mounted) {
setState(() => _isBusy = false);
}
});
2024-10-05 15:12:23 +00:00
});
2024-05-25 16:11:00 +00:00
}
2024-05-23 13:12:47 +00:00
@override
Widget build(BuildContext context) {
2024-05-25 16:11:00 +00:00
final AuthProvider auth = Get.find();
2024-10-05 15:12:23 +00:00
final RealmProvider realms = Get.find();
2024-05-25 16:11:00 +00:00
2024-10-05 15:12:23 +00:00
return Obx(
() => DefaultTabController(
length: 2 + realms.availableRealms.length,
2024-10-06 17:47:34 +00:00
child: ResponsiveRootContainer(
child: Scaffold(
appBar: AppBar(
leading: AppBarLeadingButton.adaptive(context),
title: AppBarTitle('chat'.tr),
centerTitle: true,
toolbarHeight: AppTheme.toolbarHeight(context),
actions: [
const BackgroundStateWidget(),
const NotificationButton(),
PopupMenuButton(
icon: const Icon(Icons.add_circle),
itemBuilder: (BuildContext context) => [
PopupMenuItem(
child: ListTile(
title: Text('channelOrganizeCommon'.tr),
leading: const Icon(Icons.tag),
contentPadding:
const EdgeInsets.symmetric(horizontal: 8),
2024-10-05 15:12:23 +00:00
),
onTap: () {
AppRouter.instance.pushNamed('channelOrganizing').then(
(value) {
if (value != null) {
_loadAllChannels();
}
},
);
},
2024-05-25 16:11:00 +00:00
),
PopupMenuItem(
child: ListTile(
title: Text('channelOrganizeDirect'.tr),
leading: const FaIcon(
FontAwesomeIcons.userGroup,
2024-10-05 15:12:23 +00:00
size: 16,
),
contentPadding:
const EdgeInsets.symmetric(horizontal: 8),
2024-10-05 15:12:23 +00:00
),
onTap: () {
final ChannelProvider channels = Get.find();
channels
.createDirectChannel(context, 'global')
.then((resp) {
if (resp != null) {
_loadAllChannels();
}
}).catchError((e) {
context.showErrorDialog(e);
});
},
),
],
),
SizedBox(
width: AppTheme.isLargeScreen(context) ? 8 : 16,
),
],
bottom: TabBar(
isScrollable: true,
dividerHeight: 0.3,
tabAlignment: TabAlignment.startOffset,
tabs: [
Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
CircleAvatar(
radius: 14,
backgroundColor:
Theme.of(context).colorScheme.primary,
child: const Icon(
Icons.forum,
size: 16,
color: Colors.white,
),
2024-10-05 15:12:23 +00:00
),
const Gap(8),
Text('all'.tr),
],
),
2024-10-05 15:12:23 +00:00
),
Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const CircleAvatar(
radius: 14,
child: Icon(
Icons.chat_bubble,
size: 16,
),
),
const Gap(8),
Text('channelTypeDirect'.tr),
],
),
),
...realms.availableRealms.map((x) => Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
2024-10-11 16:41:03 +00:00
AttachedCircleAvatar(
content: x.avatar,
radius: 14,
fallbackWidget: const Icon(
Icons.workspaces,
size: 16,
),
),
const Gap(8),
Text(x.name),
],
),
)),
],
),
2024-10-05 15:12:23 +00:00
),
body: Obx(() {
if (auth.isAuthorized.isFalse) {
return SigninRequiredOverlay(
onDone: () => _loadAllChannels(),
);
}
2024-10-05 15:12:23 +00:00
final selfId = auth.userProfile.value!['id'];
2024-10-05 15:12:23 +00:00
return Column(
children: [
const ChatCallCurrentIndicator(),
if (_isBusy) const LoadingIndicator(),
Expanded(
child: TabBarView(
children: [
RefreshIndicator(
onRefresh: _loadNormalChannels,
child: ChannelListWidget(
channels: _sortChannels([
..._normalChannels,
..._directChannels,
..._realmChannels.values.expand((x) => x),
]),
selfId: selfId,
useReplace: AppTheme.isLargeScreen(context),
),
2024-10-05 15:12:23 +00:00
),
RefreshIndicator(
onRefresh: _loadDirectChannels,
2024-10-05 15:12:23 +00:00
child: ChannelListWidget(
channels: _directChannels,
2024-10-05 15:12:23 +00:00
selfId: selfId,
useReplace: AppTheme.isLargeScreen(context),
2024-10-05 15:12:23 +00:00
),
),
...realms.availableRealms.map(
(x) => RefreshIndicator(
onRefresh: () => _loadRealmChannels(x.alias),
child: ChannelListWidget(
channels: _realmChannels[x.alias] ?? [],
selfId: selfId,
useReplace: AppTheme.isLargeScreen(context),
),
),
),
],
),
2024-10-05 15:12:23 +00:00
),
],
);
}),
),
2024-10-05 15:12:23 +00:00
),
2024-05-28 14:13:23 +00:00
),
2024-05-23 13:12:47 +00:00
);
}
}