Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): Fix for incorrectly naming edited files and structure change #11741

Merged
merged 6 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions mobile/lib/pages/editing/crop.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:crop_image/crop_image.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:immich_mobile/routing/router.dart';
import 'package:immich_mobile/utils/hooks/crop_controller_hook.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'edit.page.dart';
import 'package:auto_route/auto_route.dart';

Expand All @@ -14,7 +15,8 @@ import 'package:auto_route/auto_route.dart';
@RoutePage()
class CropImagePage extends HookWidget {
final Image image;
const CropImagePage({super.key, required this.image});
final Asset asset;
const CropImagePage({super.key, required this.image, required this.asset});

@override
Widget build(BuildContext context) {
Expand All @@ -34,7 +36,13 @@ class CropImagePage extends HookWidget {
),
onPressed: () async {
final croppedImage = await cropController.croppedImage();
context.pushRoute(EditImageRoute(image: croppedImage));
context.pushRoute(
EditImageRoute(
asset: asset,
image: croppedImage,
isEdited: true,
),
);
},
),
],
Expand Down
100 changes: 48 additions & 52 deletions mobile/lib/pages/editing/edit.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:immich_mobile/widgets/common/immich_toast.dart';
import 'package:auto_route/auto_route.dart';
import 'package:immich_mobile/routing/router.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:path/path.dart' as p;
import 'package:immich_mobile/providers/album/album.provider.dart';

/// A stateless widget that provides functionality for editing an image.
Expand All @@ -24,18 +25,16 @@ import 'package:immich_mobile/providers/album/album.provider.dart';
@immutable
@RoutePage()
class EditImagePage extends ConsumerWidget {
final Asset? asset;
final Image? image;
final Asset asset;
final Image image;
final bool isEdited;

const EditImagePage({
super.key,
this.image,
this.asset,
}) : assert(
(image != null && asset == null) || (image == null && asset != null),
'Must supply one of asset or image',
);

required this.asset,
required this.image,
required this.isEdited,
});
Future<Uint8List> _imageToUint8List(Image image) async {
final Completer<Uint8List> completer = Completer();
image.image.resolve(const ImageConfiguration()).addListener(
Expand All @@ -58,19 +57,34 @@ class EditImagePage extends ConsumerWidget {
return completer.future;
}

Future<void> _saveEditedImage(
BuildContext context,
Asset asset,
Image image,
WidgetRef ref,
) async {
try {
final Uint8List imageData = await _imageToUint8List(image);
await PhotoManager.editor.saveImage(
imageData,
title: "${p.withoutExtension(asset.fileName)}_edited.jpg",
);
await ref.read(albumProvider.notifier).getDeviceAlbums();
Navigator.of(context).popUntil((route) => route.isFirst);
} catch (e) {
ImmichToast.show(
durationInSecond: 6,
context: context,
msg: 'Error: $e',
gravity: ToastGravity.CENTER,
);
}
}

@override
Widget build(BuildContext context, WidgetRef ref) {
final ImageProvider provider = (asset != null)
? ImmichImage.imageProvider(asset: asset!)
: (image != null)
? image!.image
: throw Exception('Invalid image source type');

final Image imageWidget = (asset != null)
? Image(image: ImmichImage.imageProvider(asset: asset!))
: (image != null)
? image!
: throw Exception('Invalid image source type');
final Image imageWidget =
Image(image: ImmichImage.imageProvider(asset: asset));

return Scaffold(
appBar: AppBar(
Expand All @@ -85,44 +99,24 @@ class EditImagePage extends ConsumerWidget {
Navigator.of(context).popUntil((route) => route.isFirst),
),
actions: <Widget>[
if (image != null)
TextButton(
onPressed: () async {
try {
final Uint8List imageData = await _imageToUint8List(image!);
ImmichToast.show(
durationInSecond: 3,
context: context,
msg: 'Image Saved!',
gravity: ToastGravity.CENTER,
);

await PhotoManager.editor.saveImage(
imageData,
title: '${asset!.fileName}_edited.jpg',
);
await ref.read(albumProvider.notifier).getDeviceAlbums();
Navigator.of(context).popUntil((route) => route.isFirst);
} catch (e) {
ImmichToast.show(
durationInSecond: 6,
context: context,
msg: 'Error: ${e.toString()}',
gravity: ToastGravity.BOTTOM,
);
}
},
child: Text(
'Save to gallery',
style: Theme.of(context).textTheme.displayMedium,
TextButton(
onPressed: isEdited
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
? () => _saveEditedImage(context, asset, image, ref)
: null,
child: Text(
'Save to gallery',
style: TextStyle(
color:
isEdited ? Theme.of(context).iconTheme.color : Colors.grey,
),
),
),
],
),
body: Column(
children: <Widget>[
Expanded(
child: Image(image: provider),
child: image,
),
Container(
height: 80,
Expand All @@ -148,7 +142,9 @@ class EditImagePage extends ConsumerWidget {
color: Theme.of(context).iconTheme.color,
),
onPressed: () {
context.pushRoute(CropImageRoute(image: imageWidget));
context.pushRoute(
CropImageRoute(asset: asset, image: imageWidget),
);
},
),
Text('Crop', style: Theme.of(context).textTheme.displayMedium),
Expand Down
35 changes: 23 additions & 12 deletions mobile/lib/routing/router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions mobile/lib/widgets/asset_viewer/bottom_gallery_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart'
import 'package:immich_mobile/widgets/asset_viewer/video_controls.dart';
import 'package:immich_mobile/widgets/asset_grid/delete_dialog.dart';
import 'package:immich_mobile/routing/router.dart';
import 'package:immich_mobile/widgets/common/immich_image.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:immich_mobile/providers/asset.provider.dart';
import 'package:immich_mobile/providers/server_info.provider.dart';
Expand Down Expand Up @@ -236,6 +237,7 @@ class BottomGalleryBar extends ConsumerWidget {
}

void handleEdit() async {
final image = Image(image: ImmichImage.imageProvider(asset: asset));
if (asset.isOffline) {
ImmichToast.show(
durationInSecond: 1,
Expand All @@ -247,8 +249,11 @@ class BottomGalleryBar extends ConsumerWidget {
}
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
EditImagePage(asset: asset), // Send the Asset object
builder: (context) => EditImagePage(
asset: asset,
image: image,
isEdited: false,
),
),
);
}
Expand Down
Loading