Skip to content

Commit 84015e4

Browse files
committed
Add conveniences for working with optional bindings and alerts
1 parent 6c2146d commit 84015e4

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Sources/SwiftCrossUI/State/Binding.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
/// as a writable reference to the value.
33
@dynamicMemberLookup
44
@propertyWrapper
5-
public class Binding<Value> {
5+
public struct Binding<Value> {
66
public var wrappedValue: Value {
77
get {
88
getValue()
99
}
10-
set {
10+
nonmutating set {
1111
setValue(newValue)
1212
}
1313
}
@@ -32,6 +32,21 @@ public class Binding<Value> {
3232
self.setValue = set
3333
}
3434

35+
public init?(_ other: Binding<Value?>) {
36+
if let initialValue = other.wrappedValue {
37+
self.init(
38+
get: {
39+
other.wrappedValue ?? initialValue
40+
},
41+
set: { newValue in
42+
other.wrappedValue = newValue
43+
}
44+
)
45+
} else {
46+
return nil
47+
}
48+
}
49+
3550
/// Projects a property of a binding.
3651
public subscript<T>(dynamicMember keyPath: WritableKeyPath<Value, T>) -> Binding<T> {
3752
get {

Sources/SwiftCrossUI/State/State.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public struct State<Value>: DynamicProperty, StateProperty {
2828
/// setting a new value. This isn't in a didSet property accessor
2929
/// because we want more granular control over when it does and
3030
/// doesn't trigger.
31+
///
32+
/// Additionally updates the downstream observation if the
33+
/// wrapped value is an Optional<some ObservableObject> and the
34+
/// current case has toggled.
3135
func postSet() {
3236
// If the wrapped value is an Optional<some ObservableObject>
3337
// then we need to observe/unobserve whenever the optional

Sources/SwiftCrossUI/Views/Modifiers/AlertModifier.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ extension View {
1111
actions: actions()
1212
)
1313
}
14+
15+
public func alert(
16+
_ title: Binding<String?>,
17+
@AlertActionsBuilder actions: () -> [AlertAction]
18+
) -> some View {
19+
AlertModifierView(
20+
child: self,
21+
title: title.wrappedValue ?? "",
22+
isPresented: Binding {
23+
title.wrappedValue != nil
24+
} set: { newValue in
25+
if !newValue {
26+
title.wrappedValue = nil
27+
}
28+
},
29+
actions: actions()
30+
)
31+
}
1432
}
1533

1634
struct AlertModifierView<Child: View>: TypeSafeView {

Sources/SwiftCrossUI/Views/Slider.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,10 @@ public struct Slider: ElementaryView, View {
104104
maximum: maximum,
105105
decimalPlaces: decimalPlaces,
106106
environment: environment
107-
) { [weak value] newValue in
108-
guard let value = value else {
109-
return
107+
) { newValue in
108+
if let value {
109+
value.wrappedValue = newValue
110110
}
111-
value.wrappedValue = newValue
112111
}
113112

114113
if let value = value?.wrappedValue {

0 commit comments

Comments
 (0)