1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-15 10:04:13 +01:00
lemmur-app-android/lib/pages/create_post.dart

293 lines
9.1 KiB
Dart
Raw Normal View History

2020-09-22 23:50:18 +02:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2020-10-24 00:12:42 +02:00
import 'package:image_picker/image_picker.dart';
2020-09-22 23:50:18 +02:00
import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../hooks/delayed_loading.dart';
2020-10-24 00:12:42 +02:00
import '../hooks/image_picker.dart';
2020-09-22 23:50:18 +02:00
import '../hooks/logged_in_action.dart';
import '../hooks/memo_future.dart';
2020-09-22 23:50:18 +02:00
import '../hooks/stores.dart';
import '../util/extensions/api.dart';
2020-10-23 00:38:25 +02:00
import '../util/extensions/spaced.dart';
2020-09-22 23:50:18 +02:00
import '../util/goto.dart';
2020-10-24 00:12:42 +02:00
import '../util/pictrs.dart';
2020-09-22 23:50:18 +02:00
import '../widgets/markdown_text.dart';
import 'full_post.dart';
2020-09-30 19:05:00 +02:00
/// Fab that triggers the [CreatePost] modal
2020-09-22 23:50:18 +02:00
class CreatePostFab extends HookWidget {
@override
Widget build(BuildContext context) {
final loggedInAction = useLoggedInAction(null, any: true);
return FloatingActionButton(
child: Icon(Icons.add),
onPressed: loggedInAction((_) => showCupertinoModalPopup(
context: context, builder: (_) => CreatePost())),
);
}
}
2020-09-30 19:05:00 +02:00
/// Modal for creating a post to some community in some instance
2020-09-22 23:50:18 +02:00
class CreatePost extends HookWidget {
final CommunityView community;
2020-09-22 23:50:18 +02:00
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
CreatePost() : community = null;
CreatePost.toCommunity(this.community);
2020-09-22 23:50:18 +02:00
@override
Widget build(BuildContext context) {
final urlController = useTextEditingController();
final titleController = useTextEditingController();
final bodyController = useTextEditingController();
final accStore = useAccountsStore();
2020-09-26 12:43:34 +02:00
final selectedInstance =
useState(community?.instanceUrl ?? accStore.loggedInInstances.first);
final selectedCommunity = useState(community);
2020-09-22 23:50:18 +02:00
final showFancy = useState(false);
final nsfw = useState(false);
final delayed = useDelayedLoading();
2020-10-24 00:12:42 +02:00
final imagePicker = useImagePicker();
final imageUploadLoading = useState(false);
2020-10-24 11:26:52 +02:00
final pictrsDeleteToken = useState<PictrsUploadFile>(null);
2020-09-22 23:50:18 +02:00
2020-09-29 15:00:13 +02:00
final allCommunitiesSnap = useMemoFuture(
() => LemmyApi(selectedInstance.value)
.v1
.listCommunities(
sort: SortType.hot,
limit: 9999,
auth: accStore.defaultTokenFor(selectedInstance.value).raw,
)
.then(
(value) {
value.sort((a, b) => a.name.compareTo(b.name));
return value;
},
),
[selectedInstance.value],
);
2020-10-24 00:12:42 +02:00
uploadPicture() async {
2020-10-24 19:58:38 +02:00
try {
final pic = await imagePicker.getImage(source: ImageSource.gallery);
// pic is null when the picker was cancelled
if (pic != null) {
imageUploadLoading.value = true;
2020-10-24 11:26:52 +02:00
2020-10-24 19:58:38 +02:00
final pictrs = LemmyApi(selectedInstance.value).pictrs;
final upload = await pictrs.upload(pic.path);
2020-10-24 11:26:52 +02:00
2020-10-24 19:58:38 +02:00
pictrsDeleteToken.value = upload.files[0];
urlController.text =
pathToPictrs(selectedInstance.value, upload.files[0].file);
}
// ignore: avoid_catches_without_on_clauses
} catch (e) {
scaffoldKey.currentState
.showSnackBar(SnackBar(content: Text('Failed to upload image')));
} finally {
2020-10-24 00:12:42 +02:00
imageUploadLoading.value = false;
}
}
2020-10-24 11:26:52 +02:00
removePicture() {
LemmyApi(selectedInstance.value)
.pictrs
.delete(pictrsDeleteToken.value)
.catchError((_) {});
pictrsDeleteToken.value = null;
urlController.text = '';
}
2020-09-26 12:43:34 +02:00
// TODO: use drop down from AddAccountPage
2020-09-22 23:50:18 +02:00
final instanceDropdown = InputDecorator(
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 1, horizontal: 20),
border: OutlineInputBorder()),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: selectedInstance.value,
onChanged: (val) => selectedInstance.value = val,
2020-09-26 12:43:34 +02:00
items: accStore.loggedInInstances
2020-09-22 23:50:18 +02:00
.map((instance) => DropdownMenuItem(
value: instance,
child: Text(instance),
))
.toList(),
),
),
);
// TODO: use lazy autocomplete
final communitiesDropdown = InputDecorator(
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 1, horizontal: 20),
border: OutlineInputBorder()),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: selectedCommunity.value?.name,
hint: Text('Community'),
onChanged: (val) => selectedCommunity.value =
2020-09-29 15:00:13 +02:00
allCommunitiesSnap.data.firstWhere((e) => e.name == val),
items: allCommunitiesSnap.hasData
? allCommunitiesSnap.data
.map((e) => DropdownMenuItem(
value: e.name,
child: Text(e.name),
))
.toList()
: [
DropdownMenuItem(
value: '',
child: CircularProgressIndicator(),
)
],
),
),
);
2020-10-24 00:12:42 +02:00
final url = Row(children: [
Expanded(
child: TextField(
2020-10-24 11:26:52 +02:00
enabled: pictrsDeleteToken.value == null,
2020-10-24 00:12:42 +02:00
controller: urlController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'URL',
suffixIcon: Icon(Icons.link)),
),
),
SizedBox(width: 5),
IconButton(
icon: imageUploadLoading.value
? CircularProgressIndicator()
2020-10-24 11:26:52 +02:00
: Icon(pictrsDeleteToken.value == null
? Icons.add_photo_alternate
: Icons.close),
onPressed:
pictrsDeleteToken.value == null ? uploadPicture : removePicture,
tooltip:
pictrsDeleteToken.value == null ? 'Add picture' : 'Delete picture',
2020-10-24 00:12:42 +02:00
)
]);
2020-09-22 23:50:18 +02:00
final title = TextField(
controller: titleController,
minLines: 1,
maxLines: 2,
decoration:
InputDecoration(border: OutlineInputBorder(), labelText: 'Title'),
);
final body = IndexedStack(
index: showFancy.value ? 1 : 0,
children: [
TextField(
controller: bodyController,
2020-10-24 22:43:29 +02:00
keyboardType: TextInputType.multiline,
2020-09-22 23:50:18 +02:00
maxLines: null,
2020-10-25 01:16:58 +02:00
minLines: 5,
2020-09-22 23:50:18 +02:00
textAlignVertical: TextAlignVertical.top,
decoration:
InputDecoration(border: OutlineInputBorder(), labelText: 'Body'),
),
Padding(
padding: const EdgeInsets.all(16),
child: MarkdownText(
bodyController.text,
instanceUrl: selectedInstance.value,
),
2020-10-24 22:43:29 +02:00
),
2020-09-22 23:50:18 +02:00
],
);
handleSubmit() async {
if (selectedCommunity.value == null || titleController.text.isEmpty) {
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text('Choosing a community and a title is required'),
));
return;
}
2020-09-22 23:50:18 +02:00
final api = LemmyApi(selectedInstance.value).v1;
final token = accStore.defaultTokenFor(selectedInstance.value);
delayed.start();
try {
final res = await api.createPost(
url: urlController.text.isEmpty ? null : urlController.text,
body: bodyController.text.isEmpty ? null : bodyController.text,
nsfw: nsfw.value,
name: titleController.text,
communityId: selectedCommunity.value.id,
2020-09-22 23:50:18 +02:00
auth: token.raw);
2020-09-28 20:28:06 +02:00
goToReplace(context, (_) => FullPostPage.fromPostView(res));
2020-09-22 23:50:18 +02:00
return;
// ignore: avoid_catches_without_on_clauses
} catch (e) {
scaffoldKey.currentState
.showSnackBar(SnackBar(content: Text('Failed to post')));
}
delayed.cancel();
}
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.close),
onPressed: Navigator.of(context).pop,
),
actions: [
IconButton(
icon: Icon(showFancy.value ? Icons.build : Icons.brush),
onPressed: () => showFancy.value = !showFancy.value,
),
],
),
2020-09-29 15:00:50 +02:00
body: SafeArea(
2020-10-24 22:43:29 +02:00
child: ListView(
padding: EdgeInsets.all(5),
2020-10-23 00:38:25 +02:00
children: [
2020-09-29 15:00:50 +02:00
instanceDropdown,
communitiesDropdown,
url,
title,
2020-10-24 22:43:29 +02:00
body,
2020-09-29 15:00:50 +02:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () => nsfw.value = !nsfw.value,
child: Row(
children: [
Checkbox(
value: nsfw.value,
onChanged: (val) => nsfw.value = val,
),
Text('NSFW')
],
),
2020-09-22 23:50:18 +02:00
),
2020-09-29 15:00:50 +02:00
FlatButton(
onPressed: delayed.pending ? () {} : handleSubmit,
child: delayed.loading
? CircularProgressIndicator()
: Text('post'),
)
],
),
2020-10-23 00:38:25 +02:00
].spaced(6),
2020-09-29 15:00:50 +02:00
),
2020-09-22 23:50:18 +02:00
),
);
}
}