@@ -29,14 +29,12 @@ import UIKit
2929import PubNubChat
3030import PubNubChatComponents
3131
32- class RootViewController : UIViewController {
33-
32+ final class RootViewController : UIViewController {
3433 private let provider : PubNubChatProvider
3534 private let channelId : String
36-
3735 private var portraitConstraints : [ NSLayoutConstraint ] = [ ]
3836 private var landscapeConstraints : [ NSLayoutConstraint ] = [ ]
39- private weak var scene : UIWindowScene ?
37+ private var scene : UIWindowScene
4038
4139 init (
4240 channelId: String ,
@@ -57,38 +55,55 @@ class RootViewController: UIViewController {
5755 override func viewDidLoad( ) {
5856 super. viewDidLoad ( )
5957
60- guard let scene = scene else {
61- return
62- }
63- // Creates the default MessageList component
6458 guard let messageListViewModel = try ? provider. messageListComponentViewModel ( pubnubChannelId: channelId) else {
65- preconditionFailure ( " Could not create intial view models " )
59+ preconditionFailure ( " Missing required data. Make sure the channel object and current user are stored locally " )
6660 }
67-
68- // Sets the custom title view for the MessageList component
61+
6962 messageListViewModel. customNavigationTitleString = nil
70- messageListViewModel. customNavigationTitleView = { [ weak self] viewModel in
71- self ? . getMessageListCustomTitleView ( ) ?? UIView ( )
63+ messageListViewModel. customNavigationTitleView = { viewModel in
64+ let customTitleView = UILabel ( )
65+ customTitleView. font = UIFont ( name: " Poppins-Bold " , size: 14 )
66+ customTitleView. textColor = UIColor ( named: " MessageList.NavigationBar.TitleTextColor " )
67+ customTitleView. text = " STREAM CHAT "
68+ customTitleView. sizeToFit ( )
69+ return customTitleView
7270 }
7371
74- // Creates MessageList and LiveStream view controllers
7572 let messageListViewController = messageListViewModel. configuredComponentView ( )
7673 let messageListContainerViewController = UINavigationController ( rootViewController: messageListViewController)
7774 let streamViewController = LiveStreamViewController ( )
78-
79- // Combine the content from MessageList and LiveStream view controllers using a Container View Controller
75+
76+ createContainerViewController (
77+ messageListContainerViewController: messageListContainerViewController,
78+ streamViewController: streamViewController
79+ )
80+ applyConstraints (
81+ streamViewController: streamViewController,
82+ messageListContainerViewController: messageListContainerViewController
83+ )
84+ setUpMetricsView ( )
85+ }
86+
87+ private func createContainerViewController(
88+ messageListContainerViewController: UINavigationController ,
89+ streamViewController: UIViewController
90+ ) {
8091 self . addChild ( messageListContainerViewController)
8192 self . addChild ( streamViewController)
8293 self . view. addSubview ( messageListContainerViewController. view)
8394 self . view. addSubview ( streamViewController. view)
8495
8596 messageListContainerViewController. didMove ( toParent: self )
8697 streamViewController. didMove ( toParent: self )
87-
98+ }
99+
100+ private func applyConstraints(
101+ streamViewController: UIViewController ,
102+ messageListContainerViewController: UIViewController
103+ ) {
88104 messageListContainerViewController. view. translatesAutoresizingMaskIntoConstraints = false
89105 streamViewController. view. translatesAutoresizingMaskIntoConstraints = false
90-
91- // Creates constraints for Portrait and Landscape orientations
106+
92107 portraitConstraints = [
93108 messageListContainerViewController. view. leadingAnchor. constraint ( equalTo: view. leadingAnchor) ,
94109 messageListContainerViewController. view. trailingAnchor. constraint ( equalTo: view. trailingAnchor) ,
@@ -99,21 +114,24 @@ class RootViewController: UIViewController {
99114 streamViewController. view. topAnchor. constraint ( equalTo: view. topAnchor) ,
100115 streamViewController. view. bottomAnchor. constraint ( equalTo: messageListContainerViewController. view. topAnchor)
101116 ]
102-
103117 landscapeConstraints = [
104118 streamViewController. view. leadingAnchor. constraint ( equalTo: view. leadingAnchor) ,
105119 streamViewController. view. trailingAnchor. constraint ( equalTo: messageListContainerViewController. view. leadingAnchor) ,
106120 streamViewController. view. topAnchor. constraint ( equalTo: view. topAnchor) ,
107121 streamViewController. view. bottomAnchor. constraint ( equalTo: view. bottomAnchor) ,
108-
109122 messageListContainerViewController. view. leadingAnchor. constraint ( equalTo: streamViewController. view. trailingAnchor) ,
110123 messageListContainerViewController. view. trailingAnchor. constraint ( equalTo: view. trailingAnchor) ,
111124 messageListContainerViewController. view. widthAnchor. constraint ( equalTo: view. widthAnchor, multiplier: 7.0 / 16.0 ) ,
112125 messageListContainerViewController. view. bottomAnchor. constraint ( equalTo: view. bottomAnchor) ,
113126 messageListContainerViewController. view. topAnchor. constraint ( equalTo: view. topAnchor)
114127 ]
115128
116- // Creates a debug overlay to display the FPS meter
129+ NSLayoutConstraint . activate (
130+ scene. interfaceOrientation. isPortrait ? portraitConstraints : landscapeConstraints
131+ )
132+ }
133+
134+ private func setUpMetricsView( ) {
117135 let metricsView = MetricsView ( )
118136 view. addSubview ( metricsView)
119137 view. bringSubviewToFront ( metricsView)
@@ -122,22 +140,6 @@ class RootViewController: UIViewController {
122140 $0. topAnchor. constraint ( equalTo: $1. topAnchor, constant: 40 ) ,
123141 $0. trailingAnchor. constraint ( equalTo: $1. trailingAnchor, constant: - 20 )
124142 ] }
125-
126- // Activates a set of constraints depending on the orientation
127- NSLayoutConstraint . activate (
128- scene. interfaceOrientation. isPortrait ? portraitConstraints : landscapeConstraints
129- )
130- }
131-
132- private func getMessageListCustomTitleView( ) -> UIView {
133-
134- let customTitleView = UILabel ( )
135- customTitleView. font = UIFont ( name: " Poppins-Bold " , size: 14 )
136- customTitleView. textColor = UIColor ( named: " MessageList.NavigationBar.TitleTextColor " )
137- customTitleView. text = " STREAM CHAT "
138- customTitleView. sizeToFit ( )
139-
140- return customTitleView
141143 }
142144
143145 override func willTransition(
@@ -148,12 +150,9 @@ class RootViewController: UIViewController {
148150 to: newCollection,
149151 with: coordinator
150152 )
151- guard let scene = scene else {
152- return
153- }
154153 coordinator. animate { context in
155- NSLayoutConstraint . deactivate ( scene. interfaceOrientation. isPortrait ? self . landscapeConstraints : self . portraitConstraints)
156- NSLayoutConstraint . activate ( scene. interfaceOrientation. isPortrait ? self . portraitConstraints : self . landscapeConstraints)
154+ NSLayoutConstraint . deactivate ( self . scene. interfaceOrientation. isPortrait ? self . landscapeConstraints : self . portraitConstraints)
155+ NSLayoutConstraint . activate ( self . scene. interfaceOrientation. isPortrait ? self . portraitConstraints : self . landscapeConstraints)
157156 }
158157 }
159158}
0 commit comments