lemmur-app-android/lib/widgets/write_comment.dart

132 lines
3.6 KiB
Dart
Raw Normal View History

2020-09-21 01:37:34 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
2020-09-21 01:37:34 +02:00
2020-09-21 13:20:07 +02:00
import '../hooks/delayed_loading.dart';
import '../hooks/stores.dart';
2021-03-01 14:21:45 +01:00
import '../l10n/l10n.dart';
2021-02-24 20:52:18 +01:00
import 'markdown_mode_icon.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 {
2021-02-24 20:52:18 +01:00
final Post post;
final Comment comment;
2020-09-21 01:37:34 +02:00
2020-09-21 13:20:07 +02:00
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
2020-09-21 01:37:34 +02:00
2021-02-24 20:52:18 +01:00
WriteComment.toPost(this.post) : comment = null;
WriteComment.toComment({@required this.comment, @required this.post})
: assert(comment != null),
assert(post != null);
2020-09-21 01:37:34 +02:00
@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(
2021-02-24 20:52:18 +01:00
comment?.content ?? post.body,
instanceHost: post.instanceHost,
2020-09-21 01:37:34 +02:00
);
if (post != null) {
return Column(
children: [
Text(
2021-02-24 20:52:18 +01:00
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 {
2021-02-24 20:52:18 +01:00
final api = LemmyApiV2(post.instanceHost);
2020-09-21 13:20:07 +02:00
2021-02-24 20:52:18 +01:00
final token = accStore.defaultTokenFor(post.instanceHost);
2020-09-21 13:20:07 +02:00
delayed.start();
try {
2021-01-24 20:01:55 +01:00
final res = await api.run(CreateComment(
content: controller.text,
2021-02-24 20:52:18 +01:00
postId: post.id,
parentId: comment?.id,
2021-01-24 20:01:55 +01:00
auth: token.raw,
));
2021-01-24 21:51:07 +01:00
Navigator.of(context).pop(res.commentView);
2020-09-21 13:20:07 +02:00
// ignore: avoid_catches_without_on_clauses
} catch (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(
2021-02-09 15:12:13 +01:00
leading: const CloseButton(),
2020-09-21 01:37:34 +02:00
actions: [
IconButton(
2021-02-24 20:52:18 +01:00
icon: markdownModeIcon(fancy: showFancy.value),
2020-09-21 01:37:34 +02:00
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,
),
Padding(
padding: const EdgeInsets.all(16),
child: MarkdownText(
controller.text,
2021-02-24 20:52:18 +01:00
instanceHost: post.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: [
2021-02-09 15:12:13 +01:00
TextButton(
2020-09-21 13:20:07 +02:00
onPressed: delayed.pending ? () {} : handleSubmit,
child: delayed.loading
2021-01-03 19:43:39 +01:00
? const CircularProgressIndicator()
2021-03-01 14:21:45 +01:00
: Text(L10n.of(context).post),
2020-09-21 13:20:07 +02:00
)
],
),
2020-09-21 01:37:34 +02:00
],
),
);
}
}