diff --git a/ios/Classes/SwiftPhotoGalleryPlugin.swift b/ios/Classes/SwiftPhotoGalleryPlugin.swift index fc474f6..2bd4b5f 100644 --- a/ios/Classes/SwiftPhotoGalleryPlugin.swift +++ b/ios/Classes/SwiftPhotoGalleryPlugin.swift @@ -37,6 +37,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 == "getThumbnail") { let arguments = call.arguments as! Dictionary let mediumId = arguments["mediumId"] as! String @@ -221,6 +231,30 @@ 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] + deleteAssets(assets: [asset]) { success, error in + completion(success, error) + } + } + } + + private func deleteAssets(assets: [PHAsset], completion: @escaping (Bool, Error?) -> Void) { + PHPhotoLibrary.shared().performChanges({ + PHAssetChangeRequest.deleteAssets(assets as NSFastEnumeration) + }, completionHandler: completion) + } + private func getThumbnail( mediumId: String, @@ -586,6 +620,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { } return nil } + private func cachePath() -> URL { let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask) diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index 17db430..fdeb75d 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -31,7 +31,9 @@ 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 @@ -62,6 +64,21 @@ class PhotoGallery { return Medium.fromJson(json); } + /// Delete medium by medium id + static Future deleteMedium({ + required String mediumId, + }) async { + if (!Platform.isIOS) { + throw UnsupportedError('This function is only available on iOS'); + } + + final result = await _channel.invokeMethod('deleteMedium', { + 'mediumId': mediumId, + }); + + return result as bool; + } + /// Get medium thumbnail by medium id static Future> getThumbnail({ required String mediumId,