lemmur-app-android/lib/pages/write_message.dart

139 lines
4.1 KiB
Dart
Raw Normal View History

2021-02-24 20:52:18 +01: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';
2021-02-24 20:52:18 +01: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 '../util/extensions/api.dart';
import '../widgets/markdown_mode_icon.dart';
import '../widgets/markdown_text.dart';
/// Page for writing and editing a private message
class WriteMessagePage extends HookWidget {
2021-04-05 20:14:39 +02:00
final PersonSafe recipient;
2021-02-24 20:52:18 +01:00
final String instanceHost;
/// if it's non null then this page is used for edit
final PrivateMessage? privateMessage;
2021-02-24 20:52:18 +01:00
final bool _isEdit;
2021-03-10 08:34:30 +01:00
const WriteMessagePage.send({
required this.recipient,
required this.instanceHost,
}) : privateMessage = null,
2021-02-24 20:52:18 +01:00
_isEdit = false;
WriteMessagePage.edit(PrivateMessageView pmv)
: privateMessage = pmv.privateMessage,
recipient = pmv.recipient,
instanceHost = pmv.instanceHost,
_isEdit = true;
@override
Widget build(BuildContext context) {
final showFancy = useState(false);
final bodyController =
useTextEditingController(text: privateMessage?.content);
final loading = useState(false);
final loggedInAction = useLoggedInAction(instanceHost);
final submit = _isEdit ? L10n.of(context)!.save : 'send';
final title = _isEdit ? 'Edit message' : L10n.of(context)!.send_message;
2021-02-24 20:52:18 +01:00
handleSubmit(Jwt token) async {
2021-02-24 20:52:18 +01:00
if (_isEdit) {
loading.value = true;
try {
2021-04-05 20:14:39 +02:00
final msg = await LemmyApiV3(instanceHost).run(EditPrivateMessage(
auth: token.raw,
privateMessageId: privateMessage!.id,
2021-02-24 20:52:18 +01:00
content: bodyController.text,
));
Navigator.of(context).pop(msg);
// ignore: avoid_catches_without_on_clauses
} catch (e) {
2021-03-10 08:34:30 +01:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2021-02-24 20:52:18 +01:00
content: Text(e.toString()),
));
}
loading.value = false;
} else {
loading.value = true;
try {
2021-04-05 20:14:39 +02:00
await LemmyApiV3(instanceHost).run(CreatePrivateMessage(
auth: token.raw,
2021-02-24 20:52:18 +01:00
content: bodyController.text,
recipientId: recipient.id,
));
Navigator.of(context).pop();
// TODO: maybe send notification so that infinite list
// containing this widget adds new message?
// ignore: avoid_catches_without_on_clauses
} catch (e) {
2021-03-10 08:34:30 +01:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2021-02-24 20:52:18 +01:00
content: Text(e.toString()),
));
} finally {
loading.value = false;
}
}
}
final body = IndexedStack(
index: showFancy.value ? 1 : 0,
children: [
TextField(
controller: bodyController,
keyboardType: TextInputType.multiline,
maxLines: null,
minLines: 5,
autofocus: true,
),
Padding(
padding: const EdgeInsets.all(16),
child: MarkdownText(
bodyController.text,
instanceHost: instanceHost,
),
),
],
);
return Scaffold(
appBar: AppBar(
title: Text(title),
leading: const CloseButton(),
actions: [
IconButton(
icon: markdownModeIcon(fancy: showFancy.value),
onPressed: () => showFancy.value = !showFancy.value,
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
Text('to ${recipient.displayName}'),
const SizedBox(height: 16),
body,
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: loading.value ? () {} : loggedInAction(handleSubmit),
2021-02-24 20:52:18 +01:00
child: loading.value
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator())
: Text(submit),
),
),
],
),
),
);
}
}