Solian/lib/screens/realms/realm_organize.dart

205 lines
6.6 KiB
Dart
Raw Normal View History

2024-05-28 14:13:23 +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/realm.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
2024-06-22 15:59:11 +00:00
import 'package:solian/theme.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-28 14:13:23 +00:00
import 'package:uuid/uuid.dart';
class RealmOrganizeArguments {
final Realm? edit;
RealmOrganizeArguments({this.edit});
}
class RealmOrganizeScreen extends StatefulWidget {
final Realm? edit;
const RealmOrganizeScreen({super.key, this.edit});
@override
State<RealmOrganizeScreen> createState() => _RealmOrganizeScreenState();
}
class _RealmOrganizeScreenState extends State<RealmOrganizeScreen> {
bool _isBusy = false;
final _aliasController = TextEditingController();
final _nameController = TextEditingController();
final _descriptionController = TextEditingController();
bool _isCommunity = false;
bool _isPublic = false;
void applyRealm() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return;
if (_aliasController.value.text.isEmpty) randomizeAlias();
setState(() => _isBusy = true);
final client = auth.configureClient('auth');
2024-05-28 14:13:23 +00:00
final payload = {
'alias': _aliasController.value.text.toLowerCase(),
'name': _nameController.value.text,
'description': _descriptionController.value.text,
'is_public': _isPublic,
'is_community': _isCommunity,
};
Response resp;
if (widget.edit != null) {
resp = await client.put('/realms/${widget.edit!.id}', payload);
2024-05-28 14:13:23 +00:00
} else {
resp = await client.post('/realms', payload);
2024-05-28 14:13:23 +00:00
}
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
} else {
AppRouter.instance.pop(resp.body);
}
setState(() => _isBusy = false);
}
void randomizeAlias() {
_aliasController.text =
const Uuid().v4().replaceAll('-', '').substring(0, 12);
}
void syncWidget() {
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;
}
}
void cancelAction() {
AppRouter.instance.pop();
}
@override
void initState() {
syncWidget();
super.initState();
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
appBar: AppBar(
2024-07-12 14:37:58 +00:00
leading: AppBarLeadingButton.adaptive(context),
2024-06-22 15:59:11 +00:00
title: AppBarTitle('realmOrganizing'.tr),
centerTitle: false,
toolbarHeight: SolianTheme.toolbarHeight(context),
2024-05-28 14:13:23 +00:00
actions: [
TextButton(
onPressed: _isBusy ? null : () => applyRealm(),
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(
'realmEditingNotify'
.trParams({'realm': '#${widget.edit!.alias}'}),
),
actions: [
TextButton(
onPressed: cancelAction,
child: Text('cancel'.tr),
),
],
2024-05-29 16:05:39 +00:00
).paddingOnly(bottom: 6),
2024-05-28 14:13:23 +00:00
Row(
children: [
Expanded(
child: TextField(
autofocus: true,
controller: _aliasController,
decoration: InputDecoration.collapsed(
hintText: 'realmAlias'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
),
TextButton(
style: TextButton.styleFrom(
shape: const CircleBorder(),
visualDensity:
const VisualDensity(horizontal: -2, vertical: -2),
),
onPressed: () => randomizeAlias(),
child: const Icon(Icons.refresh),
)
],
).paddingSymmetric(horizontal: 16, vertical: 2),
const Divider(thickness: 0.3),
TextField(
autocorrect: true,
controller: _nameController,
decoration: InputDecoration.collapsed(
hintText: 'realmName'.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: 'realmDescription'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
).paddingSymmetric(horizontal: 16, vertical: 12),
),
const Divider(thickness: 0.3),
CheckboxListTile(
title: Text('realmPublic'.tr),
value: _isPublic,
onChanged: (newValue) =>
setState(() => _isPublic = newValue ?? false),
controlAffinity: ListTileControlAffinity.leading,
),
CheckboxListTile(
title: Text('realmCommunity'.tr),
value: _isCommunity,
onChanged: (newValue) =>
setState(() => _isCommunity = newValue ?? false),
controlAffinity: ListTileControlAffinity.leading,
),
],
),
),
),
);
}
}