Skip to content

Commit b153880

Browse files
authored
Add conditional onСhange.
1 parent bae5d65 commit b153880

5 files changed

Lines changed: 71 additions & 22 deletions

File tree

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
xcode: ['14.3']
16+
xcode: ['15.0.1']
1717
macos: ['macos-13']
1818
scheme: ['CachedAsyncImage']
1919
command: ['test']

Examples/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct ContentView: View {
8585
)
8686
)
8787
.clipped()
88-
.cornerRadius(20)
88+
.clipShape(RoundedRectangle(cornerRadius: 20))
8989
.padding([.leading, .trailing], Self.paddingStandart)
9090
}
9191
}

Sources/CachedAsyncImage/Extensions/Extension + View.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ extension View {
1414
/// Used, e.g. to use the if #available statement.
1515
///
1616
/// .conditional { view in
17-
/// if #available(iOS 16.0, *) {
17+
/// if #available(iOS 15.0, macOS 12.0, *) {
1818
/// view
19-
/// .tint(cursorColor)
19+
/// .foregroundStyle(.red)
2020
/// } else {
2121
/// view
22-
/// .accentColor(cursorColor)
22+
/// .foregroundColor(.red)
2323
/// }
2424
///
2525
/// - Parameter transform: The transform to apply to the source `View`.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// OnChange.swift
3+
// CachedAsyncImage
4+
//
5+
// Created by Dmitry Kononchuk on 23.11.2023.
6+
// Copyright © 2023 Dmitry Kononchuk. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct OnChange<Value>: ViewModifier where Value: Equatable {
12+
// MARK: - Public Properties
13+
14+
let value: Value
15+
let isInitial: Bool
16+
let action: (_ oldValue: Value?, _ newValue: Value) -> Void
17+
18+
// MARK: - Body Method
19+
20+
func body(content: Content) -> some View {
21+
if #available(iOS 17.0, macOS 14.0, *) {
22+
content
23+
.onChange(of: value, initial: isInitial) { oldValue, newValue in
24+
action(oldValue, newValue)
25+
}
26+
} else {
27+
content
28+
.onChange(of: value) { newValue in
29+
action(nil, newValue)
30+
}
31+
}
32+
}
33+
}
34+
35+
// MARK: - Ext. View
36+
37+
extension View {
38+
/// Method overloading – onChange(of:initial:_:).
39+
/// Adds a modifier for this view that fires an action when a specific value changes.
40+
///
41+
/// - Parameters:
42+
/// - value: The value to check against when determining whether to run the closure.
43+
/// - isInitial: Whether the action should be run when this view initially appears.
44+
/// - action: A closure to run when the value changes.
45+
/// - oldValue: The old value that failed the comparisoncheck
46+
/// (or the initial value when requested).
47+
/// - newValue: The new value that failed the comparison check.
48+
///
49+
/// - Returns: A view that fires an action when the specified value changes.
50+
func onChange<Value>(
51+
of value: Value,
52+
isInitial: Bool = false,
53+
action: @escaping (_ oldValue: Value?, _ newValue: Value) -> Void
54+
) -> some View where Value: Equatable {
55+
modifier(OnChange(value: value, isInitial: isInitial, action: action))
56+
}
57+
}

Sources/CachedAsyncImage/Views/CachedAsyncImage.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public struct CachedAsyncImage: View {
3232
errorOrPlaceholder
3333
}
3434
}
35-
.onChange(of: url, perform: { imageLoader.fetchImage(from: $0) })
35+
.onChange(of: url) { _, newValue in
36+
imageLoader.fetchImage(from: newValue)
37+
}
3638
.onAppear {
3739
imageLoader.fetchImage(from: url)
3840
}
@@ -57,8 +59,8 @@ public struct CachedAsyncImage: View {
5759
self.image = image
5860
self.error = error
5961

60-
self.placeholder = nil
61-
self.placeholderWithProgress = nil
62+
placeholder = nil
63+
placeholderWithProgress = nil
6264
}
6365

6466
/// - Parameters:
@@ -81,7 +83,7 @@ public struct CachedAsyncImage: View {
8183
self.image = image
8284
self.error = error
8385

84-
self.placeholderWithProgress = nil
86+
placeholderWithProgress = nil
8587
}
8688

8789
/// - Parameters:
@@ -100,11 +102,11 @@ public struct CachedAsyncImage: View {
100102
)
101103

102104
self.url = url
103-
self.placeholderWithProgress = placeholder
105+
self.placeholder = nil
104106
self.image = image
105107
self.error = error
106108

107-
self.placeholder = nil
109+
placeholderWithProgress = placeholder
108110
}
109111
}
110112

@@ -173,23 +175,13 @@ struct CachedAsyncImage_Previews: PreviewProvider {
173175
.font(.footnote)
174176
.multilineTextAlignment(.center)
175177
.conditional { view in
176-
#if os(iOS)
177-
if #available(iOS 15.0, *) {
178-
view
179-
.foregroundStyle(.red)
180-
} else {
181-
view
182-
.foregroundColor(.red)
183-
}
184-
#elseif os(macOS)
185-
if #available(macOS 12.0, *) {
178+
if #available(iOS 15.0, macOS 12.0, *) {
186179
view
187180
.foregroundStyle(.red)
188181
} else {
189182
view
190183
.foregroundColor(.red)
191184
}
192-
#endif
193185
}
194186
}
195187
.padding()

0 commit comments

Comments
 (0)