Skip to content

Commit

Permalink
⚡ WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Aug 13, 2023
1 parent 937bcae commit bb60e8d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 79 deletions.
33 changes: 26 additions & 7 deletions Sources/StorybookKit/Hosting/_ViewHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ struct _ViewHost<ContentView: UIView>:
{

private let instantiate: @MainActor () -> ContentView
private let _update:
@MainActor (_ uiView: ContentView, _ context: Context) -> Void
private let _update: @MainActor (_ uiView: ContentView, _ context: Context) -> Void
private let maxWidth: Double

init(
maxWidth: Double,
instantiate: @escaping @MainActor () -> ContentView,
update: @escaping @MainActor (_ uiViewController: ContentView, _ context: Context) ->
Void = { _, _ in }
Void = { _, _ in }
) {
self.maxWidth = maxWidth
self.instantiate = instantiate
self._update = update
}

func makeUIView(context: Context) -> _Label<ContentView> {
let instantiated = instantiate()
return .init(contentView: instantiated)
return .init(maxWidth: maxWidth, contentView: instantiated)
}

func updateUIView(_ uiView: _Label<ContentView>, context: Context) {
Expand All @@ -31,8 +33,13 @@ struct _ViewHost<ContentView: UIView>:
final class _Label<ContentView: UIView>: UILabel {

let contentView: ContentView
let maxWidth: Double

init(contentView: ContentView) {
init(
maxWidth: Double,
contentView: ContentView
) {
self.maxWidth = maxWidth
self.contentView = contentView
super.init(frame: .null)
addSubview(contentView)
Expand All @@ -44,7 +51,9 @@ final class _Label<ContentView: UIView>: UILabel {
fatalError("init(coder:) has not been implemented")
}

override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int)
-> CGRect
{

var targetSize = bounds.size

Expand All @@ -56,7 +65,17 @@ final class _Label<ContentView: UIView>: UILabel {
targetSize.height = UIView.layoutFittingCompressedSize.height
}

let size = contentView.systemLayoutSizeFitting(targetSize)
var size = contentView.systemLayoutSizeFitting(targetSize)

if size.width > maxWidth {
targetSize.width = maxWidth
size = contentView.systemLayoutSizeFitting(
targetSize,
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)
}

return .init(origin: .zero, size: size)
}

Expand Down
41 changes: 28 additions & 13 deletions Sources/StorybookKit/Primitives/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@
import ResultBuilderKit
import SwiftUI

public protocol BookProvider {
static var body: BookNavigationLink { get }
}

public protocol BookType: View {

}

public final class BookStore: ObservableObject {

@Published var historyLinks: [BookNavigationLink] = []
@Published var historyPages: [BookNavigationLink] = []

public let title: String
public let links: [BookNavigationLink]
public let groups: [BookPageGroup]

private let allPages: [BookNavigationLink]

private let userDefaults = UserDefaults(suiteName: "jp.eure.storybook2") ?? .standard

public init(
title: String,
@ArrayBuilder<BookNavigationLink> links: () -> [BookNavigationLink]
@ArrayBuilder<BookPageGroup> groups: () -> [BookPageGroup]
) {
self.title = title
self.links = links().sorted(by: { $0.title < $1.title })
self.groups = groups().sorted(by: { $0.title < $1.title })
self.allPages = self.groups.flatMap { $0.pages }

updateHistory()
}
Expand All @@ -50,29 +57,36 @@ public final class BookStore: ObservableObject {
let indexes = userDefaults.array(forKey: "history") as? [Int] ?? []

let _links = indexes.compactMap { index -> BookNavigationLink? in
guard let link = links.first(where: { $0.declarationIdentifier.index == index }) else {
guard let page = allPages.first(where: { $0.declarationIdentifier.index == index }) else {
return nil
}
return link
return page
}

historyLinks = _links
historyPages = _links

}

func onOpen(link: BookNavigationLink) {

guard allPages.contains(where: { $0.id == link.id }) else {
return
}

let index = link.declarationIdentifier.index

var current = userDefaults.array(forKey: "history") as? [Int] ?? []
if let index = current.firstIndex(of: index) {
current.remove(at: index)
}

current.insert(index, at: 0)
current = Array(current.prefix(5))

userDefaults.set(current, forKey: "history")

print("Update history", current)

updateHistory()
}

Expand All @@ -93,18 +107,19 @@ public struct BookContainer: BookType {
NavigationView {
List {
Section {
ForEach(store.historyLinks) { link in
ForEach(store.historyPages) { link in
link
}
} header: {
Text("History")
}
Section {
ForEach(store.links) { link in
link

ForEach(store.groups) { group in
Section {
group
} header: {
Text(group.title)
}
} header: {
Text("Contents")
}
}
.listStyle(.grouped)
Expand Down
34 changes: 28 additions & 6 deletions Sources/StorybookKit/Primitives/BookNavigationLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import Foundation
import SwiftUI
import ResultBuilderKit

public struct DeclarationIdentifier: Hashable, Codable {

Expand All @@ -42,6 +43,29 @@ private func issueUniqueNumber() -> Int {
return _counter
}

public struct BookPageGroup: BookView, Identifiable {

public var id: UUID = .init()

public let title: String
public let pages: [BookPage]

public init(
title: String,
@ArrayBuilder<BookNavigationLink> pages: () -> [BookPage]
) {
self.title = title
self.pages = pages().sorted(by: { $0.title < $1.title })
}

public var body: some View {
ForEach(pages) { page in
page
}
}

}

/// A component that displays a disclosure view.
public struct BookNavigationLink: BookView, Identifiable {

Expand Down Expand Up @@ -69,12 +93,10 @@ public struct BookNavigationLink: BookView, Identifiable {
NavigationLink(
title,
destination: {
GeometryReader(content: { geometry in
ScrollView {
destination
.frame(width: geometry.size.width)
}
})
List {
destination
}
.listStyle(.plain)
.onAppear(perform: {
context?.onOpen(link: self)
})
Expand Down
23 changes: 1 addition & 22 deletions Sources/StorybookKit/Primitives/BookPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,4 @@
import Foundation
import SwiftUI

public struct BookPage<Content: View>: BookView {

public let title: String
public let content: Content

public init(
title: String,
@ViewBuilder content: () -> Content
) {
self.title = title
self.content = content()
}

public var body: some View {
BookText(title)
.font(.system(size: 32, weight: .bold))
.padding(.top, 24)
.padding(.bottom, 18)
content
}

}
public typealias BookPage = BookNavigationLink
53 changes: 27 additions & 26 deletions Sources/StorybookKit/Primitives/BookPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private struct FrameConstraint {
var maxHeight: CGFloat? = nil
}

public struct BookPreview<PlatformView: UIView>: BookView {
public struct BookPreview: BookView {

public struct Context {

Expand All @@ -42,7 +42,7 @@ public struct BookPreview<PlatformView: UIView>: BookView {

}

public let viewBlock: @MainActor (inout Context) -> PlatformView
public let viewBlock: @MainActor (inout Context) -> UIView

public let declarationIdentifier: DeclarationIdentifier

Expand All @@ -55,7 +55,7 @@ public struct BookPreview<PlatformView: UIView>: BookView {
_ file: StaticString = #file,
_ line: UInt = #line,
title: String? = nil,
viewBlock: @escaping @MainActor (inout Context) -> PlatformView
viewBlock: @escaping @MainActor (inout Context) -> UIView
) {

self.title = title
Expand All @@ -70,35 +70,36 @@ public struct BookPreview<PlatformView: UIView>: BookView {

public var body: some View {

Group {
VStack {
if let title {
Text(title)
.font(.system(size: 17, weight: .semibold))
}
_ViewHost(
instantiate: {

var context: Context = .init()
let view = viewBlock(&context)

// TODO: currently using workaround
Task {
controlView = context.controlView
}

return _View(element: view, frameConstraint: frameConstraint)
},
update: { _, _ in }
)

controlView
VStack {
if let title {
Text(title)
.font(.system(size: 17, weight: .semibold))
}

_ViewHost(
maxWidth: UIScreen.main.bounds.size.width /* so bad but it's for sizing with autolayout */,
instantiate: {

var context: Context = .init()
let view = viewBlock(&context)

// TODO: currently using workaround
Task {
controlView = context.controlView
}

return _View(element: view, frameConstraint: frameConstraint)
},
update: { _, _ in }
)

controlView

Text("\(file.description):\(line.description)")
.font(.body.monospacedDigit())

BookSpacer(height: 16)

}

}
Expand Down
9 changes: 4 additions & 5 deletions Sources/StorybookKitTextureSupport/BookNodePreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ import StorybookKit
import TextureBridging
import TextureSwiftSupport

public struct BookNodePreview<Node: ASDisplayNode>: BookView {
public struct BookNodePreview: BookView {

private var backing: BookPreview<NodeView<AnyDisplayNode>>
private var backing: BookPreview

@MainActor
public init(
_ file: StaticString = #file,
_ line: UInt = #line,
title: String? = nil,
nodeBlock: @escaping @MainActor (BookPreview<NodeView<AnyDisplayNode>>.Context) -> Node
nodeBlock: @escaping @MainActor (inout BookPreview.Context) -> ASDisplayNode
) {

self.backing = .init(file, line, title: title) { context in
let body = nodeBlock(context)
let body = nodeBlock(&context)

let node = AnyDisplayNode { _, size in
return LayoutSpec {
Expand Down

0 comments on commit bb60e8d

Please sign in to comment.