Skip to content

Commit ce33d9f

Browse files
authored
Fix custom ImageLoading functions with default arguments not being called (#2695)
* Fix default arguments of `ImageLoading` custom implementation not working * Update CHANGELOG.md * Update CHANGELOG.md
1 parent cd24f3f commit ce33d9f

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
## StreamChat
77
### ✅ Added
88
- Expose Extra Data for Giphy Attachment Payloads [#2678](https://github.com/GetStream/stream-chat-swift/pull/2678)
9-
109
### 🐞 Fixed
1110
- Rescue messages that are stuck in `.sending` state [#2676](https://github.com/GetStream/stream-chat-swift/pull/2676)
1211
- Fix not being able to resend failed attachments [#2680](https://github.com/GetStream/stream-chat-swift/pull/2680)
1312

13+
## StreamChatUI
14+
### 🐞 Fixed
15+
- Fix custom `ImageLoading` functions with default arguments not being called [#2695](https://github.com/GetStream/stream-chat-swift/pull/2695)
16+
1417
# [4.33.0](https://github.com/GetStream/stream-chat-swift/releases/tag/4.33.0)
1518
_June 08, 2023_
1619

Sources/StreamChatUI/Utils/ImageLoading/ImageLoading.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public extension ImageLoading {
9999
into imageView: UIImageView,
100100
from attachmentPayload: ImageAttachmentPayload?,
101101
maxResolutionInPixels: Double,
102-
completion: ((_ result: Result<UIImage, Error>) -> Void)? = nil
102+
completion: ((_ result: Result<UIImage, Error>) -> Void)?
103103
) -> Cancellable? {
104104
guard let originalWidth = attachmentPayload?.originalWidth,
105105
let originalHeight = attachmentPayload?.originalHeight else {
@@ -125,20 +125,40 @@ public extension ImageLoading {
125125
completion: completion
126126
)
127127
}
128+
129+
@discardableResult
130+
func loadImage(
131+
into imageView: UIImageView,
132+
from attachmentPayload: ImageAttachmentPayload?,
133+
maxResolutionInPixels: Double
134+
) -> Cancellable? {
135+
loadImage(
136+
into: imageView,
137+
from: attachmentPayload,
138+
maxResolutionInPixels: maxResolutionInPixels,
139+
completion: nil
140+
)
141+
}
128142
}
129143

130144
// MARK: - Default Parameters
131145

132146
public extension ImageLoading {
133-
// Default empty completion block.
147+
@discardableResult
148+
func loadImage(
149+
into imageView: UIImageView,
150+
from url: URL?
151+
) -> Cancellable? {
152+
loadImage(into: imageView, from: url, with: ImageLoaderOptions(), completion: nil)
153+
}
154+
134155
@discardableResult
135156
func loadImage(
136157
into imageView: UIImageView,
137158
from url: URL?,
138-
with options: ImageLoaderOptions = ImageLoaderOptions(),
139-
completion: ((_ result: Result<UIImage, Error>) -> Void)? = nil
159+
with options: ImageLoaderOptions
140160
) -> Cancellable? {
141-
loadImage(into: imageView, from: url, with: options, completion: completion)
161+
loadImage(into: imageView, from: url, with: options, completion: nil)
142162
}
143163
}
144164

StreamChat.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@
11761176
AD472EF825C425FB00A96E70 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = AD472EF725C425FB00A96E70 /* SnapshotTesting */; };
11771177
AD483B962A2658970004B406 /* ChannelMemberUnbanRequestPayload.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD483B952A2658970004B406 /* ChannelMemberUnbanRequestPayload.swift */; };
11781178
AD483B972A2658970004B406 /* ChannelMemberUnbanRequestPayload.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD483B952A2658970004B406 /* ChannelMemberUnbanRequestPayload.swift */; };
1179+
AD4C15562A55874700A32955 /* ImageLoading_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD4C15552A55874700A32955 /* ImageLoading_Tests.swift */; };
11791180
AD4CDD85296499160057BC8A /* ScrollViewPaginationHandler_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD4CDD81296498D20057BC8A /* ScrollViewPaginationHandler_Tests.swift */; };
11801181
AD4CDD862964991A0057BC8A /* InvertedScrollViewPaginationHandler_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD4CDD83296498EB0057BC8A /* InvertedScrollViewPaginationHandler_Tests.swift */; };
11811182
AD52A2192804850700D0157E /* ChannelConfigDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD52A2182804850700D0157E /* ChannelConfigDTO.swift */; };
@@ -3599,6 +3600,7 @@
35993600
AD470C9B26C6D8C60090759A /* ChatMessageListVCDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMessageListVCDataSource.swift; sourceTree = "<group>"; };
36003601
AD470C9D26C6D9030090759A /* ChatMessageListVCDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMessageListVCDelegate.swift; sourceTree = "<group>"; };
36013602
AD483B952A2658970004B406 /* ChannelMemberUnbanRequestPayload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelMemberUnbanRequestPayload.swift; sourceTree = "<group>"; };
3603+
AD4C15552A55874700A32955 /* ImageLoading_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageLoading_Tests.swift; sourceTree = "<group>"; };
36023604
AD4CDD81296498D20057BC8A /* ScrollViewPaginationHandler_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollViewPaginationHandler_Tests.swift; sourceTree = "<group>"; };
36033605
AD4CDD83296498EB0057BC8A /* InvertedScrollViewPaginationHandler_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InvertedScrollViewPaginationHandler_Tests.swift; sourceTree = "<group>"; };
36043606
AD52A2182804850700D0157E /* ChannelConfigDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelConfigDTO.swift; sourceTree = "<group>"; };
@@ -6928,6 +6930,7 @@
69286930
A3960E0427DA5512003AB2B0 /* Utils */ = {
69296931
isa = PBXGroup;
69306932
children = (
6933+
AD4C15552A55874700A32955 /* ImageLoading_Tests.swift */,
69316934
640FE0F526A57903006CE703 /* ListChangeIndexPathResolver_Tests.swift */,
69326935
BDDD1EAB2632E32000BA007B /* AppearanceProvider_Tests.swift */,
69336936
C1320E07276B2E0800A06B35 /* Array+SafeSubscript_Tests.swift */,
@@ -9777,6 +9780,7 @@
97779780
795296C12582494000435B2E /* ComponentsProvider_Tests.swift in Sources */,
97789781
225503B825DC59FD00A5A65A /* UIView+Helpers.swift in Sources */,
97799782
7865705825FB6DF300974045 /* UIViewController+Extensions_Tests.swift in Sources */,
9783+
AD4C15562A55874700A32955 /* ImageLoading_Tests.swift in Sources */,
97809784
40FA4E012A12A85E00DA21D2 /* VoiceRecordingVC_Tests.swift in Sources */,
97819785
400F063C29A6632F00242A86 /* ChatMessageListDateSeparatorView_Tests.swift in Sources */,
97829786
792057AC264168A6002B145B /* UITestsEnvironmentSetup.swift in Sources */,
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
3+
//
4+
5+
@testable import StreamChat
6+
@testable import StreamChatUI
7+
import XCTest
8+
9+
final class ImageLoading_Tests: XCTestCase {
10+
var sut: ImageLoading!
11+
var spy: ImageLoaderSpy!
12+
13+
override func setUp() {
14+
super.setUp()
15+
spy = ImageLoaderSpy()
16+
sut = spy
17+
}
18+
19+
override func tearDown() {
20+
sut = nil
21+
spy = nil
22+
super.tearDown()
23+
}
24+
25+
func test_loadImageIntoWithOptions_isCalled() {
26+
sut.loadImage(into: .init(image: nil), from: nil, with: .init(), completion: nil)
27+
28+
XCTAssertEqual(spy.loadImageIntoWithOptionsCallCount, 1)
29+
}
30+
31+
func test_loadImageIntoWithOptions_whenDefaultArgumentsUsed_isCalled() {
32+
sut.loadImage(into: .init(image: nil), from: nil, with: .init(), completion: nil)
33+
sut.loadImage(into: .init(image: nil), from: nil, with: .init())
34+
35+
XCTAssertEqual(spy.loadImageIntoWithOptionsCallCount, 2)
36+
}
37+
38+
func test_downloadImage_isCalled() {
39+
sut.downloadImage(with: .init(url: .localYodaImage, options: .init()), completion: { _ in })
40+
41+
XCTAssertEqual(spy.downloadImageCallCount, 1)
42+
}
43+
44+
func test_downloadMultipleImages_isCalled() {
45+
sut.downloadMultipleImages(with: [.init(url: .localYodaImage, options: .init())], completion: { _ in })
46+
47+
XCTAssertEqual(spy.downloadMultipleImagesCallCount, 1)
48+
}
49+
50+
class ImageLoaderSpy: ImageLoading {
51+
var loadImageIntoWithOptionsCallCount = 0
52+
var loadImageIntoWithAttachmentPayloadCallCount = 0
53+
var downloadImageCallCount = 0
54+
var downloadMultipleImagesCallCount = 0
55+
56+
@discardableResult
57+
func loadImage(
58+
into imageView: UIImageView,
59+
from url: URL?,
60+
with options: ImageLoaderOptions,
61+
completion: ((_ result: Result<UIImage, Error>) -> Void)?
62+
) -> Cancellable? {
63+
loadImageIntoWithOptionsCallCount += 1
64+
return nil
65+
}
66+
67+
@discardableResult
68+
func loadImage(
69+
into imageView: UIImageView,
70+
from attachmentPayload: ImageAttachmentPayload?,
71+
maxResolutionInPixels: Double,
72+
completion: ((_ result: Result<UIImage, Error>) -> Void)?
73+
) -> Cancellable? {
74+
loadImageIntoWithAttachmentPayloadCallCount += 1
75+
return nil
76+
}
77+
78+
@discardableResult
79+
func downloadImage(
80+
with request: ImageDownloadRequest,
81+
completion: @escaping ((_ result: Result<UIImage, Error>) -> Void)
82+
) -> Cancellable? {
83+
downloadImageCallCount += 1
84+
return nil
85+
}
86+
87+
func downloadMultipleImages(
88+
with requests: [ImageDownloadRequest],
89+
completion: @escaping (([Result<UIImage, Error>]) -> Void)
90+
) {
91+
downloadMultipleImagesCallCount += 1
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)