From fe0e80e32a0b3579a2c6d2cffc1ab6855805f695 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 24 Jul 2023 00:42:07 +0800 Subject: [PATCH] rearrange and clean up code --- .../morbit/photogallery/PhotoGalleryPlugin.kt | 15 +++-- ios/Classes/SwiftPhotoGalleryPlugin.swift | 60 +++++++++---------- lib/photo_gallery.dart | 26 ++++---- 3 files changed, 48 insertions(+), 53 deletions(-) diff --git a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt index 0405e12..a6fee5f 100644 --- a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt +++ b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt @@ -483,7 +483,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } - return null + return@run null } } @@ -503,7 +503,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } - return null + return@run null } } @@ -645,7 +645,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } - return null + return@run null } } @@ -669,7 +669,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } - return null + return@run null } } @@ -901,7 +901,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } - return null + return@run null } } @@ -922,7 +922,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } - return null + return@run null } } @@ -971,8 +971,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { val out = FileOutputStream(path) compressFormat = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { Bitmap.CompressFormat.WEBP_LOSSLESS - } - else { + } else { Bitmap.CompressFormat.WEBP } it.compress(compressFormat, 100, out) diff --git a/ios/Classes/SwiftPhotoGalleryPlugin.swift b/ios/Classes/SwiftPhotoGalleryPlugin.swift index 2129c0b..fb6ef42 100644 --- a/ios/Classes/SwiftPhotoGalleryPlugin.swift +++ b/ios/Classes/SwiftPhotoGalleryPlugin.swift @@ -38,16 +38,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { } ) } - else if(call.method == "deleteMedium") { - let arguments = call.arguments as! Dictionary - let mediumId = arguments["mediumId"] as! String - deleteMedium( - mediumId: mediumId, - completion: { (success: Bool, error: Error?) -> Void in - result(success) - } - ) - } else if(call.method == "getThumbnail") { let arguments = call.arguments as! Dictionary let mediumId = arguments["mediumId"] as! String @@ -96,6 +86,16 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { } ) } + else if(call.method == "deleteMedium") { + let arguments = call.arguments as! Dictionary + let mediumId = arguments["mediumId"] as! String + deleteMedium( + mediumId: mediumId, + completion: { (success: Bool, error: Error?) -> Void in + result(success) + } + ) + } else if(call.method == "cleanCache") { cleanCache() result(nil) @@ -236,24 +236,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { ) } } - - private func deleteMedium(mediumId: String, completion: @escaping (Bool, Error?) -> Void) { - let fetchOptions = PHFetchOptions() - if #available(iOS 9, *) { - fetchOptions.fetchLimit = 1 - } - let assets: PHFetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [mediumId], options: fetchOptions) - - if assets.count <= 0 { - completion(false, NSError(domain: "photo_gallery", code: 404, userInfo: nil)) - } else { - let asset: PHAsset = assets[0] - PHPhotoLibrary.shared().performChanges({ - PHAssetChangeRequest.deleteAssets([asset] as NSFastEnumeration) - }, completionHandler: completion) - } - } - + private func getThumbnail( mediumId: String, width: NSNumber?, @@ -466,8 +449,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { "modifiedDate": (asset.modificationDate != nil) ? NSInteger(asset.modificationDate!.timeIntervalSince1970 * 1000) : nil ] } - - + private func getMediumFromAssetLightWeight(asset: PHAsset) -> [String: Any?] { return [ "id": asset.localIdentifier, @@ -634,7 +616,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { } return nil } - private func cachePath() -> URL { let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask) @@ -643,6 +624,23 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { return cacheFolder } + private func deleteMedium(mediumId: String, completion: @escaping (Bool, Error?) -> Void) { + let fetchOptions = PHFetchOptions() + if #available(iOS 9, *) { + fetchOptions.fetchLimit = 1 + } + let assets: PHFetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [mediumId], options: fetchOptions) + + if assets.count <= 0 { + completion(false, NSError(domain: "photo_gallery", code: 404, userInfo: nil)) + } else { + let asset: PHAsset = assets[0] + PHPhotoLibrary.shared().performChanges({ + PHAssetChangeRequest.deleteAssets([asset] as NSFastEnumeration) + }, completionHandler: completion) + } + } + private func cleanCache() { try? FileManager.default.removeItem(at: self.cachePath()) } diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index 2ce63ce..0628ea7 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -31,9 +31,7 @@ class PhotoGallery { 'newest': newest, 'hideIfEmpty': hideIfEmpty, }); - return json - .map((album) => Album.fromJson(album, mediumType, newest)) - .toList(); + return json.map((album) => Album.fromJson(album, mediumType, newest)).toList(); } /// List all available media in a specific album, support pagination of media @@ -66,17 +64,6 @@ class PhotoGallery { return Medium.fromJson(json); } - /// Delete medium by medium id - static Future deleteMedium({ - required String mediumId, - MediumType? mediumType, - }) async { - await _channel.invokeMethod('deleteMedium', { - 'mediumId': mediumId, - 'mediumType': mediumTypeToJson(mediumType), - }); - } - /// Get medium thumbnail by medium id static Future> getThumbnail({ required String mediumId, @@ -131,6 +118,17 @@ class PhotoGallery { return File(path); } + /// Delete medium by medium id + static Future deleteMedium({ + required String mediumId, + MediumType? mediumType, + }) async { + await _channel.invokeMethod('deleteMedium', { + 'mediumId': mediumId, + 'mediumType': mediumTypeToJson(mediumType), + }); + } + static Future cleanCache() async { _channel.invokeMethod('cleanCache', {}); }