Skip to content

Commit

Permalink
🌲 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Aug 14, 2023
1 parent b09f7d5 commit df66289
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 271 deletions.
170 changes: 87 additions & 83 deletions Development/Demo/MyBook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,113 +24,117 @@ import StorybookKit
import SwiftUISupport

@MainActor
let myBook2 = BookContainer(
store: .init(
title: "MyUI",
links: {

BookNavigationLink(title: "Preview UI") {
BookSection(title: "Section") {

Text(
"""
Something description about this section.
"""
)
let myBook2 = Book.init(
title: "MyUI",
folders: {

BookPreview { _ in
let view = UIView(frame: .init(x: 0, y: 0, width: 80, height: 80))
view.backgroundColor = .systemPurple
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalToConstant: 80),
view.heightAnchor.constraint(equalToConstant: 80),
])
return view
}

BookPreview(title: "A component") { _ in
let view = UIView(frame: .null)
view.backgroundColor = .systemPurple
return view
}
.previewFrame(width: 80, height: 80)
BookFolder(title: "A") {
BookPageGroup(title: "Preview UI") {
BookNavigationLink(title: "Preview UI") {
BookSection(title: "Section") {

BookPreview { _ in
let view = UIView(frame: .init(x: 0, y: 0, width: 80, height: 80))
view.backgroundColor = .systemPurple
return view
}
.previewFrame(maxWidth: .greatestFiniteMagnitude, idealHeight: 10)
Text(
"""
Something description about this section.
"""
)

}
BookPreview { _ in
let view = UIView(frame: .init(x: 0, y: 0, width: 80, height: 80))
view.backgroundColor = .systemPurple
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalToConstant: 80),
view.heightAnchor.constraint(equalToConstant: 80),
])
return view
}

}
BookPreview(title: "A component") { _ in
let view = UIView(frame: .null)
view.backgroundColor = .systemPurple
return view
}
.previewFrame(width: 80, height: 80)

BookNavigationLink(title: "AlertController") {
BookPreview { _ in
let view = UIView(frame: .init(x: 0, y: 0, width: 80, height: 80))
view.backgroundColor = .systemPurple
return view
}
.previewFrame(maxWidth: .greatestFiniteMagnitude, idealHeight: 10)

BookPresent(title: "Pop") {
let alert = UIAlertController(
title: "Hi Storybook",
message: "As like this, you can present any view controller to check the behavior.",
preferredStyle: .alert
)
alert.addAction(.init(title: "Got it", style: .default, handler: { _ in }))
return alert
}
}

BookPresent(title: "Another Pop") {
let alert = UIAlertController(
title: "Hi Storybook",
message: "As like this, you can present any view controller to check the behavior.",
preferredStyle: .alert
)
alert.addAction(.init(title: "Got it", style: .default, handler: { _ in }))
return alert
}
BookNavigationLink(title: "AlertController") {

BookPresent(title: "Pop") {
let alert = UIAlertController(
title: "Hi Storybook",
message: "As like this, you can present any view controller to check the behavior.",
preferredStyle: .alert
)
alert.addAction(.init(title: "Got it", style: .default, handler: { _ in }))
return alert
}

}
BookPresent(title: "Another Pop") {
let alert = UIAlertController(
title: "Hi Storybook",
message: "As like this, you can present any view controller to check the behavior.",
preferredStyle: .alert
)
alert.addAction(.init(title: "Got it", style: .default, handler: { _ in }))
return alert
}

BookNavigationLink(title: "Test Push") {
BookPush(title: "Test") {
UIViewController()
}
}

BookNavigationLink(title: "State") {
BookPreview(title: "State: Success") { _ in
MySuccessView()
BookNavigationLink(title: "Test Push") {
BookPush(title: "Test") {
UIViewController()
}
}

BookPreview(title: "State: Loading") { _ in
MyLoadingView()
}
BookNavigationLink(title: "State") {
BookPreview(title: "State: Success") { _ in
MySuccessView()
}

BookPreview(title: "State: Loading") { _ in
MyLoadingView()
}

BookPreview(title: "State: Error") { _ in
MyErrorView()
BookPreview(title: "State: Error") { _ in
MyErrorView()
}
}
}

BookNavigationLink(title: "Pattern") {
BookNavigationLink(title: "Pattern") {

BookPattern.make(
["A", "AAA", "AAAAAA"],
[UIColor.blue, UIColor.red, UIColor.orange]
)
.makeBody { args in
BookPreview { _ in
let (text, color) = args
let label = UILabel()
label.text = text
label.textColor = color
return label
BookPattern.make(
["A", "AAA", "AAAAAA"],
[UIColor.blue, UIColor.red, UIColor.orange]
)
.makeBody { args in
BookPreview { _ in
let (text, color) = args
let label = UILabel()
label.text = text
label.textColor = color
return label
}
}
}

}
}

BookFolder(title: "B") {
}

}
)

}
)

private func labelExpandingTestBook() -> some View {
Expand Down
2 changes: 1 addition & 1 deletion Development/Demo/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct RootView: View {
// book: myBook
// )

StorybookDisplayRootView2(book: myBook2)
StorybookDisplayRootView(bookStore: .init(book: myBook2))

}
}
Expand Down
68 changes: 0 additions & 68 deletions Package.resolved

This file was deleted.

98 changes: 4 additions & 94 deletions Sources/StorybookKit/Primitives/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,112 +30,22 @@ public protocol BookType: View {

}

public final class BookStore: ObservableObject {

@Published var historyPages: [BookNavigationLink] = []
public struct Book {

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

private let allPages: [BookNavigationLink]

private let userDefaults = UserDefaults(suiteName: "jp.eure.storybook2") ?? .standard
public let folders: [BookFolder]

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

updateHistory()
}

private func updateHistory() {

let indexes = userDefaults.array(forKey: "history") as? [Int] ?? []

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

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()
}

}

public struct BookContainer: BookType {

@ObservedObject var store: BookStore

@MainActor
public init(
store: BookStore
) {
self.store = store
}

public var body: some View {
NavigationView {
List {
Section {
ForEach(store.historyPages) { link in
link
}
} header: {
Text("History")
}

ForEach(store.groups) { group in
Section {
group
} header: {
Text(group.title)
}
}
}
.listStyle(.grouped)
}
.navigationTitle(store.title)
.environment(\.bookContext, store)
self.folders = folders()
}

}

public final class BookContext {

func onOpen(link: BookNavigationLink) {
print(link)
}
}

private enum BookContextKey: EnvironmentKey {
static var defaultValue: BookStore?
Expand Down
Loading

0 comments on commit df66289

Please sign in to comment.