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>
@ -105,7 +112,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
} }
} }
private var assetCollections : [PHAssetCollection] = [] private var assetCollections: [PHAssetCollection] = []
private func listAlbums(mediumType: String?, hideIfEmpty: Bool? = true) -> [[String: Any?]] { private func listAlbums(mediumType: String?, hideIfEmpty: Bool? = true) -> [[String: Any?]] {
self.assetCollections = [] self.assetCollections = []
@ -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
) )
@ -167,7 +181,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
albums.insert([ albums.insert([
"id": "__ALL__", "id": "__ALL__",
"name": "All", "name": "All",
"count" : countMedia(collection: nil, mediumType: mediumType), "count": countMedia(collection: nil, mediumType: mediumType),
], at: 0) ], at: 0)
return albums return albums
@ -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,12 +285,15 @@ 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
guard let image = uiImage else { guard let image = uiImage else {
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil)) completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
return return
} }
let bytes = image.jpegData(compressionQuality: CGFloat(70)) let bytes = image.jpegData(compressionQuality: CGFloat(70))
@ -273,7 +303,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return return
} }
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil)) completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
} }
private func getAlbumThumbnail( private func getAlbumThumbnail(
@ -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]
@ -315,14 +348,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
manager.requestImage( manager.requestImage(
for: asset, for: asset,
targetSize: CGSize( targetSize: CGSize(
width: imageSize.width * UIScreen.main.scale, width: imageSize.width * UIScreen.main.scale,
height: imageSize.height * 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
guard let image = uiImage else { guard let image = uiImage else {
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil)) completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
return return
} }
let bytes = image.jpegData(compressionQuality: CGFloat(80)) let bytes = image.jpegData(compressionQuality: CGFloat(80))
@ -332,7 +365,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return return
} }
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil)) completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
} }
private func getFile(mediumId: String, mimeType: String?, completion: @escaping (String?, Error?) -> Void) { private func getFile(mediumId: String, mimeType: String?, completion: @escaping (String?, Error?) -> Void) {
@ -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
@ -491,24 +523,24 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return 0 return 0
} }
switch orientation { switch orientation {
case UIImage.Orientation.up: case UIImage.Orientation.up:
return 1 return 1
case UIImage.Orientation.down: case UIImage.Orientation.down:
return 3 return 3
case UIImage.Orientation.left: case UIImage.Orientation.left:
return 6 return 6
case UIImage.Orientation.right: case UIImage.Orientation.right:
return 8 return 8
case UIImage.Orientation.upMirrored: case UIImage.Orientation.upMirrored:
return 2 return 2
case UIImage.Orientation.downMirrored: case UIImage.Orientation.downMirrored:
return 4 return 4
case UIImage.Orientation.leftMirrored: case UIImage.Orientation.leftMirrored:
return 5 return 5
case UIImage.Orientation.rightMirrored: case UIImage.Orientation.rightMirrored:
return 7 return 7
@unknown default: @unknown default:
return 0 return 0
} }
} }
@ -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