|
1 | 1 | import Gridicons |
2 | 2 | import WordPressUI |
3 | 3 |
|
| 4 | +private enum Constants { |
| 5 | + static let helpButtonTitleColor = UIColor(white: 1.0, alpha: 0.4) |
| 6 | + static let helpButtonInsets = UIEdgeInsets(top: 0.0, left: 5.0, bottom: 0.0, right: 5.0) |
| 7 | + //Button Item: Custom view wrapping the Help UIbutton |
| 8 | + static let helpButtonItemMarginSpace = CGFloat(-8) |
| 9 | + static let helpButtonItemMinimumSize = CGSize(width: 44.0, height: 44.0) |
| 10 | + |
| 11 | + static let zendeskBadgeSize = CGSize(width: 10, height: 10) |
| 12 | + static let zendeskBadgeCenterOffset = CGPoint(x: 5, y: 12) |
| 13 | + static let helpshiftBadgeSize = CGSize(width: 12, height: 12) |
| 14 | +} |
4 | 15 |
|
5 | 16 | /// base protocol for NUX view controllers |
6 | 17 | public protocol NUXViewControllerBase { |
@@ -102,7 +113,7 @@ extension NUXViewControllerBase where Self: UIViewController, Self: UIViewContro |
102 | 113 | func refreshSupportBadge() { |
103 | 114 | let count = WordPressAuthenticator.shared.delegate?.supportBadgeCount ?? 0 |
104 | 115 | helpBadge.text = "\(count)" |
105 | | - helpBadge.isHidden = false //(count == 0) |
| 116 | + helpBadge.isHidden = (count == 0) |
106 | 117 | } |
107 | 118 |
|
108 | 119 | func refreshSupportNotificationIndicator() { |
@@ -162,79 +173,91 @@ extension NUXViewControllerBase where Self: UIViewController, Self: UIViewContro |
162 | 173 | /// Adds the Help Button to the nav controller |
163 | 174 | /// |
164 | 175 | public func addHelpButtonToNavController() { |
165 | | - let helpButtonMarginSpacerWidth = CGFloat(-8) |
166 | | - |
167 | | - var helpNotificationSize: CGSize |
168 | | - var notificationView: UIView |
169 | | - var notificationViewCenterXConstraint: NSLayoutConstraint |
170 | | - var notificationViewCenterYConstraint: NSLayoutConstraint |
171 | | - |
172 | | - if WordPressAuthenticator.shared.configuration.supportNotificationIndicatorFeatureFlag == true { |
173 | | - // Zendesk |
174 | | - NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationReceived, object: nil, queue: nil) { [weak self] _ in |
175 | | - self?.refreshSupportNotificationIndicator() |
176 | | - } |
177 | | - |
178 | | - NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationCleared, object: nil, queue: nil) { [weak self] _ in |
179 | | - self?.refreshSupportNotificationIndicator() |
180 | | - } |
181 | | - |
182 | | - notificationView = helpIndicator |
183 | | - helpNotificationSize = CGSize(width: 10, height: 10) |
184 | | - let notificationCenterOffset = CGPoint(x: 5, y: 12) |
185 | | - |
186 | | - notificationViewCenterXConstraint = notificationView.centerXAnchor.constraint(equalTo: helpButton.trailingAnchor, |
187 | | - constant: helpButton.contentEdgeInsets.top + notificationCenterOffset.x) |
188 | | - |
189 | | - notificationViewCenterYConstraint = notificationView.centerYAnchor.constraint(equalTo: helpButton.topAnchor, |
190 | | - constant: helpButton.contentEdgeInsets.top + notificationCenterOffset.y) |
191 | | - } else { |
192 | | - // Helpshift |
193 | | - NotificationCenter.default.addObserver(forName: .wordpressSupportBadgeUpdated, object: nil, queue: nil) { [weak self] _ in |
194 | | - self?.refreshSupportBadge() |
195 | | - } |
196 | | - |
197 | | - notificationView = helpBadge |
198 | | - helpNotificationSize = CGSize(width: 12, height: 12) |
199 | | - notificationViewCenterXConstraint = notificationView.centerXAnchor.constraint(equalTo: helpButton.trailingAnchor) |
200 | | - notificationViewCenterYConstraint = notificationView.centerYAnchor.constraint(equalTo: helpButton.topAnchor) |
201 | | - } |
| 176 | + let barButtonView = createBarButtonView() |
| 177 | + addHelpButton(to: barButtonView) |
| 178 | + addBadgeView(to: barButtonView) |
| 179 | + addLeftBarButtonItem(with: barButtonView) |
| 180 | + } |
202 | 181 |
|
| 182 | + // MARK: - helpers |
| 183 | + |
| 184 | + private func addLeftBarButtonItem(with customView: UIView) { |
| 185 | + let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) |
| 186 | + spacer.width = Constants.helpButtonItemMarginSpace |
| 187 | + |
| 188 | + let barButton = UIBarButtonItem(customView: customView) |
| 189 | + navigationItem.rightBarButtonItems = [spacer, barButton] |
| 190 | + } |
| 191 | + |
| 192 | + private func createBarButtonView() -> UIView { |
203 | 193 | let customView = UIView(frame: .zero) |
204 | 194 | customView.translatesAutoresizingMaskIntoConstraints = false |
205 | | - customView.heightAnchor.constraint(equalToConstant: 44.0).isActive = true |
206 | | - customView.widthAnchor.constraint(greaterThanOrEqualToConstant: 44.0).isActive = true |
| 195 | + customView.heightAnchor.constraint(equalToConstant: Constants.helpButtonItemMinimumSize.height).isActive = true |
| 196 | + customView.widthAnchor.constraint(greaterThanOrEqualToConstant: Constants.helpButtonItemMinimumSize.width).isActive = true |
| 197 | + |
| 198 | + return customView |
| 199 | + } |
207 | 200 |
|
| 201 | + private func addHelpButton(to wrapperView: UIView) { |
208 | 202 | helpButton.setTitle(NSLocalizedString("Help", comment: "Help button"), for: .normal) |
209 | | - helpButton.setTitleColor(UIColor(white: 1.0, alpha: 0.4), for: .highlighted) |
| 203 | + helpButton.setTitleColor(Constants.helpButtonTitleColor, for: .highlighted) |
210 | 204 | helpButton.on(.touchUpInside) { [weak self] control in |
211 | | - guard let strongSelf = self else { |
212 | | - return |
213 | | - } |
214 | | - strongSelf.handleHelpButtonTapped(strongSelf.helpButton) |
| 205 | + self?.handleHelpButtonTapped(control) |
215 | 206 | } |
216 | 207 |
|
217 | | - customView.addSubview(helpButton) |
| 208 | + wrapperView.addSubview(helpButton) |
218 | 209 | helpButton.translatesAutoresizingMaskIntoConstraints = false |
219 | | - helpButton.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: 5.0).isActive = true |
220 | | - helpButton.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -5.0).isActive = true |
221 | | - helpButton.topAnchor.constraint(equalTo: customView.topAnchor).isActive = true |
222 | | - helpButton.bottomAnchor.constraint(equalTo: customView.bottomAnchor).isActive = true |
223 | 210 |
|
224 | | - notificationView.translatesAutoresizingMaskIntoConstraints = false |
225 | | - notificationView.isHidden = true |
226 | | - customView.addSubview(notificationView) |
227 | | - notificationViewCenterXConstraint.isActive = true |
228 | | - notificationViewCenterYConstraint.isActive = true |
229 | | - notificationView.widthAnchor.constraint(equalToConstant: helpNotificationSize.width).isActive = true |
230 | | - notificationView.heightAnchor.constraint(equalToConstant: helpNotificationSize.height).isActive = true |
| 211 | + helpButton.leadingAnchor.constraint(equalTo: wrapperView.leadingAnchor, constant: Constants.helpButtonInsets.left).isActive = true |
| 212 | + helpButton.trailingAnchor.constraint(equalTo: wrapperView.trailingAnchor, constant: -Constants.helpButtonInsets.right).isActive = true |
| 213 | + helpButton.topAnchor.constraint(equalTo: wrapperView.topAnchor).isActive = true |
| 214 | + helpButton.bottomAnchor.constraint(equalTo: wrapperView.bottomAnchor).isActive = true |
| 215 | + } |
231 | 216 |
|
| 217 | + // MARK: Badge settings |
232 | 218 |
|
233 | | - let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) |
234 | | - spacer.width = helpButtonMarginSpacerWidth |
| 219 | + private func addBadgeView(to superView: UIView) { |
| 220 | + if WordPressAuthenticator.shared.configuration.supportNotificationIndicatorFeatureFlag == true { |
| 221 | + setupZendeskBadge() |
| 222 | + layoutBadgeView(helpIndicator, to: superView, size: Constants.zendeskBadgeSize) |
| 223 | + } else { |
| 224 | + setupHelpshiftBadge() |
| 225 | + layoutBadgeView(helpBadge, to: superView, size: Constants.helpshiftBadgeSize) |
| 226 | + } |
| 227 | + } |
235 | 228 |
|
236 | | - let barButton = UIBarButtonItem(customView: customView) |
237 | | - navigationItem.rightBarButtonItems = [spacer, barButton] |
| 229 | + private func setupHelpshiftBadge() { |
| 230 | + helpBadge.isHidden = true |
| 231 | + NotificationCenter.default.addObserver(forName: .wordpressSupportBadgeUpdated, object: nil, queue: nil) { [weak self] _ in |
| 232 | + self?.refreshSupportBadge() |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private func setupZendeskBadge() { |
| 237 | + helpIndicator.isHidden = true |
| 238 | + |
| 239 | + NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationReceived, object: nil, queue: nil) { [weak self] _ in |
| 240 | + self?.refreshSupportNotificationIndicator() |
| 241 | + } |
| 242 | + NotificationCenter.default.addObserver(forName: .wordpressSupportNotificationCleared, object: nil, queue: nil) { [weak self] _ in |
| 243 | + self?.refreshSupportNotificationIndicator() |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private func layoutBadgeView(_ view: UIView, to superView: UIView, size: CGSize) { |
| 248 | + superView.addSubview(view) |
| 249 | + |
| 250 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 251 | + |
| 252 | + let xConstant = helpButton.contentEdgeInsets.top + Constants.zendeskBadgeCenterOffset.x |
| 253 | + let yConstant = helpButton.contentEdgeInsets.top + Constants.zendeskBadgeCenterOffset.y |
| 254 | + |
| 255 | + NSLayoutConstraint.activate([ |
| 256 | + view.centerXAnchor.constraint(equalTo: helpButton.trailingAnchor, constant: xConstant), |
| 257 | + view.centerYAnchor.constraint(equalTo: helpButton.topAnchor, constant: yConstant), |
| 258 | + view.widthAnchor.constraint(equalToConstant: size.width), |
| 259 | + view.heightAnchor.constraint(equalToConstant: size.height) |
| 260 | + ]) |
238 | 261 | } |
239 | 262 |
|
240 | 263 |
|
|
0 commit comments