Skip to content

Commit bae5d65

Browse files
authored
Merge pull request #3 from bullinnyc/add-conditional-foreground-element-color
Add conditional foreground element color.
2 parents 3259290 + 384160d commit bae5d65

5 files changed

Lines changed: 58 additions & 5 deletions

File tree

Examples/ContentView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import SwiftUI
1010
import CachedAsyncImage
1111

12+
@available(iOS 15.0, macOS 12.0, *)
1213
struct ContentView: View {
1314
// MARK: - Private Properties
1415

@@ -69,7 +70,7 @@ struct ContentView: View {
6970
}
7071
.font(.footnote)
7172
.multilineTextAlignment(.center)
72-
.foregroundColor(.red)
73+
.foregroundStyle(.red)
7374
}
7475
.padding()
7576
}
@@ -120,6 +121,7 @@ struct ContentView: View {
120121

121122
// MARK: - Preview Provider
122123

124+
@available(iOS 15.0, macOS 12.0, *)
123125
struct ContentView_Previews: PreviewProvider {
124126
static var previews: some View {
125127
ContentView()

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ CachedAsyncImage(
8585
}
8686
.font(.footnote)
8787
.multilineTextAlignment(.center)
88-
.foregroundColor(.red)
88+
.foregroundStyle(.red)
8989
}
9090
.padding()
9191
}
9292
}
9393
)
9494
```
9595

96-
### Set image cache limit (optional)
96+
### Set image cache limit if needed
9797
**Note:** The default value is `0`, e.g. is no count limit and is no total cost limit.
9898

9999
```swift
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Extension + View.swift
3+
// CachedAsyncImage
4+
//
5+
// Created by Dmitry Kononchuk on 04.11.2023.
6+
// Copyright © 2023 Dmitry Kononchuk. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
extension View {
12+
// MARK: - Public Methods
13+
14+
/// Used, e.g. to use the if #available statement.
15+
///
16+
/// .conditional { view in
17+
/// if #available(iOS 16.0, *) {
18+
/// view
19+
/// .tint(cursorColor)
20+
/// } else {
21+
/// view
22+
/// .accentColor(cursorColor)
23+
/// }
24+
///
25+
/// - Parameter transform: The transform to apply to the source `View`.
26+
///
27+
/// - Returns: Either the original `View` or the modified `View`.
28+
func conditional<Content: View>(
29+
@ViewBuilder transform: (Self) -> Content
30+
) -> Content {
31+
transform(self)
32+
}
33+
}

Sources/CachedAsyncImage/Resources/ResourcesManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ResourcesManager.swift
3-
// CachedAsyncImageTests
3+
// CachedAsyncImage
44
//
55
// Created by Dmitry Kononchuk on 18.06.2023.
66
// Copyright © 2023 Dmitry Kononchuk. All rights reserved.

Sources/CachedAsyncImage/Views/CachedAsyncImage.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,25 @@ struct CachedAsyncImage_Previews: PreviewProvider {
172172
}
173173
.font(.footnote)
174174
.multilineTextAlignment(.center)
175-
.foregroundColor(.red)
175+
.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, *) {
186+
view
187+
.foregroundStyle(.red)
188+
} else {
189+
view
190+
.foregroundColor(.red)
191+
}
192+
#endif
193+
}
176194
}
177195
.padding()
178196
}

0 commit comments

Comments
 (0)