Skip to content

Commit e6850b7

Browse files
author
Christian Elies
committed
refactor(): added default prefix to types; feat(): implemented support for a custom service
1 parent 9429ff0 commit e6850b7

16 files changed

+91
-119
lines changed

Sources/RemoteImage/private/Models/RemoteImageState.swift

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

Sources/RemoteImage/private/Services/RemoteImageServiceDependencies.swift renamed to Sources/RemoteImage/private/Services/DefaultRemoteImageServiceDependencies.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
//
2-
// RemoteImageServiceDependencies.swift
2+
// DefaultRemoteImageServiceDependencies.swift
33
// RemoteImage
44
//
55
// Created by Christian Elies on 29.10.19.
66
//
77

88
import Foundation
99

10-
protocol RemoteImageServiceDependenciesProtocol: PhotoKitServiceProvider, RemoteImageURLDataPublisherProvider {
10+
protocol DefaultRemoteImageServiceDependenciesProtocol: PhotoKitServiceProvider, RemoteImageURLDataPublisherProvider {
1111

1212
}
1313

14-
struct RemoteImageServiceDependencies: RemoteImageServiceDependenciesProtocol {
14+
struct DefaultRemoteImageServiceDependencies: DefaultRemoteImageServiceDependenciesProtocol {
1515
let photoKitService: PhotoKitServiceProtocol
1616
let remoteImageURLDataPublisher: RemoteImageURLDataPublisher
1717

Sources/RemoteImage/private/Services/PhotoKitService.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ protocol PhotoKitServiceProvider {
1212
}
1313

1414
protocol PhotoKitServiceProtocol {
15-
func getPhotoData(localIdentifier: String,
16-
_ completion: @escaping (Result<Data, Error>) -> Void)
15+
func getPhotoData(
16+
localIdentifier: String,
17+
_ completion: @escaping (Result<Data, Error>) -> Void
18+
)
1719
}
1820

1921
final class PhotoKitService {
@@ -22,8 +24,10 @@ final class PhotoKitService {
2224
}
2325

2426
extension PhotoKitService: PhotoKitServiceProtocol {
25-
func getPhotoData(localIdentifier: String,
26-
_ completion: @escaping (Result<Data, Error>) -> Void) {
27+
func getPhotoData(
28+
localIdentifier: String,
29+
_ completion: @escaping (Result<Data, Error>) -> Void
30+
) {
2731
let fetchAssetsResult = Self.asset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: nil)
2832
guard let phAsset = fetchAssetsResult.firstObject else {
2933
completion(.failure(PhotoKitServiceError.phAssetNotFound(localIdentifier: localIdentifier)))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// RemoteImageState.swift
3+
// RemoteImage
4+
//
5+
// Created by Christian Elies on 11.08.19.
6+
// Copyright © 2019 Christian Elies. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public enum RemoteImageState: Hashable {
12+
case error(_ error: NSError)
13+
case image(_ image: UniversalImage)
14+
case loading
15+
}

Sources/RemoteImage/private/Protocols/RemoteImageServiceProtocol.swift renamed to Sources/RemoteImage/public/Protocols/RemoteImageService.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//
2-
// RemoteImageServiceProtocol.swift
2+
// RemoteImageService.swift
33
// RemoteImage
44
//
55
// Created by Christian Elies on 15.12.19.
66
//
77

88
import Combine
99

10-
protocol RemoteImageServiceProtocol where Self: ObservableObject {
10+
public typealias RemoteImageCacheKeyProvider = (RemoteImageType) -> AnyObject
11+
12+
public protocol RemoteImageService where Self: ObservableObject {
1113
static var cache: RemoteImageCache { get set }
1214
static var cacheKeyProvider: RemoteImageCacheKeyProvider { get set }
1315

Sources/RemoteImage/public/Services/RemoteImageService.swift renamed to Sources/RemoteImage/public/Services/DefaultRemoteImageService.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// RemoteImageService.swift
2+
// DefaultRemoteImageService.swift
33
// RemoteImage
44
//
55
// Created by Christian Elies on 11.08.19.
@@ -9,13 +9,11 @@
99
import Combine
1010
import Foundation
1111

12-
public typealias RemoteImageCacheKeyProvider = (RemoteImageType) -> AnyObject
13-
14-
public final class RemoteImageService: NSObject, ObservableObject, RemoteImageServiceProtocol {
15-
private let dependencies: RemoteImageServiceDependenciesProtocol
12+
public final class DefaultRemoteImageService: NSObject, ObservableObject, RemoteImageService {
13+
private let dependencies: DefaultRemoteImageServiceDependenciesProtocol
1614
private var cancellable: AnyCancellable?
1715

18-
@Published var state: RemoteImageState = .loading
16+
@Published public var state: RemoteImageState = .loading
1917

2018
public static var cache: RemoteImageCache = DefaultRemoteImageCache()
2119
public static var cacheKeyProvider: RemoteImageCacheKeyProvider = { remoteImageType in
@@ -25,11 +23,11 @@ public final class RemoteImageService: NSObject, ObservableObject, RemoteImageSe
2523
}
2624
}
2725

28-
init(dependencies: RemoteImageServiceDependenciesProtocol) {
26+
init(dependencies: DefaultRemoteImageServiceDependenciesProtocol) {
2927
self.dependencies = dependencies
3028
}
3129

32-
func fetchImage(ofType type: RemoteImageType) {
30+
public func fetchImage(ofType type: RemoteImageType) {
3331
switch type {
3432
case .url(let url):
3533
fetchImage(atURL: url)
@@ -39,7 +37,7 @@ public final class RemoteImageService: NSObject, ObservableObject, RemoteImageSe
3937
}
4038
}
4139

42-
private extension RemoteImageService {
40+
private extension DefaultRemoteImageService {
4341
func fetchImage(atURL url: URL) {
4442
cancellable?.cancel()
4543

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DefaultRemoteImageServiceFactory.swift
3+
// RemoteImage
4+
//
5+
// Created by Christian Elies on 29.10.19.
6+
//
7+
8+
public final class DefaultRemoteImageServiceFactory {
9+
public static func makeDefaultRemoteImageService(remoteImageURLDataPublisher: RemoteImageURLDataPublisher) -> DefaultRemoteImageService {
10+
let dependencies = DefaultRemoteImageServiceDependencies(remoteImageURLDataPublisher: remoteImageURLDataPublisher)
11+
return DefaultRemoteImageService(dependencies: dependencies)
12+
}
13+
}

Sources/RemoteImage/public/Services/RemoteImageServiceFactory.swift

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

Sources/RemoteImage/public/Views/RemoteImage.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import Combine
1111
import SwiftUI
1212

13-
public struct RemoteImage<ErrorView: View, ImageView: View, LoadingView: View>: View {
13+
public struct RemoteImage<ErrorView: View, ImageView: View, LoadingView: View, Service: RemoteImageService>: View {
1414
private let type: RemoteImageType
1515
private let errorView: (Error) -> ErrorView
1616
private let imageView: (Image) -> ImageView
1717
private let loadingView: () -> LoadingView
1818

19-
@ObservedObject private var service: RemoteImageService
19+
@ObservedObject private var service: Service
2020

2121
public var body: some View {
2222
switch service.state {
@@ -33,13 +33,41 @@ public struct RemoteImage<ErrorView: View, ImageView: View, LoadingView: View>:
3333
}
3434
}
3535

36+
/// <#Description#>
37+
///
38+
/// - Parameters:
39+
/// - type: <#type description#>
40+
/// - service: <#service description#>
41+
/// - errorView: <#errorView description#>
42+
/// - imageView: <#imageView description#>
43+
/// - loadingView: <#loadingView description#>
44+
public init(type: RemoteImageType, service: Service, @ViewBuilder errorView: @escaping (Error) -> ErrorView, @ViewBuilder imageView: @escaping (Image) -> ImageView, @ViewBuilder loadingView: @escaping () -> LoadingView) {
45+
self.type = type
46+
self.errorView = errorView
47+
self.imageView = imageView
48+
self.loadingView = loadingView
49+
_service = ObservedObject(wrappedValue: service)
50+
51+
service.fetchImage(ofType: type)
52+
}
53+
}
54+
55+
extension RemoteImage where Service == DefaultRemoteImageService {
56+
/// <#Description#>
57+
///
58+
/// - Parameters:
59+
/// - type: <#type description#>
60+
/// - remoteImageURLDataPublisher: <#remoteImageURLDataPublisher description#>
61+
/// - errorView: <#errorView description#>
62+
/// - imageView: <#imageView description#>
63+
/// - loadingView: <#loadingView description#>
3664
public init(type: RemoteImageType, remoteImageURLDataPublisher: RemoteImageURLDataPublisher = URLSession.shared, @ViewBuilder errorView: @escaping (Error) -> ErrorView, @ViewBuilder imageView: @escaping (Image) -> ImageView, @ViewBuilder loadingView: @escaping () -> LoadingView) {
3765
self.type = type
3866
self.errorView = errorView
3967
self.imageView = imageView
4068
self.loadingView = loadingView
4169

42-
let service = RemoteImageServiceFactory.makeRemoteImageService(remoteImageURLDataPublisher: remoteImageURLDataPublisher)
70+
let service = DefaultRemoteImageServiceFactory.makeDefaultRemoteImageService(remoteImageURLDataPublisher: remoteImageURLDataPublisher)
4371
_service = ObservedObject(wrappedValue: service)
4472

4573
service.fetchImage(ofType: type)

Tests/RemoteImageTests/Extensions/URLSession+RemoteImageURLDataPublisherTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ final class URLSession_RemoteImageURLDataPublisherTests: XCTestCase {
1616
return
1717
}
1818
let urlSession: URLSession = .shared
19-
let urlRequest = URLRequest(url: url)
20-
let dataTaskPublisher = urlSession.dataTaskPublisher(for: urlRequest).eraseToAnyPublisher()
21-
let dataPublisher = urlSession.dataPublisher(for: urlRequest)
19+
let dataTaskPublisher = urlSession.dataTaskPublisher(for: url).eraseToAnyPublisher()
20+
let dataPublisher = urlSession.dataPublisher(for: url)
2221
XCTAssertEqual(dataPublisher.description, dataTaskPublisher.description)
2322
}
2423

0 commit comments

Comments
 (0)