Skip to content

Commit 8da5b62

Browse files
committed
feat: improve swift api
1 parent a7d5727 commit 8da5b62

3 files changed

Lines changed: 23 additions & 24 deletions

File tree

apps/example/swift/App.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ let initialState = BrownfieldStore(
1111

1212
@main
1313
struct MyApp: App {
14-
1514
init() {
1615
ReactNativeBrownfield.shared.startReactNative {
1716
print("loaded")
@@ -41,30 +40,25 @@ struct MyApp: App {
4140
}
4241

4342
struct NativeView: View {
44-
@UseStore<BrownfieldStore, User>(\.user, key: BrownfieldStore.storeName) var user
45-
@UseStore<BrownfieldStore, Double>(\.counter, key: BrownfieldStore.storeName) var counter
43+
@UseStore<BrownfieldStore> var store
4644

4745
var body: some View {
4846
VStack {
4947
Text("Native Side")
5048
.font(.headline)
5149
.padding(.top)
5250

53-
Text("User: \(user.name)")
54-
Text("Count: \(Int(counter))")
51+
Text("User: \(store.state.user.name)")
52+
Text("Count: \(Int(store.state.counter))")
5553

56-
TextField("Name", text: Binding(get: { user.name }, set: { data in
57-
$user.set { state in
58-
state.user.name = data
59-
}
54+
TextField("Name", text: Binding(get: { store.state.user.name }, set: { data in
55+
store.set { $0.user.name = data }
6056
}))
6157
.textFieldStyle(.roundedBorder)
6258
.padding(.horizontal)
6359

6460
Button("Increment") {
65-
$counter.set { state in
66-
state.counter += 1
67-
}
61+
store.set { $0.counter += 1 }
6862
}
6963
.buttonStyle(.borderedProminent)
7064

packages/brownie/ios/BrownieStore.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ extension Notification.Name {
66
public static let BrownieStoreUpdated = Notification.Name("BrownieStoreUpdated")
77
}
88

9+
public protocol BrownieStoreProtocol: Codable {
10+
static var storeName: String { get }
11+
}
12+
913
public struct StoreKey<State: Codable>: EnvironmentKey {
1014
public static var defaultValue: Store<State> { fatalError("Store not provided") }
1115
}
@@ -18,22 +22,17 @@ public extension EnvironmentValues {
1822

1923
@MainActor
2024
@propertyWrapper
21-
public struct UseStore<State: Codable, Value>: DynamicProperty {
22-
private let keyPath: KeyPath<State, Value>
25+
public struct UseStore<State: BrownieStoreProtocol>: DynamicProperty {
2326
@StateObject private var store: Store<State>
2427

25-
public init(_ keyPath: KeyPath<State, Value>, key: String) {
26-
self.keyPath = keyPath
28+
public init() {
29+
let key = State.storeName
2730
let foundStore = StoreManager.shared.store(key: key, as: State.self)
2831
guard let foundStore else { fatalError("Store not found for key: \(key)") }
2932
self._store = StateObject(wrappedValue: foundStore)
3033
}
3134

32-
public var wrappedValue: Value {
33-
store.state[keyPath: keyPath]
34-
}
35-
36-
public var projectedValue: Store<State> {
35+
public var wrappedValue: Store<State> {
3736
store
3837
}
3938
}
@@ -146,4 +145,9 @@ public class Store<State: Codable>: ObservableObject {
146145
public func get<Value>(_ keyPath: KeyPath<State, Value>) -> Value {
147146
state[keyPath: keyPath]
148147
}
148+
149+
/// Convenience subscript for property access
150+
public subscript<Value>(_ keyPath: KeyPath<State, Value>) -> Value {
151+
state[keyPath: keyPath]
152+
}
149153
}

packages/brownie/scripts/generators/swift.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ export async function generateSwift(
6767
});
6868

6969
const storeNameExtension = `
70-
extension ${typeName} {
71-
static let storeName = "${name}"
70+
extension ${typeName}: BrownieStoreProtocol {
71+
public static let storeName = "${name}"
7272
}
7373
`;
7474

75-
const swiftOutput = lines.join('\n') + storeNameExtension;
75+
const swiftOutput =
76+
'import Brownie\n\n' + lines.join('\n') + storeNameExtension;
7677
const absoluteOutputPath = path.resolve(process.cwd(), outputPath);
7778
const outputDir = path.dirname(absoluteOutputPath);
7879

0 commit comments

Comments
 (0)