2020-09-21 01:37:34 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
2020-09-21 13:20:07 +02:00
|
|
|
import '../hooks/delayed_loading.dart';
|
|
|
|
import '../hooks/stores.dart';
|
2020-09-21 01:37:34 +02:00
|
|
|
import 'markdown_text.dart';
|
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// Modal for writing a comment to a given post/comment (aka reply)
|
2020-09-21 01:37:34 +02:00
|
|
|
/// on submit pops the navigator stack with a [CommentView]
|
|
|
|
/// or `null` if cancelled
|
|
|
|
class WriteComment extends HookWidget {
|
|
|
|
final PostView post;
|
|
|
|
final CommentView comment;
|
|
|
|
|
2020-12-31 14:58:23 +01:00
|
|
|
final String instanceHost;
|
2020-09-21 13:20:07 +02:00
|
|
|
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
|
2020-09-21 01:37:34 +02:00
|
|
|
|
|
|
|
WriteComment.toPost(this.post)
|
2020-12-31 14:58:23 +01:00
|
|
|
: instanceHost = post.instanceHost,
|
2020-09-21 01:37:34 +02:00
|
|
|
comment = null;
|
|
|
|
WriteComment.toComment(this.comment)
|
2020-12-31 14:58:23 +01:00
|
|
|
: instanceHost = comment.instanceHost,
|
2020-09-21 01:37:34 +02:00
|
|
|
post = null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final controller = useTextEditingController();
|
|
|
|
final showFancy = useState(false);
|
2020-09-21 13:20:07 +02:00
|
|
|
final delayed = useDelayedLoading();
|
|
|
|
final accStore = useAccountsStore();
|
2020-09-21 01:37:34 +02:00
|
|
|
|
|
|
|
final preview = () {
|
|
|
|
final body = MarkdownText(
|
|
|
|
comment?.content ?? post.body ?? '',
|
2020-12-31 14:58:23 +01:00
|
|
|
instanceHost: instanceHost,
|
2020-09-21 01:37:34 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (post != null) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
post.name,
|
2021-01-03 19:43:39 +01:00
|
|
|
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
|
2020-09-21 01:37:34 +02:00
|
|
|
),
|
2021-01-03 19:43:39 +01:00
|
|
|
const SizedBox(height: 4),
|
2020-09-21 01:37:34 +02:00
|
|
|
body,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}();
|
|
|
|
|
2020-09-21 13:20:07 +02:00
|
|
|
handleSubmit() async {
|
2020-12-31 14:58:23 +01:00
|
|
|
final api = LemmyApi(instanceHost).v1;
|
2020-09-21 13:20:07 +02:00
|
|
|
|
2020-12-31 14:58:23 +01:00
|
|
|
final token = accStore.defaultTokenFor(instanceHost);
|
2020-09-21 13:20:07 +02:00
|
|
|
|
|
|
|
delayed.start();
|
|
|
|
try {
|
|
|
|
final res = await api.createComment(
|
|
|
|
content: controller.text,
|
|
|
|
postId: post?.id ?? comment.postId,
|
2020-09-28 20:24:30 +02:00
|
|
|
parentId: comment?.id,
|
2020-09-21 13:20:07 +02:00
|
|
|
auth: token.raw);
|
|
|
|
Navigator.of(context).pop(res);
|
|
|
|
// ignore: avoid_catches_without_on_clauses
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2021-01-03 19:43:39 +01:00
|
|
|
scaffoldKey.currentState.showSnackBar(
|
|
|
|
const SnackBar(content: Text('Failed to post comment')));
|
2020-09-21 13:20:07 +02:00
|
|
|
}
|
|
|
|
delayed.cancel();
|
|
|
|
}
|
2020-09-21 01:37:34 +02:00
|
|
|
|
|
|
|
return Scaffold(
|
2020-09-21 13:20:07 +02:00
|
|
|
key: scaffoldKey,
|
2020-09-21 01:37:34 +02:00
|
|
|
appBar: AppBar(
|
|
|
|
leading: IconButton(
|
2021-01-03 19:43:39 +01:00
|
|
|
icon: const Icon(Icons.close),
|
2020-09-21 01:37:34 +02:00
|
|
|
onPressed: Navigator.of(context).pop,
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(showFancy.value ? Icons.build : Icons.brush),
|
|
|
|
onPressed: () => showFancy.value = !showFancy.value,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-10-25 01:20:31 +02:00
|
|
|
body: ListView(
|
2020-09-21 01:37:34 +02:00
|
|
|
children: [
|
|
|
|
ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(
|
2020-09-21 13:20:07 +02:00
|
|
|
maxHeight: MediaQuery.of(context).size.height * .35),
|
2020-09-21 01:37:34 +02:00
|
|
|
child: SingleChildScrollView(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: preview,
|
|
|
|
),
|
|
|
|
),
|
2021-01-03 19:43:39 +01:00
|
|
|
const Divider(),
|
2020-10-25 12:30:46 +01:00
|
|
|
IndexedStack(
|
|
|
|
index: showFancy.value ? 1 : 0,
|
|
|
|
children: [
|
|
|
|
TextField(
|
|
|
|
controller: controller,
|
|
|
|
autofocus: true,
|
|
|
|
minLines: 5,
|
|
|
|
maxLines: null,
|
|
|
|
textAlignVertical: TextAlignVertical.top,
|
2021-01-03 19:43:39 +01:00
|
|
|
decoration: const InputDecoration(border: OutlineInputBorder()),
|
2020-10-25 12:30:46 +01:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
child: MarkdownText(
|
|
|
|
controller.text,
|
2020-12-31 14:58:23 +01:00
|
|
|
instanceHost: instanceHost,
|
2020-09-21 01:37:34 +02:00
|
|
|
),
|
2020-10-25 12:30:46 +01:00
|
|
|
)
|
|
|
|
],
|
2020-09-21 01:37:34 +02:00
|
|
|
),
|
2020-09-21 13:20:07 +02:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
FlatButton(
|
|
|
|
onPressed: delayed.pending ? () {} : handleSubmit,
|
|
|
|
child: delayed.loading
|
2021-01-03 19:43:39 +01:00
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const Text('post'),
|
2020-09-21 13:20:07 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2020-09-21 01:37:34 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|