Skip to content

Commit 2f13268

Browse files
committed
Merge branch 'dev' of github.com:crelies/RemoteImage into dev
# Conflicts: # Sources/RemoteImage/public/Views/RemoteImage.swift
2 parents ad0cf3c + f0f6c4a commit 2f13268

24 files changed

+251
-61
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: swift
2+
osx_image: xcode11.3
3+
script:
4+
- swift package generate-xcodeproj
5+
- xcodebuild clean test -destination 'name=iPhone 8' -scheme RemoteImage-Package -enableCodeCoverage YES -derivedDataPath .build/derivedData -quiet
6+
after_success:
7+
# upload test coverage data
8+
- bash <(curl -s https://codecov.io/bash) -J '^RemoteImage$' -D .build/derivedData

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
)

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# RemoteImage
22

3-
[![Swift 5.1](https://img.shields.io/badge/swift5.1-compatible-green.svg?longCache=true&style=flat-square)](https://developer.apple.com/swift)
4-
[![Platform](https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20tvOS-lightgrey.svg?longCache=true&style=flat-square)](https://www.apple.com)
3+
[![Swift 5.1](https://img.shields.io/badge/swift-5.1-green.svg?longCache=true&style=flat-square)](https://developer.apple.com/swift)
4+
[![Platforms](https://img.shields.io/badge/platforms-iOS%20%7C%20macOS%20%7C%20tvOS-lightgrey.svg?longCache=true&style=flat-square)](https://www.apple.com)
55
[![Current Version](https://img.shields.io/github/v/tag/crelies/RemoteImage?longCache=true&style=flat-square)](https://github.com/crelies/RemoteImage)
6+
[![Build status](https://travis-ci.com/crelies/RemoteImage.svg?token=THnaziKxRFFz1nKcsPgz&branch=dev)](https://travis-ci.com/crelies/RemoteImage)
7+
[![Code coverage](https://codecov.io/gh/crelies/RemoteImage/branch/dev/graph/badge.svg?token=DhJyoUKNPM)](https://codecov.io/gh/crelies/RemoteImage)
68
[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg?longCache=true&style=flat-square)](https://en.wikipedia.org/wiki/MIT_License)
79

810
This Swift package provides a wrapper view around the existing **SwiftUI** `Image view` which adds support for showing and caching remote images.

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

0 commit comments

Comments
 (0)