Solian/lib/screens/chat.dart

212 lines
7.4 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';
import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/content/channel.dart';
import 'package:solian/router.dart';
import 'package:solian/screens/account/notification.dart';
import 'package:solian/theme.dart';
2024-05-28 12:13:36 +00:00
import 'package:solian/widgets/account/signin_required_overlay.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';
2024-05-23 13:12:47 +00:00
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
2024-05-23 13:12:47 +00:00
2024-05-25 16:11:00 +00:00
@override
State<ChatScreen> createState() => _ChatScreenState();
2024-05-25 16:11:00 +00:00
}
class _ChatScreenState extends State<ChatScreen> {
2024-05-25 16:11:00 +00:00
bool _isBusy = true;
2024-05-28 16:14:41 +00:00
int? _accountId;
2024-05-25 16:11:00 +00:00
final List<Channel> _channels = List.empty(growable: true);
2024-05-28 16:14:41 +00:00
getProfile() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return;
2024-07-06 10:17:54 +00:00
2024-05-28 16:14:41 +00:00
final prof = await auth.getProfile();
_accountId = prof.body['id'];
}
2024-05-25 16:11:00 +00:00
getChannels() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return;
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = true);
final ChannelProvider provider = Get.find();
final resp = await provider.listAvailableChannel();
setState(() {
_channels.clear();
_channels.addAll(
resp.body.map((e) => Channel.fromJson(e)).toList().cast<Channel>(),
);
});
setState(() => _isBusy = false);
}
@override
void initState() {
super.initState();
2024-05-28 16:14:41 +00:00
getProfile();
2024-05-25 16:11:00 +00:00
getChannels();
}
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-05-23 13:12:47 +00:00
return Material(
color: Theme.of(context).colorScheme.surface,
2024-05-28 12:13:36 +00:00
child: FutureBuilder(
2024-05-28 14:13:23 +00:00
future: auth.isAuthorized,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.data == false) {
return SigninRequiredOverlay(
onSignedIn: () {
getChannels();
},
);
}
2024-05-28 12:13:36 +00:00
2024-06-22 18:25:45 +00:00
return DefaultTabController(
length: 2,
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
context),
sliver: SliverAppBar(
title: AppBarTitle('chat'.tr),
2024-06-23 10:03:46 +00:00
centerTitle: false,
2024-06-22 18:25:45 +00:00
floating: true,
toolbarHeight: SolianTheme.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),
),
onTap: () {
AppRouter.instance
.pushNamed('channelOrganizing')
.then(
(value) {
if (value != null) getChannels();
},
);
2024-05-28 14:13:23 +00:00
},
2024-05-29 12:13:53 +00:00
),
2024-06-22 18:25:45 +00:00
PopupMenuItem(
child: ListTile(
title: Text('channelOrganizeDirect'.tr),
leading: const FaIcon(
FontAwesomeIcons.userGroup,
size: 16,
),
contentPadding:
const EdgeInsets.symmetric(horizontal: 8),
),
onTap: () {
final ChannelProvider provider = Get.find();
provider
.createDirectChannel(context, 'global')
.then((resp) {
if (resp != null) {
getChannels();
}
});
},
),
],
),
SizedBox(
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
2024-05-28 14:13:23 +00:00
),
],
2024-06-22 18:25:45 +00:00
bottom: TabBar(
tabs: [
Tab(
icon: const Icon(Icons.tag),
text: 'channels'.tr,
),
Tab(
icon: const Icon(Icons.chat),
text: 'channelCategoryDirect'.tr,
),
],
),
),
),
];
},
body: Builder(builder: (context) {
if (_isBusy) {
return const Center(child: CircularProgressIndicator());
}
return TabBarView(
children: [
2024-07-06 10:17:54 +00:00
Column(
children: [
const ChatCallCurrentIndicator(),
Expanded(
child: RefreshIndicator(
onRefresh: () => getChannels(),
child: ChannelListWidget(
channels:
_channels.where((x) => x.type == 0).toList(),
selfId: _accountId ?? 0,
),
2024-06-22 18:25:45 +00:00
),
2024-07-06 10:17:54 +00:00
),
],
2024-05-25 16:11:00 +00:00
),
2024-07-06 10:17:54 +00:00
Column(
children: [
const ChatCallCurrentIndicator(),
Expanded(
child: RefreshIndicator(
onRefresh: () => getChannels(),
child: ChannelListWidget(
2024-07-12 03:39:44 +00:00
channels:
_channels.where((x) => x.type == 1).toList(),
2024-07-06 10:17:54 +00:00
selfId: _accountId ?? 0,
noCategory: true,
),
2024-06-22 18:25:45 +00:00
),
2024-07-06 10:17:54 +00:00
),
],
2024-05-28 14:13:23 +00:00
),
],
2024-06-22 18:25:45 +00:00
);
}),
2024-05-28 14:13:23 +00:00
),
);
},
),
2024-05-23 13:12:47 +00:00
);
}
}