Skip to content

Commit af0adae

Browse files
committed
onDisappear가 호출될 수 있도록 함
1 parent 7fc41c6 commit af0adae

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

Sources/WindowOverlay/Internals/WindowBridgingView.swift

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ internal struct WindowBridgingView<V: View>: UIViewRepresentable {
66
var content: V
77

88
func makeUIView(context: Context) -> _HelperView {
9-
let hostingController = UIHostingController(rootView: EnvPassingView(
10-
content: content,
11-
environment: context.environment
12-
))
13-
hostingController.view.backgroundColor = .clear
14-
return _HelperView(hostingController: hostingController)
9+
return _HelperView(
10+
isPresented: isPresented,
11+
content: EnvPassingView(
12+
content: content,
13+
environment: context.environment
14+
)
15+
)
1516
}
1617
func updateUIView(_ helper: _HelperView, context: Context) {
17-
helper.hostingController.rootView = EnvPassingView(
18-
content: content,
19-
environment: context.environment
18+
helper.setContent(
19+
isPresented: isPresented,
20+
content: EnvPassingView(
21+
content: content,
22+
environment: context.environment
23+
)
2024
)
21-
helper.isPresented = isPresented
2225
}
2326

2427
fileprivate struct EnvPassingView: View {
@@ -29,28 +32,53 @@ internal struct WindowBridgingView<V: View>: UIViewRepresentable {
2932
content.environment(\.self, environment)
3033
}
3134
}
32-
35+
3336
internal final class _HelperView: UIView {
34-
var isPresented = false {
35-
didSet { overlayWindow?.isHidden = !isPresented }
36-
}
37+
private var isPresented: Bool
38+
private var content: EnvPassingView
39+
3740
private var overlayWindow: WindowOverlayWindow?
38-
fileprivate let hostingController: UIHostingController<EnvPassingView>
41+
private var hostingController: UIHostingController<EnvPassingView>? {
42+
overlayWindow?.rootViewController as? UIHostingController<EnvPassingView>
43+
}
3944

40-
fileprivate init(hostingController: UIHostingController<EnvPassingView>) {
41-
self.hostingController = hostingController
45+
fileprivate init(isPresented: Bool, content: EnvPassingView) {
46+
self.isPresented = isPresented
47+
self.content = content
4248
super.init(frame: .zero)
4349
}
50+
4451
required init?(coder: NSCoder) { fatalError() }
4552

4653
override func willMove(toWindow newWindow: UIWindow?) {
4754
super.willMove(toWindow: newWindow)
4855
if let windowScene = newWindow?.windowScene {
49-
overlayWindow = WindowOverlayWindow(
50-
windowScene: windowScene,
51-
rootViewController: hostingController
52-
)
53-
overlayWindow?.isHidden = !isPresented
56+
overlayWindow = WindowOverlayWindow(windowScene: windowScene)
57+
updateView()
58+
}
59+
}
60+
61+
fileprivate func setContent(
62+
isPresented: Bool,
63+
content: EnvPassingView
64+
) {
65+
self.isPresented = isPresented
66+
self.content = content
67+
updateView()
68+
}
69+
70+
private func updateView() {
71+
if isPresented {
72+
if hostingController == nil {
73+
overlayWindow?.rootViewController = UIHostingController(rootView: content)
74+
overlayWindow?.rootViewController?.view.backgroundColor = .clear
75+
} else {
76+
hostingController?.rootView = content
77+
}
78+
overlayWindow?.isHidden = false
79+
} else {
80+
overlayWindow?.rootViewController = nil
81+
overlayWindow?.isHidden = true
5482
}
5583
}
5684
}

Sources/WindowOverlay/Internals/WindowOverlayWindow.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import UIKit
22

33
internal final class WindowOverlayWindow: UIWindow {
4-
init(windowScene: UIWindowScene, rootViewController: UIViewController) {
4+
override init(windowScene: UIWindowScene) {
55
super.init(windowScene: windowScene)
66
self.windowLevel = .alert
7-
self.rootViewController = rootViewController
87
}
98
required init?(coder: NSCoder) { fatalError() }
109

0 commit comments

Comments
 (0)