11//
2- // NavigationableViewModel.swift
2+ // File.swift
3+ //
34//
4- //
5- // Created by 김용우 on 2023/10/17.
5+ // Created by 김용우 on 2023/10/19.
66//
77
88import SwiftUI
99
10- open class NavigationableViewModel : ObservableObject {
11- public weak var viewController : UIViewController ?
10+ public extension ControllerableView {
1211
13- public init ( ) { }
14- }
15-
16- // MARK: - Navigation
17- @MainActor
18- extension NavigationableViewModel {
12+ var viewController : UIViewController {
13+ let viewController = HostingController ( rootView: self )
14+ self . stateView. viewController = viewController
15+ return viewController
16+ }
17+
18+ // MARK: ViewController Life Cycle
19+ func loadView( ) { }
20+ func viewDidLoad( ) { }
21+ func viewWillAppear( ) { }
22+ func viewDidAppear( ) { }
23+ func viewWillDisappear( ) { }
24+ func viewDidDisappear( ) { }
1925
20- public func push( view: some ControllerableView ) {
21- viewController? . navigationController? . pushViewController ( view. viewController, animated: true )
26+ // MARK: Navigation
27+ func push( view: some ControllerableView ) {
28+ stateView. viewController? . navigationController? . pushViewController ( view. viewController, animated: true )
2229 }
2330
24- public func pop( ) {
25- guard let currentViewController = viewController,
26- let navigationController = currentViewController. navigationController else { return }
27- guard navigationController. topViewController == currentViewController else { return }
28-
29- navigationController. popViewController ( animated: true )
31+ func pop( ) {
32+ stateView. viewController? . navigationController? . popViewController ( animated: true )
3033 }
3134
32- public func present(
35+ func present(
3336 view: some ControllerableView ,
3437 to presentationStyle: UIModalPresentationStyle ? = nil ,
3538 by transitionStyle: UIModalTransitionStyle ? = nil ,
@@ -50,22 +53,17 @@ extension NavigationableViewModel {
5053 let presentingViewController = UINavigationController ( rootViewController: nextViewController)
5154 presentingViewController. setNavigationBarHidden ( true , animated: false )
5255
53- viewController? . present ( presentingViewController, animated: true )
56+ stateView . viewController? . present ( presentingViewController, animated: true )
5457 }
5558
56- public func dismiss( ) {
57- viewController? . dismiss ( animated: true )
59+ func dismiss( ) {
60+ stateView . viewController? . dismiss ( animated: true )
5861 }
5962
60- public func popToRoot( ) {
61- viewController? . navigationController? . popToRootViewController ( animated: true )
63+ func popToRoot( ) {
64+ stateView . viewController? . navigationController? . popToRootViewController ( animated: true )
6265 }
6366
64- }
65-
66- @MainActor
67- extension NavigationableViewModel {
68-
6967 private var window : UIWindow ? {
7068 if #available( iOS 16 . 0 , * ) {
7169 return UIApplication . shared. connectedScenes
@@ -77,7 +75,7 @@ extension NavigationableViewModel {
7775 }
7876 }
7977
80- public func alert (
78+ func presentAlert (
8179 view: some ControllerableView ,
8280 to presentationStyle: UIModalPresentationStyle ? = . overFullScreen,
8381 by transitionStyle: UIModalTransitionStyle ? = . crossDissolve
@@ -101,5 +99,4 @@ extension NavigationableViewModel {
10199
102100 rootViewController. present ( nextViewController, animated: true )
103101 }
104-
105102}
0 commit comments