reformat iOS code

This commit is contained in:
Wenqi Li 2023-07-27 20:28:38 +08:00
parent d21edf5cac
commit 7c48244058

View File

@ -26,7 +26,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
let skip = arguments["skip"] as? NSNumber let skip = arguments["skip"] as? NSNumber
let take = arguments["take"] as? NSNumber let take = arguments["take"] as? NSNumber
let lightWeight = arguments["lightWeight"] as? Bool let lightWeight = arguments["lightWeight"] as? Bool
result(listMedia(albumId: albumId, mediumType: mediumType, newest: newest, skip: skip, take: take, lightWeight: lightWeight)) result(listMedia(
albumId: albumId,
mediumType: mediumType,
newest: newest,
skip: skip,
take: take,
lightWeight: lightWeight
))
} }
else if(call.method == "getMedium") { else if(call.method == "getMedium") {
let arguments = call.arguments as! Dictionary<String, AnyObject> let arguments = call.arguments as! Dictionary<String, AnyObject>
@ -147,14 +154,21 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
if let assetCollection = collection as? PHAssetCollection { if let assetCollection = collection as? PHAssetCollection {
addCollection(collection: assetCollection, hideIfEmpty: hideIfEmpty) addCollection(collection: assetCollection, hideIfEmpty: hideIfEmpty)
} else if let collectionList = collection as? PHCollectionList { } else if let collectionList = collection as? PHCollectionList {
processPHCollections(fetchResult: PHCollectionList.fetchCollections(in: collectionList, options: nil), hideIfEmpty: hideIfEmpty) processPHCollections(
fetchResult: PHCollectionList.fetchCollections(in: collectionList, options: nil),
hideIfEmpty: hideIfEmpty
)
} }
} }
} }
// Smart Albums. // Smart Albums.
processPHAssetCollections( processPHAssetCollections(
fetchResult: PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: fetchOptions), fetchResult: PHAssetCollection.fetchAssetCollections(
with: .smartAlbum,
subtype: .albumRegular,
options: fetchOptions
),
hideIfEmpty: hideIfEmpty ?? true hideIfEmpty: hideIfEmpty ?? true
) )
@ -183,7 +197,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: options).count return PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: options).count
} }
private func listMedia(albumId: String, mediumType: String?, newest: Bool, skip: NSNumber?, take: NSNumber?, lightWeight: Bool? = false) -> NSDictionary { private func listMedia(
albumId: String,
mediumType: String?,
newest: Bool,
skip: NSNumber?,
take: NSNumber?,
lightWeight: Bool? = false
) -> NSDictionary {
let fetchOptions = PHFetchOptions() let fetchOptions = PHFetchOptions()
fetchOptions.predicate = predicateFromMediumType(mediumType: mediumType) fetchOptions.predicate = predicateFromMediumType(mediumType: mediumType)
fetchOptions.sortDescriptors = [ fetchOptions.sortDescriptors = [
@ -195,9 +216,15 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
collection.localIdentifier == albumId collection.localIdentifier == albumId
}) })
let fetchResult = albumId == "__ALL__" let fetchResult: PHFetchResult<PHAsset>
? PHAsset.fetchAssets(with: fetchOptions) if(albumId == "__ALL__") {
: PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: fetchOptions) fetchResult = PHAsset.fetchAssets(with: fetchOptions)
} else {
fetchResult = PHAsset.fetchAssets(
in: collection ?? PHAssetCollection.init(),
options: fetchOptions
)
}
let start = skip?.intValue ?? 0 let start = skip?.intValue ?? 0
let total = fetchResult.count let total = fetchResult.count
let end = take == nil ? total : min(start + take!.intValue, total) let end = take == nil ? total : min(start + take!.intValue, total)
@ -258,7 +285,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
let imageSize = CGSize(width: width?.intValue ?? 128, height: height?.intValue ?? 128) let imageSize = CGSize(width: width?.intValue ?? 128, height: height?.intValue ?? 128)
manager.requestImage( manager.requestImage(
for: asset, for: asset,
targetSize: CGSize(width: imageSize.width * UIScreen.main.scale, height: imageSize.height * UIScreen.main.scale), targetSize: CGSize(
width: imageSize.width * UIScreen.main.scale,
height: imageSize.height * UIScreen.main.scale
),
contentMode: PHImageContentMode.aspectFill, contentMode: PHImageContentMode.aspectFill,
options: options, options: options,
resultHandler: { (uiImage: UIImage?, info) in resultHandler: { (uiImage: UIImage?, info) in
@ -296,11 +326,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
fetchOptions.fetchLimit = 1 fetchOptions.fetchLimit = 1
} }
let assets = albumId == "__ALL__" ? let assets: PHFetchResult<PHAsset>
PHAsset.fetchAssets(with: fetchOptions) : if(albumId == "__ALL__") {
PHAsset.fetchAssets(in: self.assetCollections.first(where: { (collection) -> Bool in assets = PHAsset.fetchAssets(with: fetchOptions)
} else {
assets = PHAsset.fetchAssets(in: self.assetCollections.first(where: { (collection) -> Bool in
collection.localIdentifier == albumId collection.localIdentifier == albumId
})!, options: fetchOptions) })!, options: fetchOptions)
}
if (assets.count > 0) { if (assets.count > 0) {
let asset: PHAsset = assets[0] let asset: PHAsset = assets[0]
@ -381,8 +414,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
}) })
} }
) )
} else if(asset.mediaType == PHAssetMediaType.video } else if(asset.mediaType == PHAssetMediaType.video || asset.mediaType == PHAssetMediaType.audio) {
|| asset.mediaType == PHAssetMediaType.audio) {
let options = PHVideoRequestOptions() let options = PHVideoRequestOptions()
options.version = .current options.version = .current
options.deliveryMode = .highQualityFormat options.deliveryMode = .highQualityFormat
@ -526,7 +558,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
guard let assetUTI = uti else { guard let assetUTI = uti else {
return "" return ""
} }
guard let ext = UTTypeCopyPreferredTagWithClass(assetUTI as CFString, kUTTagClassFilenameExtension as CFString)?.takeRetainedValue() as String? else { guard let ext = UTTypeCopyPreferredTagWithClass(
assetUTI as CFString,
kUTTagClassFilenameExtension as CFString
)?.takeRetainedValue() as String? else {
return "" return ""
} }
return "." + ext return "." + ext
@ -536,7 +571,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
guard let assetUTI = uti else { guard let assetUTI = uti else {
return nil return nil
} }
guard let mimeType = UTTypeCopyPreferredTagWithClass(assetUTI as CFString, kUTTagClassMIMEType as CFString)?.takeRetainedValue() as String? else { guard let mimeType = UTTypeCopyPreferredTagWithClass(
assetUTI as CFString,
kUTTagClassMIMEType as CFString
)?.takeRetainedValue() as String? else {
return nil return nil
} }
return mimeType return mimeType