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

feat(mobile): adds crop and rotate to mobile #10989

Merged
merged 26 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
160 changes: 160 additions & 0 deletions mobile/lib/pages/editing/crop.page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import 'package:flutter/material.dart';
import 'package:crop_image/crop_image.dart';
import 'edit.page.dart';

class CropImagePage extends StatefulWidget {
final Image image;
const CropImagePage({super.key, required this.image});

@override
State<CropImagePage> createState() => _CropImagePageState();
}

class _CropImagePageState extends State<CropImagePage> {
late CropController _cropController;
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
double? _selectedAspectRatio;

@override
void initState() {
super.initState();
_cropController = CropController(
defaultCrop: const Rect.fromLTRB(0.1, 0.1, 0.9, 0.9),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
leading: const CloseButton(color: Colors.white),
actions: [
IconButton(
icon: const Icon(Icons.done_rounded, color: Colors.white, size: 24),
onPressed: () async {
Image croppedImage = await _cropController.croppedImage();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => EditImagePage(image: croppedImage)),
);
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
},
),
],
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Column(
children: [
Container(
padding: const EdgeInsets.only(top: 20),
width: constraints.maxWidth * 0.8,
height: constraints.maxHeight * 0.6,
child: CropImage(
controller: _cropController,
image: widget.image,
gridColor: Colors.white,
),
),
Expanded(
child: Container(
width: double.infinity,
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(
left: 20, right: 20, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(Icons.rotate_left,
color: Colors.white),
onPressed: () {
_cropController.rotateLeft();
},
),
IconButton(
icon: const Icon(Icons.rotate_right,
color: Colors.white),
onPressed: () {
_cropController.rotateRight();
},
),
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
_buildAspectRatioButton(context, null, 'Free'),
_buildAspectRatioButton(context, 1.0, '1:1'),
_buildAspectRatioButton(
context, 16.0 / 9.0, '16:9'),
_buildAspectRatioButton(context, 3.0 / 2.0, '3:2'),
_buildAspectRatioButton(context, 7.0 / 5.0, '7:5'),
],
),
],
),
),
),
),
],
);
},
),
);
}

Widget _buildAspectRatioButton(
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
BuildContext context, double? aspectRatio, String label) {
IconData iconData;
switch (label) {
case 'Free':
iconData = Icons.crop_free_rounded;
break;
case '1:1':
iconData = Icons.crop_square_rounded;
break;
case '16:9':
iconData = Icons.crop_16_9_rounded;
break;
case '3:2':
iconData = Icons.crop_3_2_rounded;
break;
case '7:5':
iconData = Icons.crop_7_5_rounded;
break;
default:
iconData = Icons.crop_free_rounded;
}

return Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(
iconData,
color: _selectedAspectRatio == aspectRatio
? Colors.indigo
: Colors.white,
),
onPressed: () => setState(() {
_cropController.aspectRatio = aspectRatio;
_selectedAspectRatio = aspectRatio;
}),
),
Text(label, style: Theme.of(context).textTheme.bodyMedium),
],
);
}
}
107 changes: 107 additions & 0 deletions mobile/lib/pages/editing/edit.page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:immich_mobile/pages/editing/crop.page.dart';
import 'package:immich_mobile/entities/asset.entity.dart';
import 'package:immich_mobile/widgets/common/immich_image.dart';
import 'package:immich_mobile/widgets/common/immich_toast.dart';
@immutable
class EditImagePage extends StatelessWidget {
final Asset? asset;
final Image? image;
const EditImagePage({
super.key,
this.image,
this.asset,
}) : assert((image != null && asset == null) || (image == null && asset != null), 'Must supply one of asset or image');
ImageProvider _getImageProvider() {
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
if (asset is Asset) {
return ImmichImage.imageProvider(asset: asset);
} else if (image is Image) {
return (image as Image).image;
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw Exception('Invalid image source type');
}
}
void _navigateToCropImagePage(BuildContext context) {
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CropImagePage(image: _getImageWidget()),
),
);
}
Image _getImageWidget() {
Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
if (asset != null) {
return Image(image: ImmichImage.imageProvider(asset: asset));
} else if (image != null) {
return image!;
} else {
throw Exception('Invalid image source type');
}
}
@override
Widget build(BuildContext context) {
final ImageProvider provider = _getImageProvider();

Yuvi-raj-P marked this conversation as resolved.
Show resolved Hide resolved
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
martyfuhry marked this conversation as resolved.
Show resolved Hide resolved
leading: IconButton(
icon: const Icon(Icons.close_rounded, color: Colors.white, size: 24),
onPressed: () => Navigator.of(context).popUntil((route) => route.isFirst),
),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.done_rounded, color: Colors.white, size: 24),
onPressed: () {
// Save the image into cloud
if (asset != null) {
ImmichToast.show(
durationInSecond: 1,
context: context,
msg: 'No edits made!',
gravity: ToastGravity.BOTTOM,
);
martyfuhry marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Handle saving the edited image
}
},
),
],
),
body: Column(
children: <Widget>[
Expanded(
child: Image(image: provider),
),
Container(
height: 80,
color: Colors.black,
),
],
),
bottomNavigationBar: Container(
height: 60,
margin: const EdgeInsets.only(bottom: 50, right: 10, left: 10, top: 10),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(30),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
icon: Icon(
Platform.isAndroid ? Icons.crop_rotate_rounded : Icons.crop_rotate_rounded,
color: Colors.white,
),
onPressed: () => _navigateToCropImagePage(context),
),
],
),
),
);
}
}
26 changes: 26 additions & 0 deletions mobile/lib/widgets/asset_viewer/bottom_gallery_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:immich_mobile/providers/asset.provider.dart';
import 'package:immich_mobile/providers/server_info.provider.dart';
import 'package:immich_mobile/providers/user.provider.dart';
import 'package:immich_mobile/widgets/common/immich_toast.dart';
import 'package:immich_mobile/pages/editing/edit.page.dart';

