rearrange and clean up code

This commit is contained in:
Wenqi Li 2023-07-24 00:42:07 +08:00
parent 2d80702f09
commit fe0e80e32a
3 changed files with 48 additions and 53 deletions

View File

@ -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) val out = FileOutputStream(path)
compressFormat = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { compressFormat = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Bitmap.CompressFormat.WEBP_LOSSLESS Bitmap.CompressFormat.WEBP_LOSSLESS
} } else {
else {
Bitmap.CompressFormat.WEBP Bitmap.CompressFormat.WEBP
} }
it.compress(compressFormat, 100, out) it.compress(compressFormat, 100, out)

View File

@ -38,16 +38,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
} }
) )
} }
else if(call.method == "deleteMedium") {
let arguments = call.arguments as! Dictionary<String, AnyObject>
let mediumId = arguments["mediumId"] as! String
deleteMedium(
mediumId: mediumId,
completion: { (success: Bool, error: Error?) -> Void in
result(success)
}
)
}
else if(call.method == "getThumbnail") { else if(call.method == "getThumbnail") {
let arguments = call.arguments as! Dictionary<String, AnyObject> let arguments = call.arguments as! Dictionary<String, AnyObject>
let mediumId = arguments["mediumId"] as! String 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<String, AnyObject>
let mediumId = arguments["mediumId"] as! String
deleteMedium(
mediumId: mediumId,
completion: { (success: Bool, error: Error?) -> Void in
result(success)
}
)
}
else if(call.method == "cleanCache") { else if(call.method == "cleanCache") {
cleanCache() cleanCache()
result(nil) 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( private func getThumbnail(
mediumId: String, mediumId: String,
width: NSNumber?, width: NSNumber?,
@ -466,8 +449,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
"modifiedDate": (asset.modificationDate != nil) ? NSInteger(asset.modificationDate!.timeIntervalSince1970 * 1000) : nil "modifiedDate": (asset.modificationDate != nil) ? NSInteger(asset.modificationDate!.timeIntervalSince1970 * 1000) : nil
] ]
} }
private func getMediumFromAssetLightWeight(asset: PHAsset) -> [String: Any?] { private func getMediumFromAssetLightWeight(asset: PHAsset) -> [String: Any?] {
return [ return [
"id": asset.localIdentifier, "id": asset.localIdentifier,
@ -634,7 +616,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
} }
return nil return nil
} }
private func cachePath() -> URL { private func cachePath() -> URL {
let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask) let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
@ -643,6 +624,23 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return cacheFolder 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() { private func cleanCache() {
try? FileManager.default.removeItem(at: self.cachePath()) try? FileManager.default.removeItem(at: self.cachePath())
} }

View File

@ -31,9 +31,7 @@ class PhotoGallery {
'newest': newest, 'newest': newest,
'hideIfEmpty': hideIfEmpty, 'hideIfEmpty': hideIfEmpty,
}); });
return json return json.map<Album>((album) => Album.fromJson(album, mediumType, newest)).toList();
.map<Album>((album) => Album.fromJson(album, mediumType, newest))
.toList();
} }
/// List all available media in a specific album, support pagination of media /// List all available media in a specific album, support pagination of media
@ -66,17 +64,6 @@ class PhotoGallery {
return Medium.fromJson(json); return Medium.fromJson(json);
} }
/// Delete medium by medium id
static Future<void> deleteMedium({
required String mediumId,
MediumType? mediumType,
}) async {
await _channel.invokeMethod('deleteMedium', {
'mediumId': mediumId,
'mediumType': mediumTypeToJson(mediumType),
});
}
/// Get medium thumbnail by medium id /// Get medium thumbnail by medium id
static Future<List<int>> getThumbnail({ static Future<List<int>> getThumbnail({
required String mediumId, required String mediumId,
@ -131,6 +118,17 @@ class PhotoGallery {
return File(path); return File(path);
} }
/// Delete medium by medium id
static Future<void> deleteMedium({
required String mediumId,
MediumType? mediumType,
}) async {
await _channel.invokeMethod('deleteMedium', {
'mediumId': mediumId,
'mediumType': mediumTypeToJson(mediumType),
});
}
static Future<void> cleanCache() async { static Future<void> cleanCache() async {
_channel.invokeMethod('cleanCache', {}); _channel.invokeMethod('cleanCache', {});
} }