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 take = arguments["take"] as? NSNumber
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") {
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?]] {
self.assetCollections = []
@ -147,14 +154,21 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
if let assetCollection = collection as? PHAssetCollection {
addCollection(collection: assetCollection, hideIfEmpty: hideIfEmpty)
} 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.
processPHAssetCollections(
fetchResult: PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: fetchOptions),
fetchResult: PHAssetCollection.fetchAssetCollections(
with: .smartAlbum,
subtype: .albumRegular,
options: fetchOptions
),
hideIfEmpty: hideIfEmpty ?? true
)
@ -167,7 +181,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
albums.insert([
"id": "__ALL__",
"name": "All",
"count" : countMedia(collection: nil, mediumType: mediumType),
"count": countMedia(collection: nil, mediumType: mediumType),
], at: 0)
return albums
@ -183,7 +197,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
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()
fetchOptions.predicate = predicateFromMediumType(mediumType: mediumType)
fetchOptions.sortDescriptors = [
@ -195,9 +216,15 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
collection.localIdentifier == albumId
})
let fetchResult = albumId == "__ALL__"
? PHAsset.fetchAssets(with: fetchOptions)
: PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: fetchOptions)
let fetchResult: PHFetchResult<PHAsset>
if(albumId == "__ALL__") {
fetchResult = PHAsset.fetchAssets(with: fetchOptions)
} else {
fetchResult = PHAsset.fetchAssets(
in: collection ?? PHAssetCollection.init(),
options: fetchOptions
)
}
let start = skip?.intValue ?? 0
let total = fetchResult.count
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)
manager.requestImage(
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,
options: options,
resultHandler: {(uiImage: UIImage?, info) in
resultHandler: { (uiImage: UIImage?, info) in
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
}
let bytes = image.jpegData(compressionQuality: CGFloat(70))
@ -273,7 +303,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return
}
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil))
completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
}
private func getAlbumThumbnail(
@ -296,11 +326,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
fetchOptions.fetchLimit = 1
}
let assets = albumId == "__ALL__" ?
PHAsset.fetchAssets(with: fetchOptions) :
PHAsset.fetchAssets(in: self.assetCollections.first(where: { (collection) -> Bool in
let assets: PHFetchResult<PHAsset>
if(albumId == "__ALL__") {
assets = PHAsset.fetchAssets(with: fetchOptions)
} else {
assets = PHAsset.fetchAssets(in: self.assetCollections.first(where: { (collection) -> Bool in
collection.localIdentifier == albumId
})!, options: fetchOptions)
}
if (assets.count > 0) {
let asset: PHAsset = assets[0]
@ -320,9 +353,9 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
),
contentMode: PHImageContentMode.aspectFill,
options: options,
resultHandler: {(uiImage: UIImage?, info) in
resultHandler: { (uiImage: UIImage?, info) in
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
}
let bytes = image.jpegData(compressionQuality: CGFloat(80))
@ -332,7 +365,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
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) {
@ -381,8 +414,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
})
}
)
} else if(asset.mediaType == PHAssetMediaType.video
|| asset.mediaType == PHAssetMediaType.audio) {
} else if(asset.mediaType == PHAssetMediaType.video || asset.mediaType == PHAssetMediaType.audio) {
let options = PHVideoRequestOptions()
options.version = .current
options.deliveryMode = .highQualityFormat
@ -526,7 +558,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
guard let assetUTI = uti else {
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 "." + ext
@ -536,7 +571,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
guard let assetUTI = uti else {
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 mimeType