Merge pull request #74 from krawieck/post-creation-pictrs

This commit is contained in:
Filip Krawczyk 2020-10-24 21:49:07 +02:00 committed by GitHub
commit 8b141ca6d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 101 additions and 9 deletions

View File

@ -41,5 +41,13 @@
</array> </array>
<key>UIViewControllerBasedStatusBarAppearance</key> <key>UIViewControllerBasedStatusBarAppearance</key>
<false/> <false/>
<!-- Image picker -->
<key>NSPhotoLibraryUsageDescription</key>
<string>For uploading images for posts/avatars</string>
<key>NSCameraUsageDescription</key>
<string>For uploading images for posts/avatars</string>
<key>NSMicrophoneUsageDescription</key>
<string>For recording videos for posts</string>
</dict> </dict>
</plist> </plist>

View File

@ -0,0 +1,4 @@
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:image_picker/image_picker.dart';
ImagePicker useImagePicker() => useMemoized(() => ImagePicker());

View File

@ -1,14 +1,17 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:image_picker/image_picker.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../hooks/delayed_loading.dart'; import '../hooks/delayed_loading.dart';
import '../hooks/image_picker.dart';
import '../hooks/logged_in_action.dart'; import '../hooks/logged_in_action.dart';
import '../hooks/memo_future.dart'; import '../hooks/memo_future.dart';
import '../hooks/stores.dart'; import '../hooks/stores.dart';
import '../util/extensions/api.dart'; import '../util/extensions/api.dart';
import '../util/goto.dart'; import '../util/goto.dart';
import '../util/pictrs.dart';
import '../util/spaced.dart'; import '../util/spaced.dart';
import '../widgets/markdown_text.dart'; import '../widgets/markdown_text.dart';
import 'full_post.dart'; import 'full_post.dart';
@ -48,6 +51,9 @@ class CreatePost extends HookWidget {
final showFancy = useState(false); final showFancy = useState(false);
final nsfw = useState(false); final nsfw = useState(false);
final delayed = useDelayedLoading(); final delayed = useDelayedLoading();
final imagePicker = useImagePicker();
final imageUploadLoading = useState(false);
final pictrsDeleteToken = useState<PictrsUploadFile>(null);
final allCommunitiesSnap = useMemoFuture( final allCommunitiesSnap = useMemoFuture(
() => LemmyApi(selectedInstance.value) () => LemmyApi(selectedInstance.value)
@ -66,6 +72,39 @@ class CreatePost extends HookWidget {
[selectedInstance.value], [selectedInstance.value],
); );
uploadPicture() async {
try {
final pic = await imagePicker.getImage(source: ImageSource.gallery);
// pic is null when the picker was cancelled
if (pic != null) {
imageUploadLoading.value = true;
final pictrs = LemmyApi(selectedInstance.value).pictrs;
final upload = await pictrs.upload(pic.path);
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 {
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(
@ -113,13 +152,30 @@ class CreatePost extends HookWidget {
), ),
); );
final url = TextField( final url = Row(children: [
controller: urlController, Expanded(
decoration: InputDecoration( child: TextField(
border: OutlineInputBorder(), enabled: pictrsDeleteToken.value == null,
labelText: 'URL', controller: urlController,
suffixIcon: Icon(Icons.link)), decoration: InputDecoration(
); border: OutlineInputBorder(),
labelText: 'URL',
suffixIcon: Icon(Icons.link)),
),
),
SizedBox(width: 5),
IconButton(
icon: imageUploadLoading.value
? CircularProgressIndicator()
: Icon(pictrsDeleteToken.value == null
? Icons.add_photo_alternate
: Icons.close),
onPressed:
pictrsDeleteToken.value == null ? uploadPicture : removePicture,
tooltip:
pictrsDeleteToken.value == null ? 'Add picture' : 'Delete picture',
)
]);
final title = TextField( final title = TextField(
controller: titleController, controller: titleController,

2
lib/util/pictrs.dart Normal file
View File

@ -0,0 +1,2 @@
String pathToPictrs(String instanceUrl, String imgId) =>
'https://$instanceUrl/pictrs/image/$imgId';

View File

@ -265,6 +265,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0+2" version: "1.1.0+2"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.11"
flutter_slidable: flutter_slidable:
dependency: "direct main" dependency: "direct main"
description: description:
@ -338,6 +345,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.1.4" version: "3.1.4"
image_picker:
dependency: "direct main"
description:
name: image_picker
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.7+12"
image_picker_platform_interface:
dependency: transitive
description:
name: image_picker_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
intl: intl:
dependency: transitive dependency: transitive
description: description:
@ -379,7 +400,7 @@ packages:
name: lemmy_api_client name: lemmy_api_client
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.0" version: "0.7.3"
logging: logging:
dependency: transitive dependency: transitive
description: description:

View File

@ -34,6 +34,7 @@ dependencies:
url_launcher: ^5.5.1 url_launcher: ^5.5.1
shared_preferences: ">=0.5.0 <2.0.0" shared_preferences: ">=0.5.0 <2.0.0"
package_info: ^0.4.3 package_info: ^0.4.3
image_picker: ^0.6.7
# state management # state management
flutter_hooks: ^0.13.2 flutter_hooks: ^0.13.2
@ -44,7 +45,7 @@ dependencies:
# utils # utils
timeago: ^2.0.27 timeago: ^2.0.27
fuzzy: <1.0.0 fuzzy: <1.0.0
lemmy_api_client: ^0.6.0 lemmy_api_client: ^0.7.3
flutter: flutter:
sdk: flutter sdk: flutter