From d10965767ae1856651509cd87fe0f092b217d135 Mon Sep 17 00:00:00 2001 From: insub Date: Sun, 15 Oct 2023 13:17:53 +0900 Subject: [PATCH] feat: Support functional programming --- README.md | 11 +++++++- .../UIViewController+.swift | 13 --------- .../UIViewController+functional.swift | 28 +++++++++++++++++++ .../UIViewController+indentifier.swift | 26 +++++++++++++++++ .../WrapperViewController.swift | 4 +-- 5 files changed, 65 insertions(+), 17 deletions(-) rename Sources/SwiftUINavigator/{ => UIViewController+}/UIViewController+.swift (60%) create mode 100644 Sources/SwiftUINavigator/UIViewController+/UIViewController+functional.swift create mode 100644 Sources/SwiftUINavigator/UIViewController+/UIViewController+indentifier.swift diff --git a/README.md b/README.md index da28bc2..7333565 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,16 @@ #### 🤔 Why do you need SwiftUINavigator when you have a UIHostingController? > With UIHostingController, I faced bugs that were hard to predict and could not be resolved. That's why I made SwiftUINavigator to solve those problems. -## ✔️ Example +## ✔️ Simple Example +```swift +SwiftUIView() // return View + .asViewController() // return WrapperViewController + .title("SwiftUIView") // return UIViewController + .backgroundColor(.gray) // return UIViewController + .hidesBottomBarWhenPushed(true) // return UIViewController +``` + +## ✔️ Project Example ### UIKit side ```swift diff --git a/Sources/SwiftUINavigator/UIViewController+.swift b/Sources/SwiftUINavigator/UIViewController+/UIViewController+.swift similarity index 60% rename from Sources/SwiftUINavigator/UIViewController+.swift rename to Sources/SwiftUINavigator/UIViewController+/UIViewController+.swift index 2f86fef..1659144 100644 --- a/Sources/SwiftUINavigator/UIViewController+.swift +++ b/Sources/SwiftUINavigator/UIViewController+/UIViewController+.swift @@ -9,20 +9,7 @@ import UIKit public extension UIViewController { - - private struct AssociatedKeys { - static var identifier = "Identifier" - } - var identifier: String? { - get { - objc_getAssociatedObject(self, &AssociatedKeys.identifier) as? String - } - set { - objc_setAssociatedObject(self, &AssociatedKeys.identifier, newValue, .OBJC_ASSOCIATION_RETAIN) - } - } - func addChildAndSubView(_ controller: UIViewController) { self.addChild(controller) controller.view.frame = self.view.frame diff --git a/Sources/SwiftUINavigator/UIViewController+/UIViewController+functional.swift b/Sources/SwiftUINavigator/UIViewController+/UIViewController+functional.swift new file mode 100644 index 0000000..bc053dd --- /dev/null +++ b/Sources/SwiftUINavigator/UIViewController+/UIViewController+functional.swift @@ -0,0 +1,28 @@ +// +// UIViewController+functional.swift +// +// +// Created by 김인섭 on 10/15/23. +// + +#if canImport(UIKit) +import UIKit + +public extension UIViewController { + + func title(_ title: String?) -> Self { + self.title = title + return self + } + + func hidesBottomBarWhenPushed(_ state: Bool) -> Self { + self.hidesBottomBarWhenPushed = state + return self + } + + func backgroundColor(_ color: UIColor?) -> Self { + self.view.backgroundColor = color + return self + } +} +#endif diff --git a/Sources/SwiftUINavigator/UIViewController+/UIViewController+indentifier.swift b/Sources/SwiftUINavigator/UIViewController+/UIViewController+indentifier.swift new file mode 100644 index 0000000..8f88911 --- /dev/null +++ b/Sources/SwiftUINavigator/UIViewController+/UIViewController+indentifier.swift @@ -0,0 +1,26 @@ +// +// UIViewController+indentifier.swift +// +// +// Created by 김인섭 on 10/15/23. +// + +#if canImport(UIKit) +import UIKit + +public extension UIViewController { + + private struct AssociatedKeys { + static var identifier = "Identifier" + } + + var identifier: String? { + get { + objc_getAssociatedObject(self, &AssociatedKeys.identifier) as? String + } + set { + objc_setAssociatedObject(self, &AssociatedKeys.identifier, newValue, .OBJC_ASSOCIATION_RETAIN) + } + } +} +#endif diff --git a/Sources/SwiftUINavigator/WrapperViewController.swift b/Sources/SwiftUINavigator/WrapperViewController.swift index d6aa86c..e379f81 100644 --- a/Sources/SwiftUINavigator/WrapperViewController.swift +++ b/Sources/SwiftUINavigator/WrapperViewController.swift @@ -17,8 +17,7 @@ public class WrapperViewController: UIViewController { init( content: Content, indentifier: String, - backgroundColor: UIColor? = nil, - hidesBottomBarWhenPushed: Bool = true + backgroundColor: UIColor? = nil ) { self.navigator = Navigator() self.content = content @@ -27,7 +26,6 @@ public class WrapperViewController: UIViewController { self.identifier = indentifier self.view.backgroundColor = backgroundColor ?? .systemBackground - self.hidesBottomBarWhenPushed = hidesBottomBarWhenPushed setHController() }