-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommunityPageViewModel.swift
247 lines (221 loc) · 9.5 KB
/
CommunityPageViewModel.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
//
// CommunityViewModel.swift
// Commun
//
// Created by Chung Tran on 10/23/19.
// Copyright © 2019 Commun Limited. All rights reserved.
//
import Foundation
import RxSwift
import RxCocoa
import CyberSwift
import SwiftyJSON
class CommunityPageViewModel: ProfileViewModel<ResponseAPIContentGetCommunity> {
// MARK: - Nested type
enum SegmentioItem: String, CaseIterable {
case posts = "posts"
case leads = "leads"
case about = "about"
case rules = "rules"
}
// MARK: - Input
var communityForRequest: ResponseAPIContentGetCommunity? {
return profileForRequest
}
var communityId: String? {
return profileId
}
var communityAlias: String?
lazy var ruleRowHeights = [String: CGFloat]()
// MARK: - Objects
var community: BehaviorRelay<ResponseAPIContentGetCommunity?> {
return profile
}
let segmentedItem = BehaviorRelay<SegmentioItem>(value: .posts)
lazy var postsVM = PostsViewModel(filter: PostsListFetcher.Filter(authorizationRequired: authorizationRequired, type: .community, sortBy: .time, timeframe: .all, communityId: communityId, communityAlias: communityAlias))
lazy var leadsVM = LeadersViewModel(communityId: communityId, communityAlias: communityAlias, authorizationRequired: authorizationRequired)
lazy var aboutSubject = PublishSubject<String?>()
lazy var rules = BehaviorRelay<[ResponseAPIContentGetCommunityRule]>(value: [])
lazy var proposalsVM = ProposalsViewModel()
lazy var reportsVM = ReportsViewModel()
// MARK: - Initializers
init(communityId: String?, authorizationRequired: Bool = true) {
super.init(profileId: communityId, authorizationRequired: authorizationRequired)
leadsVM.fetchNext()
}
init(communityAlias: String, authorizationRequired: Bool = true) {
self.communityAlias = communityAlias
super.init(profileId: nil, authorizationRequired: authorizationRequired)
leadsVM.fetchNext()
}
// MARK: - Methods
override var loadProfileRequest: Single<ResponseAPIContentGetCommunity> {
if let alias = communityAlias {
return RestAPIManager.instance.getCommunity(alias: alias, authorizationRequired: authorizationRequired)
}
return RestAPIManager.instance.getCommunity(id: communityId ?? "", authorizationRequired: authorizationRequired)
}
override var listLoadingStateObservable: Observable<ListFetcherState> {
Observable.merge(postsVM.state.asObservable().filter {[weak self] _ in self?.segmentedItem.value == .posts}, leadsVM.state.asObservable().filter {[weak self] _ in self?.segmentedItem.value == .leads})
}
var walletGetBuyPriceRequest: Single<ResponseAPIWalletGetPrice> {
return RestAPIManager.instance.getBuyPrice(symbol: communityId ?? communityAlias?.uppercased() ?? "CMN", quantity: "1 CMN", authorizationRequired: authorizationRequired)
}
override func bind() {
super.bind()
segmentedItem
.filter {_ in self.community.value != nil}
.subscribe(onNext: { (item) in
switch item {
case .posts:
self.postsVM.reload()
case .leads:
self.leadsVM.reload()
case .about:
if let description = self.community.value?.description,
!description.isEmpty {
self.aboutSubject.onNext(description)
self.listLoadingState.accept(.listEnded)
return
}
self.aboutSubject.onNext(nil)
self.listLoadingState.accept(.listEmpty)
case .rules:
self.rules.accept(self.rules.value)
if self.rules.value.isEmpty {
self.listLoadingState.accept(.listEmpty)
} else {
self.listLoadingState.accept(.listEnded)
}
}
})
.disposed(by: disposeBag)
Observable.merge(
postsVM.items.map {$0 as [Any]}.skip(1),
leadsVM.items.map {$0 as [Any]}.skip(1),
aboutSubject.map {$0 != nil ? [$0!] as [Any]: [Any]()},
rules.map {$0 as [Any]}
)
.filter({ items -> Bool in
if items is [ResponseAPIContentGetPost] && self.segmentedItem.value == .posts {
return true
}
if items is [ResponseAPIContentGetLeader] && self.segmentedItem.value == .leads {
return true
}
if items is [String] && self.segmentedItem.value == .about {
return true
}
if items is [ResponseAPIContentGetCommunityRule] && self.segmentedItem.value == .rules {
return true
}
return false
})
.skip(1)
.asDriver(onErrorJustReturn: [])
.drive(items)
.disposed(by: disposeBag)
profile
.map {$0?.rules ?? []}
.bind(to: rules)
.disposed(by: disposeBag)
profile
.filter {$0?.isLeader ?? false}
.subscribe(onNext: { community in
(self.proposalsVM.fetcher as! ProposalsListFetcher).communityIds = [community!.communityId]
(self.reportsVM.fetcher as! ReportsListFetcher).communityIds = [community!.communityId]
if let id = community?.communityId, let issuer = community?.issuer {
self.reportsVM.issuers[id] = issuer
}
self.proposalsVM.reload(clearResult: true)
self.reportsVM.reload(clearResult: true)
})
.disposed(by: disposeBag)
// Observable.combineLatest(
// profile,
// leadsVM.items
// )
// .subscribe(onNext: { (community, leaders) in
// // if regLeader was not called
// if let communityId = community?.communityId,
// ResponseAPIContentGetProfile.current?.createdCommunities?.contains(where: {$0.communityId == communityId}) == true,
// !leaders.contains(where: {$0.userId == Config.currentUser?.id})
// {
// self.regLeader(communityId: communityId)
// .subscribe()
// .disposed(by: self.disposeBag)
// }
// })
// .disposed(by: disposeBag)
// Rule changed (ex: isExpanded)
ResponseAPIContentGetCommunityRule
.observeItemChanged()
.subscribe(onNext: { rule in
var rules = self.rules.value
if let index = rules.firstIndex(where: {$0.identity == rule.identity})
{
if rule.isExpanded != rules[index].isExpanded {
self.ruleRowHeights.removeValue(forKey: rule.identity)
}
rules[index] = rule
self.rules.accept(rules)
}
})
.disposed(by: disposeBag)
// Update friends when user follow someone
ResponseAPIContentGetProfile.observeProfileFollowed()
.filter {profile in
self.community.value?.friends?.contains(where: {$0.identity == profile.identity}) == false
}
.subscribe(onNext: { [weak self] (followedProfile) in
guard let strongSelf = self,
var community = strongSelf.community.value
else {return}
community.friends?.append(followedProfile)
strongSelf.community.accept(community)
})
.disposed(by: disposeBag)
}
override func reload() {
switch segmentedItem.value {
case .posts:
postsVM.reload()
case .leads:
leadsVM.reload()
default:
break
}
super.reload()
}
override func fetchNext(forceRetry: Bool = false) {
super.fetchNext(forceRetry: forceRetry)
switch segmentedItem.value {
case .posts:
postsVM.fetchNext(forceRetry: forceRetry)
case .leads:
leadsVM.fetchNext(forceRetry: forceRetry)
default:
return
}
}
func regLeader(communityId: String) -> Completable {
BlockchainManager.instance.regLeader(communityId: communityId)
.flatMapCompletable {RestAPIManager.instance.waitForTransactionWith(id: $0)}
.do(onCompleted: {
self.reload()
if self.segmentedItem.value != .leads {
self.leadsVM.reload(clearResult: true)
}
BlockchainManager.instance.voteLeader(communityId: communityId, leader: Config.currentUser?.id ?? "")
.flatMapCompletable {RestAPIManager.instance.waitForTransactionWith(id: $0)}
.subscribe(onCompleted: {
if var leader = self.leadsVM.items.value.first(where: {$0.userId == Config.currentUser?.id})
{
leader.isVoted = true
leader.notifyChanged()
}
})
.disposed(by: self.disposeBag)
})
}
}