Solian/lib/screens/posts/post_publish.dart

273 lines
8.9 KiB
Dart
Raw Normal View History

2024-05-19 10:01:00 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
2024-05-23 12:00:26 +00:00
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2024-05-19 10:01:00 +00:00
import 'package:get/get.dart';
2024-05-19 16:08:20 +00:00
import 'package:solian/exts.dart';
import 'package:solian/models/post.dart';
2024-05-19 10:01:00 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/services.dart';
import 'package:solian/widgets/account/account_avatar.dart';
2024-05-19 16:08:20 +00:00
import 'package:solian/widgets/attachments/attachment_publish.dart';
import 'package:solian/widgets/posts/post_item.dart';
2024-05-22 15:18:01 +00:00
import 'package:solian/widgets/prev_page.dart';
class PostPublishingArguments {
final Post? edit;
final Post? reply;
final Post? repost;
PostPublishingArguments({this.edit, this.reply, this.repost});
}
2024-05-19 10:01:00 +00:00
class PostPublishingScreen extends StatefulWidget {
final Post? edit;
final Post? reply;
final Post? repost;
final String? realm;
const PostPublishingScreen({
super.key,
this.edit,
this.reply,
this.repost,
this.realm,
});
2024-05-19 10:01:00 +00:00
@override
State<PostPublishingScreen> createState() => _PostPublishingScreenState();
}
class _PostPublishingScreenState extends State<PostPublishingScreen> {
final _contentController = TextEditingController();
2024-05-25 16:11:00 +00:00
bool _isBusy = false;
2024-05-19 10:01:00 +00:00
List<int> _attachments = List.empty();
2024-05-19 16:08:20 +00:00
2024-05-26 13:03:25 +00:00
void showAttachments() {
2024-05-19 16:08:20 +00:00
showModalBottomSheet(
context: context,
builder: (context) => AttachmentPublishingPopup(
usage: 'i.attachment',
current: _attachments,
onUpdate: (value) => _attachments = value,
),
);
}
2024-05-19 10:01:00 +00:00
void applyPost() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return;
if (_contentController.value.text.isEmpty) return;
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = true);
2024-05-19 10:01:00 +00:00
2024-05-26 05:39:21 +00:00
final client = GetConnect(maxAuthRetries: 3);
2024-05-19 10:01:00 +00:00
client.httpClient.baseUrl = ServiceFinder.services['interactive'];
client.httpClient.addAuthenticator(auth.requestAuthenticator);
2024-05-19 10:01:00 +00:00
final payload = {
2024-05-19 10:01:00 +00:00
'content': _contentController.value.text,
2024-05-19 16:08:20 +00:00
'attachments': _attachments,
if (widget.edit != null) 'alias': widget.edit!.alias,
if (widget.reply != null) 'reply_to': widget.reply!.id,
if (widget.repost != null) 'repost_to': widget.repost!.id,
};
Response resp;
if (widget.edit != null) {
resp = await client.put('/api/posts/${widget.edit!.id}', payload);
} else {
resp = await client.post('/api/posts', payload);
}
2024-05-19 10:01:00 +00:00
if (resp.statusCode != 200) {
2024-05-19 16:08:20 +00:00
context.showErrorDialog(resp.bodyString);
2024-05-19 10:01:00 +00:00
} else {
AppRouter.instance.pop(resp.body);
}
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = false);
2024-05-19 10:01:00 +00:00
}
void syncWidget() {
if (widget.edit != null) {
_contentController.text = widget.edit!.content;
_attachments = widget.edit!.attachments ?? List.empty();
}
}
void cancelAction() {
AppRouter.instance.pop();
}
@override
void initState() {
syncWidget();
super.initState();
}
2024-05-19 10:01:00 +00:00
@override
Widget build(BuildContext context) {
final AuthProvider auth = Get.find();
final notifyBannerActions = [
TextButton(
onPressed: cancelAction,
child: Text('cancel'.tr),
)
];
2024-05-19 10:01:00 +00:00
return Material(
color: Theme.of(context).colorScheme.surface,
2024-05-19 10:01:00 +00:00
child: Scaffold(
appBar: AppBar(
title: Text('postPublishing'.tr),
2024-05-22 15:18:01 +00:00
leading: const PrevPageButton(),
2024-05-19 10:01:00 +00:00
actions: [
TextButton(
2024-05-25 16:11:00 +00:00
onPressed: _isBusy ? null : () => applyPost(),
2024-05-19 10:01:00 +00:00
child: Text('postAction'.tr.toUpperCase()),
)
],
),
body: SafeArea(
top: false,
child: Column(
children: [
2024-05-25 16:11:00 +00:00
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('postEditingNotify'.tr),
actions: notifyBannerActions,
),
if (widget.reply != null)
Container(
color: Theme.of(context).colorScheme.surfaceContainerLow,
child: Column(
children: [
MaterialBanner(
2024-05-23 12:00:26 +00:00
leading: const FaIcon(
FontAwesomeIcons.reply,
size: 18,
),
leadingPadding:
const EdgeInsets.only(left: 10, right: 20),
backgroundColor: Colors.transparent,
dividerColor: Colors.transparent,
content: Text(
'postReplyingNotify'.trParams(
{'username': '@${widget.reply!.author.name}'},
),
),
actions: notifyBannerActions,
),
const Divider(thickness: 0.3, height: 0.3),
Container(
constraints: const BoxConstraints(maxHeight: 280),
child: SingleChildScrollView(
child: PostItem(
item: widget.reply!,
isReactable: false,
),
),
),
],
),
),
if (widget.repost != null)
Container(
color: Theme.of(context).colorScheme.surfaceContainerLow,
child: Column(
children: [
MaterialBanner(
2024-05-23 12:00:26 +00:00
leading: const FaIcon(
FontAwesomeIcons.retweet,
size: 18,
),
leadingPadding:
const EdgeInsets.only(left: 10, right: 20),
dividerColor: Colors.transparent,
content: Text(
'postRepostingNotify'.trParams(
{'username': '@${widget.repost!.author.name}'},
),
),
actions: notifyBannerActions,
),
const Divider(thickness: 0.3, height: 0.3),
Container(
constraints: const BoxConstraints(maxHeight: 280),
child: SingleChildScrollView(
child: PostItem(
item: widget.repost!,
isReactable: false,
),
),
),
],
),
),
2024-05-19 10:01:00 +00:00
FutureBuilder(
future: auth.getProfile(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListTile(
leading: AccountAvatar(
content: snapshot.data?.body!['avatar'], radius: 22),
2024-05-19 10:01:00 +00:00
title: Text(snapshot.data?.body!['nick']),
subtitle: Text('postIdentityNotify'.tr),
);
} else {
return Container();
}
},
),
const Divider(thickness: 0.3),
Expanded(
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
2024-05-19 10:01:00 +00:00
child: TextField(
maxLines: null,
autofocus: true,
autocorrect: true,
keyboardType: TextInputType.multiline,
controller: _contentController,
decoration: InputDecoration.collapsed(
hintText: 'postContentPlaceholder'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
2024-05-19 10:01:00 +00:00
),
),
),
Container(
constraints: const BoxConstraints(minHeight: 56),
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 0.3, color: Theme.of(context).dividerColor),
2024-05-19 10:01:00 +00:00
),
),
child: Row(
children: [
TextButton(
style: TextButton.styleFrom(shape: const CircleBorder()),
child: const Icon(Icons.camera_alt),
2024-05-26 13:03:25 +00:00
onPressed: () => showAttachments(),
2024-05-19 10:01:00 +00:00
)
],
),
),
],
),
),
),
);
}
}