Skip to content

Commit

Permalink
feat: Support functional programming
Browse files Browse the repository at this point in the history
  • Loading branch information
insub4067 committed Oct 15, 2023
1 parent 7fe3b1b commit d109657
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions Sources/SwiftUINavigator/WrapperViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class WrapperViewController<Content: Wrappable>: UIViewController {
init(
content: Content,
indentifier: String,
backgroundColor: UIColor? = nil,
hidesBottomBarWhenPushed: Bool = true
backgroundColor: UIColor? = nil
) {
self.navigator = Navigator()
self.content = content
Expand All @@ -27,7 +26,6 @@ public class WrapperViewController<Content: Wrappable>: UIViewController {

self.identifier = indentifier
self.view.backgroundColor = backgroundColor ?? .systemBackground
self.hidesBottomBarWhenPushed = hidesBottomBarWhenPushed

setHController()
}
Expand Down

0 comments on commit d109657

Please sign in to comment.