Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into feature/TDP-197402
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-michaelbrown committed Nov 8, 2024
2 parents 9990699 + 535e279 commit 08f563d
Show file tree
Hide file tree
Showing 30 changed files with 384 additions and 550 deletions.
2 changes: 1 addition & 1 deletion Customization/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ viewStateOperators: |-
.eraseToAnyPublisher()
viewStatePropertyComment: The view state publisher.
viewStatePropertyName: statePublisher
viewStateTransform: Publishers.Map(upstream: context.$state, transform: viewStateFactory).eraseToAnyPublisher()
viewStateTransform: store.viewStatePublisher
publisherType: AnyPublisher
publisherFailureType: Never
contextGenericTypes:
Expand Down
123 changes: 22 additions & 101 deletions Customization/Perception.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public protocol PerceptibleViewStateStore<ViewState>: AnyObject, Perceptible {
associatedtype ViewState: Equatable

var viewState: ViewState { get }

func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T>

func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T>
}

// MARK: - State Store
Expand Down Expand Up @@ -119,20 +109,6 @@ public final class AnyPerceptibleViewStateStore<
) where Base.ViewState == ViewState {
box = PerceptibleViewStateStoreBox(base)
}

public func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T> {
box.bind(to: keyPath, onChange: onChange)
}

public func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T> {
box.bind(to: keyPath, onChange: onChange)
}
}

@preconcurrency
Expand All @@ -150,20 +126,6 @@ private class PerceptibleViewStateStoreBox<
init(_ base: Base) {
self.base = base
}

override func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T> {
base.bind(to: keyPath, onChange: onChange)
}

override func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T> {
base.bind(to: keyPath, onChange: onChange)
}
}

@preconcurrency
Expand All @@ -175,55 +137,22 @@ private class PerceptibleViewStateStoreBase<
var viewState: ViewState {
preconditionFailure("Property in abstract base class must be overridden")
}

// swiftlint:disable:next unavailable_function
func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T> {
preconditionFailure("Method in abstract base class must be overridden")
}

// swiftlint:disable:next unavailable_function
func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T> {
preconditionFailure("Method in abstract base class must be overridden")
}
}

// MARK: - Preview

#if DEBUG

@Perceptible
@preconcurrency
@MainActor
public final class PerceptiblePreviewStore<ViewState: Equatable>: PerceptibleViewStateStore {

public let viewState: ViewState
public var viewState: ViewState

public init(viewState: ViewState) {
self.viewState = viewState
}

public func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T> {
.constant(viewState[keyPath: keyPath])
}

public func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T> {
.constant(viewState[keyPath: keyPath])
}
}

#endif

// MARK: - Scope

@Perceptible
Expand All @@ -248,28 +177,14 @@ private final class PerceptibleScope<
self.store = store
self.keyPath = keyPath
}

func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T> {
store.bind(to: self.keyPath.appending(path: keyPath), onChange: onChange)
}

func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T> {
store.bind(to: self.keyPath.appending(path: keyPath), onChange: onChange)
}
}

// MARK: - Store

@Perceptible
@preconcurrency
@MainActor
open class PerceptibleStore<
public final class PerceptibleStore<
State: Equatable,
ViewState: Equatable
>: PerceptibleStateStore, PerceptibleViewStateStore {
Expand Down Expand Up @@ -313,30 +228,36 @@ open class PerceptibleStore<
self.viewStateSubject = viewStateSubject
self.transform = transform
}
}

// MARK: - Extensions

extension PerceptibleViewStateStore {

public func scope<T: Equatable>(
viewState keyPath: KeyPath<ViewState, T>
) -> AnyPerceptibleViewStateStore<T> {
AnyPerceptibleViewStateStore(PerceptibleScope(store: self, keyPath: keyPath))
}

public func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: @escaping @MainActor (T) -> Void
) -> Binding<T> {
Binding(get: { [self] in viewState[keyPath: keyPath] }, set: { onChange($0) })
Binding { [self] in
viewState[keyPath: keyPath]
} set: { value in
onChange(value)
}
}

public func bind<T>(
to keyPath: KeyPath<ViewState, T>,
onChange: (@MainActor (T) -> Void)?
) -> Binding<T> {
Binding(get: { [self] in viewState[keyPath: keyPath] }, set: { onChange?($0) })
}
}

// MARK: - Extensions

extension PerceptibleViewStateStore {

public func scope<T: Equatable>(
viewState keyPath: KeyPath<ViewState, T>
) -> AnyPerceptibleViewStateStore<T> {
AnyPerceptibleViewStateStore(PerceptibleScope(store: self, keyPath: keyPath))
bind(to: keyPath) { value in
onChange?(value)
}
}
}
```
77 changes: 0 additions & 77 deletions Sources/Nodes/Extensions/Binding.swift

This file was deleted.

Loading

0 comments on commit 08f563d

Please sign in to comment.