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

152 lines
4.3 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-04-05 20:14:39 +02:00
import 'package:lemmy_api_client/v3.dart';
2020-09-21 01:37:34 +02:00
2020-09-21 13:20:07 +02:00
import '../hooks/delayed_loading.dart';
2021-04-11 00:20:47 +02:00
import '../hooks/logged_in_action.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';
2021-04-17 22:45:18 +02:00
/// Modal for writing/editing 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;
2021-04-17 22:45:18 +02:00
final bool _isEdit;
2020-09-21 01:37:34 +02:00
2021-04-17 22:45:18 +02:00
const WriteComment.toPost(this.post)
: comment = null,
_isEdit = false;
const WriteComment.toComment({
required Comment this.comment,
required this.post,
2021-04-17 22:45:18 +02:00
}) : _isEdit = false;
const WriteComment.edit({
required Comment this.comment,
required this.post,
}) : _isEdit = true;
2020-09-21 01:37:34 +02:00
@override
Widget build(BuildContext context) {
2021-04-17 22:45:18 +02:00
final controller =
useTextEditingController(text: _isEdit ? comment?.content : null);
2020-09-21 01:37:34 +02:00
final showFancy = useState(false);
2020-09-21 13:20:07 +02:00
final delayed = useDelayedLoading();
final loggedInAction = useLoggedInAction(post.instanceHost);
2020-09-21 01:37:34 +02:00
final preview = () {
2021-04-05 20:14:39 +02:00
final body = () {
final text = comment?.content ?? post.body;
if (text == null) return const SizedBox.shrink();
return MarkdownText(
text,
instanceHost: post.instanceHost,
selectable: true,
);
2021-04-05 20:14:39 +02:00
}();
2020-09-21 01:37:34 +02:00
return Column(
children: [
SelectableText(
post.name,
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
),
const SizedBox(height: 4),
body,
],
);
2020-09-21 01:37:34 +02:00
}();
handleSubmit(Jwt token) async {
2021-04-05 20:14:39 +02:00
final api = LemmyApiV3(post.instanceHost);
2020-09-21 13:20:07 +02:00
delayed.start();
try {
2021-04-17 22:45:18 +02:00
final res = await () {
if (_isEdit) {
return api.run(EditComment(
commentId: comment!.id,
content: controller.text,
auth: token.raw,
));
} else {
return api.run(CreateComment(
content: controller.text,
postId: post.id,
parentId: comment?.id,
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-03-10 08:34:30 +01:00
ScaffoldMessenger.of(context).showSnackBar(
2021-01-03 19:43:39 +01:00
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(
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,
2021-04-11 17:19:44 +02:00
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
2020-10-25 12:30:46 +01:00
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(
onPressed:
delayed.pending ? () {} : loggedInAction(handleSubmit),
2020-09-21 13:20:07 +02:00
child: delayed.loading
2021-01-03 19:43:39 +01:00
? const CircularProgressIndicator()
2021-04-17 22:45:18 +02:00
: Text(_isEdit
? L10n.of(context)!.edit
: L10n.of(context)!.post),
2020-09-21 13:20:07 +02:00
)
],
),
2020-09-21 01:37:34 +02:00
],
),
);
}
}