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

Add AppearanceActionModifier, onAppear/onDisappear #145

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions Sources/TokamakCore/Modifiers/AppearanceActionModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 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.

protocol AppearanceActionProtocol {
var appear: (() -> ())? { get }
var disappear: (() -> ())? { get }
}

/// Underscore is present in the name for SwiftUI compatibility.
struct _AppearanceActionModifier: ViewModifier {
var appear: (() -> ())?
var disappear: (() -> ())?
init(appear: (() -> ())? = nil, disappear: (() -> ())? = nil) {
self.appear = appear
self.disappear = disappear
}

typealias Body = Never
}

extension ModifiedContent: AppearanceActionProtocol
where Content: View, Modifier == _AppearanceActionModifier {
var appear: (() -> ())? { modifier.appear }
var disappear: (() -> ())? { modifier.disappear }
}

extension View {
public func onAppear(perform action: (() -> Swift.Void)? = nil) -> some View {
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
modifier(_AppearanceActionModifier(appear: action, disappear: nil))
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
}

public func onDisappear(perform action: (() -> Swift.Void)? = nil) -> some View {
modifier(_AppearanceActionModifier(appear: nil, disappear: action))
}
}
4 changes: 2 additions & 2 deletions Sources/TokamakCore/Modifiers/ViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public struct _ViewModifier_Content<Modifier>: View where Modifier: ViewModifier
}

public extension View {
func modifier<Modifier>(_ modifier: Modifier) -> Modifier.Body where Modifier: ViewModifier {
modifier.body(content: .init(modifier: modifier, view: AnyView(self)))
func modifier<Modifier>(_ modifier: Modifier) -> ModifiedContent<Self, Modifier> {
.init(content: self, modifier: modifier)
}
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/TokamakCore/MountedViews/MountedCompositeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ final class MountedCompositeView<R: Renderer>: MountedView<R>, Hashable {
override func mount(with reconciler: StackReconciler<R>) {
let childBody = reconciler.render(compositeView: self)

if let appearanceAction = view.view as? AppearanceActionProtocol {
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
appearanceAction.appear?()
}

let child: MountedView<R> = childBody.makeMountedView(parentTarget,
environmentValues)
mountedChildren = [child]
Expand All @@ -52,6 +56,10 @@ final class MountedCompositeView<R: Renderer>: MountedView<R>, Hashable {

override func unmount(with reconciler: StackReconciler<R>) {
mountedChildren.forEach { $0.unmount(with: reconciler) }

if let appearanceAction = view.view as? AppearanceActionProtocol {
appearanceAction.disappear?()
}
}

override func update(with reconciler: StackReconciler<R>) {
Expand Down
8 changes: 3 additions & 5 deletions Sources/TokamakDOM/Modifiers/_ViewModifier_Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
import Runtime
import TokamakCore

public typealias _ViewModifier_Content = TokamakCore._ViewModifier_Content

extension _ViewModifier_Content: ViewDeferredToRenderer {
extension ModifiedContent: ViewDeferredToRenderer where Content: View {
public var deferredBody: AnyView {
if let domModifier = modifier as? DOMViewModifier {
return AnyView(HTML("div", domModifier.attributes) {
view
content
})
} else {
return AnyView(HTML("div") {
view
content
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/TokamakDemo/Counter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public struct Counter: View {
Button("Increment") { count += 1 }
Text("\(count)")
}
.onAppear { print("Counter.VStack onAppear") }
.onDisappear { print("Counter.VStack onDisappear") }
Copy link
Member

Choose a reason for hiding this comment

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

Where do print statements get outputted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In Web Inspector console, all print output is redirected with console.log.

) : AnyView(
VStack { Text("Limit exceeded") }
)
Expand Down