Skip to content

Commit

Permalink
wip: work
Browse files Browse the repository at this point in the history
@MattsAttack, you'll be pleased to hear that there's less mapping.
I'd forgotten about "collection for", which is like a list comprehension.
  • Loading branch information
lishaduck committed Feb 19, 2025
1 parent 698526e commit faa93d7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 99 deletions.
41 changes: 20 additions & 21 deletions packages/app/lib/src/app/wrapper_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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' show kIsWeb;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -22,8 +22,6 @@ import '../features/home/domain/uploaded_image_entity.dart';
import '../utils/hooks.dart';
import 'router.gr.dart';

// final uploadedImageProvider = StateProvider<PlatformFile?>((ref) => null);

/// {@template nexus.app.wrapper_page}
/// Wrap the pages in a Material Design scaffold.
/// {@endtemplate}
Expand Down Expand Up @@ -100,9 +98,6 @@ class _Dialog extends HookConsumerWidget {
final userId = ref.watch(idProvider);
final userName = ref.watch(userNameProvider);

///
// PlatformFile? selectedFile;

final handleSubmit = useCallback(() async {
final uploadedImages = ref.watch(uploadedImagesServiceProvider);

Check warning on line 102 in packages/app/lib/src/app/wrapper_page.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/app/wrapper_page.dart#L102

Added line #L102 was not covered by tests
final location = await ref.read(locationServiceProvider.future);
Expand All @@ -119,8 +114,6 @@ class _Dialog extends HookConsumerWidget {
formKey.currentState?.save();

// Create a list off all uploaded images ids
final List<String> uploadedImageIDs =
uploadedImages.map((image) => image.imageID).toList();

await ref
.read(postRepositoryProvider)
Expand All @@ -135,8 +128,11 @@ class _Dialog extends HookConsumerWidget {
timestamp: DateTime.timestamp(),
likes: const IList.empty(),
id: PostId(ID.unique()),
imageID: uploadedImageIDs, //Read in list of uploaded images ids
imageID:
// Read in the list of uploaded images ids.
uploadedImages.map((image) => image.imageID).toIList(),

Check warning on line 133 in packages/app/lib/src/app/wrapper_page.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/app/wrapper_page.dart#L133

Added line #L133 was not covered by tests
),
uploadedImages,
);

if (!context.mounted) return;
Expand Down Expand Up @@ -257,18 +253,21 @@ class _UploadedImagesView extends HookConsumerWidget {
width: 500,
child: ListView(

Check warning on line 254 in packages/app/lib/src/app/wrapper_page.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/app/wrapper_page.dart#L254

Added line #L254 was not covered by tests
scrollDirection: Axis.horizontal,
children:
uploadedImages.map((image) {
final file = image.file;
//TODOadd 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();
}).toList(),
children: [
for (final image in uploadedImages)
Builder(
builder: (context) {
final file = image.file;

Check warning on line 260 in packages/app/lib/src/app/wrapper_page.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/app/wrapper_page.dart#L256-L260

Added lines #L256 - L260 were not covered by tests
// 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);

Check warning on line 265 in packages/app/lib/src/app/wrapper_page.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/app/wrapper_page.dart#L262-L265

Added lines #L262 - L265 were not covered by tests
}
return const Placeholder();
},
),
],
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,5 @@ base class WipPost extends _$WipPost {

@riverpod

Check warning on line 103 in packages/app/lib/src/features/home/application/feed_service.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/application/feed_service.dart#L103

Added line #L103 was not covered by tests
Future<Uint8List> image(Ref ref, String id) {
return ref
.watch(
postRepositoryProvider,
)
.getImages(id);
return ref.watch(postRepositoryProvider).getImages(id);

Check warning on line 105 in packages/app/lib/src/features/home/application/feed_service.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/application/feed_service.dart#L105

Added line #L105 was not covered by tests
}
57 changes: 22 additions & 35 deletions packages/app/lib/src/features/home/data/post_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../../env/env.dart';
import '../../../utils/api.dart';
import '../../auth/domain/user.dart';
import '../application/uploaded_image_service.dart';
import '../domain/feed_entity.dart';
import '../domain/post_entity.dart';
import '../domain/uploaded_image_entity.dart';

part 'post_repository.g.dart';

Expand All @@ -29,15 +29,14 @@ abstract interface class PostRepository {
/// Returns the created post.
Future<void> createNewPost(
PostEntity post,
IList<UploadedImageEntity> images,
);

/// Toggle if a user is listed as having liked a post.
Future<void> toggleLikePost(PostId postId, UserId userId, Likes likes);

///Uploads image to Appwrite and returns id
Future<void> uploadImage(
int index,
);
/// Upload an image to Appwrite.
Future<void> uploadImage(UploadedImageEntity file);

Future<Uint8List> getImages(String id);
}
Expand All @@ -48,7 +47,6 @@ final class _AppwritePostRepository implements PostRepository {
required this.storage,
required this.databaseId,
required this.collectionId,
required this.ref,
});

final Databases database;
Expand All @@ -57,8 +55,6 @@ final class _AppwritePostRepository implements PostRepository {
final String databaseId;
final String collectionId;

final Ref ref; //@lishaduck is this the correct way to access a ref here?

@override
Future<IList<PostEntity>> readPosts(FeedEntity feed, PostId? cursor) async {
final documentList = await database.listDocuments(
Expand Down Expand Up @@ -91,23 +87,18 @@ final class _AppwritePostRepository implements PostRepository {

@override
Future<Uint8List> getImages(String id) async {
// Update for multi image
// TODO(MattsAttack): Update for multi-image support.
return await storage.getFileView(bucketId: 'post-media', fileId: id);

Check warning on line 91 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L91

Added line #L91 was not covered by tests
}

@override

Check warning on line 94 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L94

Added line #L94 was not covered by tests
Future<void> createNewPost(
PostEntity post,
IList<UploadedImageEntity> images,
) async {
//Need to figure out why images aren't uploading
final imageID =
post.imageID; // Extracting locally reduces chance for null upon access
if (imageID != null) {
for (var i = 0; i < imageID.length; i++) {
await uploadImage(
i,
); // Could be cool to implement a upload status bar. Posts with 10 images will take a while and that should help
}
// Could be cool to implement a upload status bar. Posts with 10 images will take a while and that should help
for (final image in images) {
await uploadImage(image);

Check warning on line 101 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L100-L101

Added lines #L100 - L101 were not covered by tests
}

await database.createDocument(
Expand All @@ -129,37 +120,34 @@ final class _AppwritePostRepository implements PostRepository {
}

@override

Check warning on line 122 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L122

Added line #L122 was not covered by tests
Future<void> uploadImage(
int index,
) async {
final uploadedImages = ref.watch(uploadedImagesServiceProvider);
final selectedFile = uploadedImages[index].file;

Future<void> uploadImage(UploadedImageEntity file) async {
//Path is for non web
if (!kIsWeb) {
final path = selectedFile.path;
final path = file.file.path;

Check warning on line 126 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L126

Added line #L126 was not covered by tests
if (path != null) {
// Upload via path
final fileName = '${DateTime.now().microsecondsSinceEpoch}'
final fileName =
'${DateTime.now().microsecondsSinceEpoch}'
"${path.split(".").last}";

Check warning on line 131 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L129-L131

Added lines #L129 - L131 were not covered by tests

final file = await storage.createFile(
await storage.createFile(

Check warning on line 133 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L133

Added line #L133 was not covered by tests
bucketId: 'post-media', //maybe change to env variable
fileId: uploadedImages[index].imageID,
fileId: file.imageID,
file: InputFile.fromPath(path: path, filename: fileName),

Check warning on line 136 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L135-L136

Added lines #L135 - L136 were not covered by tests
);
}
}
//Byte upload is for web
else if (kIsWeb) {
final bytes = selectedFile.bytes;
final bytes = file.file.bytes;

Check warning on line 142 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L142

Added line #L142 was not covered by tests
if (bytes != null) {
// Upload via bytes
final fileName = '${DateTime.now().microsecondsSinceEpoch}'
'-${selectedFile.name}'; //TODOimplement time here
final file = await storage.createFile(
bucketId: 'post-media', //maybe change to env variable
fileId: uploadedImages[index].imageID,
final fileName =
'${DateTime.now().microsecondsSinceEpoch}'
'-${file.file.name}'; // TODO(MattsAttack): Add time here.
await storage.createFile(

Check warning on line 148 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L145-L148

Added lines #L145 - L148 were not covered by tests
bucketId: 'post-media', // TODO(MattsAttack): Use an dotenv variable.

Check warning on line 149 in packages/app/lib/src/features/home/data/post_repository.dart

View workflow job for this annotation

GitHub Actions / Check Spelling

Unknown word (dotenv)
fileId: file.imageID,
file: InputFile.fromBytes(bytes: bytes, filename: fileName),

Check warning on line 151 in packages/app/lib/src/features/home/data/post_repository.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/data/post_repository.dart#L150-L151

Added lines #L150 - L151 were not covered by tests
);
}
Expand All @@ -179,6 +167,5 @@ PostRepository postRepository(Ref ref) {
storage: storage,
databaseId: Env.databaseId,
collectionId: Env.postsCollectionId,
ref: ref,
);
}
2 changes: 1 addition & 1 deletion packages/app/lib/src/features/home/domain/post_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ sealed class PostEntity with _$PostEntity {
@JsonKey(includeToJson: false) required PostId id,

/// Contains ID of image in bucket
List<String?>? imageID,
required IList<String> imageID,
}) = _PostEntity;

factory PostEntity.fromJson(Map<String, dynamic> json) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ part 'uploaded_image_entity.freezed.dart';
@immutable
@freezed
class UploadedImageEntity with _$UploadedImageEntity {
// Might need to add IsWeb in here

const factory UploadedImageEntity({
required String imageID,
required PlatformFile file,
Expand Down
52 changes: 17 additions & 35 deletions packages/app/lib/src/features/home/presentation/home/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,42 +179,24 @@ class _PostImage extends ConsumerWidget {

@override

Check warning on line 180 in packages/app/lib/src/features/home/presentation/home/post.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/presentation/home/post.dart#L180

Added line #L180 was not covered by tests
Widget build(BuildContext context, WidgetRef ref) {
//TODOfix images not caching correctly
// TODO(lishaduck): fix images not caching correctly.
final post = ref.watch(wipPostProvider(postId))!;

Check warning on line 183 in packages/app/lib/src/features/home/presentation/home/post.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/presentation/home/post.dart#L183

Added line #L183 was not covered by tests
if (post.imageID != null) {
final imageIDs = post.imageID;
final imageIDsLength = imageIDs?.length;
if (imageIDsLength == null) return const SizedBox(height: 1);
if (imageIDsLength == 1) {
final image = ref.watch(imageProvider(imageIDs![0]!));
return switch (image) {
AsyncData(:final value) => Image.memory(value),
AsyncError() => const Text('Error loading image'),
_ => const CircularProgressIndicator(),
};
} else if (imageIDsLength > 1) {
final List<AsyncValue<Uint8List>> images = [];
for (var i = 0; i < imageIDsLength; i++) {
images.add(ref.watch(imageProvider(imageIDs![i]!)));
}
return SizedBox(
height: 500,
width: MediaQuery.sizeOf(context).width / 1.5,
child: ListView(
scrollDirection: Axis.horizontal,
children:
images.map((image) {
return switch (image) {
AsyncData(:final value) => Image.memory(value),
AsyncError() => const Text('Error loading image'),
_ => const CircularProgressIndicator(),
};
}).toList(),
),
);
}
}
return const SizedBox(height: 1);

return SizedBox(

Check warning on line 185 in packages/app/lib/src/features/home/presentation/home/post.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/presentation/home/post.dart#L185

Added line #L185 was not covered by tests
height: 500,
width: MediaQuery.sizeOf(context).width / 1.5,
child: ListView(

Check warning on line 188 in packages/app/lib/src/features/home/presentation/home/post.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/presentation/home/post.dart#L187-L188

Added lines #L187 - L188 were not covered by tests
scrollDirection: Axis.horizontal,
children: [
for (final imageId in post.imageID)
switch (ref.watch(imageProvider(imageId))) {
AsyncData(:final value) => Image.memory(value),
AsyncError() => const Text('Error loading image'),

Check warning on line 194 in packages/app/lib/src/features/home/presentation/home/post.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/presentation/home/post.dart#L190-L194

Added lines #L190 - L194 were not covered by tests
_ => const CircularProgressIndicator(),
},
],
),
);
}

@override

Check warning on line 202 in packages/app/lib/src/features/home/presentation/home/post.dart

View check run for this annotation

Codecov / codecov/patch

packages/app/lib/src/features/home/presentation/home/post.dart#L202

Added line #L202 was not covered by tests
Expand Down

0 comments on commit faa93d7

Please sign in to comment.