Move Playground to example

This commit is contained in:
Kylmakalle 2024-09-27 22:42:43 +03:00
parent cf418360dc
commit 3bf947c458
2 changed files with 0 additions and 174 deletions

View File

@ -1,174 +0,0 @@
import Foundation
import UIKit
import AsyncDisplayKit
import Display
import SwiftUI
private final class PlaygroundScreenNode: ASDisplayNode {
private let headerBackgroundNode: ASDisplayNode
private let headerCornerNode: ASImageNode
private let swiftUINode: ASDisplayNode
private var pushController: ((ViewController) -> Void)?
private var isDismissed = false
private var validLayout: (layout: ContainerViewLayout, navigationHeight: CGFloat)?
init(pushController: @escaping (ViewController) -> Void) {
self.pushController = pushController
self.headerBackgroundNode = ASDisplayNode()
self.headerBackgroundNode.backgroundColor = .black
self.headerCornerNode = ASImageNode()
self.headerCornerNode.displaysAsynchronously = false
self.headerCornerNode.displayWithoutProcessing = true
self.headerCornerNode.image = generateImage(CGSize(width: 20.0, height: 10.0), rotatedContext: { size, context in
context.setFillColor(UIColor.black.cgColor)
context.fill(CGRect(origin: CGPoint(), size: size))
context.setBlendMode(.copy)
context.setFillColor(UIColor.clear.cgColor)
context.fillEllipse(in: CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: 20.0, height: 20.0)))
})?.stretchableImage(withLeftCapWidth: 10, topCapHeight: 1)
// Create SwiftUI view and wrap it in UIHostingController
let swiftUIView = MySwiftUIView(pushController: pushController)
let hostingController = UIHostingController(rootView: swiftUIView)
// Create ASDisplayNode to host the SwiftUI view
self.swiftUINode = ASDisplayNode { hostingController.view }
super.init()
self.backgroundColor = THEME.list.itemBlocksBackgroundColor
self.addSubnode(self.headerBackgroundNode)
self.addSubnode(self.headerCornerNode)
self.addSubnode(self.swiftUINode)
}
func containerLayoutUpdated(layout: ContainerViewLayout, navigationHeight: CGFloat, transition: ContainedViewLayoutTransition) {
if self.isDismissed {
return
}
self.validLayout = (layout, navigationHeight)
let headerHeight = navigationHeight + 260.0
transition.updateFrame(node: self.headerBackgroundNode, frame: CGRect(origin: CGPoint(x: -1.0, y: 0), size: CGSize(width: layout.size.width + 2.0, height: headerHeight)))
transition.updateFrame(node: self.headerCornerNode, frame: CGRect(origin: CGPoint(x: 0.0, y: headerHeight), size: CGSize(width: layout.size.width, height: 10.0)))
// Position the SwiftUI view
transition.updateFrame(node: self.swiftUINode, frame: CGRect(origin: CGPoint(x: 0, y: headerHeight + 10), size: CGSize(width: layout.size.width, height: (layout.size.height - headerHeight - 10) / 2.0)))
}
func animateOut(completion: @escaping () -> Void) {
guard let (layout, navigationHeight) = self.validLayout else {
completion()
return
}
self.isDismissed = true
let transition: ContainedViewLayoutTransition = .animated(duration: 0.4, curve: .spring)
let headerHeight = navigationHeight + 260.0
transition.updateFrame(node: self.headerBackgroundNode, frame: CGRect(origin: CGPoint(x: -1.0, y: -headerHeight - 10.0), size: CGSize(width: layout.size.width + 2.0, height: headerHeight)), completion: { _ in
completion()
})
transition.updateFrame(node: self.headerCornerNode, frame: CGRect(origin: CGPoint(x: 0.0, y: -10.0), size: CGSize(width: layout.size.width, height: 10.0)))
// Animate out the SwiftUI view as well
transition.updateFrame(node: self.swiftUINode, frame: CGRect(origin: CGPoint(x: 0, y: layout.size.height), size: CGSize(width: layout.size.width, height: (layout.size.height - headerHeight - 10) / 2.0)))
}
}
// Example SwiftUI view
struct MySwiftUIView: View {
let pushController: (ViewController) -> Void
var body: some View {
ScrollView {
VStack(spacing: 10) {
// Your buttons here
Button("Button 1") { }
.buttonStyle(CustomButtonStyle())
Button("Button 2") { }
.buttonStyle(CustomButtonStyle())
Button("Button 3") { }
.buttonStyle(CustomButtonStyle())
Button("Button 4") { }
.buttonStyle(CustomButtonStyle())
Button("Button 5") { }
.buttonStyle(CustomButtonStyle())
Button("Button 6") { }
.buttonStyle(CustomButtonStyle())
Button("Button 7") { }
.buttonStyle(CustomButtonStyle())
Button("Button 8") { }
.buttonStyle(CustomButtonStyle())
Button("Button 9") { }
.buttonStyle(CustomButtonStyle())
Button("Button 10") { }
.buttonStyle(CustomButtonStyle())
Button("Button 11") { }
.buttonStyle(CustomButtonStyle())
Button("Button 12") { }
.buttonStyle(CustomButtonStyle())
Button("Button 13") { }
.buttonStyle(CustomButtonStyle())
Button("Button 14") { }
.buttonStyle(CustomButtonStyle())
// ... more buttons
}
.padding()
}
}
}
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
.frame(height: 44) // Set a fixed height for all buttons
}
}
public final class PlaygroundScreen: ViewController {
public init() {
let navigationBarTheme = NavigationBarTheme(buttonColor: .white, disabledButtonColor: .white, primaryTextColor: .white, backgroundColor: .clear, enableBackgroundBlur: true, separatorColor: .clear, badgeBackgroundColor: THEME.navigationBar.badgeBackgroundColor, badgeStrokeColor: THEME.navigationBar.badgeStrokeColor, badgeTextColor: THEME.navigationBar.badgeTextColor)
super.init(navigationBarPresentationData: NavigationBarPresentationData(theme: navigationBarTheme, strings: NavigationBarStrings(back: "", close: "")))
self.statusBar.statusBarStyle = .White
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func loadDisplayNode() {
self.displayNode = PlaygroundScreenNode(pushController: { [weak self] c in
if let strongSelf = self {
strongSelf.push(c)
}
})
}
override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
super.containerLayoutUpdated(layout, transition: transition)
(self.displayNode as! PlaygroundScreenNode).containerLayoutUpdated(layout: layout, navigationHeight: self.navigationLayout(layout: layout).navigationFrame.maxY, transition: transition)
}
public func animateOut(completion: @escaping () -> Void) {
self.statusBar.statusBarStyle = .Black
(self.displayNode as! PlaygroundScreenNode).animateOut(completion: completion)
}
}