Solian/lib/screens/posts/moment_editor.dart

188 lines
5.8 KiB
Dart
Raw Normal View History

2024-04-14 07:58:27 +00:00
import 'dart:convert';
2024-04-13 17:45:27 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
2024-04-14 10:38:44 +00:00
import 'package:http/http.dart';
2024-04-14 07:58:27 +00:00
import 'package:provider/provider.dart';
import 'package:solian/models/post.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/utils/service_url.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2024-04-26 12:49:21 +00:00
import 'package:solian/widgets/account/avatar.dart';
2024-04-29 12:22:06 +00:00
import 'package:solian/widgets/exts.dart';
2024-04-14 07:58:27 +00:00
import 'package:solian/widgets/indent_wrapper.dart';
import 'package:solian/widgets/posts/attachment_editor.dart';
2024-04-13 17:45:27 +00:00
2024-04-14 10:38:44 +00:00
class MomentEditorScreen extends StatefulWidget {
final Post? editing;
const MomentEditorScreen({super.key, this.editing});
2024-04-13 17:45:27 +00:00
2024-04-14 07:58:27 +00:00
@override
2024-04-14 10:38:44 +00:00
State<MomentEditorScreen> createState() => _MomentEditorScreenState();
2024-04-14 07:58:27 +00:00
}
2024-04-14 10:38:44 +00:00
class _MomentEditorScreenState extends State<MomentEditorScreen> {
2024-04-14 07:58:27 +00:00
final _textController = TextEditingController();
2024-04-14 10:38:44 +00:00
String? _alias;
2024-04-14 07:58:27 +00:00
bool _isSubmitting = false;
List<Attachment> _attachments = List.empty(growable: true);
void viewAttachments(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context) => AttachmentEditor(
2024-04-19 11:36:03 +00:00
provider: 'interactive',
2024-04-14 07:58:27 +00:00
current: _attachments,
onUpdate: (value) => _attachments = value,
),
);
}
2024-04-14 10:38:44 +00:00
Future<void> applyPost(BuildContext context) async {
setState(() => _isSubmitting = true);
2024-04-14 07:58:27 +00:00
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
2024-04-14 07:58:27 +00:00
2024-04-14 10:38:44 +00:00
final uri = widget.editing == null
? getRequestUri('interactive', '/api/p/moments')
: getRequestUri('interactive', '/api/p/moments/${widget.editing!.id}');
2024-05-01 16:49:38 +00:00
final req = Request(widget.editing == null ? 'POST' : 'PUT', uri);
2024-04-14 10:38:44 +00:00
req.headers['Content-Type'] = 'application/json';
req.body = jsonEncode(<String, dynamic>{
'alias': _alias,
'content': _textController.value.text,
'attachments': _attachments,
});
var res = await Response.fromStream(await auth.client!.send(req));
2024-04-14 07:58:27 +00:00
if (res.statusCode != 200) {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-14 07:58:27 +00:00
} else {
if (router.canPop()) {
router.pop(true);
}
}
setState(() => _isSubmitting = false);
}
2024-04-16 14:45:22 +00:00
void cancelEditing() {
if (router.canPop()) {
router.pop(false);
2024-04-16 14:45:22 +00:00
}
}
2024-04-14 10:38:44 +00:00
@override
void initState() {
if (widget.editing != null) {
_alias = widget.editing!.alias;
_textController.text = widget.editing!.content;
_attachments = widget.editing!.attachments ?? List.empty(growable: true);
}
super.initState();
}
2024-04-13 17:45:27 +00:00
@override
Widget build(BuildContext context) {
2024-04-14 07:58:27 +00:00
final auth = context.read<AuthProvider>();
2024-04-13 17:45:27 +00:00
2024-04-16 14:45:22 +00:00
final editingBanner = MaterialBanner(
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 20),
leading: const Icon(Icons.edit_note),
2024-05-01 14:35:58 +00:00
backgroundColor: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.9),
2024-04-16 14:45:22 +00:00
dividerColor: const Color.fromARGB(1, 0, 0, 0),
content: Text(AppLocalizations.of(context)!.postEditNotify),
actions: [
TextButton(
child: Text(AppLocalizations.of(context)!.cancel),
onPressed: () => cancelEditing(),
),
],
);
2024-04-14 07:58:27 +00:00
return IndentWrapper(
hideDrawer: true,
title: AppLocalizations.of(context)!.newMoment,
appBarActions: <Widget>[
TextButton(
2024-04-14 10:38:44 +00:00
onPressed: !_isSubmitting ? () => applyPost(context) : null,
child: Text(AppLocalizations.of(context)!.postVerb.toUpperCase()),
2024-04-14 07:58:27 +00:00
),
],
2024-05-01 09:37:34 +00:00
child: Column(
children: [
_isSubmitting
? const LinearProgressIndicator().animate().scaleX()
: Container(),
FutureBuilder(
future: auth.getProfiles(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var userinfo = snapshot.data;
return ListTile(
2024-05-01 16:49:38 +00:00
title: Text(userinfo['nick']),
2024-05-01 09:37:34 +00:00
subtitle: Text(
AppLocalizations.of(context)!.postIdentityNotify,
2024-04-14 07:58:27 +00:00
),
2024-05-01 09:37:34 +00:00
leading: AccountAvatar(
2024-05-01 16:49:38 +00:00
source: userinfo['picture'],
2024-05-01 09:37:34 +00:00
direct: true,
2024-04-14 07:58:27 +00:00
),
2024-05-01 09:37:34 +00:00
);
} else {
return Container();
}
},
),
const Divider(thickness: 0.3),
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: TextField(
maxLines: null,
autofocus: true,
autocorrect: true,
keyboardType: TextInputType.multiline,
controller: _textController,
decoration: InputDecoration.collapsed(
hintText:
AppLocalizations.of(context)!.postContentPlaceholder,
2024-04-14 07:58:27 +00:00
),
2024-05-01 09:37:34 +00:00
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
2024-04-14 07:58:27 +00:00
),
2024-05-01 09:37:34 +00:00
),
2024-04-14 07:58:27 +00:00
),
2024-05-01 09:37:34 +00:00
widget.editing != null ? editingBanner : Container(),
Container(
2024-05-02 04:16:01 +00:00
decoration: BoxDecoration(
2024-05-01 09:37:34 +00:00
border: Border(
2024-05-02 04:16:01 +00:00
top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
2024-05-01 09:37:34 +00:00
),
),
child: Row(
children: [
TextButton(
style: TextButton.styleFrom(shape: const CircleBorder()),
child: const Icon(Icons.camera_alt),
onPressed: () => viewAttachments(context),
)
],
),
),
],
2024-04-13 17:45:27 +00:00
),
);
}
2024-04-14 07:58:27 +00:00
}