-
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.
[fix] View environment customizations on hosting VCs now propagate to…
… SwiftUI environment (#297) * ModeledHostingController implementations now conform to ViewEnvironmentObserving * Make StateAccessor init public
- Loading branch information
1 parent
b027b1d
commit 3495af0
Showing
5 changed files
with
177 additions
and
12 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
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,80 @@ | ||
#if canImport(UIKit) | ||
|
||
import SwiftUI | ||
import ViewEnvironment | ||
@_spi(ViewEnvironmentWiring) import ViewEnvironmentUI | ||
import WorkflowSwiftUI | ||
import XCTest | ||
|
||
final class ObservableScreenTests: XCTestCase { | ||
func test_viewEnvironmentObservation() { | ||
// Ensure that environment customizations made on the view controller | ||
// are propagated to the SwiftUI view environment. | ||
|
||
var state = MyState() | ||
|
||
let viewController = TestKeyEmittingScreen( | ||
model: MyModel( | ||
accessor: StateAccessor( | ||
state: state, | ||
sendValue: { $0(&state) } | ||
) | ||
) | ||
) | ||
.buildViewController(in: .empty) | ||
|
||
let lifetime = viewController.addEnvironmentCustomization { environment in | ||
environment[TestKey.self] = 1 | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
XCTAssertEqual(state.emittedValue, 1) | ||
|
||
withExtendedLifetime(lifetime) {} | ||
} | ||
} | ||
|
||
private struct TestKey: ViewEnvironmentKey { | ||
static var defaultValue: Int = 0 | ||
} | ||
|
||
@ObservableState | ||
private struct MyState { | ||
var emittedValue: TestKey.Value? | ||
} | ||
|
||
private struct MyModel: ObservableModel { | ||
typealias State = MyState | ||
|
||
let accessor: StateAccessor<State> | ||
} | ||
|
||
private struct TestKeyEmittingScreen: ObservableScreen { | ||
typealias Model = MyModel | ||
|
||
var model: Model | ||
|
||
let sizingOptions: WorkflowSwiftUI.SwiftUIScreenSizingOptions = [.preferredContentSize] | ||
|
||
static func makeView(store: Store<Model>) -> some View { | ||
ContentView(store: store) | ||
} | ||
|
||
struct ContentView: View { | ||
@Environment(\.viewEnvironment) | ||
var viewEnvironment: ViewEnvironment | ||
|
||
var store: Store<Model> | ||
|
||
var body: some View { | ||
WithPerceptionTracking { | ||
let _ = { store.emittedValue = viewEnvironment[TestKey.self] }() | ||
Color.clear | ||
.frame(width: 1, height: 1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#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
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