Skip to content

Commit 5c4f154

Browse files
committed
Make contexts inject themselves as environment object
1 parent fa6fbf6 commit 5c4f154

File tree

8 files changed

+66
-35
lines changed

8 files changed

+66
-35
lines changed

RELEASE_NOTES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur
44

55

66

7+
## 4.2.1
8+
9+
### 💡 Behavior changes
10+
11+
* `View.alert` now injects the context as environment object.
12+
* `View.label` now takes a localized string key instead of a key.
13+
* `View.sheet` now injects the context as environment object.
14+
* `View.fullScreenCover` now injects the context as environment object.
15+
16+
17+
718
## 4.2
819

920
This version refactors many views to take their styles & configs as environment values, instead of injecting them in the initializer.

Sources/SwiftUIKit/Extensions/View+Label.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ public extension View {
1414
///
1515
/// - Parameters:
1616
/// - text: The label text.
17-
func label(_ text: String) -> some View {
17+
func label(
18+
_ text: LocalizedStringKey,
19+
bundle: Bundle? = nil
20+
) -> some View {
1821
Label {
1922
Text(text)
2023
} icon: {
2124
self
2225
}
2326
}
27+
28+
/// Convert the view to a localized label.
29+
///
30+
/// - Parameters:
31+
/// - text: The label text.
32+
func localizedLabel(
33+
_ text: LocalizedStringKey,
34+
bundle: Bundle? = nil
35+
) -> some View {
36+
Label {
37+
Text(text, bundle: bundle)
38+
} icon: {
39+
self
40+
}
41+
}
2442
}
2543

2644
#Preview {

Sources/SwiftUIKit/Gestures/GestureButton.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111

1212
/**
1313
This button supports triggering different gestures in a way
14-
that maximized performance.
14+
that maximizes performance.
1515

1616
This button can't be used in a `ScrollView` since it blocks
1717
the scroll view gesture. To implement multi-gesture support

Sources/SwiftUIKit/Gestures/ScrollViewGestureButton.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ import SwiftUI
1313
This button supports triggering different gestures in a way
1414
that works within a `ScrollView`.
1515

16-
This button can't be used in a `ScrollView` since it blocks
17-
the scroll view gesture. To implement multi-gesture support
18-
in a `ScrollView`, use a ``ScrollViewGestureButton``.
19-
20-
This button can be used in a `ScrollView` and doesn't block
21-
the scroll view gesture despite applying many gestures. The
22-
complicated code is the result of much trial and error, and
23-
has been tested to not affect the scrolling or any gestures.
16+
This button does npt block scroll view gesture. The code is
17+
the result of much trial & error and has been tested to not
18+
affect scrolling.
2419

2520
If you don't need to use a scroll view, you should consider
26-
using a ``GestureButton`` instead.
21+
using a ``GestureButton`` instead.-
2722

2823
Note that the view uses an underlying `ButtonStyle` to make
2924
gestures work. It can thus not apply another style, but you

Sources/SwiftUIKit/Lists/ListButtonStyle.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
import SwiftUI
1010

11-
/**
12-
This style makes buttons take up the entire row and applies
13-
a content shape that makes the entire view tappable.
14-
15-
You can apply the style with `.buttonStyle(.list)`, and can
16-
apply it to an entire list, just as with other styles.
17-
*/
11+
/// This style makes the button take up the entire row, then
12+
/// applies a shape that makes the entire view tappable.
13+
///
14+
/// You can apply this style with `.buttonStyle(.list)`, and
15+
/// can apply it to an entire list, like any other style.
1816
public struct ListButtonStyle: ButtonStyle {
1917

2018
/// Create a custom style.
@@ -61,9 +59,11 @@ public extension ButtonStyle where Self == ListButtonStyle {
6159

6260
var body: some View {
6361
List {
64-
ForEach(0...100, id: \.self) {
65-
button("Button \($0)")
66-
.buttonStyle($0 == 0 ? .list : .list(pressedOpacity: 0.1))
62+
ForEach(0...100, id: \.self) { index in
63+
Button("Button \(index)") {
64+
overlayText = "\(index) tapped!"
65+
}
66+
.buttonStyle(index == 0 ? .list : .list(pressedOpacity: 0.1))
6767
}
6868
}
6969
.overlay(overlay)
@@ -78,12 +78,6 @@ public extension ButtonStyle where Self == ListButtonStyle {
7878
.opacity(overlayText.isEmpty ? 0 : 1)
7979
.onTapGesture { overlayText = "" }
8080
}
81-
82-
func button(_ name: String) -> some View {
83-
Button(name) {
84-
overlayText = "\(name) tapped!"
85-
}
86-
}
8781
}
8882

8983
return Preview()

Sources/SwiftUIKit/Presentation/AlertContext.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ import SwiftUI
3535

3636
In the code above, we create a custom, static `Alert` value
3737
to easily let us share and reuse alerts in an app or domain.
38-
You can also use enums to define multiple alerts.
3938

40-
You can also setup a global context in the application root,
41-
and pass it as an environment object, to get a single value.
39+
This view modifier will also inject the provided context as
40+
an environment object into the view hierarchy, to let other
41+
views in the same view hierarchy reuse the same context.
4242
*/
4343
public class AlertContext: PresentationContext<Alert> {
4444

@@ -52,10 +52,13 @@ public class AlertContext: PresentationContext<Alert> {
5252
public extension View {
5353

5454
/// Bind an ``AlertContext`` to the view.
55+
///
56+
/// This also injects this context as environment object.
5557
func alert(_ context: AlertContext) -> some View {
5658
alert(
5759
isPresented: context.isActiveBinding,
5860
content: context.content ?? { Alert(title: Text("")) }
5961
)
62+
.environmentObject(context)
6063
}
6164
}

Sources/SwiftUIKit/Presentation/FullScreenCoverContext.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import SwiftUI
2828
}
2929
```
3030

31-
You can also setup a global context in the application root,
32-
and pass it as an environment object, to get a single value.
31+
This view modifier will also inject the provided context as
32+
an environment object into the view hierarchy, to let other
33+
views in the same view hierarchy reuse the same context.
3334
*/
3435
public class FullScreenCoverContext: PresentationContext<AnyView> {
3536

@@ -46,11 +47,16 @@ import SwiftUI
4647
public extension View {
4748

4849
/// Bind an ``FullScreenCoverContext`` to the view.
49-
func fullScreenCover(_ context: FullScreenCoverContext) -> some View {
50+
///
51+
/// This also injects this context as environment object.
52+
func fullScreenCover(
53+
_ context: FullScreenCoverContext
54+
) -> some View {
5055
fullScreenCover(
5156
isPresented: context.isActiveBinding,
5257
content: context.content ?? EmptyView().any
5358
)
59+
.environmentObject(context)
5460
}
5561
}
5662
#endif

Sources/SwiftUIKit/Presentation/SheetContext.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import SwiftUI
2828
}
2929
```
3030

31-
You can also setup a global context in the application root,
32-
and pass it as an environment object, to get a single value.
31+
This view modifier will also inject the provided context as
32+
an environment object into the view hierarchy, to let other
33+
views in the same view hierarchy reuse the same context.
3334
*/
3435
public class SheetContext: PresentationContext<AnyView> {
3536

@@ -41,10 +42,13 @@ public class SheetContext: PresentationContext<AnyView> {
4142
public extension View {
4243

4344
/// Bind an ``SheetContext`` to the view.
45+
///
46+
/// This also injects this context as environment object.
4447
func sheet(_ context: SheetContext) -> some View {
4548
sheet(
4649
isPresented: context.isActiveBinding,
4750
content: context.content ?? EmptyView().any
4851
)
52+
.environmentObject(context)
4953
}
5054
}

0 commit comments

Comments
 (0)