-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathBlockedPeersViewController.swift
315 lines (261 loc) · 10.9 KB
/
BlockedPeersViewController.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
import Cocoa
import TGUIKit
import Postbox
import TelegramCore
import SwiftSignalKit
private final class BlockedPeerControllerArguments {
let context: AccountContext
let removePeer: (PeerId) -> Void
let openPeer:(PeerId) -> Void
init(context: AccountContext, removePeer: @escaping (PeerId) -> Void, openPeer: @escaping(PeerId)->Void) {
self.context = context
self.removePeer = removePeer
self.openPeer = openPeer
}
}
private enum BlockedPeerEntryStableId: Hashable {
case peer(PeerId)
case empty
case sectionId(Int32)
var hashValue: Int {
switch self {
case let .peer(peerId):
return peerId.hashValue
case .empty:
return 0
case .sectionId:
return 1
}
}
}
private enum BlockedPeerEntry: Identifiable, Comparable {
case section(Int32)
case peerItem(Int32, Int32, Peer, ShortPeerDeleting?, Bool, GeneralViewType)
case empty(Bool)
var stableId: BlockedPeerEntryStableId {
switch self {
case let .peerItem(_, _, peer, _, _, _):
return .peer(peer.id)
case .empty:
return .empty
case let .section(id):
return .sectionId(id)
}
}
static func ==(lhs: BlockedPeerEntry, rhs: BlockedPeerEntry) -> Bool {
switch lhs {
case let .peerItem(lhsSectionId, lhsIndex, lhsPeer, lhsEditing, lhsEnabled, lhsViewType):
if case let .peerItem(rhsSectionId, rhsIndex, rhsPeer, rhsEditing, rhsEnabled, rhsViewType) = rhs {
if lhsIndex != rhsIndex {
return false
}
if !lhsPeer.isEqual(rhsPeer) {
return false
}
if lhsSectionId != rhsSectionId {
return false
}
if lhsEditing != rhsEditing {
return false
}
if lhsEnabled != rhsEnabled {
return false
}
if rhsViewType != lhsViewType {
return false
}
return true
} else {
return false
}
case let .empty(loading):
if case .empty(loading) = rhs {
return true
} else {
return false
}
case let .section(id):
if case .section(id) = rhs {
return true
} else {
return false
}
}
}
var index: Int32 {
switch self {
case .empty:
return 0
case let .peerItem(sectionId, index, _, _, _, _):
return (sectionId * 1000) + index
case let .section(sectionId):
return (sectionId * 1000) + sectionId
}
}
static func <(lhs: BlockedPeerEntry, rhs: BlockedPeerEntry) -> Bool {
return lhs.index < rhs.index
}
func item(_ arguments: BlockedPeerControllerArguments, initialSize:NSSize) -> TableRowItem {
switch self {
case let .peerItem(_, _, peer, editing, enabled, viewType):
let interactionType:ShortPeerItemInteractionType
if let editing = editing {
interactionType = .deletable(onRemove: { peerId in
arguments.removePeer(peerId)
}, deletable: editing.editable)
} else {
interactionType = .plain
}
return ShortPeerRowItem(initialSize, peer: peer, account: arguments.context.account, context: arguments.context, stableId: stableId, enabled: enabled, height: 46, photoSize: NSMakeSize(32, 32), inset: NSEdgeInsets(left: 20, right: 20), interactionType: interactionType, generalType: .none, viewType: viewType, action: {
arguments.openPeer(peer.id)
}, contextMenuItems: {
if case .plain = interactionType {
return .single([ContextMenuItem(strings().chatInputUnblock, handler: {
arguments.removePeer(peer.id)
}, itemImage: MenuAnimation.menu_unblock.value)])
} else {
return .single([])
}
})
case let .empty(progress):
return SearchEmptyRowItem(initialSize, stableId: stableId, isLoading: progress, text: strings().blockedPeersEmptyDescrpition, viewType: .singleItem)
case .section:
return GeneralRowItem(initialSize, height: 20, stableId: stableId, viewType: .separator)
}
}
}
private struct BlockedPeerControllerState: Equatable {
let editing: Bool
let removingPeerId: PeerId?
init() {
self.editing = false
self.removingPeerId = nil
}
init(editing: Bool, removingPeerId: PeerId?) {
self.editing = editing
self.removingPeerId = removingPeerId
}
static func ==(lhs: BlockedPeerControllerState, rhs: BlockedPeerControllerState) -> Bool {
if lhs.editing != rhs.editing {
return false
}
if lhs.removingPeerId != rhs.removingPeerId {
return false
}
return true
}
func withUpdatedEditing(_ editing: Bool) -> BlockedPeerControllerState {
return BlockedPeerControllerState(editing: editing, removingPeerId: self.removingPeerId)
}
func withUpdatedPeerIdWithRevealedOptions(_ peerIdWithRevealedOptions: PeerId?) -> BlockedPeerControllerState {
return BlockedPeerControllerState(editing: self.editing, removingPeerId: self.removingPeerId)
}
func withUpdatedRemovingPeerId(_ removingPeerId: PeerId?) -> BlockedPeerControllerState {
return BlockedPeerControllerState(editing: self.editing, removingPeerId: removingPeerId)
}
}
private func blockedPeersControllerEntries(state: BlockedPeerControllerState, blockedState: BlockedPeersContextState) -> [BlockedPeerEntry] {
var entries: [BlockedPeerEntry] = []
var index: Int32 = 0
var sectionId: Int32 = 0
if !blockedState.peers.isEmpty {
entries.append(.section(sectionId))
sectionId += 1
}
let list = blockedState.peers.filter { $0.peer?.isDeleted == false }
for rendered in list {
if let peer = rendered.peer {
var deleting:ShortPeerDeleting? = nil
if state.editing {
deleting = ShortPeerDeleting(editable: true)
}
entries.append(.peerItem(sectionId, index, peer, deleting, state.removingPeerId != peer.id, bestGeneralViewType(list, for: rendered)))
index += 1
}
}
if blockedState.peers.isEmpty {
entries.append(.empty(blockedState.isLoadingMore))
} else {
entries.append(.section(sectionId))
sectionId += 1
}
return entries
}
fileprivate func prepareTransition(left:[AppearanceWrapperEntry<BlockedPeerEntry>], right: [AppearanceWrapperEntry<BlockedPeerEntry>], initialSize:NSSize, arguments:BlockedPeerControllerArguments) -> TableUpdateTransition {
let (removed, inserted, updated) = proccessEntriesWithoutReverse(left, right: right) { entry -> TableRowItem in
return entry.entry.item(arguments, initialSize: initialSize)
}
return TableUpdateTransition(deleted: removed, inserted: inserted, updated: updated, animated: true, grouping: false)
}
class BlockedPeersViewController: EditableViewController<TableView> {
private let statePromise = ValuePromise(BlockedPeerControllerState(), ignoreRepeated: true)
private let stateValue = Atomic(value: BlockedPeerControllerState())
private let removePeerDisposable:MetaDisposable = MetaDisposable()
private let disposable:MetaDisposable = MetaDisposable()
override func viewDidLoad() {
super.viewDidLoad()
genericView.getBackgroundColor = {
theme.colors.listBackground
}
let context = self.context
let updateState: ((BlockedPeerControllerState) -> BlockedPeerControllerState) -> Void = { [weak self] f in
if let strongSelf = self {
strongSelf.statePromise.set(strongSelf.stateValue.modify { f($0) })
}
}
let arguments = BlockedPeerControllerArguments(context: context, removePeer: { [weak self] memberId in
updateState {
return $0.withUpdatedRemovingPeerId(memberId)
}
self?.removePeerDisposable.set((context.blockedPeersContext.remove(peerId: memberId) |> deliverOnMainQueue).start(error: { error in
switch error {
case .generic:
alert(for: context.window, info: strings().unknownError)
}
updateState {
return $0.withUpdatedRemovingPeerId(nil)
}
}, completed: {
updateState {
return $0.withUpdatedRemovingPeerId(nil)
}
}))
}, openPeer: { [weak self] peerId in
guard let `self` = self else {return}
if let navigation = self.navigationController {
PeerInfoController.push(navigation: navigation, context: self.context, peerId: peerId)
}
})
let initialSize = atomicSize
let previousEntries:Atomic<[AppearanceWrapperEntry<BlockedPeerEntry>]> = Atomic(value: [])
let signal = combineLatest(statePromise.get(), context.blockedPeersContext.state, appearanceSignal)
|> deliverOnMainQueue
|> map { state, blockedState, appearance -> TableUpdateTransition in
let entries = blockedPeersControllerEntries(state: state, blockedState: blockedState).map{AppearanceWrapperEntry(entry: $0, appearance: appearance)}
return prepareTransition(left: previousEntries.swap(entries), right: entries, initialSize: initialSize.modify{$0}, arguments: arguments)
}
disposable.set((signal |> deliverOnMainQueue).start(next: { [weak self] transition in
if let strongSelf = self {
strongSelf.genericView.merge(with: transition)
strongSelf.readyOnce()
strongSelf.rightBarView.isHidden = strongSelf.genericView.item(at: 0) is SearchEmptyRowItem
}
}))
genericView.setScrollHandler { position in
switch position.direction {
case .bottom:
context.blockedPeersContext.loadMore()
default:
break
}
}
}
deinit {
disposable.dispose()
removePeerDisposable.dispose()
}
override func update(with state: ViewControllerState) {
super.update(with: state)
self.statePromise.set(stateValue.modify({$0.withUpdatedEditing(state == .Edit)}))
}
}