From 1db276121f564446ab353fe99e784726ee5b9ae5 Mon Sep 17 00:00:00 2001 From: ra1028 Date: Fri, 2 Feb 2024 14:34:07 +0900 Subject: [PATCH 1/2] 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) From d832d2454c0a47f207b8b730d80ca2079e27158f Mon Sep 17 00:00:00 2001 From: ra1028 Date: Fri, 2 Feb 2024 14:35:35 +0900 Subject: [PATCH 2/2] Fix tests --- Playbook.xcodeproj/project.pbxproj | 4 ---- Tests/ScenarioContentTests.swift | 18 ------------------ 2 files changed, 22 deletions(-) delete mode 100644 Tests/ScenarioContentTests.swift diff --git a/Playbook.xcodeproj/project.pbxproj b/Playbook.xcodeproj/project.pbxproj index 579b5b3..d50ea98 100644 --- a/Playbook.xcodeproj/project.pbxproj +++ b/Playbook.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ 04DAC4EBEE89D365F72AD5DB /* SnapshotSupport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 023425CAEE4AA111BE174F66 /* SnapshotSupport.swift */; }; 04E6DA15062197311CB80A23 /* ScenarioProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68E1EB13B5ED8DD48245B1E8 /* ScenarioProvider.swift */; }; - 06CC7A9EF2AEA3FEEC75B0B3 /* ScenarioContentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F291632484870BA6A8BA1609 /* ScenarioContentTests.swift */; }; 0D0659EBE8EF0971AF40405D /* Scheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2C24C0BB3864D3AF550A05D /* Scheduler.swift */; }; 0E1A77DB134CE41112BE7EFB /* Counter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BDCEF4FDC1DE7D50B6E73D /* Counter.swift */; }; 0FEB2071F01AF72A62527F63 /* Highlight.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA6FEAB12579B7940EA763BB /* Highlight.swift */; }; @@ -200,7 +199,6 @@ EA762F5B447EF9828E1F86F2 /* PlaybookCatalog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaybookCatalog.swift; sourceTree = ""; }; EB7DE56F774FFD4DB73EBD37 /* CatalogBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CatalogBarItem.swift; sourceTree = ""; }; EC23506D3CC07FABB1992787 /* Playbook-Tests.xctest */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.cfbundle; path = "Playbook-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; - F291632484870BA6A8BA1609 /* ScenarioContentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScenarioContentTests.swift; sourceTree = ""; }; F49057856849F915AB692364 /* ScenarioSearchStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScenarioSearchStore.swift; sourceTree = ""; }; F6B0431D91C939D181493ECC /* PlaybookGallery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaybookGallery.swift; sourceTree = ""; }; F830987472F2FBD2C3BA7284 /* ScenarioKind.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScenarioKind.swift; sourceTree = ""; }; @@ -392,7 +390,6 @@ 9BAD3FE8D577886981218938 /* GalleryScenarios.swift */, AEB78746E7DE13CB4BBD42FC /* Mocks.swift */, 9370D44977281AAEBF6DEF44 /* PlaybookTests.swift */, - F291632484870BA6A8BA1609 /* ScenarioContentTests.swift */, 81538A32C0A57BA96700E55E /* ScenarioStoreTests.swift */, CEB51C85B7D63CEF5DC83B60 /* ScenarioSwiftUITests.swift */, F913250C8A5AB7148459FEC6 /* ScenarioViewControllerTests.swift */, @@ -538,7 +535,6 @@ 334B859A3103A83A4EC996BF /* GalleryScenarios.swift in Sources */, F9DF0B63F62E3A7B5ED2E619 /* Mocks.swift in Sources */, E7597D8F364FF8D2CD4DF584 /* PlaybookTests.swift in Sources */, - 06CC7A9EF2AEA3FEEC75B0B3 /* ScenarioContentTests.swift in Sources */, B945D03AD556F3F8B3161147 /* ScenarioStoreTests.swift in Sources */, 72DDF1BB8E77EF11CB9BB4FC /* ScenarioSwiftUITests.swift in Sources */, 6CF7056823EA1730746AB145 /* ScenarioViewControllerTests.swift in Sources */, diff --git a/Tests/ScenarioContentTests.swift b/Tests/ScenarioContentTests.swift deleted file mode 100644 index 1e57bdf..0000000 --- a/Tests/ScenarioContentTests.swift +++ /dev/null @@ -1,18 +0,0 @@ -import Playbook -import XCTest - -final class ScenarioContentTests: XCTestCase { - func testUIView() { - let view = UIView() - let controller = view.makeUIViewController() - - XCTAssertEqual(view, controller.view) - } - - func testUIViewController() { - let controller0 = UIViewController() - let controller1 = controller0.makeUIViewController() - - XCTAssertEqual(controller0, controller1) - } -}