-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDiscoveryVC.swift
351 lines (300 loc) · 11.7 KB
/
DiscoveryVC.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
//
// DiscoveryVC.swift
// Commun
//
// Created by Chung Tran on 2/13/20.
// Copyright © 2020 Commun Limited. All rights reserved.
//
import Foundation
import RxSwift
class DiscoverySearchBarStackView: UIStackView {
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
}
class DiscoveryVC: BaseViewController, SearchableViewControllerType {
// MARK: - Properties
private weak var currentChildVC: UIViewController?
var tableView: UITableView? {
currentChildVC?.view.subviews.first(where: {$0 is UITableView}) as? UITableView
}
private var searchWasCancelled = false
private var searchBarShouldBeginEditting = true
private var contentViewTopConstraint: NSLayoutConstraint?
// MARK: - ChildVCs
lazy var searchController = UISearchController.default()
lazy var suggestionsVC = DiscoverySuggestionsVC(showAllHandler: {
let originalText = self.searchController.searchBar.text ?? ""
self.searchController.isActive = false
self.searchBar.changeTextNotified(text: originalText)
DispatchQueue.main.async {
self.setTopBarHidden(false, animated: true)
if self.topTabBar.selectedIndex.value != 0 {
self.topTabBar.selectedIndex.accept(0)
} else {
self.showChildVCWithIndex(0)
}
}
}) {
self.searchController.isActive = false
self.cancelSearch()
}
lazy var discoveryAllVC = DiscoveryAllVC { index in
self.topTabBar.selectedIndex.accept(index)
}
lazy var communitiesVC = DiscoveryCommunitiesVC(prefetch: self.searchController.searchBar.text?.isEmpty == true)
lazy var usersVC = DiscoveryUsersVC(prefetch: self.searchController.searchBar.text?.isEmpty == true)
lazy var postsVC = DiscoveryPostsVC(prefetch: self.searchController.searchBar.text?.isEmpty == true)
// MARK: - Subviews
lazy var topBarContainerView: UIView = {
let view = UIView(backgroundColor: .appWhiteColor)
view.addSubview(topTabBar)
topTabBar.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: 5, left: 0, bottom: 10, right: 0))
return view
}()
lazy var topTabBar = CMTopTabBar(
height: 35,
labels: [
"all".localized().uppercaseFirst,
"communities".localized().uppercaseFirst,
"users".localized().uppercaseFirst,
"posts".localized().uppercaseFirst
],
selectedIndex: 0,
contentInset: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
)
lazy var contentView = UIView(forAutoLayout: ())
var searchBar: UISearchBar {
get {searchController.searchBar}
set {}
}
lazy var searchBarContainerView: DiscoverySearchBarStackView = {
let stackView = DiscoverySearchBarStackView(axis: .horizontal, spacing: 8, alignment: .fill, distribution: .fill)
searchBar.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubviews([searchBar, plusButton])
return stackView
}()
lazy var plusButton: UIButton = {
let button = UIButton.circle(size: 35, backgroundColor: .clear, imageName: "add-community")
button.addTarget(self, action: #selector(plusButtonDidTouch), for: .touchUpInside)
return button
}()
// MARK: - Methods
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
extendedLayoutIncludesOpaqueBars = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
searchController.roundCorners()
navigationController?.navigationBar.shadowOpacity = 0
// avoid tabbar
tableView?.contentInset.bottom = 10 + tabBarHeight
}
// MARK: - Setup
override func setUp() {
super.setUp()
// modify view
view.backgroundColor = .appLightGrayColor
// search controller
layoutSearchBar()
// contentView
view.addSubview(contentView)
contentView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
setTopBarHidden(false)
searchBarContainerView.layoutIfNeeded()
plusButton.isHidden = true
}
func layoutSearchBar() {
self.definesPresentationContext = true
self.navigationItem.titleView = searchBarContainerView
}
private func setTopBarHidden(_ hidden: Bool, animated: Bool = false) {
if hidden {
if topBarContainerView.isDescendant(of: view) {
topBarContainerView.removeFromSuperview()
contentViewTopConstraint?.isActive = false
contentViewTopConstraint = contentView.autoPinEdge(toSuperviewSafeArea: .top)
}
} else {
if !topBarContainerView.isDescendant(of: view) {
// top tabBar
view.addSubview(topBarContainerView)
topBarContainerView.autoPinEdgesToSuperviewSafeArea(with: .zero, excludingEdge: .bottom)
topTabBar.scrollView.contentOffset.x = -10
contentViewTopConstraint?.isActive = false
contentViewTopConstraint = contentView.autoPinEdge(.top, to: .bottom, of: topBarContainerView)
}
}
UIView.animate(withDuration: animated ? 0.3: 0) {
self.view.layoutIfNeeded()
}
}
// MARK: - Binding
override func bind() {
super.bind()
// search controller
bindSearchBar()
searchController.searchBar.rx.textDidBeginEditing
.subscribe(onNext: { (_) in
self.searchWasCancelled = false
if self.currentChildVC != self.suggestionsVC {
self.activeSearch()
}
self.plusButton.isHidden = true
self.searchBarContainerView.layoutIfNeeded()
})
.disposed(by: disposeBag)
searchController.searchBar.rx.cancelButtonClicked
.subscribe(onNext: { (_) in
self.searchWasCancelled = true
self.plusButton.isHidden = self.topTabBar.selectedIndex.value != 1
self.searchBarContainerView.layoutIfNeeded()
})
.disposed(by: disposeBag)
searchController.searchBar.rx.textDidEndEditing
.subscribe(onNext: { (_) in
if self.searchWasCancelled {
self.cancelSearch()
}
self.plusButton.isHidden = self.topTabBar.selectedIndex.value != 1
self.searchBarContainerView.layoutIfNeeded()
})
.disposed(by: disposeBag)
// topTabBar
topTabBar.selectedIndex
.distinctUntilChanged()
.subscribe(onNext: { (index) in
self.showChildVCWithIndex(index)
UIView.animate(withDuration: 0.3) {
self.searchBarContainerView.layoutIfNeeded()
self.plusButton.isHidden = index != 1
self.searchBarContainerView.layoutIfNeeded()
}
})
.disposed(by: disposeBag)
// forward searchBar Delegate
searchController.searchBar.rx.setDelegate(self)
.disposed(by: disposeBag)
}
private func cancelSearch() {
self.setTopBarHidden(false, animated: true)
self.showChildVCWithIndex(self.topTabBar.selectedIndex.value)
}
private func activeSearch() {
self.setTopBarHidden(true, animated: true)
self.showChildVC(self.suggestionsVC)
}
// MARK: - ChildVC manager
private func showChildVCWithIndex(_ index: Int) {
let vc: UIViewController
// select vc
switch index {
case 0:
// All
vc = discoveryAllVC
case 1:
// Community
vc = communitiesVC
case 2:
// Users
vc = usersVC
case 3:
// Posts
vc = postsVC
default:
return
}
// show as child
showChildVC(vc)
}
private func showChildVC(_ newVC: UIViewController) {
// search
self.search(self.searchController.searchBar.text)
// get oldVC
guard let oldVC = currentChildVC else {
addChild(newVC)
contentView.addSubview(newVC.view)
newVC.view.autoPinEdgesToSuperviewEdges()
newVC.didMove(toParent: self)
// assign current childVC
self.currentChildVC = newVC
return
}
// move oldVC out
oldVC.willMove(toParent: nil)
// add newVC
addChild(newVC)
contentView.insertSubview(newVC.view, belowSubview: oldVC.view)
newVC.view.autoPinEdgesToSuperviewEdges()
// Transtion
UIView.transition(from: oldVC.view, to: newVC.view, duration: 0.3, options: [.transitionCrossDissolve]) { (_) in
oldVC.view.removeFromSuperview()
oldVC.removeFromParent()
newVC.didMove(toParent: self)
// assign current childVC
self.currentChildVC = newVC
}
}
// MARK: - Search manager
private func search(_ keyword: String?) {
tableView?.scrollToTop()
DispatchQueue.main.async {
if let keyword = keyword, !keyword.isEmpty {
self.searchBarIsSearchingWithQuery(keyword)
} else {
self.searchBarDidCancelSearching()
}
}
}
func searchBarIsSearchingWithQuery(_ query: String) {
// if self.searchController.searchBar.isFirstResponder {
self.suggestionsVC.searchBarIsSearchingWithQuery(query)
// } else {
self.discoveryAllVC.searchBarIsSearchingWithQuery(query)
self.communitiesVC.searchBarIsSearchingWithQuery(query)
self.usersVC.searchBarIsSearchingWithQuery(query)
self.postsVC.searchBarIsSearchingWithQuery(query)
// }
// switch self.topTabBar.selectedIndex.value {
// case 0:
// self.discoveryAllVC.search(keyword)
// case 1:
// self.communitiesVC.search(keyword)
// case 2:
// self.usersVC.search(keyword)
// case 3:
// self.postsVC.search(keyword)
// default:
// return
// }
}
func searchBarDidCancelSearching() {
// if self.searchController.searchBar.isFirstResponder {
self.suggestionsVC.searchBarDidCancelSearching()
// } else {
self.discoveryAllVC.searchBarDidCancelSearching()
self.communitiesVC.searchBarDidCancelSearching()
self.usersVC.searchBarDidCancelSearching()
self.postsVC.searchBarDidCancelSearching()
// }
}
@objc func plusButtonDidTouch() {
present(CreateCommunityGettingStartedVC(), animated: true, completion: nil)
}
}
extension DiscoveryVC: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if !searchBar.isFirstResponder {
searchBarShouldBeginEditting = false
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
let boolToReturn = searchBarShouldBeginEditting
searchBarShouldBeginEditting = true
return boolToReturn
}
}