-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTabBarVC.swift
409 lines (351 loc) · 15.9 KB
/
TabBarVC.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// TabBarVC.swift
// Commun
//
// Created by Maxim Prigozhenkov on 14/03/2019.
// Copyright © 2019 Commun Limited. All rights reserved.
//
import UIKit
import RxSwift
import CyberSwift
import NotificationView
public let tabBarHeight: CGFloat = 60 + (UIDevice.hasNotch ? UIDevice.safeAreaInsets.bottom : 0.0)
class TabBarVC: UITabBarController {
// MARK: - Nested type
class Tabbar: CMBottomToolbar {
override func commonInit() {
super.commonInit()
stackView.spacing = 0
stackView.distribution = .fillEqually
}
override func pinStackView() {
stackView.autoPinEdgesToSuperviewSafeArea(with: contentInset)
}
}
// MARK: - Constants
let feedTabIndex = 0
let discoveryTabIndex = 1
let notificationTabIndex = 2
let profileTabIndex = 3
let selectedColor: UIColor = .appBlackColor
let unselectedColor: UIColor = .appGrayColor
// MARK: - Properties
let viewModel = TabBarViewModel()
let disposeBag = DisposeBag()
// MARK: - Subviews
private lazy var customTabbar = Tabbar(height: tabBarHeight, cornerRadius: 20)
// Notification
private lazy var notificationsItem = buttonTabBarItem(image: UIImage(named: "notifications")!, tag: notificationTabIndex)
private lazy var notificationRedMark: UIView = {
let notificationRedMark = UIView(width: 10, height: 10, backgroundColor: .appRedColor, cornerRadius: 5)
notificationRedMark.borderColor = .appWhiteColor
notificationRedMark.borderWidth = 1
return notificationRedMark
}()
// MARK: - Methods
override func viewDidLoad() {
super.viewDidLoad()
// Config styles
configStyles()
// Config tabs
configTabs()
// bind view model
bind()
// show post
let post = RequestsManager.shared.pendingRequests.compactMap { request -> ResponseAPIContentGetPost? in
switch request {
case .newComment(let post, _, _):
return post
case .replyToComment(_, let post, _, _):
return post
default:
return nil
}
}.first
if let post = post {
let vc = PostPageVC(post: post)
selectedViewController?.show(vc, sender: self)
}
}
func setTabBarHiden(_ hide: Bool) {
if hide {
customTabbar.isHidden = true
customTabbar.heightConstraint?.constant = 0
} else {
customTabbar.isHidden = false
customTabbar.heightConstraint?.constant = tabBarHeight
}
}
func setNotificationRedMarkHidden(_ hide: Bool) {
if hide {
notificationRedMark.removeFromSuperview()
return
}
if !notificationRedMark.isDescendant(of: notificationsItem) {
notificationsItem.addSubview(notificationRedMark)
notificationRedMark.centerXAnchor.constraint(equalTo: notificationsItem.centerXAnchor, constant: 6).isActive = true
notificationRedMark.autoPinEdge(toSuperviewEdge: .top)
}
}
private func configStyles() {
view.backgroundColor = .appWhiteColor
// hide default tabBar
tabBar.isHidden = true
// shadow
view.addSubview(customTabbar)
customTabbar.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// https://stackoverflow.com/questions/39578530/since-xcode-8-and-ios10-views-are-not-sized-properly-on-viewdidlayoutsubviews
customTabbar.layoutIfNeeded()
}
private func configTabs() {
// Feed Tab
let feed = createViewController(index: feedTabIndex)
let feedNC = SwipeNavigationController(rootViewController: feed, tabBarVC: self)
let feedItem = buttonTabBarItem(image: UIImage(named: "feed")!, tag: feedTabIndex)
feed.accessibilityLabel = "TabBarFeedTabBarItem"
// Comunities Tab
let discoveryVC = createViewController(index: discoveryTabIndex)
let discoveryNC = SwipeNavigationController(rootViewController: discoveryVC, tabBarVC: self)
let discoveryItem = buttonTabBarItem(image: UIImage(named: "tabbar-discovery-icon")!, tag: discoveryTabIndex)
discoveryVC.accessibilityLabel = "TabBarDiscoveryTabBarItem"
// Notifications Tab
let notifications = createViewController(index: notificationTabIndex)
let notificationsNC = SwipeNavigationController(rootViewController: notifications, tabBarVC: self)
notificationsNC.navigationBar.prefersLargeTitles = true
notifications.accessibilityLabel = "TabBarNotificationsTabBarItem"
// Profile Tab
let profile = createViewController(index: profileTabIndex)
let profileNC = SwipeNavigationController(rootViewController: profile, tabBarVC: self)
let profileItem = buttonTabBarItem(image: UIImage(named: "tabbar-profile")!, tag: profileTabIndex)
profileNC.accessibilityLabel = "TabBarProfileTabBarItem"
profileNC.navigationBar.tintColor = UIColor.appMainColor
// Set up controllers
viewControllers = [feedNC, discoveryNC, /* wallet,*/ notificationsNC, profileNC]
customTabbar.stackView.addArrangedSubviews([
feedItem,
discoveryItem,
tabBarItemAdd,
notificationsItem,
profileItem
])
// highlight first
feedItem.tintColor = selectedColor
}
func createViewController(index: Int) -> BaseViewController {
switch index {
case feedTabIndex:
return FeedPageVC()
case discoveryTabIndex:
return DiscoveryVC()
case notificationTabIndex:
return NotificationsPageVC()
case profileTabIndex:
return MyProfilePageVC()
default:
fatalError()
}
}
private func buttonTabBarItem(image: UIImage, tag: Int) -> UIButton {
let button = UIButton(type: .system)
button.setImage(image, for: .normal)
button.tintColor = unselectedColor
button.tag = tag
button.touchAreaEdgeInsets = UIEdgeInsets(inset: -10)
button.addTarget(self, action: #selector(switchTab(button:)), for: .touchUpInside)
return button
}
var tabBarItemAdd: UIButton {
let button = UIButton(type: .system)
let itemSize: CGFloat = 45
let itemPadding: CGFloat = 14
let view = UIView(width: itemSize, height: itemSize, backgroundColor: .appMainColor)
view.cornerRadius = itemSize / 2
let imageView = UIImageView(image: UIImage(named: "add"))
imageView.configureForAutoLayout()
imageView.tintColor = .white
view.addSubview(imageView)
imageView.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: itemPadding, left: itemPadding, bottom: itemPadding, right: itemPadding))
button.addSubview(view)
view.autoAlignAxis(toSuperviewAxis: .vertical)
view.autoAlignAxis(toSuperviewAxis: .horizontal)
view.isUserInteractionEnabled = false
view.addShadow(ofColor: UIColor.onlyLightModeShadowColor(UIColor(red: 106, green: 128, blue: 245)!), radius: 10, offset: CGSize(width: 0, height: 6), opacity: 0.35)
button.tag = viewControllers!.count + 1
button.addTarget(self, action: #selector(buttonAddTapped), for: .touchUpInside)
return button
}
@objc func switchTab(button: UIButton) {
switchTab(index: button.tag)
}
func switchTab(index: Int) {
// Remove notifications red marker
if index == notificationTabIndex {
self.setNotificationRedMarkHidden(true)
}
// pop to first if index is selected
if selectedIndex == index {
if let navController = viewControllers?[index] as? UINavigationController {
if navController.viewControllers.count > 1 {
navController.popViewController(animated: true)
} else {
navController.topViewController?.scrollToTop()
}
}
return
}
// change selected index
selectedIndex = index
// change tabs' color
let items = customTabbar.stackView.arrangedSubviews.filter {$0.tag != (viewControllers?.count ?? 0) + 1}
let selectedItem = items.first {$0.tag == selectedIndex}
let unselectedItems = items.filter {$0.tag != selectedIndex}
selectedItem?.tintColor = selectedColor
for item in unselectedItems {
item.tintColor = unselectedColor
}
// markAllAsViewed
if let nc = selectedViewController as? BaseNavigationController,
let vc = nc.topViewController as? NotificationsPageVC
{
let vm = vc.viewModel as! NotificationsPageViewModel
vm.items
.filter {$0.count > 0}
.take(1)
.asSingle()
.subscribe(onSuccess: { (items) in
guard let timestamp = items.first?.timestamp else {
return
}
vm.markAllAsViewed(timestamp: timestamp)
})
.disposed(by: disposeBag)
}
}
@objc func buttonAddTapped() {
var community: ResponseAPIContentGetCommunity?
if let vc = UIApplication.topViewController() as? CommunityPageVCType,
let comm = vc.community,
comm.isSubscribed == true
{
community = comm
}
let basicEditorScene = BasicEditorVC(community: community)
self.present(basicEditorScene, animated: true, completion: nil)
}
func bind() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.notificationTappedRelay
.skipWhile {$0 == .empty}
.subscribe(onNext: { (item) in
if UIApplication.topViewController() is CreateCommunityVC {return}
self.selectedViewController?.navigateWithNotificationItem(item)
})
.disposed(by: disposeBag)
appDelegate.deepLinkPath
.filter {!$0.isEmpty}
.subscribe(onNext: { (path) in
self.navigateWithDeeplinkPath(path)
})
.disposed(by: disposeBag)
appDelegate.shareExtensionDataRelay
.filter {$0 != nil}
.map {$0!}
.subscribe(onNext: { (data) in
if let presentedVC = self.presentedViewController as? BasicEditorVC {
if !presentedVC.contentTextView.text.isEmpty || presentedVC._viewModel.attachment.value != nil {
presentedVC.showAlert(title: "replace content".localized().uppercaseFirst, message: "you are currently editing a post".localized().uppercaseFirst + ".\n" + "would you like to replace this content".localized().uppercaseFirst, buttonTitles: ["OK".localized(), "Cancel".localized()], highlightedButtonIndex: 1) { (index) in
if index == 0 {
presentedVC.shareExtensionData = data
presentedVC.loadShareExtensionData()
}
}
} else {
presentedVC.shareExtensionData = data
presentedVC.loadShareExtensionData()
}
} else if let presentedVC = self.presentedViewController {
if let activityVC = presentedVC as? UIActivityViewController {
activityVC.dismiss(animated: true, completion: {
let basicEditorScene = BasicEditorVC(shareExtensionData: data, chooseCommunityAfterLoading: true)
self.present(basicEditorScene, animated: true, completion: nil)
})
} else {
presentedVC.showAlert(title: "open editor".localized().uppercaseFirst, message: "close this screen and open editor".localized().uppercaseFirst + "?", buttonTitles: ["OK".localized(), "Cancel".localized()], highlightedButtonIndex: 0) { (index) in
if index == 0 {
presentedVC.dismiss(animated: true) {
let basicEditorScene = BasicEditorVC(shareExtensionData: data)
self.present(basicEditorScene, animated: true, completion: nil)
}
}
}
}
} else {
let basicEditorScene = BasicEditorVC(shareExtensionData: data)
self.present(basicEditorScene, animated: true, completion: nil)
}
DispatchQueue.main.async {
appDelegate.shareExtensionDataRelay.accept(nil)
}
})
.disposed(by: disposeBag)
NotificationsManager.shared
.unseenNotificationsRelay
.subscribe(onNext: { (unseen) in
self.setNotificationRedMarkHidden(unseen == 0)
if let nc = self.selectedViewController as? BaseNavigationController,
let vc = nc.topViewController as? NotificationsPageVC
{
let vm = vc.viewModel as? NotificationsPageViewModel
vm?.markAllAsViewed(timestamp: Date().iso8601String)
}
})
.disposed(by: disposeBag)
// in-app notifications
NotificationsManager.shared.newNotificationsRelay
.filter {$0.count > 0}
.map {$0.first!}
.distinctUntilChanged()
.subscribe(onNext: { (item) in
self.showNotificationViewWithNotification(item)
})
.disposed(by: disposeBag)
}
private func navigateWithDeeplinkPath(_ path: [String]) {
guard path.count == 1 || path.count == 3 else {return}
if path.count == 1 {
if path[0].starts(with: "@") {
// user's profile
let userId = String(path[0].dropFirst())
self.selectedViewController?.showProfileWithUserId(userId)
} else {
// community
let alias = path[0]
if !alias.isEmpty {
self.selectedViewController?.showCommunityWithCommunityAlias(alias)
}
}
} else {
let communityAlias = path[0]
let username = String(path[1].dropFirst())
let permlink = path[2]
let postVC = PostPageVC(username: username, permlink: permlink, communityAlias: communityAlias)
self.selectedViewController?.show(postVC, sender: nil)
}
}
private func showNotificationViewWithNotification(_ notification: ResponseAPIGetNotificationItem) {
let notificationView = NotificationView.default
notificationView.body = notification.content
notificationView.identifier = notification.identity
notificationView.delegate = self
notificationView.show()
}
}
extension TabBarVC: NotificationViewDelegate {
func notificationViewDidTap(_ notificationView: NotificationView) {
guard let notif = NotificationsManager.shared.newNotificationsRelay.value.first(where: {$0.identity == notificationView.identifier}) else {return}
(UIApplication.shared.delegate as! AppDelegate).notificationTappedRelay.accept(notif)
}
}