Skip to content

Commit f9ce00e

Browse files
committed
Deprecate context.state(_:) and rename it to context.binding(_:)
1 parent 5de971d commit f9ce00e

File tree

7 files changed

+45
-49
lines changed

7 files changed

+45
-49
lines changed

Examples/Packages/iOS/Sources/ExampleVoiceMemo/VoiceMemoRow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct VoiceMemoRow: View {
99
var context
1010

1111
var isPlaying: Binding<Bool> {
12-
context.state(IsPlayingAtom(voiceMemo: voiceMemo))
12+
context.binding(IsPlayingAtom(voiceMemo: voiceMemo))
1313
}
1414

1515
var elapsedTime: TimeInterval {

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,6 @@ Context is a structure for using and interacting with atom values from views or
10251025
|[set](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomcontext/set(_:for:))|Sets a new value to the atom.|
10261026
|[modify](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomcontext/modify(_:body:))|Modifies the cached atom value.|
10271027
|[subscript[]](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomcontext/subscript(_:))|Read-write access for applying mutating methods.|
1028-
|[state](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomwatchablecontext/state(_:))|Gets a binding to the atom state.|
10291028
|[refresh](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomcontext/refresh(_:)-1gb3a)|Produce a new value of the atom after waiting until asynchronous operation is complete.|
10301029
|[reset](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomcontext/reset(_:))|Reset an atom to the default value or a first output.|
10311030

@@ -1038,6 +1037,7 @@ A context available through the `@ViewContext` property wrapper when using atoms
10381037

10391038
|API|Use|
10401039
|:--|:--|
1040+
|[binding](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomwatchablecontext/binding(_:))|Gets a binding to the atom state.|
10411041
|[snapshot()](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomviewcontext/snapshot())|For debugging, takes a snapshot that captures specific set of values of atoms.|
10421042

10431043
<details><summary><code>📖 Example</code></summary>
@@ -1063,8 +1063,8 @@ struct BooksView: View {
10631063
var body: some View {
10641064
// watch
10651065
let booksTask = context.watch(FetchBooksAtom()) // Task<[Book], Error>
1066-
// state
1067-
let searchQuery = context.state(SearchQueryAtom()) // Binding<String>
1066+
// binding
1067+
let searchQuery = context.binding(SearchQueryAtom()) // Binding<String>
10681068

10691069
List {
10701070
Suspense(booksTask) { books in

Sources/Atoms/Context/AtomContext.swift

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -190,33 +190,3 @@ public protocol AtomWatchableContext: AtomContext {
190190
@discardableResult
191191
func watch<Node: Atom>(_ atom: Node) -> Node.Loader.Value
192192
}
193-
194-
public extension AtomWatchableContext {
195-
/// Creates a `Binding` that accesses the value associated with the given read-write atom.
196-
///
197-
/// This method only accepts read-write atoms such as types conforming to ``StateAtom``,
198-
/// and returns a binding that accesses the value or assigns a new value for the atom.
199-
/// When you set a new value to the `wrappedValue` property of the binding, it assigns the value
200-
/// to the atom, and immediately notifies downstream atoms and views.
201-
/// Note that the binding initiates watching the given atom when the value is accessed through the
202-
/// `wrappedValue` property.
203-
///
204-
/// ```swift
205-
/// let context = ...
206-
/// let binding = context.state(TextAtom())
207-
/// binding.wrappedValue = "New text"
208-
/// binding.wrappedValue.append(" is mutated!")
209-
/// print(binding.wrappedValue) // Prints "New text is mutated!"
210-
/// ```
211-
///
212-
/// - Parameter atom: An atom to create binding for.
213-
///
214-
/// - Returns: The value associated with the given atom.
215-
@inlinable
216-
func state<Node: StateAtom>(_ atom: Node) -> Binding<Node.Loader.Value> {
217-
Binding(
218-
get: { watch(atom) },
219-
set: { self[atom] = $0 }
220-
)
221-
}
222-
}

Sources/Atoms/Context/AtomViewContext.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import SwiftUI
2+
13
/// A context structure to read, watch, and otherwise interact with atoms.
24
///
35
/// When an atom is watched through this context, and that atom is updated,
@@ -202,6 +204,34 @@ public struct AtomViewContext: AtomWatchableContext {
202204
)
203205
}
204206

207+
/// Creates a `Binding` that accesses the value of the given read-write atom.
208+
///
209+
/// This method only accepts read-write atoms such as ones conforming to ``StateAtom``,
210+
/// and returns a binding that accesses the value or set a new value for the atom.
211+
/// When you set a new value to the `wrappedValue` of the returned binding, it assigns the value
212+
/// to the atom, and immediately notifies downstream atoms and views.
213+
/// Note that the binding initiates watching the given atom when the value is accessed through the
214+
/// `wrappedValue`.
215+
///
216+
/// ```swift
217+
/// let context = ...
218+
/// let binding = context.binding(TextAtom())
219+
/// binding.wrappedValue = "New text"
220+
/// binding.wrappedValue.append(" is mutated!")
221+
/// print(binding.wrappedValue) // Prints "New text is mutated!"
222+
/// ```
223+
///
224+
/// - Parameter atom: An atom to create binding to.
225+
///
226+
/// - Returns: A binding to the atom value.
227+
@inlinable
228+
public func binding<Node: StateAtom>(_ atom: Node) -> Binding<Node.Loader.Value> {
229+
Binding(
230+
get: { watch(atom) },
231+
set: { set($0, for: atom) }
232+
)
233+
}
234+
205235
/// Takes a snapshot of an atom hierarchy for debugging purposes.
206236
///
207237
/// This method captures all of the atom values and dependencies currently in use in

Sources/Atoms/Deprecated.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ public extension Atom {
99
}
1010
}
1111

12+
public extension AtomWatchableContext {
13+
@available(*, deprecated, renamed: "AtomViewContext.binding(_:)")
14+
func state<Node: StateAtom>(_ atom: Node) -> Binding<Node.Loader.Value> {
15+
Binding(
16+
get: { watch(atom) },
17+
set: { set($0, for: atom) }
18+
)
19+
}
20+
}
21+
1222
public extension Resettable {
1323
@available(*, deprecated, renamed: "CurrentContext")
1424
typealias ResetContext = AtomCurrentContext<Loader.Coordinator>

Sources/Atoms/PropertyWrapper/WatchState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ public struct WatchState<Node: StateAtom>: DynamicProperty {
6262
/// Accessing this property itself does not start watching the atom, but does when
6363
/// the view accesses to the getter of the binding.
6464
public var projectedValue: Binding<Node.Loader.Value> {
65-
context.state(atom)
65+
context.binding(atom)
6666
}
6767
}

Tests/AtomsTests/Context/AtomContextTests.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,4 @@ final class AtomContextTests: XCTestCase {
1414

1515
XCTAssertEqual(context[atom], 100)
1616
}
17-
18-
@MainActor
19-
func testState() {
20-
let atom = TestStateAtom(defaultValue: 0)
21-
let context: AtomWatchableContext = AtomTestContext()
22-
let state = context.state(atom)
23-
24-
XCTAssertEqual(context.read(atom), 0)
25-
26-
state.wrappedValue = 100
27-
28-
XCTAssertEqual(state.wrappedValue, 100)
29-
XCTAssertEqual(context.read(atom), 100)
30-
}
3117
}

0 commit comments

Comments
 (0)