@@ -5,15 +5,39 @@ import SwiftUI
55/// Hosting Controller for the `AnalyticsHubView` view.
66///
77final class AnalyticsHubHostingViewController : UIHostingController < AnalyticsHubView > {
8- init ( siteID: Int64 , timeRange: StatsTimeRangeV4 ) {
8+
9+ /// Presents an error notice in the tab bar context after this `self` is dismissed.
10+ ///
11+ private let systemNoticePresenter : NoticePresenter
12+
13+ /// Defines a notice that should be presented after `self` is dismissed.
14+ /// Defaults to `nil`.
15+ ///
16+ var notice : Notice ?
17+
18+ init ( siteID: Int64 , timeRange: StatsTimeRangeV4 , systemNoticePresenter: NoticePresenter = ServiceLocator . noticePresenter) {
919 let viewModel = AnalyticsHubViewModel ( siteID: siteID, statsTimeRange: timeRange)
20+ self . systemNoticePresenter = systemNoticePresenter
1021 super. init ( rootView: AnalyticsHubView ( viewModel: viewModel) )
22+
23+ // Needed to pop the hosting controller from within the SwiftUI view
24+ rootView. dismissWithNotice = { [ weak self] notice in
25+ self ? . notice = notice
26+ self ? . navigationController? . popViewController ( animated: true )
27+ }
1128 }
1229
1330 @available ( * , unavailable)
1431 required dynamic init ? ( coder aDecoder: NSCoder ) {
1532 fatalError ( " init(coder:) has not been implemented " )
1633 }
34+
35+ override func viewWillDisappear( _ animated: Bool ) {
36+ super. viewWillDisappear ( animated)
37+
38+ // Show any notice that should be presented after the underlying disappears.
39+ enqueuePendingNotice ( notice, using: systemNoticePresenter)
40+ }
1741}
1842
1943/// Main Analytics Hub View
@@ -23,6 +47,11 @@ struct AnalyticsHubView: View {
2347 /// Environment safe areas
2448 @Environment ( \. safeAreaInsets) var safeAreaInsets : EdgeInsets
2549
50+ /// Set this closure with UIKit code to pop the view controller and display the provided notice.
51+ /// Needed because we need access to the UIHostingController `popViewController` method.
52+ ///
53+ var dismissWithNotice : ( ( Notice ) -> Void ) = { _ in }
54+
2655 @StateObject var viewModel : AnalyticsHubViewModel
2756
2857 var body : some View {
@@ -81,6 +110,10 @@ struct AnalyticsHubView: View {
81110 . task {
82111 await viewModel. updateData ( )
83112 }
113+ . onReceive ( viewModel. $dismissNotice) { notice in
114+ guard let notice else { return }
115+ dismissWithNotice ( notice)
116+ }
84117 }
85118}
86119
0 commit comments