Skip to content

Commit

Permalink
feat: image picker and cropper
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanRns committed May 9, 2024
1 parent 94d10b7 commit e14e6fe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 124 deletions.
9 changes: 8 additions & 1 deletion evently/lib/repository/repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import 'package:evently/utils/file_utils_helper.dart';
import 'package:injectable/injectable.dart';

abstract class Repository {}

@LazySingleton(as: Repository)
class RepositoryImp implements Repository {}
class RepositoryImp implements Repository {

RepositoryImp({required this.fileUtilsHelper});

final FileUtilsHelper fileUtilsHelper;

}
4 changes: 4 additions & 0 deletions evently/lib/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const kMaxPriceLength = 14;
const int kBigIntBase = 1000000;
const int kEthIntBase = 1000000000000000000;

final List<String> imageAllowedExts = ["png", "jpg", "jpeg", "heif"];
const kPylons = "Pylons";

/// ```SVG assets
class SVGUtils {
static const kSvgSplash = 'assets/images/svg/splash.svg';
Expand All @@ -48,6 +51,7 @@ class PngUtils {
static const kIconDenomUsd = 'assets/images/denom_usd.png';
static const kIconDenomPylon = 'assets/images/denom_pylon.png';
static const kHostPreview = "assets/images/host_preview.png";

/// will remove this variable for ui dev
/// i need this variable to be used
static const kPhantom = "assets/images/phantom.png";
Expand Down
134 changes: 11 additions & 123 deletions evently/lib/utils/file_utils_helper.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
import 'dart:io';
import 'dart:math';

import 'package:evently/models/picked_file_model.dart';

import '../generated/locale_keys.g.dart';

import 'package:evently/utils/constants.dart';
import 'package:evently/utils/evently_app_theme.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:injectable/injectable.dart';

@lazySingleton
abstract class FileUtilsHelper {
/// This function picks a file with the given format from device storage
/// Input: [format] it is the file format which needs to be picked from local storage
/// returns [PlatformFile] the selected file or null if aborted
Future<PickedFileModel> pickFile();

/// This function is used to get the file size in String format
/// Input: [fileLength] the file length in bytes and [precision]
/// Output: [String] returns the file size in String format
String getFileSizeString({required int fileLength, required int precision});

/// This function is used to generate the NFT link to be shared with others after publishing
/// Input: [recipeId] and [cookbookId] used in the link generation as query parameters
/// Output: [String] returns the generated NFTs link to be shared with others
String generateEaselLinkForShare({required String recipeId, required String cookbookId});

/// This function is used to generate the NFT link to be open in the pylons wallet
/// Input: [recipeId] and [cookbookId] used in the link generation as query parameters
/// Output: [String] returns the generated NFTs link to be shared with others
String generateEventlyLinkForOpeningInPylonsApp({required String recipeId, required String cookbookId});

/// This function is used to launch the link generated and open the link in external source platform
/// Input: [url] is the link to be launched by the launcher
Future<void> launchMyUrl({required String url});

/// This function will copy the file to internal memory returns the updated path
/// Input: [filePickerResult] is the filePickerResult instance of the file to be copied
/// Output: [String] This will return the updated filePath
Future<File> copyFileToInternal({required FilePickerResult filePickerResult});
}

class FileUtilsHelperImpl implements FileUtilsHelper {
Expand All @@ -44,36 +22,7 @@ class FileUtilsHelperImpl implements FileUtilsHelper {

@override
Future<PickedFileModel> pickFile() async {
FileType type;
List<String>? allowedExtensions;
switch (format.format) {
case NFTTypes.image:
if (Platform.isAndroid) {
type = FileType.custom;
allowedExtensions = imageAllowedExts;
break;
}
type = FileType.image;
break;

case NFTTypes.video:
type = FileType.video;
break;

case NFTTypes.audio:
if (!Platform.isAndroid) {
type = FileType.custom;
allowedExtensions = audioAllowedExts;
} else {
type = FileType.audio;
}
break;
default:
type = FileType.any;
break;
}

final FilePickerResult? result = await filePicker.pickFiles(type: type, allowedExtensions: allowedExtensions);
final FilePickerResult? result = await filePicker.pickFiles(type: FileType.image, allowedExtensions: imageAllowedExts);

if (result == null) {
return PickedFileModel(
Expand All @@ -83,15 +32,7 @@ class FileUtilsHelperImpl implements FileUtilsHelper {
);
}

if (format.format == NFTTypes.threeD && result.files.single.path != null) {
final newFile = await copyFileToInternal(filePickerResult: result);
return PickedFileModel(
path: newFile.path,
fileName: newFile.path.split('/').last,
extension: result.files.single.extension ?? "",
);
}
if (format.format == NFTTypes.image && result.files.single.path != null && result.files.single.extension != 'gif') {
if (result.files.single.path != null) {
final cropperImage = await cropImage(filePath: result.files.single.path!);
return PickedFileModel(
path: cropperImage,
Expand All @@ -107,34 +48,13 @@ class FileUtilsHelperImpl implements FileUtilsHelper {
);
}

@override
String getFileSizeString({required int fileLength, required int precision}) {
final i = (log(fileLength) / log(1024)).floor();
return ((fileLength / pow(1024, i)).toStringAsFixed(precision)) + suffixes[i];
}

@override
String generateEaselLinkForShare({required String recipeId, required String cookbookId}) {
return "$kWalletWebLink/?action=purchase_nft&recipe_id=$recipeId&cookbook_id=$cookbookId";
}

@override
Future<void> launchMyUrl({required String url}) async {
final canLaunch = await canLaunchUrlString(url);
if (canLaunch) {
launchUrlString(url, mode: LaunchMode.externalApplication);
} else {
throw LocaleKeys.cannot_launch_url.tr();
}
}

Future<String> cropImage({required String filePath}) async {
try {
final CroppedFile? croppedFile = await imageCropper.cropImage(
sourcePath: filePath,
aspectRatioPresets: [CropAspectRatioPreset.square, CropAspectRatioPreset.ratio3x2, CropAspectRatioPreset.original, CropAspectRatioPreset.ratio4x3, CropAspectRatioPreset.ratio16x9],
uiSettings: [
AndroidUiSettings(toolbarTitle: kPylons, toolbarColor: EaselAppTheme.kBlue, toolbarWidgetColor: Colors.white, initAspectRatio: CropAspectRatioPreset.original, lockAspectRatio: false),
AndroidUiSettings(toolbarTitle: kPylons, toolbarColor: EventlyAppTheme.kBlue, toolbarWidgetColor: Colors.white, initAspectRatio: CropAspectRatioPreset.original, lockAspectRatio: false),
IOSUiSettings(
title: kPylons,
),
Expand All @@ -145,36 +65,4 @@ class FileUtilsHelperImpl implements FileUtilsHelper {
return "";
}
}

@override
String generateEventlyLinkForOpeningInPylonsApp({required String recipeId, required String cookbookId}) {
return Uri.https('pylons.page.link', "/", {
kAmvKey: "1",
kApnKey: "tech.pylons.wallet",
kIbiKey: "xyz.pylons.wallet",
kImvKey: "1",
kLinkKey: "https://wallet.pylons.tech/?action=purchase_nft&recipe_id=$recipeId&cookbook_id=$cookbookId&nft_amount=1"
}).toString();
}

@override
Future<File> copyFileToInternal({required FilePickerResult filePickerResult}) async {
await deleteFilesIfAlreadyPresent();
final tempDirectory = await getTemporaryDirectory();
return File(filePickerResult.files.single.path.toString()).copySync('${tempDirectory.path}/$k3dFileName.${filePickerResult.files.single.extension}');
}

Future<void> deleteFilesIfAlreadyPresent() async {
final tempDirectory = await getTemporaryDirectory();

final fileRef1 = File('${tempDirectory.path}/$k3dFileName.${threedAllowedExts[0]}');
final fileRef2 = File('${tempDirectory.path}/$k3dFileName.${threedAllowedExts[1]}');

if (fileRef1.existsSync()) {
fileRef1.deleteSync();
}
if (fileRef2.existsSync()) {
fileRef2.deleteSync();
}
}
}

0 comments on commit e14e6fe

Please sign in to comment.