2024-04-14 07:58:27 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-04-13 17:45:27 +00:00
|
|
|
import 'package:flutter/material.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';
|
|
|
|
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(
|
|
|
|
current: _attachments,
|
|
|
|
onUpdate: (value) => _attachments = value,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-14 10:38:44 +00:00
|
|
|
Future<void> applyPost(BuildContext context) async {
|
2024-04-14 07:58:27 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
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}');
|
|
|
|
|
|
|
|
final req = Request(widget.editing == null ? "POST" : "PUT", uri);
|
|
|
|
req.headers['Content-Type'] = 'application/json';
|
|
|
|
req.body = jsonEncode(<String, dynamic>{
|
|
|
|
'alias': _alias,
|
|
|
|
'content': _textController.value.text,
|
|
|
|
'attachments': _attachments,
|
|
|
|
});
|
|
|
|
|
2024-04-14 07:58:27 +00:00
|
|
|
setState(() => _isSubmitting = true);
|
2024-04-14 10:38:44 +00:00
|
|
|
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);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(content: Text("Something went wrong... $message")),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (router.canPop()) {
|
|
|
|
router.pop(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
),
|
|
|
|
],
|
|
|
|
child: Center(
|
|
|
|
child: Container(
|
|
|
|
constraints: const BoxConstraints(maxWidth: 640),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
_isSubmitting ? const LinearProgressIndicator() : Container(),
|
|
|
|
FutureBuilder(
|
|
|
|
future: auth.getProfiles(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
var userinfo = snapshot.data;
|
|
|
|
return ListTile(
|
|
|
|
title: Text(userinfo["nick"]),
|
|
|
|
subtitle: Text(
|
|
|
|
AppLocalizations.of(context)!.postIdentityNotify,
|
|
|
|
),
|
|
|
|
leading: CircleAvatar(
|
|
|
|
backgroundImage: NetworkImage(userinfo["picture"]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} 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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
decoration: const BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
top: BorderSide(width: 0.3, color: Color(0xffdedede)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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
|
|
|
}
|