-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPostPageVC+Binding.swift
165 lines (145 loc) · 6.56 KB
/
PostPageVC+Binding.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
//
// PostPageVC+Binding.swift
// Commun
//
// Created by Chung Tran on 11/8/19.
// Copyright © 2019 Commun Limited. All rights reserved.
//
import Foundation
extension PostPageVC {
func observePostDeleted() {
ResponseAPIContentGetPost.observeItemDeleted()
.subscribe(onNext: { (deletedPost) in
guard deletedPost.identity == (self.viewModel as! PostPageViewModel).post.value?.identity
else {return}
self.showAlert(title: "deleted".localized().uppercaseFirst, message: "the post has been deleted".localized().uppercaseFirst, completion: { (_) in
self.back()
})
})
.disposed(by: disposeBag)
}
func observeUserBlocked() {
ResponseAPIContentGetProfile.observeEvent(eventName: ResponseAPIContentGetProfile.blockedEventName)
.subscribe(onNext: {blockedUser in
let post = (self.viewModel as! PostPageViewModel).post.value
if post?.author?.userId == blockedUser.userId {
self.back()
}
})
.disposed(by: disposeBag)
}
func observeCommunityBlocked() {
ResponseAPIContentGetCommunity.observeEvent(eventName: ResponseAPIContentGetCommunity.blockedEventName)
.subscribe(onNext: { (blockedCommunity) in
let post = (self.viewModel as! PostPageViewModel).post.value
if post?.community?.communityId == blockedCommunity.communityId {
self.back()
}
})
.disposed(by: disposeBag)
}
func observeCommentAdded() {
NotificationCenter.default.rx.notification(.init(rawValue: "\(ResponseAPIContentGetPost.self)\(ResponseAPIContentGetPost.commentAddedEventName)"))
.subscribe(onNext: { (notification) in
guard let newComment = notification.object as? ResponseAPIContentGetComment else {return}
// add newComment to bottom of the list
var items = self.viewModel.items.value
items += [newComment]
self.viewModel.items.accept(items)
self.handleListEnded()
DispatchQueue.main.async {
self.tableView.safeScrollToRow(at: IndexPath(row: 0, section: self.tableView.numberOfSections - 1), at: .bottom, animated: true)
}
})
.disposed(by: disposeBag)
}
func bindControls() {
// tableView.rx.willDragDown
// .map {$0 ? true: false}
// .distinctUntilChanged()
// .subscribe(onNext: {hide in
// UIView.animate(withDuration: 0.25, animations: {
// self.navigationBar.isHidden = hide
// self.view.layoutIfNeeded()
// })
// })
// .disposed(by: disposeBag)
// Shadow on navigation bar
tableView.rx.contentOffset
.map { $0.y <= self.startContentOffsetY }
.distinctUntilChanged()
.skip(1)
.subscribe(onNext: { (showShadow) in
self.navigationBar.addShadow(ofColor: showShadow ? .clear : .shadow, radius: 20, offset: CGSize(width: 0, height: 3), opacity: 0.07)
})
.disposed(by: disposeBag)
commentForm.textView.rx.text.orEmpty
.subscribe(onNext: { (text) in
// textView
let contentSize = self.commentForm.textView.sizeThatFits(CGSize(width: self.commentForm.textView.width, height: .greatestFiniteMagnitude))
if self.shadowView.frame.minY > self.commentFormMinPaddingTop || contentSize.height < self.commentForm.textView.height
{
if self.commentForm.textView.isScrollEnabled {
// TODO: - Temporary solution for fixing textView layout
self.commentForm.textView.text = text + " "
DispatchQueue.main.async {
self.commentForm.textView.text = text
}
}
self.commentForm.textView.isScrollEnabled = false
} else {
if !self.commentForm.textView.isScrollEnabled {
// self.commentForm.textView.setNeedsLayout()
}
self.commentForm.textView.isScrollEnabled = true
}
})
.disposed(by: disposeBag)
// forward delegate
commentForm.textView.rx.setDelegate(self).disposed(by: disposeBag)
}
func bindPost() {
let viewModel = self.viewModel as! PostPageViewModel
// bind post loading state
viewModel.loadingState
.subscribe(onNext: { [weak self] loadingState in
switch loadingState {
case .loading:
self?.postHeaderView.hideLoader()
self?.postHeaderView.showLoader()
case .finished:
self?.postHeaderView.hideLoader()
case .error:
self?.postHeaderView.hideLoader()
guard let strongSelf = self else {return}
strongSelf.view.showErrorView {
strongSelf.view.hideErrorView()
strongSelf.refresh()
}
strongSelf.view.bringSubviewToFront(strongSelf.navigationBar)
}
})
.disposed(by: disposeBag)
// bind post
viewModel.post.skip(1)
.subscribe(onNext: { post in
guard let post = post else {return}
self.navigationBar.setUp(with: post)
self.postHeaderView.setUp(with: post)
// DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
// self.scrollToTopAfterLoadingComment = true
// }
})
.disposed(by: disposeBag)
// Mark post as read
viewModel.post.skip(1)
.filter {$0 != nil}.map {$0!}
.take(1).asSingle()
.subscribe(onSuccess: { (post) in
// if !RestAPIManager.instance.markedAsViewedPosts.contains(post.identity) {
post.markAsViewed().disposed(by: self.disposeBag)
// }
})
.disposed(by: disposeBag)
}
}