Skip to content
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

Implement Environment #135

Merged
merged 4 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion Sources/TokamakCore/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

@propertyWrapper public struct Environment<Value> {
protocol EnvironmentReader {
mutating func setContent(from values: EnvironmentValues)
}

@propertyWrapper public struct Environment<Value>: EnvironmentReader {
enum Content {
case keyPath(KeyPath<EnvironmentValues, Value>)
case value(Value)
}

var content: Environment<Value>.Content
let keyPath: KeyPath<EnvironmentValues, Value>
public init(_ keyPath: KeyPath<EnvironmentValues, Value>) {
content = .keyPath(keyPath)
self.keyPath = keyPath
}

mutating func setContent(from values: EnvironmentValues) {
content = .value(values[keyPath: keyPath])
}

public var wrappedValue: Value {
Expand Down
18 changes: 15 additions & 3 deletions Sources/TokamakCore/Environment/EnvironmentKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

public protocol EnvironmentKey {
associatedtype Value
static var defaultValue: Self.Value { get }
static var defaultValue: Value { get }
}

public struct _EnvironmentKeyWritingModifier<Value>: ViewModifier {
public typealias Body = Never
protocol EnvironmentModifier {
func _modifyEnvironment(_ values: inout EnvironmentValues)
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
}

public struct _EnvironmentKeyWritingModifier<Value>: ViewModifier, EnvironmentModifier {
// public typealias Body = Never

carson-katri marked this conversation as resolved.
Show resolved Hide resolved
public let keyPath: WritableKeyPath<EnvironmentValues, Value>
public let value: Value
Expand All @@ -27,6 +31,14 @@ public struct _EnvironmentKeyWritingModifier<Value>: ViewModifier {
self.keyPath = keyPath
self.value = value
}

public func body(content: Content) -> some View {
content
}

func _modifyEnvironment(_ values: inout EnvironmentValues) {
values[keyPath: keyPath] = value
}
}

extension View {
Expand Down
11 changes: 7 additions & 4 deletions Sources/TokamakCore/MountedComponents/MountedCompositeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ final class MountedCompositeView<R: Renderer>: MountedView<R>, Hashable {
private let parentTarget: R.TargetType

var state = [Any]()
var environmentValues: EnvironmentValues

init(_ view: AnyView, _ parentTarget: R.TargetType) {
init(_ view: AnyView, _ parentTarget: R.TargetType,
environmentValues: EnvironmentValues) {
self.parentTarget = parentTarget
self.environmentValues = environmentValues

super.init(view)
}

override func mount(with reconciler: StackReconciler<R>) {
let childBody = reconciler.render(compositeView: self)

let child: MountedView<R> = childBody.makeMountedView(parentTarget)
let child: MountedView<R> = childBody.makeMountedView(parentTarget, withEnvironment: environmentValues)
mountedChildren = [child]
child.mount(with: reconciler)
}
Expand All @@ -58,7 +61,7 @@ final class MountedCompositeView<R: Renderer>: MountedView<R>, Hashable {
switch (mountedChildren.last, reconciler.render(compositeView: self)) {
// no mounted children, but children available now
case let (nil, childBody):
let child: MountedView<R> = childBody.makeMountedView(parentTarget)
let child: MountedView<R> = childBody.makeMountedView(parentTarget, withEnvironment: environmentValues)
mountedChildren = [child]
child.mount(with: reconciler)

Expand All @@ -81,7 +84,7 @@ final class MountedCompositeView<R: Renderer>: MountedView<R>, Hashable {
// wrapper, then mount a new one with the new `childBody`
wrapper.unmount(with: reconciler)

let child: MountedView<R> = childBody.makeMountedView(parentTarget)
let child: MountedView<R> = childBody.makeMountedView(parentTarget, withEnvironment: environmentValues)
mountedChildren = [child]
child.mount(with: reconciler)
}
Expand Down
14 changes: 9 additions & 5 deletions Sources/TokamakCore/MountedComponents/MountedHostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public final class MountedHostView<R: Renderer>: MountedView<R> {
/// Target of this host view supplied by a renderer after mounting has completed.
private var target: R.TargetType?

private let environmentValues: EnvironmentValues

init(_ view: AnyView,
_ parentTarget: R.TargetType) {
_ parentTarget: R.TargetType,
environmentValues: EnvironmentValues) {
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
self.parentTarget = parentTarget
self.environmentValues = environmentValues

super.init(view)
}
Expand All @@ -48,7 +52,7 @@ public final class MountedHostView<R: Renderer>: MountedView<R> {

guard !view.children.isEmpty else { return }

mountedChildren = view.children.map { $0.makeMountedView(target) }
mountedChildren = view.children.map { $0.makeMountedView(target, withEnvironment: environmentValues) }
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
mountedChildren.forEach { $0.mount(with: reconciler) }
}

Expand Down Expand Up @@ -81,7 +85,7 @@ public final class MountedHostView<R: Renderer>: MountedView<R> {

// if no existing children then mount all new children
case (true, false):
mountedChildren = childrenViews.map { $0.makeMountedView(target) }
mountedChildren = childrenViews.map { $0.makeMountedView(target, withEnvironment: environmentValues) }
mountedChildren.forEach { $0.mount(with: reconciler) }

// if both arrays have items then reconcile by types and keys
Expand All @@ -99,7 +103,7 @@ public final class MountedHostView<R: Renderer>: MountedView<R> {
newChild = child
} else {
child.unmount(with: reconciler)
newChild = firstChild.makeMountedView(target)
newChild = firstChild.makeMountedView(target, withEnvironment: environmentValues)
newChild.mount(with: reconciler)
}
newChildren.append(newChild)
Expand All @@ -118,7 +122,7 @@ public final class MountedHostView<R: Renderer>: MountedView<R> {
// mount remaining views
for firstChild in childrenViews {
let newChild: MountedView<R> =
firstChild.makeMountedView(target)
firstChild.makeMountedView(target, withEnvironment: environmentValues)
newChild.mount(with: reconciler)
newChildren.append(newChild)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/TokamakCore/MountedComponents/MountedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class MountedView<R: Renderer> {
}

extension View {
func makeMountedView<R: Renderer>(_ parentTarget: R.TargetType)
func makeMountedView<R: Renderer>(_ parentTarget: R.TargetType, withEnvironment environmentValues: EnvironmentValues)
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
-> MountedView<R> {
let anyView = self as? AnyView ?? AnyView(self)
if anyView.type == EmptyView.self {
return MountedNull(anyView)
} else if anyView.bodyType == Never.self && !(anyView.type is ViewDeferredToRenderer.Type) {
return MountedHostView(anyView, parentTarget)
return MountedHostView(anyView, parentTarget, environmentValues: environmentValues)
} else {
return MountedCompositeView(anyView, parentTarget)
return MountedCompositeView(anyView, parentTarget, environmentValues: environmentValues)
}
}
}
21 changes: 20 additions & 1 deletion Sources/TokamakCore/StackReconciler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class StackReconciler<R: Renderer> {
self.scheduler = scheduler
rootTarget = target

rootView = view.makeMountedView(target)
rootView = view.makeMountedView(target, withEnvironment: EnvironmentValues())

rootView.mount(with: self)
}
Expand Down Expand Up @@ -88,6 +88,25 @@ public final class StackReconciler<R: Renderer> {
}
try! stateProperty.set(value: state, on: &compositeView.view.view)
}

let viewInfo = try! typeInfo(of: compositeView.view.type)
if viewInfo
.genericTypes
.filter({ $0 is EnvironmentModifier.Type }).count > 0 {
// Apply Environment changes:
if let modifier = try? viewInfo.property(named: "modifier").get(from: compositeView.view.view) as? EnvironmentModifier {
modifier._modifyEnvironment(&compositeView.environmentValues)
}
}

// Inject @Environment values
// In the future we can also inject @EnvironmentObject values
for prop in viewInfo.properties.filter({ $0.type is EnvironmentReader.Type }) {
// swiftlint:disable:next force_cast
var wrapper = try! prop.get(from: compositeView.view.view) as! EnvironmentReader
wrapper.setContent(from: compositeView.environmentValues)
try? prop.set(value: wrapper, on: &compositeView.view.view)
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
}
// swiftlint:enable force_try

let result = compositeView.view.bodyClosure(compositeView.view.view)
Expand Down
10 changes: 10 additions & 0 deletions Sources/TokamakDOM/Environment/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// File.swift
//
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
//
// Created by Carson Katri on 6/30/20.
//

import TokamakCore

public typealias Environment = TokamakCore.Environment
26 changes: 26 additions & 0 deletions Sources/TokamakDemo/EnvironmentDemo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019-2020 Tokamak contributors
//
// 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.
//
// Created by Carson Katri on 6/30/20.
//

import TokamakDOM

struct EnvironmentDemo: View {
@Environment(\.font) var font: Font?

var body: some View {
Text("\(font)")
}
}
2 changes: 2 additions & 0 deletions Sources/TokamakDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ let renderer = DOMRenderer(
SpacerDemo()
Spacer()
Text("Forced to bottom.")
EnvironmentDemo()
.font(.system(size: 21))
}
},
div
Expand Down