From 1db276121f564446ab353fe99e784726ee5b9ae5 Mon Sep 17 00:00:00 2001 From: ra1028 Date: Fri, 2 Feb 2024 14:34:07 +0900 Subject: [PATCH] Remove ScenarioContent protocol --- Playbook.xcodeproj/project.pbxproj | 4 - Sources/Playbook/Scenario.swift | 76 ++++++++++++++++++- Sources/Playbook/ScenarioContent.swift | 37 --------- Sources/Playbook/ScenarioViewController.swift | 2 +- 4 files changed, 74 insertions(+), 45 deletions(-) delete mode 100644 Sources/Playbook/ScenarioContent.swift diff --git a/Playbook.xcodeproj/project.pbxproj b/Playbook.xcodeproj/project.pbxproj index 214df9b..579b5b3 100644 --- a/Playbook.xcodeproj/project.pbxproj +++ b/Playbook.xcodeproj/project.pbxproj @@ -65,7 +65,6 @@ C0F32DC787F41414E09C37FB /* HorizontalSeparator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5576F4CB67593B259E12C387 /* HorizontalSeparator.swift */; }; C51E0AB09C31FA61FE7D90BF /* ExtraScenarios.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D23A73BBBCC7E6D4027CDB /* ExtraScenarios.swift */; }; C6A0A71A7D1DB2BECD5B4A2D /* ScenarioSearchTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 068FE06929659477F04AA176 /* ScenarioSearchTree.swift */; }; - CB15497812BC08565D686486 /* ScenarioContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4FB169EFF0C0FF72C2E69C4 /* ScenarioContent.swift */; }; CF8244A4AB05E5ED99B68742 /* SnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B59B5A8B33031C6AF8A049E /* SnapshotTests.swift */; }; D016FBABE74D6996F5648DDC /* SnapshotDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C5F051089936C546DFBB95 /* SnapshotDevice.swift */; }; D45CD95A63FC59D24E4F5E65 /* GalleryStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38F7F9F34307C4DEEDF210F9 /* GalleryStore.swift */; }; @@ -186,7 +185,6 @@ A8FCF24068A7FDD9BAD27ECA /* ScenarioDisplaySheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScenarioDisplaySheet.swift; sourceTree = ""; }; ABBE0D7EADE227862CB3EB1F /* TableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableView.swift; sourceTree = ""; }; AEB78746E7DE13CB4BBD42FC /* Mocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mocks.swift; sourceTree = ""; }; - B4FB169EFF0C0FF72C2E69C4 /* ScenarioContent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScenarioContent.swift; sourceTree = ""; }; B6C5F051089936C546DFBB95 /* SnapshotDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotDevice.swift; sourceTree = ""; }; B6CEC4F17D84235922932498 /* ScenarioStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScenarioStore.swift; sourceTree = ""; }; C65E03F37BA759FC6E55D1C2 /* Drawer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Drawer.swift; sourceTree = ""; }; @@ -276,7 +274,6 @@ children = ( 385C5E36DF37E9A2AF379859 /* Playbook.swift */, 6E50413A08A9CF285D97A65C /* Scenario.swift */, - B4FB169EFF0C0FF72C2E69C4 /* ScenarioContent.swift */, D63CD111722A3B8067BFE548 /* ScenarioContext.swift */, F830987472F2FBD2C3BA7284 /* ScenarioKind.swift */, FF835DB024B80E2FADFFD0FC /* ScenarioLayout.swift */, @@ -556,7 +553,6 @@ 99904F2A1ECCD16DC130B51A /* OrderedStorage.swift in Sources */, FD3C77D1442C7CAADC579CB2 /* Playbook.swift in Sources */, BF44BEAFAA1183BE4B9952AF /* Scenario.swift in Sources */, - CB15497812BC08565D686486 /* ScenarioContent.swift in Sources */, 341406194DDF8C7A82AB96D0 /* ScenarioContext.swift in Sources */, BE7BF04B28E47309D2D7608D /* ScenarioKind.swift in Sources */, DDF763A23C1AE9464827CCCE /* ScenarioLayout.swift in Sources */, diff --git a/Sources/Playbook/Scenario.swift b/Sources/Playbook/Scenario.swift index ef12332..380f765 100644 --- a/Sources/Playbook/Scenario.swift +++ b/Sources/Playbook/Scenario.swift @@ -15,7 +15,7 @@ public struct Scenario { public var line: UInt /// A closure that make a new content with passed context. - public var content: (ScenarioContext) -> ScenarioContent + public var content: (ScenarioContext) -> UIViewController /// Creates a new scenario. /// @@ -30,7 +30,7 @@ public struct Scenario { layout: ScenarioLayout, file: StaticString = #file, line: UInt = #line, - content: @escaping (ScenarioContext) -> ScenarioContent + content: @escaping (ScenarioContext) -> UIViewController ) { self.name = name self.layout = layout @@ -39,6 +39,32 @@ public struct Scenario { self.content = content } + /// Creates a new scenario. + /// + /// - Parameters: + /// - name: A unique name of this scenario. + /// - layout: Represents how the component should be laid out. + /// - file: A file path where defined this scenario. + /// - line: A line number where defined this scenario in file. + /// - content: A closure that make a new content with passed context. + public init( + _ name: ScenarioName, + layout: ScenarioLayout, + file: StaticString = #file, + line: UInt = #line, + content: @escaping (ScenarioContext) -> UIView + ) { + self.init( + name, + layout: layout, + file: file, + line: line, + content: { context in + UIViewHostingController(view: content(context)) + } + ) + } + /// Creates a new scenario. /// /// - Parameters: @@ -52,7 +78,7 @@ public struct Scenario { layout: ScenarioLayout, file: StaticString = #file, line: UInt = #line, - content: @escaping () -> ScenarioContent + content: @escaping () -> UIViewController ) { self.init( name, @@ -62,4 +88,48 @@ public struct Scenario { content: { _ in content() } ) } + + /// Creates a new scenario. + /// + /// - Parameters: + /// - name: A unique name of this scenario. + /// - layout: Represents how the component should be laid out. + /// - file: A file path where defined this scenario. + /// - line: A line number where defined this scenario in file. + /// - content: A closure that make a new content. + public init( + _ name: ScenarioName, + layout: ScenarioLayout, + file: StaticString = #file, + line: UInt = #line, + content: @escaping () -> UIView + ) { + self.init( + name, + layout: layout, + file: file, + line: line, + content: { _ in + UIViewHostingController(view: content()) + } + ) + } +} + +private final class UIViewHostingController: UIViewController { + private let _view: UIView + + init(view: UIView) { + self._view = view + super.init(nibName: nil, bundle: nil) + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func loadView() { + view = _view + } } diff --git a/Sources/Playbook/ScenarioContent.swift b/Sources/Playbook/ScenarioContent.swift deleted file mode 100644 index 1c68008..0000000 --- a/Sources/Playbook/ScenarioContent.swift +++ /dev/null @@ -1,37 +0,0 @@ -import UIKit - -/// Represents a content of scenario. -public protocol ScenarioContent { - /// Makes a new `UIViewController` instance thats wraps `self`. - func makeUIViewController() -> UIViewController -} - -extension UIView: ScenarioContent { - /// Makes a new `UIViewController` instance thats wraps `self`. - public func makeUIViewController() -> UIViewController { - UIViewHostingController(view: self) - } -} - -extension UIViewController: ScenarioContent { - /// Makes a new `UIViewController` instance thats wraps `self`. - public func makeUIViewController() -> UIViewController { self } -} - -private final class UIViewHostingController: UIViewController { - private let _view: UIView - - init(view: UIView) { - self._view = view - super.init(nibName: nil, bundle: nil) - } - - @available(*, unavailable) - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func loadView() { - view = _view - } -} diff --git a/Sources/Playbook/ScenarioViewController.swift b/Sources/Playbook/ScenarioViewController.swift index 08e820c..691174e 100644 --- a/Sources/Playbook/ScenarioViewController.swift +++ b/Sources/Playbook/ScenarioViewController.swift @@ -81,7 +81,7 @@ private extension ScenarioViewController { return } - let contentViewController = scenario.content(context).makeUIViewController() + let contentViewController = scenario.content(context) self.contentViewController = contentViewController contentViewController.willMove(toParent: self)