-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFeedPageVC.swift
218 lines (184 loc) · 7.69 KB
/
FeedPageVC.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
//
// FeedPageVC.swift
// Commun
//
// Created by Chung Tran on 11/29/19.
// Copyright © 2019 Commun Limited. All rights reserved.
//
import Foundation
import RxSwift
import RxDataSources
class FeedPageVC: PostsViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {.lightContent}
override var prefersNavigationBarStype: BaseViewController.NavigationBarStyle {.hidden}
// MARK: - Properties
lazy var floatView = FeedPageFloatView(forAutoLayout: ())
var floatViewTopConstraint: NSLayoutConstraint!
var headerView: FeedPageHeaderView!
var floatViewHeight: CGFloat = 0
var lastContentOffset: CGFloat = 0
// MARK: - Initializers
init() {
let viewModel = FeedPageViewModel(prefetch: true)
super.init(viewModel: viewModel)
}
override init(viewModel: PostsViewModel) {
super.init(viewModel: viewModel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Methods
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = floatView.height
if floatViewHeight == 0 {
tableView.contentInset.top = height
tableView.scrollIndicatorInsets = UIEdgeInsets(top: height, left: 0.0, bottom: 0.0, right: 0.0)
floatViewHeight = height
scrollToTop()
}
}
override func setUp() {
super.setUp()
view.backgroundColor = .appLightGrayColor
// tableView
tableView.backgroundColor = .appLightGrayColor
tableView.keyboardDismissMode = .onDrag
let statusBarView = UIView(backgroundColor: .appMainColorDarkBlack)
view.addSubview(statusBarView)
statusBarView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .bottom)
statusBarView.autoPinEdge(.bottom, to: .top, of: tableView)
view.addSubview(floatView)
floatViewTopConstraint = floatView.autoPinEdge(toSuperviewSafeArea: .top)
floatView.autoPinEdge(toSuperviewSafeArea: .leading)
floatView.autoPinEdge(toSuperviewSafeArea: .trailing)
statusBarView.autoPinEdge(.bottom, to: .top, of: floatView)
view.bringSubviewToFront(statusBarView)
headerView = FeedPageHeaderView(tableView: tableView)
headerView.delegate = self
floatView.changeFeedTypeButton.addTarget(self, action: #selector(changeFeedTypeButtonDidTouch(_:)), for: .touchUpInside)
floatView.sortButton.addTarget(self, action: #selector(changeFilterButtonDidTouch(_:)), for: .touchUpInside)
headerView.getButton.addTarget(self, action: #selector(promoGetButtonDidTouch), for: .touchUpInside)
dataSource.animationConfiguration = AnimationConfiguration(insertAnimation: dataSource.animationConfiguration.insertAnimation, reloadAnimation: dataSource.animationConfiguration.reloadAnimation, deleteAnimation: .bottom)
}
override func bind() {
super.bind()
tableView.rx.willBeginDragging.subscribe { _ in
self.lastContentOffset = self.tableView.contentOffset.y
}.disposed(by: disposeBag)
tableView.rx.contentOffset.observeOn(MainScheduler.asyncInstance)
.subscribe {
guard let offset = $0.element else { return }
var needAnimation = false
var newConstraint: CGFloat = 0.0
let lastOffset: CGFloat = self.lastContentOffset
let indent: CGFloat = 100
if lastOffset > offset.y + indent || offset.y <= 0 {
needAnimation = self.floatViewTopConstraint.constant <= 0
newConstraint = 0.0
} else if lastOffset < offset.y - indent {
let position = -self.floatView.frame.size.height
needAnimation = self.floatViewTopConstraint.constant >= position
newConstraint = position
}
if needAnimation {
self.view.layoutIfNeeded()
self.floatViewTopConstraint.constant = newConstraint
UIView.animate(withDuration: 0.3, animations: { [unowned self] in
self.view.layoutIfNeeded()
})
}
}.disposed(by: disposeBag)
// promo
(viewModel as? FeedPageViewModel)?.claimedPromos
.filter {$0 != nil}
.map {$0!}
.subscribe(onNext: { (_) in
// if !claimedPromos.contains("DANK") {
// self.headerView.showPromoBanner()
// }
})
.disposed(by: disposeBag)
}
override func modifyFilter(filter: PostsListFetcher.Filter) -> PostsListFetcher.Filter {
var filter = filter
switch filter.type {
case .new, .subscriptions:
filter.timeframe = nil
if filter.sortBy == nil {
filter.sortBy = .time
}
case .hot, .subscriptionsHot:
filter.timeframe = nil
filter.sortBy = nil
case .topLikes, .subscriptionsPopular:
filter.sortBy = nil
if filter.timeframe == nil {
filter.timeframe = .day
}
default:
break
}
return filter
}
override func filterChanged(filter: PostsListFetcher.Filter) {
super.filterChanged(filter: filter)
floatView.setUp(with: filter)
saveFilter(filter: filter)
}
func saveFilter(filter: PostsListFetcher.Filter) {
// save filter
do {
try filter.save()
} catch {
print(error)
}
}
func openEditor(completion: ((BasicEditorVC) -> Void)? = nil) {
let editorVC = BasicEditorVC(chooseCommunityAfterLoading: completion == nil)
present(editorVC, animated: true, completion: {
completion?(editorVC)
})
}
// MARK: - Actions
@objc func promoGetButtonDidTouch() {
// AnalyticsManger.shared.clickGetDankMeme()
// headerView.getButton.showLoading(cover: true, coverColor: .appMainColor, spinnerColor: .appWhiteColor, size: 20)
// RestAPIManager.instance.getAirdrop(communityId: "DANK")
// .subscribe(onSuccess: { (_) in
// self.headerView.getButton.hideLoading()
// self.headerView.hidePromoBanner()
// UIView.animate(withDuration: 0.3) {
// self.headerView.layoutIfNeeded()
// }
// self.showDone("done".localized().uppercaseFirst + "!")
// }) { (error) in
// self.headerView.getButton.hideLoading()
// self.showError(error)
// }
// .disposed(by: disposeBag)
}
@objc func changeFeedTypeButtonDidTouch(_ sender: Any) {
guard let viewModel = viewModel as? PostsViewModel else {return}
switch viewModel.filter.value.type {
case .subscriptions, .subscriptionsPopular, .subscriptionsHot:
viewModel.filter.accept(PostsListFetcher.Filter(type: .topLikes, timeframe: .day, userId: Config.currentUser?.id))
default:
viewModel.filter.accept(PostsListFetcher.Filter(type: .subscriptions, sortBy: .time))
}
}
@objc func changeFilterButtonDidTouch(_ sender: Any) {
openFilterVC()
}
}
extension FeedPageVC: FeedPageHeaderViewDelegate {
func feedPageHeaderViewDidTouchWhatsNew(_ headerView: FeedPageHeaderView) {
openEditor()
}
func feedPageHeaderViewDidTouchImageButton(_ headerView: FeedPageHeaderView) {
openEditor { (editorVC) in
editorVC.addImage()
}
}
}