-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommunityPageVC.swift
583 lines (490 loc) · 23.1 KB
/
CommunityPageVC.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//
// CommunityVC.swift
// Commun
//
// Created by Chung Tran on 10/23/19.
// Copyright © 2019 Commun Limited. All rights reserved.
//
import Foundation
import RxSwift
import RxDataSources
import CyberSwift
class CommunityPageVC: ProfileVC<ResponseAPIContentGetCommunity>, LeaderCellDelegate, PostCellDelegate, CommunityPageVCType, HasLeadersVM {
// MARK: - Nested type
enum CustomElementType: IdentifiableType, Equatable {
case post(ResponseAPIContentGetPost)
case leader(ResponseAPIContentGetLeader)
case about(String)
case rule(ResponseAPIContentGetCommunityRule)
var identity: String {
switch self {
case .post(let post):
return post.identity
case .leader(let leader):
return leader.identity
case .about(let string):
return string
case .rule(let rule):
return rule.identity
}
}
}
// MARK: - Properties
var communityId: String?
var communityAlias: String?
var dataSource: MyRxTableViewSectionedAnimatedDataSource<AnimatableSectionModel<String, CustomElementType>>!
var community: ResponseAPIContentGetCommunity? {
return viewModel.profile.value
}
var price: Double?
override func createViewModel() -> ProfileViewModel<ResponseAPIContentGetCommunity> {
if let alias = communityAlias {
return CommunityPageViewModel(communityAlias: alias, authorizationRequired: authorizationRequired)
}
return CommunityPageViewModel(communityId: communityId, authorizationRequired: authorizationRequired)
}
var leadersVM: LeadersViewModel {(viewModel as! CommunityPageViewModel).leadsVM}
var posts: [ResponseAPIContentGetPost] {(viewModel as! CommunityPageViewModel).postsVM.items.value}
// MARK: - Subviews
lazy var headerView = createHeaderView()
func createHeaderView() -> CommunityHeaderView {
CommunityHeaderView(tableView: tableView)
}
override var _headerView: ProfileHeaderView! {
return headerView
}
lazy var postSortingView: UIView = {
let feedTypeLabel = UILabel(forAutoLayout: ())
feedTypeLabel.tag = 1
let view = UIView(forAutoLayout: ())
view.addSubview(feedTypeLabel)
feedTypeLabel.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(inset: 16), excludingEdge: .trailing)
let arrow = UIImageView(width: 10, height: 6, imageNamed: "drop-down")
view.addSubview(arrow)
arrow.autoPinEdge(.leading, to: .trailing, of: feedTypeLabel, withOffset: 6)
arrow.autoAlignAxis(toSuperviewAxis: .horizontal)
arrow.autoPinEdge(toSuperviewEdge: .trailing, withInset: 16)
view.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(openFilterVC))
view.addGestureRecognizer(tap)
let containerView = UIView(frame: .zero)
containerView.backgroundColor = .appWhiteColor
containerView.addSubview(view)
view.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: 0, left: 0, bottom: 2, right: 0), excludingEdge: .trailing)
let separator = UIView(height: 2, backgroundColor: .appLightGrayColor)
containerView.addSubview(separator)
separator.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
return containerView
}()
lazy var manageCommunityBarButton: UIButton = {
let button = UIButton.settings(tintColor: .white)
button.addTarget(self, action: #selector(showCommunityControlPanel), for: .touchUpInside)
return button
}()
lazy var tagsCollectionView: TagsCollectionView = {
let collectionView = TagsCollectionView(height: 32)
collectionView.delegate = self
return collectionView
}()
// MARK: - Initializers
init(communityId: String) {
self.communityId = communityId
super.init(nibName: nil, bundle: nil)
commonInit()
}
init(communityAlias: String) {
self.communityAlias = communityAlias
super.init(nibName: nil, bundle: nil)
commonInit()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func commonInit() {
leadersVM.fetchNext()
}
// MARK: - Methods
override func setUp() {
super.setUp()
view.insertSubview(tagsCollectionView, belowSubview: customNavigationBar)
tagsCollectionView.autoPinEdge(toSuperviewEdge: .trailing)
tagsCollectionView.autoPinEdge(toSuperviewEdge: .leading)
tagsCollectionView.autoPinEdge(.bottom, to: .top, of: headerView, withOffset: -10)
}
override func setUpTableView() -> UITableView {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.configureForAutoLayout()
tableView.backgroundColor = .clear
tableView.insetsContentViewsToSafeArea = false
tableView.contentInsetAdjustmentBehavior = .never
tableView.showsVerticalScrollIndicator = false
return tableView
}
override func bind() {
super.bind()
bindSelectedIndex()
bindCommunityManager()
// forward delegate
tableView.rx.setDelegate(self)
.disposed(by: disposeBag)
// topic
viewModel.profile
.map {$0?.getTopics() ?? []}
.bind(to: tagsCollectionView.rx.items(cellIdentifier: "TagCell", cellType: TagsCollectionView.TagCell.self)) { _, topic, cell in
cell.label.text = topic
}
.disposed(by: disposeBag)
}
override func setUp(profile: ResponseAPIContentGetCommunity) {
super.setUp(profile: profile)
if profile.isLeader == true {
rightBarButtonsStackView.insertArrangedSubview(manageCommunityBarButton, at: 0)
}
// Register new cell type
tableView.register(CommunityLeaderCell.self, forCellReuseIdentifier: "CommunityLeaderCell")
tableView.register(CommunityAboutCell.self, forCellReuseIdentifier: "CommunityAboutCell")
tableView.register(CommunityRuleCell.self, forCellReuseIdentifier: "CommunityRuleCell")
// title
title = profile.name
// cover
if let coverURL = profile.coverUrl {
coverImageView.setImageDetectGif(with: coverURL)
let imageViewTemp = UIImageView(frame: CGRect(origin: CGPoint(x: 0.0, y: -70.0), size: CGSize(width: UIScreen.main.bounds.width, height: 70.0)))
imageViewTemp.backgroundColor = .clear
imageViewTemp.addTapToViewer(with: coverURL)
imageViewTemp.highlightedImage = coverImageView.image
tableView.addSubview(imageViewTemp)
}
// header
headerView.setUp(with: profile)
headerView.walletView.nextButton.isUserInteractionEnabled = true
headerView.walletView.nextButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(getPointsButtonTapped)))
(viewModel as! CommunityPageViewModel).walletGetBuyPriceRequest
.subscribe(onSuccess: { (buyPrice) in
self.price = buyPrice.priceValue
self.headerView.setUp(walletPrice: buyPrice)
}, onError: { (error) in
self.showError(error)
})
.disposed(by: disposeBag)
// community manager
if community?.isLeader == true {
headerView.manageCommunityButtonsView.proposalsButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(proposalsButtonDidTouch)))
headerView.manageCommunityButtonsView.reportsButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reportsButtonDidTouch)))
headerView.manageCommunityButtonsView.manageCommunityButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(manageCommunityDidTouch)))
}
// become a leader
headerView.becomeALeaderButton.addTarget(self, action: #selector(becomeALeaderButtonDidTouch), for: .touchUpInside)
}
override func handleListLoading() {
switch (viewModel as! CommunityPageViewModel).segmentedItem.value {
case .posts:
tableView.addPostLoadingFooterView()
case .leads:
tableView.addNotificationsLoadingFooterView()
default:
break
}
}
override func handleListEnded() {
tableView.tableFooterView = UIView()
}
override func handleListEmpty() {
var title = "empty"
var description = "not found"
switch (viewModel as! CommunityPageViewModel).segmentedItem.value {
case .posts:
title = "no posts"
description = "posts not found"
case .leads:
title = "no leaders"
description = "leaders not found"
case .rules:
title = "no rules"
description = "rules not found"
case .about:
title = "no description"
description = "description not found"
}
tableView.addEmptyPlaceholderFooterView(title: title.localized().uppercaseFirst, description: description.localized().uppercaseFirst)
}
override func bindProfile() {
super.bindProfile()
bindProfileBlocked()
ResponseAPIContentGetCommunity.observeItemChanged()
.filter {$0.identity == self.viewModel.profile.value?.identity}
.subscribe(onNext: {newCommunity in
let community = self.viewModel.profile.value?.newUpdatedItem(from: newCommunity)
self.viewModel.profile.accept(community)
})
.disposed(by: disposeBag)
}
override func bindItems() {
dataSource = MyRxTableViewSectionedAnimatedDataSource<AnimatableSectionModel<String, CustomElementType>>(
configureCell: { (_, tableView, indexPath, element) -> UITableViewCell in
switch element {
case .post(let post):
switch post.document?.attributes?.type {
case "article":
let cell = self.tableView.dequeueReusableCell(withIdentifier: "ArticlePostCell") as! ArticlePostCell
cell.setUp(with: post)
cell.delegate = self
return cell
case "basic":
let cell = self.tableView.dequeueReusableCell(withIdentifier: "BasicPostCell") as! BasicPostCell
cell.setUp(with: post)
cell.delegate = self
return cell
default:
break
}
case .leader(let leader):
let cell = self.tableView.dequeueReusableCell(withIdentifier: "CommunityLeaderCell") as! CommunityLeaderCell
cell.setUp(with: leader)
cell.delegate = self
return cell
case .about(let string):
let cell = self.tableView.dequeueReusableCell(withIdentifier: "CommunityAboutCell") as! CommunityAboutCell
cell.label.text = string
return cell
case .rule(let rule):
let cell = self.tableView.dequeueReusableCell(withIdentifier: "CommunityRuleCell") as! CommunityRuleCell
cell.rowIndex = indexPath.row
cell.setUp(with: rule)
return cell
}
return UITableViewCell()
}
)
dataSource.animationConfiguration = AnimationConfiguration(reloadAnimation: .none)
viewModel.items
.map { items in
items.compactMap {item -> CustomElementType? in
if let item = item as? ResponseAPIContentGetPost {
return .post(item)
}
if let item = item as? ResponseAPIContentGetLeader {
return .leader(item)
}
if let item = item as? String {
return .about(item)
}
if let item = item as? ResponseAPIContentGetCommunityRule {
return .rule(item)
}
return nil
}
}
.map {items -> [AnimatableSectionModel<String, CustomElementType>] in
if (self.viewModel as! CommunityPageViewModel).segmentedItem.value == .leads {
var leaders = [CustomElementType]()
var nominees = [CustomElementType]()
for item in items {
switch item {
case .leader(let leader):
if leader.inTop {
leaders.append(.leader(leader))
} else {
nominees.append(.leader(leader))
}
default:
break
}
}
var sections = [AnimatableSectionModel<String, CustomElementType>]()
if !leaders.isEmpty {sections.append(AnimatableSectionModel<String, CustomElementType>(model: "leaders", items: leaders))}
if !nominees.isEmpty {sections.append(AnimatableSectionModel<String, CustomElementType>(model: "nominees", items: nominees))}
return sections
}
return [AnimatableSectionModel<String, CustomElementType>(model: "", items: items)]
}
.do(onNext: { (items) in
if items.count == 0 {
self.handleListEmpty()
}
})
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
override func cellSelected(_ indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
switch cell {
case is PostCell:
let post = (viewModel as! CommunityPageViewModel).postsVM.items.value[indexPath.row]
let postPageVC = PostPageVC(post: post)
self.show(postPageVC, sender: nil)
case is CommunityLeaderCell:
break
default:
break
}
}
override func moreActionsButtonDidTouch(_ sender: CommunButton) {
guard let profile = viewModel.profile.value, let currentUserID = Config.currentUser?.id else {return}
let headerView = CMMetaView(forAutoLayout: ())
headerView.avatarImageView.setAvatar(urlString: profile.avatarUrl)
headerView.titleLabel.text = profile.name
headerView.subtitleLabel.text = profile.communityId
showCMActionSheet(
headerView: headerView,
actions: [
.default(
title: "share".localized().uppercaseFirst,
iconName: "share",
handle: {
ShareHelper.share(urlString: self.shareWith(name: profile.alias ?? "", userID: currentUserID, isCommunity: true))
}, bottomMargin: 10
),
.default(
title: (profile.isInBlacklist == true ? "unhide": "hide").localized().uppercaseFirst,
iconName: "profile_options_blacklist",
handle: {
self.showAlert(
title: (profile.isInBlacklist == true ? "unhide community" : "hide community").localized().uppercaseFirst,
message: (profile.isInBlacklist == true ? "do you really want to unhide all posts of" : "do you really want to hide all posts of").localized().uppercaseFirst + " " + profile.name + "?",
buttonTitles: ["yes".localized().uppercaseFirst, "no".localized().uppercaseFirst],
highlightedButtonIndex: 1) { (index) in
if index != 0 {return}
if profile.isInBlacklist == true {
self.unhideCommunity()
} else {
self.hideCommunity()
}
}
})
]
)
}
override func configureNavigationBar() {
super.configureNavigationBar()
manageCommunityBarButton.tintColor = showNavigationBar ? .appBlackColor: .white
}
}
// MARK: - UITableViewDelegate
extension CommunityPageVC: UITableViewDelegate, UICollectionViewDelegateFlowLayout {
// MARK: - Sorting
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let viewModel = self.viewModel as! CommunityPageViewModel
if viewModel.segmentedItem.value == .posts {
return 48
}
if viewModel.segmentedItem.value == .leads {
return 42
}
return 0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewModel = self.viewModel as! CommunityPageViewModel
if viewModel.segmentedItem.value == .posts {
updatePostSortingView()
return postSortingView
}
if viewModel.segmentedItem.value == .leads {
let headerView = UIView(frame: .zero)
let label = UILabel.with(text: dataSource.sectionModels[section].model.localized().uppercaseFirst, textSize: 17, weight: .semibold)
headerView.addSubview(label)
label.autoPinEdge(toSuperviewEdge: .leading, withInset: 20)
label.autoAlignAxis(toSuperviewAxis: .horizontal)
return headerView
}
return nil
}
func updatePostSortingView() {
let viewModel = self.viewModel as! CommunityPageViewModel
if viewModel.segmentedItem.value != .posts {
return
}
let filter = viewModel.postsVM.filter.value
var type = filter.type
if type == .community || type.localizedLabel == nil {
type = .new
}
let aStr = NSMutableAttributedString()
.semibold("sort".localized().uppercaseFirst + ":", color: .appGrayColor)
.semibold(" ")
.semibold(type.localizedLabel!.uppercaseFirst)
if filter.type == .topLikes {
aStr
.semibold(", \(filter.timeframe?.localizedLabel.uppercaseFirst ?? "")")
}
(postSortingView.viewWithTag(1) as! UILabel).attributedText = aStr
}
@objc func openFilterVC() {
let viewModel = (self.viewModel as! CommunityPageViewModel).postsVM
// Create FiltersVC
var filter = viewModel.filter.value
if filter.type == .community {filter.type = .new}
let vc = PostsFilterVC(filter: filter)
vc.completion = { filter in
var filter = filter
if filter.type == .new {
filter.type = .community
}
viewModel.filter.accept(filter)
self.updatePostSortingView()
}
let nc = SwipeNavigationController(rootViewController: vc)
nc.transitioningDelegate = vc
nc.modalPresentationStyle = .custom
present(nc, animated: true, completion: nil)
}
// MARK: - rowHeight caching
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
guard let item = viewModel.items.value[safe: indexPath.row] else {
return UITableView.automaticDimension
}
switch item {
case let post as ResponseAPIContentGetPost:
return (viewModel as! CommunityPageViewModel).postsVM.rowHeights[post.identity] ?? 200
// case let leader as ResponseAPIContentGetLeader:
// return (viewModel as! CommunityPageViewModel).leadsVM.rowHeights[leader.identity] ?? 121
case let rule as ResponseAPIContentGetCommunityRule:
return (viewModel as! CommunityPageViewModel).ruleRowHeights[rule.identity] ?? 68
default:
return UITableView.automaticDimension
}
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let item = viewModel.items.value[safe: indexPath.row] else {
return
}
switch item {
case var post as ResponseAPIContentGetPost:
(viewModel as! CommunityPageViewModel).postsVM.rowHeights[post.identity] = cell.bounds.height
// record post view
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
if tableView.isCellVisible(indexPath: indexPath) &&
(cell as? PostCell)?.postIdentity == post.identity &&
!RestAPIManager.instance.markedAsViewedPosts.contains(post.identity)
{
post.markAsViewed().disposed(by: self.disposeBag)
}
}
// hide donation buttons when cell was removed
if !tableView.isCellVisible(indexPath: indexPath), post.showDonationButtons == true {
post.showDonationButtons = false
post.notifyChanged()
}
case let leader as ResponseAPIContentGetLeader:
(viewModel as! CommunityPageViewModel).leadsVM.rowHeights[leader.identity] = cell.bounds.height
case let rule as ResponseAPIContentGetCommunityRule:
(viewModel as! CommunityPageViewModel).ruleRowHeights[rule.identity] = cell.bounds.height
default:
break
}
}
// https://stackoverflow.com/questions/1074006/is-it-possible-to-disable-floating-headers-in-uitableview-with-uitableviewstylep
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
UIView(frame: .zero)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == tagsCollectionView, let topic = viewModel.profile.value?.getTopics()[safe: indexPath.row] {
let size = (topic as NSString).size(withAttributes: [.font: UIFont.systemFont(ofSize: 12, weight: .bold)])
return CGSize(width: size.width + 16 + 16, height: 32)
}
return .zero
}
}