add option to remove pictures

This commit is contained in:
shilangyu 2020-10-24 11:26:52 +02:00
parent affd078fc8
commit a41e5c19c3
1 changed files with 22 additions and 4 deletions

View File

@ -53,6 +53,7 @@ class CreatePost extends HookWidget {
final delayed = useDelayedLoading(); final delayed = useDelayedLoading();
final imagePicker = useImagePicker(); final imagePicker = useImagePicker();
final imageUploadLoading = useState(false); final imageUploadLoading = useState(false);
final pictrsDeleteToken = useState<PictrsUploadFile>(null);
final allCommunitiesSnap = useMemoFuture( final allCommunitiesSnap = useMemoFuture(
() => LemmyApi(selectedInstance.value) () => LemmyApi(selectedInstance.value)
@ -76,16 +77,27 @@ class CreatePost extends HookWidget {
// pic is null when the picker was cancelled // pic is null when the picker was cancelled
if (pic != null) { if (pic != null) {
imageUploadLoading.value = true; imageUploadLoading.value = true;
final pictrs = LemmyApi(selectedInstance.value).pictrs; final pictrs = LemmyApi(selectedInstance.value).pictrs;
// TODO: consider auto removing pictures when not used
// TODO: would help with preventing people from abusing uploads
final upload = await pictrs.upload(pic.path); final upload = await pictrs.upload(pic.path);
pictrsDeleteToken.value = upload.files[0];
urlController.text = urlController.text =
pathToPictrs(selectedInstance.value, upload.files[0].file); pathToPictrs(selectedInstance.value, upload.files[0].file);
imageUploadLoading.value = false; imageUploadLoading.value = false;
} }
} }
removePicture() {
LemmyApi(selectedInstance.value)
.pictrs
.delete(pictrsDeleteToken.value)
.catchError((_) {});
pictrsDeleteToken.value = null;
urlController.text = '';
}
// TODO: use drop down from AddAccountPage // TODO: use drop down from AddAccountPage
final instanceDropdown = InputDecorator( final instanceDropdown = InputDecorator(
decoration: const InputDecoration( decoration: const InputDecoration(
@ -136,6 +148,7 @@ class CreatePost extends HookWidget {
final url = Row(children: [ final url = Row(children: [
Expanded( Expanded(
child: TextField( child: TextField(
enabled: pictrsDeleteToken.value == null,
controller: urlController, controller: urlController,
decoration: InputDecoration( decoration: InputDecoration(
border: OutlineInputBorder(), border: OutlineInputBorder(),
@ -147,8 +160,13 @@ class CreatePost extends HookWidget {
IconButton( IconButton(
icon: imageUploadLoading.value icon: imageUploadLoading.value
? CircularProgressIndicator() ? CircularProgressIndicator()
: Icon(Icons.add_photo_alternate), : Icon(pictrsDeleteToken.value == null
onPressed: uploadPicture, ? Icons.add_photo_alternate
: Icons.close),
onPressed:
pictrsDeleteToken.value == null ? uploadPicture : removePicture,
tooltip:
pictrsDeleteToken.value == null ? 'Add picture' : 'Delete picture',
) )
]); ]);