Skip to content

Commit

Permalink
refactor: switch to image_picker
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Feb 19, 2025
1 parent faa93d7 commit ebece12
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 71 deletions.
81 changes: 23 additions & 58 deletions packages/app/lib/src/app/wrapper_page.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/// This library wraps the application in a shared scaffold.
library;

import 'dart:io';
import 'dart:math';

import 'package:appwrite/appwrite.dart';
import 'package:auto_route/auto_route.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:image_picker/image_picker.dart';

import '../features/auth/application/auth_service.dart';
import '../features/home/application/location_service.dart';
Expand Down Expand Up @@ -160,7 +158,7 @@ class _Dialog extends HookConsumerWidget {
padding: const EdgeInsets.all(16),
child: Column(
children: [
//TODOcreate guards against creating empty posts
// TODO(MattsAttack): guard against creating empty posts.
TextFormField(
initialValue: title.value,
onSaved: (value) {
Expand All @@ -184,38 +182,17 @@ class _Dialog extends HookConsumerWidget {
const _UploadedImagesView(),
ElevatedButton(
onPressed: () async {
//TODOupload max image count. probably 10
//Would also need to update post entity
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: [
'jpg',
'jpeg',
'png',
'webp',
], // Might be weird on iPhones with HEIC. Could convert to png with some library
allowMultiple: true,
);
// TODO(MattsAttack): Set a max image upload count, probably 10.
final picker = ImagePicker();
final pickedFiles = await picker.pickMultiImage();

if (result == null) return;
if (result.files.length > 1) {
for (var i = 0; i < result.files.length; i++) {
ref
.read(uploadedImagesServiceProvider.notifier)
.addImage(
UploadedImageEntity(
imageID: ID.unique(),
file: result.files[i],
),
);
}
} else {
for (final pickedFile in pickedFiles) {
ref
.read(uploadedImagesServiceProvider.notifier)
.addImage(
UploadedImageEntity(
imageID: ID.unique(),
file: result.files.first,
file: pickedFile,
),
);
}
Expand All @@ -241,34 +218,22 @@ class _UploadedImagesView extends HookConsumerWidget {
//Need to build a list of images. have it so you can horizontally scroll through images and have an x to remove them
@override
Widget build(BuildContext context, WidgetRef ref) {
print('this is running fr');
final uploadedImages = ref.watch(uploadedImagesServiceProvider);
if (uploadedImages.isEmpty) {
return const SizedBox();
}

//TODOmake this scale better
return SizedBox(
height: 500,
width: 500,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
for (final image in uploadedImages)
Builder(
builder: (context) {
final file = image.file;
// TODO(MattsAttack): add a way to remove an image
if (kIsWeb && file.bytes != null) {
return Image.memory(file.bytes!, width: 400, height: 400);
} else if (file.path != null) {
return Image.file(File(file.path!), width: 400, height: 400);
}
return const Placeholder();
},
),
],
final uploadedImages = ref.watch(uploadedImagesBytesProvider);

return switch (uploadedImages) {
AsyncData(:final value) when value.isNotEmpty => SizedBox(
height: 500,
width: 500,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
// TODO(MattsAttack): make this scale better
for (final bytes in value) Image.memory(bytes),
],
),
),
);
AsyncLoading() => const CircularProgressIndicator(),
_ => const SizedBox(),
};
}
}
4 changes: 3 additions & 1 deletion packages/app/lib/src/features/auth/data/auth_repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// This library provides authentication fetchers.
library;

import 'dart:developer';

import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand Down Expand Up @@ -66,7 +68,7 @@ final class _AppwriteAuthRepository implements AuthRepository {
password: password,
);
} on AppwriteException catch (e) {
print(e);
log('$e');
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:typed_data';

import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../domain/uploaded_image_entity.dart';
Expand All @@ -24,3 +27,13 @@ class UploadedImagesService extends _$UploadedImagesService {
);
}
}

@riverpod
Future<IList<Uint8List>> uploadedImagesBytes(Ref ref) async {
final uploadedImages = ref.watch(uploadedImagesServiceProvider);
final bytes = await Future.wait(
uploadedImages.map((image) async => await image.file.readAsBytes()),
);

return bytes.lock;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:file_picker/file_picker.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:image_picker/image_picker.dart';

part 'uploaded_image_entity.freezed.dart';

Expand All @@ -8,6 +8,6 @@ part 'uploaded_image_entity.freezed.dart';
class UploadedImageEntity with _$UploadedImageEntity {
const factory UploadedImageEntity({
required String imageID,
required PlatformFile file,
required XFile file,
}) = _UploadedImageEntity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class _PostInteractables extends HookConsumerWidget {

return Row(
children: [
Text(post.likes.length.toString()),
Text('${post.likes.length}'),
IconButton(
onPressed: () async {
if (userId == null) {
Expand Down
8 changes: 0 additions & 8 deletions packages/app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.0.1"
file_picker:
dependency: "direct main"
description:
name: file_picker
sha256: c904b4ab56d53385563c7c39d8e9fa9af086f91495dfc48717ad84a42c3cf204
url: "https://pub.dev"
source: hosted
version: "8.1.7"
file_selector_linux:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion packages/app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ dependencies:
dio: ^5.8.0+1
envied: ^1.1.1
fast_immutable_collections: ^11.0.3
file_picker: ^8.1.7
flutter:
sdk: flutter
flutter_hooks: ^0.21.1-pre.4
Expand Down

0 comments on commit ebece12

Please sign in to comment.