Solian/lib/screens/channel/channel_organize.dart

229 lines
7.4 KiB
Dart
Raw Normal View History

2024-05-25 16:11:00 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart';
import 'package:solian/exts.dart';
import 'package:solian/models/channel.dart';
2024-05-29 15:22:24 +00:00
import 'package:solian/models/realm.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/providers/auth.dart';
2024-05-28 16:14:41 +00:00
import 'package:solian/providers/content/channel.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/router.dart';
2024-06-22 15:59:11 +00:00
import 'package:solian/theme.dart';
import 'package:solian/widgets/app_bar_title.dart';
2024-10-06 09:37:07 +00:00
import 'package:solian/widgets/root_container.dart';
2024-05-25 16:11:00 +00:00
import 'package:uuid/uuid.dart';
class ChannelOrganizeArguments {
final Channel? edit;
2024-05-29 15:22:24 +00:00
final Realm? realm;
2024-05-25 16:11:00 +00:00
2024-05-29 15:22:24 +00:00
ChannelOrganizeArguments({this.edit, this.realm});
2024-05-25 16:11:00 +00:00
}
class ChannelOrganizeScreen extends StatefulWidget {
final Channel? edit;
2024-05-29 15:22:24 +00:00
final Realm? realm;
2024-05-25 16:11:00 +00:00
const ChannelOrganizeScreen({super.key, this.edit, this.realm});
@override
State<ChannelOrganizeScreen> createState() => _ChannelOrganizeScreenState();
}
class _ChannelOrganizeScreenState extends State<ChannelOrganizeScreen> {
bool _isBusy = false;
final _aliasController = TextEditingController();
final _nameController = TextEditingController();
final _descriptionController = TextEditingController();
bool _isPublic = false;
bool _isCommunity = false;
2024-05-25 16:11:00 +00:00
void _applyChannel() async {
2024-05-25 16:11:00 +00:00
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return;
2024-05-25 16:11:00 +00:00
if (_aliasController.value.text.isEmpty) _randomizeAlias();
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = true);
2024-05-28 16:14:41 +00:00
final ChannelProvider provider = Get.find();
2024-05-29 15:22:24 +00:00
final scope = widget.realm != null ? widget.realm!.alias : 'global';
2024-05-25 16:11:00 +00:00
final payload = {
'alias': _aliasController.value.text.toLowerCase(),
'name': _nameController.value.text,
'description': _descriptionController.value.text,
'is_encrypted': _isPublic,
2024-05-25 16:11:00 +00:00
};
2024-05-28 16:14:41 +00:00
Response? resp;
try {
if (widget.edit != null) {
2024-05-29 15:22:24 +00:00
resp = await provider.updateChannel(scope, widget.edit!.id, payload);
2024-05-28 16:14:41 +00:00
} else {
2024-05-29 15:22:24 +00:00
resp = await provider.createChannel(scope, payload);
2024-05-28 16:14:41 +00:00
}
} catch (e) {
context.showErrorDialog(e);
2024-05-25 16:11:00 +00:00
}
2024-05-28 16:14:41 +00:00
AppRouter.instance.pop(resp!.body);
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = false);
}
void _randomizeAlias() {
2024-05-25 16:11:00 +00:00
_aliasController.text =
const Uuid().v4().replaceAll('-', '').substring(0, 12);
}
void _syncWidget() {
2024-05-25 16:11:00 +00:00
if (widget.edit != null) {
_aliasController.text = widget.edit!.alias;
_nameController.text = widget.edit!.name;
_descriptionController.text = widget.edit!.description;
_isPublic = widget.edit!.isPublic;
_isCommunity = widget.edit!.isCommunity;
2024-05-25 16:11:00 +00:00
}
}
void _cancelAction() {
2024-05-25 16:11:00 +00:00
AppRouter.instance.pop();
}
@override
void initState() {
_syncWidget();
2024-05-25 16:11:00 +00:00
super.initState();
}
2024-09-17 06:21:37 +00:00
@override
void dispose() {
_aliasController.dispose();
_nameController.dispose();
_descriptionController.dispose();
super.dispose();
}
2024-05-25 16:11:00 +00:00
@override
Widget build(BuildContext context) {
2024-05-29 15:22:24 +00:00
final notifyBannerActions = [
TextButton(
onPressed: _cancelAction,
2024-05-29 15:22:24 +00:00
child: Text('cancel'.tr),
),
];
2024-10-06 09:37:07 +00:00
return RootContainer(
2024-05-25 16:11:00 +00:00
child: Scaffold(
appBar: AppBar(
2024-06-22 15:59:11 +00:00
title: AppBarTitle('channelOrganizing'.tr),
centerTitle: false,
toolbarHeight: AppTheme.toolbarHeight(context),
2024-05-25 16:11:00 +00:00
actions: [
TextButton(
onPressed: _isBusy ? null : () => _applyChannel(),
2024-05-25 16:11:00 +00:00
child: Text('apply'.tr.toUpperCase()),
)
],
),
body: SafeArea(
top: false,
child: Column(
children: [
if (_isBusy) const LinearProgressIndicator().animate().scaleX(),
if (widget.edit != null)
MaterialBanner(
leading: const Icon(Icons.edit),
leadingPadding: const EdgeInsets.only(left: 10, right: 20),
dividerColor: Colors.transparent,
content: Text(
'channelEditingNotify'
.trParams({'channel': '#${widget.edit!.alias}'}),
),
2024-05-29 15:22:24 +00:00
actions: notifyBannerActions,
).paddingOnly(bottom: 6),
2024-06-09 15:00:11 +00:00
if (widget.realm != null && widget.edit == null)
2024-05-29 15:22:24 +00:00
MaterialBanner(
leading: const Icon(Icons.group),
leadingPadding: const EdgeInsets.only(left: 10, right: 20),
dividerColor: Colors.transparent,
content: Text(
'channelInRealmNotify'
.trParams({'realm': '#${widget.realm!.alias}'}),
),
actions: notifyBannerActions,
).paddingOnly(bottom: 6),
2024-05-25 16:11:00 +00:00
Row(
children: [
Expanded(
child: TextField(
autofocus: true,
controller: _aliasController,
decoration: InputDecoration.collapsed(
hintText: 'channelAlias'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
),
TextButton(
style: TextButton.styleFrom(
shape: const CircleBorder(),
visualDensity:
const VisualDensity(horizontal: -2, vertical: -2),
),
onPressed: () => _randomizeAlias(),
2024-05-25 16:11:00 +00:00
child: const Icon(Icons.refresh),
)
],
).paddingSymmetric(horizontal: 16, vertical: 2),
const Divider(thickness: 0.3),
TextField(
autocorrect: true,
controller: _nameController,
decoration: InputDecoration.collapsed(
hintText: 'channelName'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
).paddingSymmetric(horizontal: 16, vertical: 8),
const Divider(thickness: 0.3),
Expanded(
child: TextField(
minLines: 5,
maxLines: null,
autocorrect: true,
keyboardType: TextInputType.multiline,
controller: _descriptionController,
decoration: InputDecoration.collapsed(
hintText: 'channelDescription'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
).paddingSymmetric(horizontal: 16, vertical: 12),
),
const Divider(thickness: 0.3),
CheckboxListTile(
title: Text('channelPublic'.tr),
value: _isPublic,
onChanged: (value) =>
setState(() => _isPublic = value ?? false),
controlAffinity: ListTileControlAffinity.leading,
),
CheckboxListTile(
title: Text('channelCommunity'.tr),
value: _isCommunity,
onChanged: (value) =>
setState(() => _isCommunity = value ?? false),
2024-05-25 16:11:00 +00:00
controlAffinity: ListTileControlAffinity.leading,
),
],
),
),
),
);
}
}