-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathArchivedStickerPacksController.swift
315 lines (251 loc) · 12.2 KB
/
ArchivedStickerPacksController.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
//
// ArchivedStickerPacksController.swift
// Telegram
//
// Created by keepcoder on 28/03/2017.
// Copyright © 2017 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import SwiftSignalKit
import Postbox
import TelegramCore
private final class ArchivedStickerPacksControllerArguments {
let context: AccountContext
let openStickerPack: (StickerPackCollectionInfo) -> Void
let removePack: (StickerPackCollectionInfo) -> Void
init(context: AccountContext, openStickerPack: @escaping (StickerPackCollectionInfo) -> Void, removePack: @escaping (StickerPackCollectionInfo) -> Void) {
self.context = context
self.openStickerPack = openStickerPack
self.removePack = removePack
}
}
private enum ArchivedStickerPacksEntryId: Hashable {
case index(Int32)
case pack(ItemCollectionId)
case loading
var hashValue: Int {
switch self {
case let .index(index):
return index.hashValue
case let .pack(id):
return id.hashValue
case .loading:
return -100
}
}
}
private enum ArchivedStickerPacksEntry: TableItemListNodeEntry {
case section(sectionId:Int32)
case info(sectionId:Int32, String, GeneralViewType)
case pack(sectionId:Int32, Int32, StickerPackCollectionInfo, StickerPackItem?, Int32, Bool, ItemListStickerPackItemEditing, GeneralViewType)
case loading(Bool)
var stableId: ArchivedStickerPacksEntryId {
switch self {
case .info:
return .index(0)
case .loading:
return .loading
case let .pack(_, _, info, _, _, _, _, _):
return .pack(info.id)
case let .section(sectionId):
return .index((sectionId + 1) * 1000 - sectionId)
}
}
var stableIndex:Int32 {
switch self {
case .info:
return 0
case .loading:
return -1
case .pack:
fatalError("")
case let .section(sectionId):
return (sectionId + 1) * 1000 - sectionId
}
}
var index:Int32 {
switch self {
case .loading:
return 0
case let .info(sectionId, _, _):
return (sectionId * 1000) + stableIndex
case let .pack( sectionId, index, _, _, _, _, _, _):
return (sectionId * 1000) + 100 + index
case let .section(sectionId):
return (sectionId + 1) * 1000 - sectionId
}
}
static func <(lhs: ArchivedStickerPacksEntry, rhs: ArchivedStickerPacksEntry) -> Bool {
return lhs.index < rhs.index
}
func item(_ arguments: ArchivedStickerPacksControllerArguments, initialSize: NSSize) -> TableRowItem {
switch self {
case let .info(_, text, viewType):
return GeneralTextRowItem(initialSize, stableId: stableId, text: text, viewType: viewType)
case let .pack(_, _, info, topItem, count, enabled, editing, viewType):
return StickerSetTableRowItem(initialSize, context: arguments.context, stableId: stableId, info: info, topItem: topItem, itemCount: count, unread: false, editing: editing, enabled: enabled, control: .remove, viewType: viewType, action: {
arguments.openStickerPack(info)
}, addPack: {
}, removePack: {
arguments.removePack(info)
})
case .section:
return GeneralRowItem(initialSize, height: 20, stableId: stableId, viewType: .separator)
case .loading(let loading):
return SearchEmptyRowItem(initialSize, stableId: stableId, isLoading: loading, text: strings().archivedStickersEmpty)
}
}
}
private struct ArchivedStickerPacksControllerState: Equatable {
let editing: Bool
let removingPackIds: Set<ItemCollectionId>
init() {
self.editing = false
self.removingPackIds = Set()
}
init(editing: Bool, removingPackIds: Set<ItemCollectionId>) {
self.editing = editing
self.removingPackIds = removingPackIds
}
static func ==(lhs: ArchivedStickerPacksControllerState, rhs: ArchivedStickerPacksControllerState) -> Bool {
if lhs.editing != rhs.editing {
return false
}
if lhs.removingPackIds != rhs.removingPackIds {
return false
}
return true
}
func withUpdatedEditing(_ editing: Bool) -> ArchivedStickerPacksControllerState {
return ArchivedStickerPacksControllerState(editing: editing, removingPackIds: self.removingPackIds)
}
func withUpdatedRemovingPackIds(_ removingPackIds: Set<ItemCollectionId>) -> ArchivedStickerPacksControllerState {
return ArchivedStickerPacksControllerState(editing: editing, removingPackIds: removingPackIds)
}
}
private func archivedStickerPacksControllerEntries(state: ArchivedStickerPacksControllerState, packs: [ArchivedStickerPackItem]?, installedView: CombinedView) -> [ArchivedStickerPacksEntry] {
var entries: [ArchivedStickerPacksEntry] = []
if let packs = packs {
if packs.isEmpty {
entries.append(.loading(false))
} else {
var sectionId:Int32 = 1
entries.append(.section(sectionId: sectionId))
sectionId += 1
entries.append(.info(sectionId: sectionId, strings().archivedStickersDescription, .textTopItem))
var installedIds = Set<ItemCollectionId>()
if let view = installedView.views[.itemCollectionIds(namespaces: [Namespaces.ItemCollection.CloudStickerPacks])] as? ItemCollectionIdsView, let ids = view.idsByNamespace[Namespaces.ItemCollection.CloudStickerPacks] {
installedIds = ids
}
let packs = packs.filter { item in
return !installedIds.contains(item.info.id)
}
var index: Int32 = 0
for item in packs {
entries.append(.pack(sectionId: sectionId, index, item.info, item.topItems.first, item.info.count, !state.removingPackIds.contains(item.info.id), ItemListStickerPackItemEditing(editable: true, editing: state.editing), bestGeneralViewType(packs, for: item)))
index += 1
}
entries.append(.section(sectionId: sectionId))
sectionId += 1
}
} else {
entries.append(.loading(true))
}
return entries
}
private func prepareTransition(left:[AppearanceWrapperEntry<ArchivedStickerPacksEntry>], right: [AppearanceWrapperEntry<ArchivedStickerPacksEntry>], initialSize: NSSize, arguments: ArchivedStickerPacksControllerArguments) -> 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)
}
class ArchivedStickerPacksController: TableViewController {
private let disposable = MetaDisposable()
private let archived: [ArchivedStickerPackItem]?
private let updatedPacks: ([ArchivedStickerPackItem]?) -> Void
init(_ context: AccountContext, archived: [ArchivedStickerPackItem]?, updatedPacks: @escaping ([ArchivedStickerPackItem]?) -> Void) {
self.archived = archived
self.updatedPacks = updatedPacks
super.init(context)
}
deinit {
disposable.dispose()
}
override func viewDidLoad() {
super.viewDidLoad()
let context = self.context
let statePromise = ValuePromise(ArchivedStickerPacksControllerState(), ignoreRepeated: true)
let stateValue = Atomic(value: ArchivedStickerPacksControllerState())
let updateState: ((ArchivedStickerPacksControllerState) -> ArchivedStickerPacksControllerState) -> Void = { f in
statePromise.set(stateValue.modify { f($0) })
}
let updatedPacks = self.updatedPacks
let actionsDisposable = DisposableSet()
let resolveDisposable = MetaDisposable()
actionsDisposable.add(resolveDisposable)
let removePackDisposables = DisposableDict<ItemCollectionId>()
actionsDisposable.add(removePackDisposables)
let stickerPacks = Promise<[ArchivedStickerPackItem]?>()
stickerPacks.set(.single(archived) |> then(context.engine.stickers.archivedStickerPacks() |> map { Optional($0) }))
let installedStickerPacks = Promise<CombinedView>()
installedStickerPacks.set(context.account.postbox.combinedView(keys: [.itemCollectionIds(namespaces: [Namespaces.ItemCollection.CloudStickerPacks])]))
actionsDisposable.add(stickerPacks.get().start(next: { packs in
updatedPacks(packs)
}))
let arguments = ArchivedStickerPacksControllerArguments(context: context, openStickerPack: { info in
showModal(with: StickerPackPreviewModalController(context, peerId: nil, references: [.stickers(.name(info.shortName))]), for: context.window)
}, removePack: { info in
verifyAlert_button(for: context.window, information: strings().chatConfirmActionUndonable, successHandler: { _ in
var remove = false
updateState { state in
var removingPackIds = state.removingPackIds
if !removingPackIds.contains(info.id) {
removingPackIds.insert(info.id)
remove = true
}
return state.withUpdatedRemovingPackIds(removingPackIds)
}
if remove {
let applyPacks: Signal<Void, NoError> = stickerPacks.get()
|> filter { $0 != nil }
|> take(1)
|> deliverOnMainQueue
|> mapToSignal { packs -> Signal<Void, NoError> in
if let packs = packs {
var updatedPacks = packs
for i in 0 ..< updatedPacks.count {
if updatedPacks[i].info.id == info.id {
updatedPacks.remove(at: i)
break
}
}
stickerPacks.set(.single(updatedPacks))
}
return .complete()
}
removePackDisposables.set((context.engine.stickers.removeArchivedStickerPack(info: info) |> then(applyPacks) |> deliverOnMainQueue).start(completed: {
updateState { state in
var removingPackIds = state.removingPackIds
removingPackIds.remove(info.id)
return state.withUpdatedRemovingPackIds(removingPackIds)
}
}), forKey: info.id)
}
})
})
let previousEntries:Atomic<[AppearanceWrapperEntry<ArchivedStickerPacksEntry>]> = Atomic(value: [])
let initialSize = atomicSize
let signal = combineLatest(queue: prepareQueue, statePromise.get(), stickerPacks.get(), installedStickerPacks.get(), appearanceSignal)
|> map { state, packs, installedView, appearance -> TableUpdateTransition in
let entries = archivedStickerPacksControllerEntries(state: state, packs: packs, installedView: installedView).map {AppearanceWrapperEntry(entry: $0, appearance: appearance)}
return prepareTransition(left: previousEntries.swap(entries), right: entries, initialSize: initialSize.modify({$0}), arguments: arguments)
} |> afterDisposed {
actionsDisposable.dispose()
} |> deliverOnMainQueue
disposable.set(signal.start(next: { [weak self] transition in
self?.genericView.merge(with: transition)
self?.readyOnce()
}))
}
}