Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
natsuk4ze committed Aug 8, 2023
1 parent 6291bf9 commit df290bf
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions ios/Classes/GalPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,15 @@ public class GalPlugin: NSObject, FlutterPlugin {
album: args["album"] as? String,
isImage: call.method == "putImage"
) { _, error in
if let error = error as NSError? {
result(self.handleError(error: error))
} else {
result(nil)
}
result(error == nil ? nil : self.handleError(error: error!))
}
case "putImageBytes":
let args = call.arguments as! [String: Any]
putMediaBytes(
bytes: (args["bytes"] as! FlutterStandardTypedData).data,
album: args["album"] as? String
) { _, error in
if let error = error as NSError? {
result(self.handleError(error: error))
} else {
result(nil)
}
result(error == nil ? nil : self.handleError(error: error!))
}
case "open":
open { result(nil) }
Expand All @@ -53,12 +45,6 @@ public class GalPlugin: NSObject, FlutterPlugin {
result(granted)
}
)
requestAccess(
toAlbum: toAlbum,
completion: { granted in
result(granted)
}
)
default:
result(FlutterMethodNotImplemented)
}
Expand Down Expand Up @@ -123,11 +109,9 @@ public class GalPlugin: NSObject, FlutterPlugin {
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: album)
}, completionHandler: { success, error in
if success {
self.getAlbum(album: album, completion: completion)
return
}
completion(nil, error)
success
? self.getAlbum(album: album, completion: completion)
: completion(nil, error)
})
}

Expand Down Expand Up @@ -162,24 +146,27 @@ public class GalPlugin: NSObject, FlutterPlugin {
}
}

private func handleError(error: NSError) -> FlutterError {
let message = error.localizedDescription
let details = Thread.callStackSymbols
private func handleError(error: Error) -> FlutterError {
if let error = error as? NSError {
let message = error.localizedDescription
let details = Thread.callStackSymbols

switch PHErrorCode(rawValue: error.code) {
case .accessRestricted, .accessUserDenied:
return FlutterError(code: "ACCESS_DENIED", message: message, details: details)
switch PHErrorCode(rawValue: error.code) {
case .accessRestricted, .accessUserDenied:
return FlutterError(code: "ACCESS_DENIED", message: message, details: details)

case .identifierNotFound, .multipleIdentifiersFound, .requestNotSupportedForAsset,
.videoConversionFailed, .unsupportedVideoCodec:
return FlutterError(code: "NOT_SUPPORTED_FORMAT", message: message, details: details)
case .identifierNotFound, .multipleIdentifiersFound, .requestNotSupportedForAsset,
.videoConversionFailed, .unsupportedVideoCodec:
return FlutterError(code: "NOT_SUPPORTED_FORMAT", message: message, details: details)

case .notEnoughSpace:
return FlutterError(code: "NOT_ENOUGH_SPACE", message: message, details: details)
case .notEnoughSpace:
return FlutterError(code: "NOT_ENOUGH_SPACE", message: message, details: details)

default:
return FlutterError(code: "UNEXPECTED", message: message, details: details)
default:
return FlutterError(code: "UNEXPECTED", message: message, details: details)
}
}
return FlutterError(code: "UNEXPECTED", message: "Did not NSError.", details: nil)
}
}

Expand Down

0 comments on commit df290bf

Please sign in to comment.