-
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.
Add WorkflowSwiftUIExperimental (#252)
* move sources from WorkflowSwiftUIExperimental repo * rename `isDuplicate` to `isEquivalent` * update podspecs
- Loading branch information
1 parent
4399ddc
commit ada3b7e
Showing
8 changed files
with
348 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require_relative('version') | ||
|
||
Pod::Spec.new do |s| | ||
s.name = 'WorkflowSwiftUIExperimental' | ||
s.version = '0.1' | ||
s.summary = 'Infrastructure for Workflow-powered SwiftUI' | ||
s.homepage = 'https://www.github.com/square/workflow-swift' | ||
s.license = 'Apache License, Version 2.0' | ||
s.author = 'Square' | ||
s.source = { :git => 'https://github.com/square/workflow-swift.git', :tag => "swiftui-experimental/v#{s.version}" } | ||
|
||
# 1.7 is needed for `swift_versions` support | ||
s.cocoapods_version = '>= 1.7.0' | ||
|
||
s.swift_versions = [WORKFLOW_SWIFT_VERSION] | ||
s.ios.deployment_target = WORKFLOW_IOS_DEPLOYMENT_TARGET | ||
s.osx.deployment_target = WORKFLOW_MACOS_DEPLOYMENT_TARGET | ||
|
||
s.source_files = 'WorkflowSwiftUIExperimental/Sources/*.swift' | ||
|
||
s.dependency 'Workflow', WORKFLOW_VERSION | ||
s.dependency 'WorkflowUI', WORKFLOW_VERSION | ||
|
||
s.pod_target_xcconfig = { 'APPLICATION_EXTENSION_API_ONLY' => 'YES' } | ||
end |
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,11 @@ | ||
# WorkflowSwiftUIExperimental | ||
|
||
Experimental extensions to Workflow for writing Screens in SwiftUI. | ||
|
||
## Versioning | ||
|
||
Because this module is experimental, it is versioned separately from other modules in Workflow. You should bump its version as part of any pull request that changes it, and need not bump its version in PRs that change only other modules. | ||
|
||
Per semantic versioning, its major version remains at `0`, and only its minor version is incremented. Any increase in the minor version may come with breaking changes. | ||
|
||
To bump the minor version, update `s.version` in `WorkflowSwiftUIExperimental.podspec`. |
29 changes: 29 additions & 0 deletions
29
WorkflowSwiftUIExperimental/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 } | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
WorkflowSwiftUIExperimental/Sources/ObservableValue+Binding.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,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 |
107 changes: 107 additions & 0 deletions
107
WorkflowSwiftUIExperimental/Sources/ObservableValue.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,107 @@ | ||
/* | ||
* 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 let isEquivalent: ((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, | ||
isEquivalent: ((Value, Value) -> Bool)? = nil | ||
) -> (ObservableValue, Sink<Value>) { | ||
let observableValue = ObservableValue(value: value, isEquivalent: isEquivalent) | ||
let sink = Sink { newValue in | ||
observableValue.value = newValue | ||
} | ||
|
||
return (observableValue, sink) | ||
} | ||
|
||
private init(value: Value, isEquivalent: ((Value, Value) -> Bool)?) { | ||
self.internalValue = value | ||
self.isEquivalent = isEquivalent | ||
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. | ||
/// - isEquivalent: An optional closure that checks to see if a LocalValue is equivalent. | ||
/// - Returns: a scoped ObservableValue of LocalValue. | ||
public func scope<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue, isEquivalent: ((LocalValue, LocalValue) -> Bool)? = nil) -> ObservableValue<LocalValue> { | ||
return scopeToLocalValue(toLocalValue, isEquivalent: isEquivalent) | ||
} | ||
|
||
/// 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, isEquivalent: { $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, isEquivalent: ((LocalValue, LocalValue) -> Bool)? = nil) -> ObservableValue<LocalValue> { | ||
let localObservableValue = ObservableValue<LocalValue>( | ||
value: toLocalValue(internalValue), | ||
isEquivalent: isEquivalent | ||
) | ||
localObservableValue.parentCancellable = valuePublisher().sink(receiveValue: { newValue in | ||
localObservableValue.value = toLocalValue(newValue) | ||
}) | ||
return localObservableValue | ||
} | ||
|
||
private func valuePublisher() -> AnyPublisher<Value, Never> { | ||
guard let isEquivalent = isEquivalent else { | ||
return subject.eraseToAnyPublisher() | ||
} | ||
|
||
return subject.removeDuplicates(by: isEquivalent).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 isEquivalent: ((Self, Self) -> Bool)? { get } | ||
} | ||
|
||
public extension SwiftUIScreen { | ||
static var isEquivalent: ((Self, Self) -> Bool)? { return nil } | ||
} | ||
|
||
public extension SwiftUIScreen where Self: Equatable { | ||
static var isEquivalent: ((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, isEquivalent: Self.isEquivalent) | ||
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) | ||
} | ||
} |