Skip to content

Commit d25504e

Browse files
author
Christian Elies
committed
refactor(): refactored photo kit service to use result type
1 parent 3875e60 commit d25504e

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

Sources/RemoteImage/private/Services/PhotoKitService.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,31 @@ protocol PhotoKitServiceProvider {
66

77
protocol PhotoKitServiceProtocol {
88
func getPhotoData(localIdentifier: String,
9-
success: @escaping (Data) -> Void,
10-
failure: @escaping (Error) -> Void)
9+
_ completion: @escaping (Result<Data, Error>) -> Void)
1110
}
1211

13-
final class PhotoKitService {
14-
15-
}
12+
final class PhotoKitService {}
1613

1714
extension PhotoKitService: PhotoKitServiceProtocol {
1815
func getPhotoData(localIdentifier: String,
19-
success: @escaping (Data) -> Void,
20-
failure: @escaping (Error) -> Void) {
16+
_ completion: @escaping (Result<Data, Error>) -> Void) {
2117
let fetchAssetsResult = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: nil)
2218
guard let phAsset = fetchAssetsResult.firstObject else {
23-
failure(PhotoKitServiceError.phAssetNotFound(localIdentifier: localIdentifier))
19+
completion(.failure(PhotoKitServiceError.phAssetNotFound(localIdentifier: localIdentifier)))
2420
return
2521
}
26-
22+
2723
let options = PHImageRequestOptions()
2824
options.isNetworkAccessAllowed = true
2925
PHImageManager.default().requestImageDataAndOrientation(for: phAsset,
3026
options: options,
3127
resultHandler: { data, _, _, info in
3228
if let error = info?[PHImageErrorKey] as? Error {
33-
failure(error)
29+
completion(.failure(error))
3430
} else if let data = data {
35-
success(data)
31+
completion(.success(data))
3632
} else {
37-
failure(PhotoKitServiceError.missingData)
33+
completion(.failure(PhotoKitServiceError.missingData))
3834
}
3935
})
4036
}

Sources/RemoteImage/public/Services/RemoteImageService.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,18 @@ extension RemoteImageService {
7878
return
7979
}
8080

81-
dependencies.photoKitService.getPhotoData(localIdentifier: localIdentifier, success: { data in
82-
if let image = PlatformSpecificImageType(data: data) {
83-
Self.cache.setObject(image, forKey: cacheKey)
84-
self.state = .image(image)
85-
} else {
86-
self.state = .error(RemoteImageServiceError.couldNotCreateImage)
81+
dependencies.photoKitService.getPhotoData(localIdentifier: localIdentifier) { result in
82+
switch result {
83+
case .success(let data):
84+
if let image = PlatformSpecificImageType(data: data) {
85+
Self.cache.setObject(image, forKey: cacheKey)
86+
self.state = .image(image)
87+
} else {
88+
self.state = .error(RemoteImageServiceError.couldNotCreateImage)
89+
}
90+
case .failure(let error):
91+
self.state = .error(error)
8792
}
88-
}) { error in
89-
self.state = .error(error)
9093
}
9194
}
9295
}

0 commit comments

Comments
 (0)