Skip to content

Commit 6fa244d

Browse files
author
Chris
authored
Merge pull request #6 from crelies/dev
Improvements
2 parents 9fe0b6b + afe7706 commit 6fa244d

15 files changed

+130
-58
lines changed

Package.resolved

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ let package = Package(
1515
name: "RemoteImage",
1616
targets: ["RemoteImage"]),
1717
],
18+
dependencies: [
19+
.package(url: "https://github.com/nalexn/ViewInspector.git", from: "0.3.8")
20+
],
1821
targets: [
1922
.target(
2023
name: "RemoteImage",
2124
dependencies: []),
2225
.testTarget(
2326
name: "RemoteImageTests",
24-
dependencies: ["RemoteImage"]),
27+
dependencies: ["RemoteImage", "ViewInspector"]),
2528
]
2629
)

Sources/RemoteImage/private/Models/RemoteImageState.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,28 @@
66
// Copyright © 2019 Christian Elies. All rights reserved.
77
//
88

9-
import Foundation
9+
#if canImport(UIKit)
10+
import UIKit
1011

1112
enum RemoteImageState: Hashable {
1213
case error(_ error: NSError)
13-
case image(_ image: PlatformSpecificImageType)
14+
case image(_ image: UIImage)
1415
case loading
1516
}
17+
18+
extension RemoteImageState {
19+
var error: NSError? {
20+
guard case let RemoteImageState.error(error) = self else {
21+
return nil
22+
}
23+
return error
24+
}
25+
26+
var image: UIImage? {
27+
guard case let RemoteImageState.image(uiImage) = self else {
28+
return nil
29+
}
30+
return uiImage
31+
}
32+
}
33+
#endif

Sources/RemoteImage/private/Protocols/RemoteImageServiceProtocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Christian Elies on 15.12.19.
66
//
77

8+
#if canImport(UIKit)
89
import Combine
910

1011
protocol RemoteImageServiceProtocol where Self: ObservableObject {
@@ -14,3 +15,4 @@ protocol RemoteImageServiceProtocol where Self: ObservableObject {
1415
var state: RemoteImageState { get set }
1516
func fetchImage(ofType type: RemoteImageType)
1617
}
18+
#endif

Sources/RemoteImage/private/Services/DefaultRemoteImageCache.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
// Created by Christian Elies on 14.12.19.
66
//
77

8-
import Foundation
8+
#if canImport(UIKit)
9+
import UIKit
910

1011
struct DefaultRemoteImageCache {
11-
let cache = NSCache<AnyObject, PlatformSpecificImageType>()
12+
let cache = NSCache<AnyObject, UIImage>()
1213
}
1314

1415
extension DefaultRemoteImageCache: RemoteImageCache {
15-
func object(forKey key: AnyObject) -> PlatformSpecificImageType? { cache.object(forKey: key) }
16+
func object(forKey key: AnyObject) -> UIImage? { cache.object(forKey: key) }
1617

17-
func setObject(_ object: PlatformSpecificImageType, forKey key: AnyObject) { cache.setObject(object, forKey: key) }
18+
func setObject(_ object: UIImage, forKey key: AnyObject) { cache.setObject(object, forKey: key) }
1819

1920
func removeObject(forKey key: AnyObject) { cache.removeObject(forKey: key) }
2021

2122
func removeAllObjects() { cache.removeAllObjects() }
2223
}
24+
#endif

Sources/RemoteImage/public/Models/PlatformSpecificImageType.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/RemoteImage/public/Protocols/RemoteImageCache.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
// Created by Christian Elies on 14.12.19.
66
//
77

8-
import Foundation
8+
#if canImport(UIKit)
9+
import UIKit
910

1011
public protocol RemoteImageCache {
11-
func object(forKey key: AnyObject) -> PlatformSpecificImageType?
12-
func setObject(_ object: PlatformSpecificImageType, forKey key: AnyObject)
12+
func object(forKey key: AnyObject) -> UIImage?
13+
func setObject(_ object: UIImage, forKey key: AnyObject)
1314
func removeObject(forKey key: AnyObject)
1415
func removeAllObjects()
1516
}
17+
#endif

Sources/RemoteImage/public/Services/RemoteImageService.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
// Copyright © 2019 Christian Elies. All rights reserved.
77
//
88

9+
#if canImport(UIKit)
910
import Combine
10-
import Foundation
11+
import UIKit
1112

1213
public typealias RemoteImageCacheKeyProvider = (RemoteImageType) -> AnyObject
1314

@@ -52,7 +53,7 @@ extension RemoteImageService {
5253
let urlRequest = URLRequest(url: url)
5354

5455
cancellable = dependencies.remoteImageURLDataPublisher.dataPublisher(for: urlRequest)
55-
.map { PlatformSpecificImageType(data: $0.data) }
56+
.map { UIImage(data: $0.data) }
5657
.receive(on: RunLoop.main)
5758
.sink(receiveCompletion: { completion in
5859
switch completion {
@@ -80,7 +81,7 @@ extension RemoteImageService {
8081
dependencies.photoKitService.getPhotoData(localIdentifier: localIdentifier) { result in
8182
switch result {
8283
case .success(let data):
83-
if let image = PlatformSpecificImageType(data: data) {
84+
if let image = UIImage(data: data) {
8485
Self.cache.setObject(image, forKey: cacheKey)
8586
self.state = .image(image)
8687
} else {
@@ -92,3 +93,4 @@ extension RemoteImageService {
9293
}
9394
}
9495
}
96+
#endif

Sources/RemoteImage/public/Services/RemoteImageServiceFactory.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Christian Elies on 29.10.19.
66
//
77

8+
#if canImport(UIKit)
89
import Foundation
910

1011
public final class RemoteImageServiceFactory {
@@ -13,3 +14,4 @@ public final class RemoteImageServiceFactory {
1314
return RemoteImageService(dependencies: dependencies)
1415
}
1516
}
17+
#endif

Sources/RemoteImage/public/Views/RemoteImage.swift

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright © 2019 Christian Elies. All rights reserved.
77
//
88

9+
#if canImport(SwiftUI) && canImport(UIKit)
910
import Combine
1011
import SwiftUI
1112

@@ -17,33 +18,18 @@ public struct RemoteImage<ErrorView: View, ImageView: View, LoadingView: View>:
1718

1819
@ObservedObject private var service = RemoteImageServiceFactory.makeRemoteImageService()
1920

20-
public var body: AnyView {
21-
switch service.state {
22-
case .error(let error):
23-
return AnyView(
24-
errorView(error)
25-
)
26-
case .image(let image):
27-
#if canImport(UIKit)
28-
return AnyView(
29-
self.imageView(Image(uiImage: image))
30-
)
31-
#elseif os(macOS)
32-
return AnyView(
33-
self.imageView(Image(nsImage: image))
34-
)
35-
#else
36-
return AnyView(
37-
Text("Cannot render image: unsupported platform")
38-
)
39-
#endif
40-
case .loading:
41-
return AnyView(
42-
loadingView()
21+
public var body: some View {
22+
Group {
23+
if service.state == .loading {
24+
loadingView()
4325
.onAppear {
4426
self.service.fetchImage(ofType: self.type)
4527
}
46-
)
28+
} else {
29+
service.state.error.map { errorView($0) }
30+
31+
service.state.image.map { self.imageView(Image(uiImage: $0)) }
32+
}
4733
}
4834
}
4935

@@ -69,3 +55,5 @@ struct RemoteImage_Previews: PreviewProvider {
6955
}
7056
}
7157
#endif
58+
59+
#endif

0 commit comments

Comments
 (0)