-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add CustomHostingController - Use @_spi attributes for CustomView/Window loading
- Loading branch information
1 parent
177a60c
commit 8750c34
Showing
6 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
Sources/CocoaExtensions/Custom/Controllers/CustomHostingController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
#if canImport(SwiftUI) | ||
import DeclarativeConfiguration | ||
import CocoaAliases | ||
import SwiftUI | ||
|
||
extension CocoaHostingController where Content: ExpressibleByNilLiteral { | ||
public convenience init() { | ||
self.init(rootView: nil) | ||
} | ||
} | ||
|
||
#if canImport(UIKit) && !os(watchOS) | ||
|
||
extension CustomHostingController: NavigationControllerDynamicOverridable {} | ||
|
||
open class CustomHostingController<Content: View>: | ||
CocoaHostingController<Content>, | ||
CustomCocoaViewControllerProtocol | ||
{ | ||
private(set) open var isVisible = false | ||
|
||
@OptionalDataSource<Void, UINavigationController?> | ||
public var overrideNavigationController | ||
|
||
override open var navigationController: UINavigationController? { | ||
_overrideNavigationController() ?? super.navigationController | ||
} | ||
|
||
@Handler<Void> | ||
public var onDismiss | ||
|
||
@Handler<Void> | ||
public var onViewDidLoad | ||
|
||
@Handler<Void> | ||
public var onViewWillAppear | ||
|
||
@Handler<Void> | ||
public var onViewDidAppear | ||
|
||
@Handler<Void> | ||
public var onViewWillDisappear | ||
|
||
@Handler<Void> | ||
public var onViewDidDisappear | ||
|
||
@Handler<Void> | ||
public var onViewWillLayout | ||
|
||
@Handler<Void> | ||
public var onViewDidLayout | ||
|
||
open override func viewDidLoad() { | ||
super.viewDidLoad() | ||
_onViewDidLoad() | ||
} | ||
|
||
open override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
_onViewWillAppear() | ||
} | ||
|
||
open override func viewDidAppear(_ animated: Bool) { | ||
isVisible = true | ||
super.viewDidAppear(animated) | ||
_onViewDidAppear() | ||
} | ||
|
||
open override func viewWillDisappear(_ animated: Bool) { | ||
super.viewWillDisappear(animated) | ||
_onViewWillDisappear() | ||
} | ||
|
||
open override func viewDidDisappear(_ animated: Bool) { | ||
super.viewDidDisappear(animated) | ||
_onViewDidDisappear() | ||
isVisible = false | ||
} | ||
|
||
open override func viewWillLayoutSubviews() { | ||
super.viewWillLayoutSubviews() | ||
_onViewWillLayout() | ||
} | ||
|
||
open override func viewDidLayoutSubviews() { | ||
super.viewDidLayoutSubviews() | ||
_onViewDidLayout() | ||
} | ||
|
||
open override func dismiss(animated: Bool, completion: (() -> Void)? = nil) { | ||
super.dismiss(animated: animated, completion: completion) | ||
_onDismiss() | ||
} | ||
|
||
/// Use `override _init` instead of overriding this initializer | ||
public override init(rootView: Content) { | ||
super.init(rootView: rootView) | ||
self._init() | ||
} | ||
|
||
/// Use `override _init` instead of overriding this initializer | ||
public override init?(coder: NSCoder, rootView: Content) { | ||
super.init(coder: coder, rootView: rootView) | ||
self._init() | ||
} | ||
|
||
/// Use `override _init` instead of overriding this initializer | ||
public required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
self._init() | ||
} | ||
|
||
/// Only for `override` purposes, do not call directly | ||
open func _init() {} | ||
} | ||
|
||
#elseif canImport(AppKit) | ||
import DeclarativeConfiguration | ||
import CocoaAliases | ||
import SwiftUI | ||
|
||
open class CustomHostingController<Content: View>: | ||
CocoaHostingController<Content>, | ||
CustomCocoaViewControllerProtocol | ||
{ | ||
private(set) open var isVisible = false | ||
|
||
@Handler<Void> | ||
public var onDismiss | ||
|
||
@Handler<Void> | ||
public var onViewDidLoad | ||
|
||
@Handler<Void> | ||
public var onViewWillAppear | ||
|
||
@Handler<Void> | ||
public var onViewDidAppear | ||
|
||
@Handler<Void> | ||
public var onViewWillDisappear | ||
|
||
@Handler<Void> | ||
public var onViewDidDisappear | ||
|
||
@Handler<Void> | ||
public var onViewWillLayout | ||
|
||
@Handler<Void> | ||
public var onViewDidLayout | ||
|
||
open override func viewDidLoad() { | ||
super.viewDidLoad() | ||
_onViewDidLoad() | ||
} | ||
|
||
open override func viewWillAppear() { | ||
super.viewWillAppear() | ||
_onViewWillAppear() | ||
} | ||
|
||
open override func viewDidAppear() { | ||
isVisible = true | ||
super.viewDidAppear() | ||
_onViewDidAppear() | ||
} | ||
|
||
open override func viewWillDisappear() { | ||
super.viewWillDisappear() | ||
_onViewWillDisappear() | ||
} | ||
|
||
open override func viewDidDisappear() { | ||
super.viewDidDisappear() | ||
_onViewDidDisappear() | ||
isVisible = false | ||
} | ||
|
||
open override func viewWillLayout() { | ||
super.viewWillLayout() | ||
_onViewWillLayout() | ||
} | ||
|
||
open override func viewDidLayout() { | ||
super.viewDidLayout() | ||
_onViewDidLayout() | ||
} | ||
|
||
open override func dismiss(_ sender: Any?) { | ||
super.dismiss(sender) | ||
self._onDismiss() | ||
} | ||
|
||
/// Use `override _init` instead of overriding this initializer | ||
public override init(rootView: Content) { | ||
super.init(rootView: rootView) | ||
self._init() | ||
} | ||
|
||
/// Use `override _init` instead of overriding this initializer | ||
public override init?(coder: NSCoder, rootView: Content) { | ||
super.init(coder: coder, rootView: rootView) | ||
self._init() | ||
} | ||
|
||
/// Use `override _init` instead of overriding this initializer | ||
public required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
self._init() | ||
} | ||
|
||
/// Only for `override` purposes, do not call directly | ||
open func _init() {} | ||
} | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters