Solian/lib/screens/channel/channel_detail.dart

227 lines
6.8 KiB
Dart
Raw Normal View History

2024-06-08 16:09:01 +00:00
import 'package:dropdown_button2/dropdown_button2.dart';
2024-05-26 15:13:43 +00:00
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
2024-06-08 16:09:01 +00:00
import 'package:solian/exts.dart';
2024-05-26 15:13:43 +00:00
import 'package:solian/models/channel.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/screens/channel/channel_organize.dart';
import 'package:solian/widgets/channel/channel_deletion.dart';
2024-05-27 13:21:10 +00:00
import 'package:solian/widgets/channel/channel_member.dart';
2024-05-26 15:13:43 +00:00
2024-06-08 16:09:01 +00:00
class ChannelDetailArguments {
final Channel channel;
final ChannelMember profile;
ChannelDetailArguments({required this.channel, required this.profile});
}
2024-05-26 15:13:43 +00:00
class ChannelDetailScreen extends StatefulWidget {
final String realm;
final Channel channel;
2024-06-08 16:09:01 +00:00
final ChannelMember profile;
2024-05-26 15:13:43 +00:00
const ChannelDetailScreen({
super.key,
required this.channel,
required this.realm,
2024-06-08 16:09:01 +00:00
required this.profile,
2024-05-26 15:13:43 +00:00
});
@override
State<ChannelDetailScreen> createState() => _ChannelDetailScreenState();
}
class _ChannelDetailScreenState extends State<ChannelDetailScreen> {
2024-06-08 16:09:01 +00:00
bool _isBusy = false;
2024-05-26 15:13:43 +00:00
bool _isOwned = false;
2024-06-08 16:09:01 +00:00
int _notifyLevel = 0;
2024-05-26 15:13:43 +00:00
void checkOwner() async {
final AuthProvider auth = Get.find();
final prof = await auth.getProfile();
setState(() {
_isOwned = prof.body['id'] == widget.channel.account.externalId;
});
}
2024-05-27 13:21:10 +00:00
void showMemberList() {
showModalBottomSheet(
context: context,
useRootNavigator: true,
isScrollControlled: true,
builder: (context) => ChannelMemberListPopup(
channel: widget.channel,
realm: widget.realm,
),
);
}
2024-05-26 15:13:43 +00:00
void promptLeaveChannel() async {
final did = await showDialog(
context: context,
builder: (context) => ChannelDeletionDialog(
2024-05-26 15:13:43 +00:00
channel: widget.channel,
realm: widget.realm,
isOwned: _isOwned,
),
);
if (did == true && AppRouter.instance.canPop()) {
AppRouter.instance.pop(false);
}
}
2024-06-08 16:09:01 +00:00
void applyProfileChanges() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return;
setState(() => _isBusy = true);
final client = auth.configureClient(service: 'messaging');
final resp = await client
.put('/api/channels/${widget.realm}/${widget.channel.alias}/members/me', {
'nick': null,
'notify_level': _notifyLevel,
});
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
} else {
context.showSnackbar('channelNotifyLevelApplied'.tr);
}
setState(() => _isBusy = false);
}
2024-05-26 15:13:43 +00:00
@override
void initState() {
2024-06-08 16:09:01 +00:00
_notifyLevel = widget.profile.notify;
2024-05-26 15:13:43 +00:00
super.initState();
checkOwner();
}
@override
Widget build(BuildContext context) {
2024-06-08 16:09:01 +00:00
final notifyTypes = {
0: 'channelNotifyLevelAll'.tr,
1: 'channelNotifyLevelMentioned'.tr,
2: 'channelNotifyLevelNone'.tr,
};
2024-05-26 15:13:43 +00:00
final ownerActions = [
ListTile(
leading: const Icon(Icons.edit),
2024-05-27 13:21:10 +00:00
trailing: const Icon(Icons.chevron_right),
2024-06-08 13:35:50 +00:00
title: Text('channelAdjust'.tr.capitalize!),
2024-05-26 15:13:43 +00:00
onTap: () async {
AppRouter.instance
.pushNamed(
'channelOrganizing',
extra: ChannelOrganizeArguments(edit: widget.channel),
)
.then((resp) {
if (resp != null) {
AppRouter.instance.pop(resp);
}
});
},
),
];
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Row(
children: [
2024-05-28 16:14:41 +00:00
const CircleAvatar(
2024-05-26 15:13:43 +00:00
radius: 28,
backgroundColor: Colors.teal,
child: FaIcon(
2024-05-28 16:14:41 +00:00
FontAwesomeIcons.hashtag,
2024-05-26 15:13:43 +00:00
color: Colors.white,
size: 18,
),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.channel.name,
style: Theme.of(context).textTheme.bodyLarge),
Text(widget.channel.description,
style: Theme.of(context).textTheme.bodySmall),
Text(
'#${widget.channel.id.toString().padLeft(8, '0')} · ${widget.channel.alias}',
style: const TextStyle(fontSize: 11),
),
],
),
)
],
),
),
const Divider(thickness: 0.3),
Expanded(
child: ListView(
children: [
ListTile(
2024-06-08 16:09:01 +00:00
leading: const Icon(Icons.notifications_active),
title: Text('channelNotifyLevel'.tr.capitalize!),
trailing: DropdownButtonHideUnderline(
child: DropdownButton2<int>(
isExpanded: true,
items: notifyTypes.entries
.map((item) => DropdownMenuItem<int>(
enabled: !_isBusy,
value: item.key,
child: Text(
item.value,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: _notifyLevel,
onChanged: (int? value) {
setState(() => _notifyLevel = value ?? 0);
applyProfileChanges();
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.only(left: 16, right: 1),
height: 40,
width: 140,
),
menuItemStyleData: const MenuItemStyleData(
height: 40,
),
),
),
2024-05-26 15:13:43 +00:00
),
ListTile(
leading: const Icon(Icons.supervisor_account),
2024-05-27 13:21:10 +00:00
trailing: const Icon(Icons.chevron_right),
2024-06-08 13:35:50 +00:00
title: Text('channelMembers'.tr.capitalize!),
2024-05-27 13:21:10 +00:00
onTap: () => showMemberList(),
2024-05-26 15:13:43 +00:00
),
...(_isOwned ? ownerActions : List.empty()),
const Divider(thickness: 0.3),
ListTile(
leading: _isOwned
? const Icon(Icons.delete)
: const Icon(Icons.exit_to_app),
title: Text(_isOwned ? 'delete'.tr : 'leave'.tr),
onTap: () => promptLeaveChannel(),
),
],
),
),
],
);
}
}