Skip to content

[feat]: add ViewEnvironmentUI and support automatic bridging in WorkflowUI #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Development.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Pod::Spec.new do |s|
s.dependency 'WorkflowCombine'
s.dependency 'WorkflowConcurrency'
s.dependency 'ViewEnvironment'
s.dependency 'ViewEnvironmentUI'

s.source_files = 'Samples/Dummy.swift'

Expand Down Expand Up @@ -101,6 +102,12 @@ Pod::Spec.new do |s|
}
end

s.test_spec 'ViewEnvironmentUITests' do |test_spec|
test_spec.requires_app_host = true
test_spec.source_files = 'ViewEnvironmentUI/Tests/**/*.swift'
test_spec.framework = 'XCTest'
end

s.test_spec 'WorkflowTests' do |test_spec|
test_spec.requires_app_host = true
test_spec.source_files = 'Workflow/Tests/**/*.swift'
Expand Down
17 changes: 16 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ let package = Package(
name: "ViewEnvironment",
targets: ["ViewEnvironment"]
),

// MARK: ViewEnvironmentUI

.library(
name: "ViewEnvironmentUI",
targets: ["ViewEnvironmentUI"]
),
],
dependencies: [
.package(url: "https://github.com/ReactiveCocoa/ReactiveSwift.git", from: "7.1.1"),
Expand Down Expand Up @@ -116,7 +123,7 @@ let package = Package(

.target(
name: "WorkflowUI",
dependencies: ["Workflow", "ViewEnvironment"],
dependencies: ["Workflow", "ViewEnvironment", "ViewEnvironmentUI"],
path: "WorkflowUI/Sources"
),
.testTarget(
Expand Down Expand Up @@ -228,6 +235,14 @@ let package = Package(
name: "ViewEnvironment",
path: "ViewEnvironment/Sources"
),

// MARK: ViewEnvironmentUI

.target(
name: "ViewEnvironmentUI",
dependencies: ["ViewEnvironment"],
path: "ViewEnvironmentUI/Sources"
),
],
swiftLanguageVersions: [.v5]
)
3 changes: 2 additions & 1 deletion RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For Squares, membership is managed through the `Workflow Swift Owners` registry

> ⚠️ [Optional] To avoid possible headaches when publishing podspecs, validation can be performed before updating the Workflow version number(s). To do this, run the following in the root directory of this repo:
> ```bash
> bundle exec pod lib lint Workflow.podspec ViewEnvironment.podspec WorkflowTesting.podspec WorkflowReactiveSwift.podspec WorkflowUI.podspec WorkflowRxSwift.podspec WorkflowReactiveSwiftTesting.podspec WorkflowRxSwiftTesting.podspec WorkflowSwiftUI.podspec WorkflowCombine.podspec WorkflowCombineTesting.podspec WorkflowConcurrency.podspec WorkflowConcurrencyTesting.podspec
> bundle exec pod lib lint Workflow.podspec ViewEnvironment.podspec ViewEnvironmentUI.podspec WorkflowTesting.podspec WorkflowReactiveSwift.podspec WorkflowUI.podspec WorkflowRxSwift.podspec WorkflowReactiveSwiftTesting.podspec WorkflowRxSwiftTesting.podspec WorkflowSwiftUI.podspec WorkflowCombine.podspec WorkflowCombineTesting.podspec WorkflowConcurrency.podspec WorkflowConcurrencyTesting.podspec
> ```

1. Update `VERSION` file based on [`semver`](https://semver.org/).
Expand All @@ -37,6 +37,7 @@ For Squares, membership is managed through the `Workflow Swift Owners` registry
bundle exec pod trunk push WorkflowTesting.podspec --synchronous
bundle exec pod trunk push WorkflowReactiveSwift.podspec --synchronous
bundle exec pod trunk push ViewEnvironment.podspec --synchronous
bundle exec pod trunk push ViewEnvironmentUI.podspec --synchronous
bundle exec pod trunk push WorkflowUI.podspec --synchronous
bundle exec pod trunk push WorkflowRxSwift.podspec --synchronous
bundle exec pod trunk push WorkflowReactiveSwiftTesting.podspec --synchronous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct LoginScreen: Screen {

func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
return ViewControllerDescription(
environment: environment,
build: { LoginViewController() },
update: { $0.update(with: self) }
)
Expand Down
1 change: 1 addition & 0 deletions Samples/Tutorial/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target 'Tutorial' do
pod 'Workflow', path: '../../Workflow.podspec', :testspecs => ['Tests']
pod 'WorkflowUI', path: '../../WorkflowUI.podspec', :testspecs => ['Tests']
pod 'ViewEnvironment', path: '../../ViewEnvironment.podspec'
pod 'ViewEnvironmentUI', path: '../../ViewEnvironmentUI.podspec', :testspecs => ['Tests']
pod 'WorkflowReactiveSwift', path: '../../WorkflowReactiveSwift.podspec', :testspecs => ['Tests']
pod 'BackStackContainer', path: '../BackStackContainer/BackStackContainer.podspec'

Expand Down
36 changes: 36 additions & 0 deletions ViewEnvironmentUI.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative('version')

Pod::Spec.new do |s|
s.name = 'ViewEnvironmentUI'
s.version = WORKFLOW_VERSION
s.summary = 'Provides a way to propagate a ViewEnvironment through an imperative hierarchy'
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 => "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 = 'ViewEnvironmentUI/Sources/**/*.swift'

s.dependency 'ViewEnvironment', "#{s.version}"

s.pod_target_xcconfig = { 'APPLICATION_EXTENSION_API_ONLY' => 'YES' }

s.test_spec 'Tests' do |test_spec|
test_spec.source_files = 'ViewEnvironmentUI/Tests/**/*.swift'
test_spec.framework = 'XCTest'
test_spec.library = 'swiftos'

# Create an app host so that we can host
# view or view controller based tests in a real environment.
test_spec.requires_app_host = true

test_spec.pod_target_xcconfig = { 'APPLICATION_EXTENSION_API_ONLY' => 'NO' }
end
end
6 changes: 6 additions & 0 deletions ViewEnvironmentUI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ViewEnvironmentUI

`ViewEnvironmentUI` provides a means to propagate a `ViewEnvironment` through a hierarchy of object nodes.

Support for propagation of `ViewEnvironment` through `UIViewController`s and `UIView`s is provided by this framework.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 UIKit
import ViewEnvironment

extension UIView: ViewEnvironmentPropagating {
@_spi(ViewEnvironmentWiring)
public var defaultEnvironmentAncestor: ViewEnvironmentPropagating? { superview }

@_spi(ViewEnvironmentWiring)
public var defaultEnvironmentDescendants: [ViewEnvironmentPropagating] { subviews }

@_spi(ViewEnvironmentWiring)
public func setNeedsApplyEnvironment() {
setNeedsLayout()
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 UIKit
import ViewEnvironment

extension UIViewController: ViewEnvironmentPropagating {
@_spi(ViewEnvironmentWiring)
public var defaultEnvironmentAncestor: ViewEnvironmentPropagating? { parent ?? presentingViewController }

@_spi(ViewEnvironmentWiring)
public var defaultEnvironmentDescendants: [ViewEnvironmentPropagating] {
var descendants = children

if let presentedViewController = presentedViewController {
descendants.append(presentedViewController)
}

return descendants
}

@_spi(ViewEnvironmentWiring)
public func setNeedsApplyEnvironment() {
viewIfLoaded?.setNeedsLayout()
}
}

#endif
86 changes: 86 additions & 0 deletions ViewEnvironmentUI/Sources/ViewEnvironmentObserving.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 ViewEnvironment

/// `ViewEnvironmentObserving` allows an environment propagation node to observe updates to the
/// `ViewEnvironment` as it flows through the node hierarchy and have
/// the environment applied to the node.
///
/// For example, for a `UIViewController` hierarchy observing `ViewEnvironment`:
/// ```swift
/// final class MyViewController:
/// UIViewController, ViewEnvironmentObserving
/// {
/// override func viewWillLayoutSubviews() {
/// super.viewWillLayoutSubviews()
///
/// // You _must_ call this function in viewWillLayoutSubviews()
/// applyEnvironmentIfNeeded()
/// }
///
/// func apply(environment: ViewEnvironment) {
/// // Apply values from the environment to your view controller (e.g. a theme)
/// }
///
/// // If you'd like to override values in the environment you can provide them here. If you'd
/// // like to just inherit the context from above there is no need to implement this function.
/// func customize(environment: inout ViewEnvironment) {
/// environment.traits.mode = .dark
/// }
/// }
/// ```
///
/// - Important: `UIViewController` and `UIView` conformers _must_ call `applyEnvironmentIfNeeded()`
/// in `viewWillLayoutSubviews()` and `layoutSubviews()` respectively.
///
public protocol ViewEnvironmentObserving: ViewEnvironmentPropagating {
/// Customizes the `ViewEnvironment` as it flows through this propagation node to provide overrides to environment
/// values. These customizations will apply to the environment on this node (e.g. `self.environment` and the
/// parameter of `apply(environment:)`) and will be propagated to all descendant nodes.
///
/// If you'd like to just inherit the environment from above, leave this function body empty.
///
func customize(environment: inout ViewEnvironment)

/// Consumers should apply the `ViewEnvironment` to their node when this function is called.
///
func apply(environment: ViewEnvironment)

/// Consumers must call this function when environment updates should be applied.
///
/// This will call `apply(environment:)` on the receiver if the node has been flagged for needing update.
///
/// When working with `UIViewController` and `UIView` nodes, consumers _must_ call this function in
/// `viewWillLayoutSubviews()` for `UIViewController`s and `layoutSubviews()` for `UIView`s.
///
func applyEnvironmentIfNeeded()

/// Called when the environment has been set for needing update, but before it has been applied.
///
/// This may be called frequently when compared to `apply(environment:)` which should only be called
/// when it's appropriate to apply the environment to the backing object (e.g. `viewWillLayoutSubviews`).
///
func environmentDidChange()
}

extension ViewEnvironmentObserving {
public func customize(environment: inout ViewEnvironment) {}

public func apply(environment: ViewEnvironment) {}

public func environmentDidChange() {}
}
Loading