Skip to content

Commit

Permalink
Supports push and present in history #25
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Jan 18, 2022
1 parent 7c2a40b commit 48cd0bb
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 149 deletions.
12 changes: 8 additions & 4 deletions Storybook.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
4B81F7A5246F41E5009C7602 /* BookPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B81F7A4246F41E5009C7602 /* BookPadding.swift */; };
4B81F7A7246F49F5009C7602 /* Book.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B81F7A6246F49F5009C7602 /* Book.swift */; };
4B81F7A9246F4A45009C7602 /* BookView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B81F7A8246F4A45009C7602 /* BookView.swift */; };
4B9FF5DC2796AF7200EDCD77 /* History.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B9FF5DB2796AF7200EDCD77 /* History.swift */; };
4BB03C0122266C1F006E9E49 /* TapStackCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB03C0022266C1F006E9E49 /* TapStackCell.swift */; };
4BB03C0922267064006E9E49 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB03C0822267064006E9E49 /* AppDelegate.swift */; };
4BB03C0B22267064006E9E49 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB03C0A22267064006E9E49 /* ViewController.swift */; };
Expand Down Expand Up @@ -192,6 +193,7 @@
4B81F7A4246F41E5009C7602 /* BookPadding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookPadding.swift; sourceTree = "<group>"; };
4B81F7A6246F49F5009C7602 /* Book.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Book.swift; sourceTree = "<group>"; };
4B81F7A8246F4A45009C7602 /* BookView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookView.swift; sourceTree = "<group>"; };
4B9FF5DB2796AF7200EDCD77 /* History.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = History.swift; sourceTree = "<group>"; };
4BB03C0022266C1F006E9E49 /* TapStackCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TapStackCell.swift; sourceTree = "<group>"; };
4BB03C0622267064006E9E49 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4BB03C0822267064006E9E49 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -425,6 +427,7 @@
957A89722226697600CDD25D /* StackScrollViewController.swift */,
957A89712226697600CDD25D /* StorybookViewController.swift */,
4B1582072619BC740072569A /* StorybookFilterViewController.swift */,
4B9FF5DB2796AF7200EDCD77 /* History.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -914,6 +917,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4B9FF5DC2796AF7200EDCD77 /* History.swift in Sources */,
4BC54CC22791DDE600E91FE0 /* PresentCell.swift in Sources */,
9523243423D201F600677D5A /* HighlightStackCell.swift in Sources */,
4B1FE787248900C300C90F6A /* StackCellType.swift in Sources */,
Expand Down Expand Up @@ -1286,7 +1290,7 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = StorybookKit/Info.plist;
GENERATE_INFOPLIST_FILE = YES;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -1314,7 +1318,7 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = StorybookKit/Info.plist;
GENERATE_INFOPLIST_FILE = YES;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -1342,7 +1346,7 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = StorybookUI/Info.plist;
GENERATE_INFOPLIST_FILE = YES;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -1372,7 +1376,7 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = StorybookUI/Info.plist;
GENERATE_INFOPLIST_FILE = YES;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Storybook::StorybookKit"
BlueprintIdentifier = "957A89402226620900CDD25D"
BuildableName = "StorybookKit.framework"
BlueprintName = "StorybookKit"
ReferencedContainer = "container:Storybook.xcodeproj">
Expand All @@ -28,7 +28,7 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Storybook::StorybookUI"
BlueprintIdentifier = "957A894D2226621400CDD25D"
BuildableName = "StorybookUI.framework"
BlueprintName = "StorybookUI"
ReferencedContainer = "container:Storybook.xcodeproj">
Expand Down
2 changes: 1 addition & 1 deletion StorybookKit/Primitives/BookNavigationLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public struct BookNavigationLink: BookView {
public let declarationIdentifier: DeclarationIdentifier

public init(
title: String,
_ file: StaticString = #file,
_ line: UInt = #line,
_ column: UInt = #column,
title: String,
@ComponentBuilder closure: () -> _BookView
) {
self.title = title
Expand Down
10 changes: 10 additions & 0 deletions StorybookKit/Primitives/BookPresent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ public struct BookPresent: BookView {
return .present(self)
}

public let declarationIdentifier: DeclarationIdentifier
public let presentedViewControllerBlock: () -> UIViewController

public let title: String

public init(
_ file: StaticString = #file,
_ line: UInt = #line,
_ column: UInt = #column,
title: String,
presentingViewControllerBlock: @escaping () -> UIViewController
) {
self.title = title
self.presentedViewControllerBlock = presentingViewControllerBlock
self.declarationIdentifier = .init(
file: file.description,
line: line,
column: column,
typeName: _typeName(type(of: self))
)
}

public var body: BookView {
Expand Down
10 changes: 10 additions & 0 deletions StorybookKit/Primitives/BookPush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import UIKit
public struct BookPush: BookView {

public let pushingViewControllerBlock: () -> UIViewController
public let declarationIdentifier: DeclarationIdentifier

public let title: String

Expand All @@ -33,11 +34,20 @@ public struct BookPush: BookView {
}

public init(
_ file: StaticString = #file,
_ line: UInt = #line,
_ column: UInt = #column,
title: String,
pushingViewControllerBlock: @escaping () -> UIViewController
) {
self.title = title
self.pushingViewControllerBlock = pushingViewControllerBlock
self.declarationIdentifier = .init(
file: file.description,
line: line,
column: column,
typeName: _typeName(type(of: self))
)
}

public func asTree() -> BookTree {
Expand Down
10 changes: 10 additions & 0 deletions StorybookUI/Components/PresentCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ import UIKit

final class PresentCell: UIView {

enum Action {
case onSelected
}

public let actionHandler: (Action) -> Void

private let presentButton: UIButton
private let presentedViewControllerBlock: () -> UIViewController

public init(
title: String,
actionHandler: @escaping (Action) -> Void,
presentedViewControllerBlock: @escaping () -> UIViewController
) {

self.presentButton = UIButton(type: .system)
self.presentedViewControllerBlock = presentedViewControllerBlock
self.actionHandler = actionHandler

super.init(frame: .zero)

Expand Down Expand Up @@ -79,6 +87,8 @@ final class PresentCell: UIView {
return
}

actionHandler(.onSelected)

let viewController = presentedViewControllerBlock()

presentingViewController.present(viewController, animated: true, completion: nil)
Expand Down
25 changes: 21 additions & 4 deletions StorybookUI/Components/PushCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import UIKit
import MondrianLayout
import UIKit

final class PushCell: UIView {

enum Action {
case onSelected
}

public let actionHandler: (Action) -> Void

private let pushButton: UIButton
private let pushingViewControllerBlock: () -> UIViewController

init(title: String, pushingViewControllerBlock: @escaping () -> UIViewController) {
init(
title: String,
actionHandler: @escaping (Action) -> Void,
pushingViewControllerBlock: @escaping () -> UIViewController
) {

self.pushButton = UIButton(type: .system)
self.pushingViewControllerBlock = pushingViewControllerBlock
self.actionHandler = actionHandler

super.init(frame: .zero)

Expand All @@ -50,20 +61,26 @@ final class PushCell: UIView {

}

public required init?(coder: NSCoder) {
public required init?(
coder: NSCoder
) {
fatalError("init(coder:) has not been implemented")
}

@objc
private func onTapPushButton() {

let presentingViewControllerCandidate = sequence(first: next, next: { $0?.next }).first { $0 is UIViewController } as? UIViewController
let presentingViewControllerCandidate =
sequence(first: next, next: { $0?.next }).first { $0 is UIViewController }
as? UIViewController

guard let navigationController = presentingViewControllerCandidate?.navigationController else {
assertionFailure()
return
}

actionHandler(.onSelected)

let viewController = pushingViewControllerBlock()

navigationController.pushViewController(viewController, animated: true)
Expand Down
20 changes: 14 additions & 6 deletions StorybookUI/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ extension BookTree {

}

func findLink(by identifier: DeclarationIdentifier) -> BookNavigationLink? {
func findLink(by identifier: DeclarationIdentifier) -> BookView? {

func findLink(_ tree: BookTree) -> BookNavigationLink? {
func findLink(_ tree: BookTree) -> BookView? {
switch tree {
case .folder(let v):
if v.declarationIdentifier == identifier {
Expand All @@ -88,10 +88,18 @@ extension BookTree {
}
}
return nil
case .present:
return nil
case .push:
return nil
case .present(let v):
if v.declarationIdentifier == identifier {
return v
} else {
return nil
}
case .push(let v):
if v.declarationIdentifier == identifier {
return v
} else {
return nil
}
case .spacer:
return nil
}
Expand Down
61 changes: 45 additions & 16 deletions StorybookUI/ViewControllers/ComponentListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import UIKit
import StorybookKit
import UIKit

final class ComponentListViewController: StackScrollViewController {

var onSelectedLink: (BookNavigationLink) -> Void = { _ in }
enum Action {
case onSelected(DeclarationIdentifier)
}

init(component: BookTree, onSelectedLink: @escaping (BookNavigationLink) -> Void) {
init(
component: BookTree,
actionHandler: @escaping (Action) -> Void
) {

super.init()

Expand All @@ -35,17 +40,20 @@ final class ComponentListViewController: StackScrollViewController {
switch component {
case .folder(let v):
buffer.append(
FolderCell(title: v.title, didTap: { [weak self] in
FolderCell(
title: v.title,
didTap: { [weak self] in

onSelectedLink(v)
actionHandler(.onSelected(v.declarationIdentifier))

let nextController = ComponentListViewController(
component: v.component,
onSelectedLink: onSelectedLink
)
nextController.title = v.title
self?.showDetailViewController(nextController, sender: self)
})
let nextController = ComponentListViewController(
component: v.component,
actionHandler: actionHandler
)
nextController.title = v.title
self?.showDetailViewController(nextController, sender: self)
}
)
)
case .single(let v):
if let v = v {
Expand All @@ -60,9 +68,31 @@ final class ComponentListViewController: StackScrollViewController {
makeCells(buffer: &buffer, component: $0)
}
case .push(let push):
buffer.append(PushCell(title: push.title, pushingViewControllerBlock: push.pushingViewControllerBlock))
case .present(let v):
buffer.append(PresentCell(title: v.title, presentedViewControllerBlock: v.presentedViewControllerBlock))
buffer.append(
PushCell(
title: push.title,
actionHandler: { action in
switch action {
case .onSelected:
actionHandler(.onSelected(push.declarationIdentifier))
}
},
pushingViewControllerBlock: push.pushingViewControllerBlock
)
)
case .present(let present):
buffer.append(
PresentCell(
title: present.title,
actionHandler: { action in
switch action {
case .onSelected:
actionHandler(.onSelected(present.declarationIdentifier))
}
},
presentedViewControllerBlock: present.presentedViewControllerBlock
)
)
case .spacer(let spacer):
buffer.append(SpacerCell(height: spacer.height))
}
Expand All @@ -77,4 +107,3 @@ final class ComponentListViewController: StackScrollViewController {
}

}

Loading

0 comments on commit 48cd0bb

Please sign in to comment.