Skip to content

[WIP] WorkflowSwiftUI without ObservedObject #253

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

Closed
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
29 changes: 11 additions & 18 deletions Samples/SwiftUITestbed/Sources/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import MarketUI
import MarketWorkflowUI
import ViewEnvironment
import WorkflowSwiftUIExperimental
import WorkflowUI

struct MainScreen: SwiftUIScreen {
struct MainScreen: View, Screen {
let title: String
let didChangeTitle: (String) -> Void

Expand All @@ -32,14 +33,6 @@ struct MainScreen: SwiftUIScreen {

let didTapClose: (() -> Void)?

static func makeView(model: ObservableValue<MainScreen>) -> some View {
MainScreenView(model: model)
}
}

private struct MainScreenView: View {
@ObservedObject var model: ObservableValue<MainScreen>

@Environment(\.viewEnvironment.marketStylesheet) private var styles: MarketStylesheet
@Environment(\.viewEnvironment.marketContext) private var context: MarketContext

Expand All @@ -56,9 +49,9 @@ private struct MainScreenView: View {

TextField(
"Text",
text: model.binding(
get: \.title,
set: \.didChangeTitle
text: Binding(
get: { title },
set: didChangeTitle
)
)
.focused($focusedField, equals: .title)
Expand All @@ -67,10 +60,10 @@ private struct MainScreenView: View {
ToggleRow(
style: context.stylesheets.testbed.toggleRow,
label: "All Caps",
isEnabled: model.allCapsToggleIsEnabled,
isOn: model.binding(
get: \.allCapsToggleIsOn,
set: \.didChangeAllCapsToggle
isEnabled: allCapsToggleIsEnabled,
isOn: Binding(
get: { allCapsToggleIsOn },
set: didChangeAllCapsToggle
)
)

Expand All @@ -81,12 +74,12 @@ private struct MainScreenView: View {

Button(
"Push Screen",
action: model.didTapPushScreen
action: didTapPushScreen
)

Button(
"Present Screen",
action: model.didTapPresentScreen
action: didTapPresentScreen
)

Button(
Expand Down

This file was deleted.

107 changes: 0 additions & 107 deletions WorkflowSwiftUIExperimental/Sources/ObservableValue.swift

This file was deleted.

66 changes: 66 additions & 0 deletions WorkflowSwiftUIExperimental/Sources/Screen+View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 extension Screen where Self: View {
func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever!

return ViewControllerDescription(
type: UIHostingController<RootView<Self>>.self,
environment: environment,
build: {
UIHostingController(
rootView: RootView(
model: RootViewModel(
content: self,
environment: environment
)
)
)
},
update: { hostingController in
let object = hostingController.rootView.model
object.content = self
object.environment = environment
}
)
}
}

private final class RootViewModel<Content: View>: ObservableObject {
@Published var content: Content
@Published var environment: ViewEnvironment

init(content: Content, environment: ViewEnvironment) {
self.content = content
self.environment = environment
}
}

private struct RootView<Content: View>: View {
@ObservedObject var model: RootViewModel<Content>

var body: some View {
model.content
.environment(\.viewEnvironment, model.environment)
}
}

#endif
93 changes: 0 additions & 93 deletions WorkflowSwiftUIExperimental/Sources/SwiftUIScreen.swift

This file was deleted.

Loading