class BottomGalleryBar extends ConsumerWidget {
final Asset asset;
Expand Down Expand Up @@ -66,6 +67,12 @@ class BottomGalleryBar extends ConsumerWidget {
label: 'control_bottom_app_bar_share'.tr(),
tooltip: 'control_bottom_app_bar_share'.tr(),
),
if (asset.isImage)
BottomNavigationBarItem(
icon: const Icon(Icons.edit_outlined),
label: 'control_bottom_app_bar_edit'.tr(),
tooltip: 'control_bottom_app_bar_edit'.tr(),
),
if (isOwner)
asset.isArchived
? BottomNavigationBarItem(
Expand Down Expand Up @@ -270,6 +277,24 @@ class BottomGalleryBar extends ConsumerWidget {
ref.read(imageViewerStateProvider.notifier).shareAsset(asset, context);
}

void handleEdit() async {
if (asset.isOffline) {
ImmichToast.show(
durationInSecond: 1,
context: context,
msg: 'asset_action_edit_err_offline'.tr(),
gravity: ToastGravity.BOTTOM,
);
return;
}
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
EditImagePage(asset: asset), // Send the Asset object
),
);
}

handleArchive() {
ref.read(assetProvider.notifier).toggleArchive([asset]);
if (isParent) {
Expand Down Expand Up @@ -301,6 +326,7 @@ class BottomGalleryBar extends ConsumerWidget {

List<Function(int)> actionslist = [
(_) => shareAsset(),
if (asset.isImage) (_) => handleEdit(),
if (isOwner) (_) => handleArchive(),
if (isOwner && stack.isNotEmpty) (_) => showStackActionItems(),
if (isOwner) (_) => handleDelete(),
Expand Down
8 changes: 8 additions & 0 deletions mobile/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.1"
crop_image:
dependency: "direct main"
description:
name: crop_image
sha256: "6cf20655ecbfba99c369d43ec7adcfa49bf135af88fb75642173d6224a95d3f1"
url: "https://pub.dev"
source: hosted
version: "1.0.13"
cross_file:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ dependencies:
thumbhash: 0.1.0+1
async: ^2.11.0

#image editing packages
crop_image: ^1.0.13
openapi:
path: openapi

Expand Down