-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95ddd1e
commit c7d51b6
Showing
7 changed files
with
312 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
WorkflowSwiftUI/Sources/EnvironmentValues+ViewEnvironment.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2023 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
import WorkflowUI | ||
|
||
private struct ViewEnvironmentKey: EnvironmentKey { | ||
static let defaultValue: ViewEnvironment = .empty | ||
} | ||
|
||
public extension EnvironmentValues { | ||
var viewEnvironment: ViewEnvironment { | ||
get { self[ViewEnvironmentKey.self] } | ||
set { self[ViewEnvironmentKey.self] = newValue } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2023 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#if canImport(UIKit) | ||
|
||
import SwiftUI | ||
|
||
public extension ObservableValue { | ||
func binding<T>( | ||
get: @escaping (Value) -> T, | ||
set: @escaping (Value) -> (T) -> Void | ||
) -> Binding<T> { | ||
// This convoluted way of creating a `Binding`, relative to `Binding.init(get:set:)`, is | ||
// a workaround borrowed from TCA for a SwiftUI issue: | ||
// https://github.com/pointfreeco/swift-composable-architecture/pull/770 | ||
ObservedObject(wrappedValue: self) | ||
.projectedValue[get: .init(rawValue: get), set: .init(rawValue: set)] | ||
} | ||
|
||
private subscript<T>( | ||
get get: HashableWrapper<(Value) -> T>, | ||
set set: HashableWrapper<(Value) -> (T) -> Void> | ||
) -> T { | ||
get { get.rawValue(value) } | ||
set { set.rawValue(value)(newValue) } | ||
} | ||
|
||
private struct HashableWrapper<Value>: Hashable { | ||
let rawValue: Value | ||
static func == (lhs: Self, rhs: Self) -> Bool { false } | ||
func hash(into hasher: inout Hasher) {} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2023 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Combine | ||
import Workflow | ||
|
||
@dynamicMemberLookup | ||
public final class ObservableValue<Value>: ObservableObject { | ||
private var internalValue: Value | ||
private let subject = PassthroughSubject<Value, Never>() | ||
private var cancellable: AnyCancellable? | ||
private var isDuplicate: ((Value, Value) -> Bool)? | ||
public private(set) var value: Value { | ||
get { | ||
return internalValue | ||
} | ||
set { | ||
subject.send(newValue) | ||
} | ||
} | ||
|
||
public private(set) lazy var objectWillChange = ObservableObjectPublisher() | ||
private var parentCancellable: AnyCancellable? | ||
|
||
public static func makeObservableValue( | ||
_ value: Value, | ||
isDuplicate: ((Value, Value) -> Bool)? = nil | ||
) -> (ObservableValue, Sink<Value>) { | ||
let observableValue = ObservableValue(value: value, isDuplicate: isDuplicate) | ||
let sink = Sink { newValue in | ||
observableValue.value = newValue | ||
} | ||
|
||
return (observableValue, sink) | ||
} | ||
|
||
private init(value: Value, isDuplicate: ((Value, Value) -> Bool)?) { | ||
self.internalValue = value | ||
self.isDuplicate = isDuplicate | ||
self.cancellable = valuePublisher() | ||
.dropFirst() | ||
.sink { [weak self] newValue in | ||
guard let self = self else { return } | ||
self.objectWillChange.send() | ||
self.internalValue = newValue | ||
} | ||
// Allows removeDuplicates operator to have the initial value. | ||
subject.send(value) | ||
} | ||
|
||
//// Scopes the ObservableValue to a subset of Value to LocalValue given the supplied closure while allowing to optionally remove duplicates. | ||
/// - Parameters: | ||
/// - toLocalValue: A closure that takes a Value and returns a LocalValue. | ||
/// - isDuplicate: An optional closure that checks to see if a LocalValue is a duplicate. | ||
/// - Returns: a scoped ObservableValue of LocalValue. | ||
public func scope<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue, isDuplicate: ((LocalValue, LocalValue) -> Bool)? = nil) -> ObservableValue<LocalValue> { | ||
return scopeToLocalValue(toLocalValue, isDuplicate: isDuplicate) | ||
} | ||
|
||
/// Scopes the ObservableValue to a subset of Value to LocalValue given the supplied closure and removes duplicate values using Equatable. | ||
/// - Parameter toLocalValue: A closure that takes a Value and returns a LocalValue. | ||
/// - Returns: a scoped ObservableValue of LocalValue. | ||
public func scope<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue) -> ObservableValue<LocalValue> where LocalValue: Equatable { | ||
return scopeToLocalValue(toLocalValue, isDuplicate: { $0 == $1 }) | ||
} | ||
|
||
/// Returns the value at the given keypath of ``Value``. | ||
/// | ||
/// In combination with `@dynamicMemberLookup`, this allows us to write `model.myProperty` instead of | ||
/// `model.value.myProperty` where `model` has type `ObservableValue<T>`. | ||
public subscript<T>(dynamicMember keyPath: KeyPath<Value, T>) -> T { | ||
internalValue[keyPath: keyPath] | ||
} | ||
|
||
private func scopeToLocalValue<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue, isDuplicate: ((LocalValue, LocalValue) -> Bool)? = nil) -> ObservableValue<LocalValue> { | ||
let localObservableValue = ObservableValue<LocalValue>( | ||
value: toLocalValue(internalValue), | ||
isDuplicate: isDuplicate | ||
) | ||
localObservableValue.parentCancellable = valuePublisher().sink(receiveValue: { newValue in | ||
localObservableValue.value = toLocalValue(newValue) | ||
}) | ||
return localObservableValue | ||
} | ||
|
||
private func valuePublisher() -> AnyPublisher<Value, Never> { | ||
guard let isDuplicate = isDuplicate else { | ||
return subject.eraseToAnyPublisher() | ||
} | ||
|
||
return subject.removeDuplicates(by: isDuplicate).eraseToAnyPublisher() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2023 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#if canImport(UIKit) | ||
|
||
import SwiftUI | ||
import Workflow | ||
import WorkflowUI | ||
|
||
public protocol SwiftUIScreen: Screen { | ||
associatedtype Content: View | ||
|
||
@ViewBuilder | ||
static func makeView(model: ObservableValue<Self>) -> Content | ||
|
||
static var isDuplicate: ((Self, Self) -> Bool)? { get } | ||
} | ||
|
||
public extension SwiftUIScreen { | ||
static var isDuplicate: ((Self, Self) -> Bool)? { return nil } | ||
} | ||
|
||
public extension SwiftUIScreen where Self: Equatable { | ||
static var isDuplicate: ((Self, Self) -> Bool)? { { $0 == $1 } } | ||
} | ||
|
||
public extension SwiftUIScreen { | ||
func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription { | ||
ViewControllerDescription( | ||
type: ModeledHostingController<Self, WithModel<Self, EnvironmentInjectingView<Content>>>.self, | ||
environment: environment, | ||
build: { | ||
let (model, modelSink) = ObservableValue.makeObservableValue(self, isDuplicate: Self.isDuplicate) | ||
let (viewEnvironment, envSink) = ObservableValue.makeObservableValue(environment) | ||
return ModeledHostingController( | ||
modelSink: modelSink, | ||
viewEnvironmentSink: envSink, | ||
rootView: WithModel(model, content: { model in | ||
EnvironmentInjectingView( | ||
viewEnvironment: viewEnvironment, | ||
content: Self.makeView(model: model) | ||
) | ||
}) | ||
) | ||
}, | ||
update: { | ||
$0.modelSink.send(self) | ||
$0.viewEnvironmentSink.send(environment) | ||
} | ||
) | ||
} | ||
} | ||
|
||
private struct EnvironmentInjectingView<Content: View>: View { | ||
@ObservedObject var viewEnvironment: ObservableValue<ViewEnvironment> | ||
let content: Content | ||
|
||
var body: some View { | ||
content | ||
.environment(\.viewEnvironment, viewEnvironment.value) | ||
} | ||
} | ||
|
||
private final class ModeledHostingController<Model, Content: View>: UIHostingController<Content> { | ||
let modelSink: Sink<Model> | ||
let viewEnvironmentSink: Sink<ViewEnvironment> | ||
|
||
init(modelSink: Sink<Model>, viewEnvironmentSink: Sink<ViewEnvironment>, rootView: Content) { | ||
self.modelSink = modelSink | ||
self.viewEnvironmentSink = viewEnvironmentSink | ||
|
||
super.init(rootView: rootView) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("not implemented") | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2023 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
|
||
struct WithModel<Model, Content: View>: View { | ||
@ObservedObject private var model: ObservableValue<Model> | ||
private let content: (ObservableValue<Model>) -> Content | ||
|
||
init( | ||
_ model: ObservableValue<Model>, | ||
@ViewBuilder content: @escaping (ObservableValue<Model>) -> Content | ||
) { | ||
self.model = model | ||
self.content = content | ||
} | ||
|
||
var body: Content { | ||
content(model) | ||
} | ||
} |