mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-08 08:31:13 +00:00
Improve sticker reordering performance
This commit is contained in:
parent
cb939af2c0
commit
f18b55bbd0
@ -249,6 +249,7 @@ open class ListView: ASDisplayNode, UIScrollViewAccessibilityDelegate, UIGesture
|
|||||||
public final var generalScrollDirectionUpdated: (GeneralScrollDirection) -> Void = { _ in }
|
public final var generalScrollDirectionUpdated: (GeneralScrollDirection) -> Void = { _ in }
|
||||||
|
|
||||||
public final var reorderItem: (Int, Int, Any?) -> Signal<Bool, NoError> = { _, _, _ in return .single(false) }
|
public final var reorderItem: (Int, Int, Any?) -> Signal<Bool, NoError> = { _, _, _ in return .single(false) }
|
||||||
|
public final var reorderCompleted: (Any?) -> Void = { _ in }
|
||||||
|
|
||||||
private final var animations: [ListViewAnimation] = []
|
private final var animations: [ListViewAnimation] = []
|
||||||
private final var actionsForVSync: [() -> ()] = []
|
private final var actionsForVSync: [() -> ()] = []
|
||||||
@ -439,6 +440,7 @@ open class ListView: ASDisplayNode, UIScrollViewAccessibilityDelegate, UIGesture
|
|||||||
reorderNode.removeFromSupernode()
|
reorderNode.removeFromSupernode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.reorderCompleted(self.opaqueTransactionState)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateReordering(offset: CGFloat) {
|
private func updateReordering(offset: CGFloat) {
|
||||||
@ -2680,6 +2682,7 @@ open class ListView: ASDisplayNode, UIScrollViewAccessibilityDelegate, UIGesture
|
|||||||
if highlightedItemNode.index != self.highlightedItemIndex {
|
if highlightedItemNode.index != self.highlightedItemIndex {
|
||||||
highlightedItemNode.setHighlighted(false, at: CGPoint(), animated: false)
|
highlightedItemNode.setHighlighted(false, at: CGPoint(), animated: false)
|
||||||
self.highlightedItemIndex = nil
|
self.highlightedItemIndex = nil
|
||||||
|
self.selectionTouchLocation = nil
|
||||||
}
|
}
|
||||||
} else if self.highlightedItemIndex != nil {
|
} else if self.highlightedItemIndex != nil {
|
||||||
self.highlightedItemIndex = nil
|
self.highlightedItemIndex = nil
|
||||||
|
@ -209,6 +209,19 @@ open class ItemListController: ViewController, KeyShortcutResponder, Presentable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func setReorderCompleted<T: ItemListNodeEntry>(_ f: @escaping ([T]) -> Void) {
|
||||||
|
self.reorderCompleted = { list in
|
||||||
|
f(list.map { $0 as! T })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private var reorderCompleted: (([ItemListNodeAnyEntry]) -> Void)? {
|
||||||
|
didSet {
|
||||||
|
if self.isNodeLoaded {
|
||||||
|
(self.displayNode as! ItemListControllerNode).reorderCompleted = self.reorderCompleted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public var previewItemWithTag: ((ItemListItemTag) -> UIViewController?)?
|
public var previewItemWithTag: ((ItemListItemTag) -> UIViewController?)?
|
||||||
public var commitPreview: ((UIViewController) -> Void)?
|
public var commitPreview: ((UIViewController) -> Void)?
|
||||||
|
|
||||||
@ -431,6 +444,7 @@ open class ItemListController: ViewController, KeyShortcutResponder, Presentable
|
|||||||
displayNode.contentScrollingEnded = self.contentScrollingEnded
|
displayNode.contentScrollingEnded = self.contentScrollingEnded
|
||||||
displayNode.searchActivated = self.searchActivated
|
displayNode.searchActivated = self.searchActivated
|
||||||
displayNode.reorderEntry = self.reorderEntry
|
displayNode.reorderEntry = self.reorderEntry
|
||||||
|
displayNode.reorderCompleted = self.reorderCompleted
|
||||||
displayNode.listNode.experimentalSnapScrollToItem = self.experimentalSnapScrollToItem
|
displayNode.listNode.experimentalSnapScrollToItem = self.experimentalSnapScrollToItem
|
||||||
displayNode.requestLayout = { [weak self] transition in
|
displayNode.requestLayout = { [weak self] transition in
|
||||||
self?.requestLayout(transition: transition)
|
self?.requestLayout(transition: transition)
|
||||||
|
@ -216,6 +216,7 @@ open class ItemListControllerNode: ASDisplayNode, UIScrollViewDelegate {
|
|||||||
public var contentScrollingEnded: ((ListView) -> Bool)?
|
public var contentScrollingEnded: ((ListView) -> Bool)?
|
||||||
public var searchActivated: ((Bool) -> Void)?
|
public var searchActivated: ((Bool) -> Void)?
|
||||||
public var reorderEntry: ((Int, Int, [ItemListNodeAnyEntry]) -> Void)?
|
public var reorderEntry: ((Int, Int, [ItemListNodeAnyEntry]) -> Void)?
|
||||||
|
public var reorderCompleted: (([ItemListNodeAnyEntry]) -> Void)?
|
||||||
public var requestLayout: ((ContainedViewLayoutTransition) -> Void)?
|
public var requestLayout: ((ContainedViewLayoutTransition) -> Void)?
|
||||||
|
|
||||||
public var enableInteractiveDismiss = false {
|
public var enableInteractiveDismiss = false {
|
||||||
@ -272,6 +273,12 @@ open class ItemListControllerNode: ASDisplayNode, UIScrollViewDelegate {
|
|||||||
return .single(false)
|
return .single(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.listNode.reorderCompleted = { [weak self] opaqueTransactionState in
|
||||||
|
if let strongSelf = self, let reorderCompleted = strongSelf.reorderCompleted, let mergedEntries = (opaqueTransactionState as? ItemListNodeOpaqueState)?.mergedEntries {
|
||||||
|
reorderCompleted(mergedEntries)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.listNode.visibleBottomContentOffsetChanged = { [weak self] offset in
|
self.listNode.visibleBottomContentOffsetChanged = { [weak self] offset in
|
||||||
self?.visibleBottomContentOffsetChanged?(offset)
|
self?.visibleBottomContentOffsetChanged?(offset)
|
||||||
}
|
}
|
||||||
|
@ -376,7 +376,7 @@ private func namespaceForMode(_ mode: InstalledStickerPacksControllerMode) -> It
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func installedStickerPacksControllerEntries(presentationData: PresentationData, state: InstalledStickerPacksControllerState, mode: InstalledStickerPacksControllerMode, view: CombinedView, featured: [FeaturedStickerPackItem], archived: [ArchivedStickerPackItem]?, stickerSettings: StickerSettings) -> [InstalledStickerPacksEntry] {
|
private func installedStickerPacksControllerEntries(presentationData: PresentationData, state: InstalledStickerPacksControllerState, mode: InstalledStickerPacksControllerMode, view: CombinedView, temporaryPackOrder: [ItemCollectionId]?, featured: [FeaturedStickerPackItem], archived: [ArchivedStickerPackItem]?, stickerSettings: StickerSettings) -> [InstalledStickerPacksEntry] {
|
||||||
var entries: [InstalledStickerPacksEntry] = []
|
var entries: [InstalledStickerPacksEntry] = []
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
@ -418,8 +418,30 @@ private func installedStickerPacksControllerEntries(presentationData: Presentati
|
|||||||
|
|
||||||
if let stickerPacksView = view.views[.itemCollectionInfos(namespaces: [namespaceForMode(mode)])] as? ItemCollectionInfosView {
|
if let stickerPacksView = view.views[.itemCollectionInfos(namespaces: [namespaceForMode(mode)])] as? ItemCollectionInfosView {
|
||||||
if let packsEntries = stickerPacksView.entriesByNamespace[namespaceForMode(mode)] {
|
if let packsEntries = stickerPacksView.entriesByNamespace[namespaceForMode(mode)] {
|
||||||
var index: Int32 = 0
|
var sortedPacks: [ItemCollectionInfoEntry] = []
|
||||||
for entry in packsEntries {
|
for entry in packsEntries {
|
||||||
|
if let info = entry.info as? StickerPackCollectionInfo {
|
||||||
|
sortedPacks.append(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let temporaryPackOrder = temporaryPackOrder {
|
||||||
|
var packDict: [ItemCollectionId: Int] = [:]
|
||||||
|
for i in 0 ..< sortedPacks.count {
|
||||||
|
packDict[sortedPacks[i].id] = i
|
||||||
|
}
|
||||||
|
var tempSortedPacks: [ItemCollectionInfoEntry] = []
|
||||||
|
var processedPacks = Set<ItemCollectionId>()
|
||||||
|
for id in temporaryPackOrder {
|
||||||
|
if let index = packDict[id] {
|
||||||
|
tempSortedPacks.append(sortedPacks[index])
|
||||||
|
processedPacks.insert(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let restPacks = sortedPacks.filter { !processedPacks.contains($0.id) }
|
||||||
|
sortedPacks = restPacks + tempSortedPacks
|
||||||
|
}
|
||||||
|
var index: Int32 = 0
|
||||||
|
for entry in sortedPacks {
|
||||||
if let info = entry.info as? StickerPackCollectionInfo {
|
if let info = entry.info as? StickerPackCollectionInfo {
|
||||||
entries.append(.pack(index, presentationData.theme, presentationData.strings, info, entry.firstItem as? StickerPackItem, presentationData.strings.StickerPack_StickerCount(info.count == 0 ? entry.count : info.count), stickerSettings.loopAnimatedStickers, true, ItemListStickerPackItemEditing(editable: true, editing: state.editing, revealed: state.packIdWithRevealedOptions == entry.id, reorderable: true)))
|
entries.append(.pack(index, presentationData.theme, presentationData.strings, info, entry.firstItem as? StickerPackItem, presentationData.strings.StickerPack_StickerCount(info.count == 0 ? entry.count : info.count), stickerSettings.loopAnimatedStickers, true, ItemListStickerPackItemEditing(editable: true, editing: state.editing, revealed: state.packIdWithRevealedOptions == entry.id, reorderable: true)))
|
||||||
index += 1
|
index += 1
|
||||||
@ -568,6 +590,7 @@ public func installedStickerPacksController(context: AccountContext, mode: Insta
|
|||||||
})
|
})
|
||||||
let stickerPacks = Promise<CombinedView>()
|
let stickerPacks = Promise<CombinedView>()
|
||||||
stickerPacks.set(context.account.postbox.combinedView(keys: [.itemCollectionInfos(namespaces: [namespaceForMode(mode)])]))
|
stickerPacks.set(context.account.postbox.combinedView(keys: [.itemCollectionInfos(namespaces: [namespaceForMode(mode)])]))
|
||||||
|
let temporaryPackOrder = Promise<[ItemCollectionId]?>(nil)
|
||||||
|
|
||||||
let featured = Promise<[FeaturedStickerPackItem]>()
|
let featured = Promise<[FeaturedStickerPackItem]>()
|
||||||
|
|
||||||
@ -581,9 +604,14 @@ public func installedStickerPacksController(context: AccountContext, mode: Insta
|
|||||||
}
|
}
|
||||||
|
|
||||||
var previousPackCount: Int?
|
var previousPackCount: Int?
|
||||||
let signal = combineLatest(queue: .mainQueue(), context.sharedContext.presentationData, statePromise.get(), stickerPacks.get(), combineLatest(queue: .mainQueue(), featured.get(), archivedPromise.get()), context.sharedContext.accountManager.sharedData(keys: [ApplicationSpecificSharedDataKeys.stickerSettings]))
|
let signal = combineLatest(queue: .mainQueue(), context.sharedContext.presentationData,
|
||||||
|
statePromise.get(),
|
||||||
|
stickerPacks.get(),
|
||||||
|
temporaryPackOrder.get(),
|
||||||
|
combineLatest(queue: .mainQueue(), featured.get(), archivedPromise.get()),
|
||||||
|
context.sharedContext.accountManager.sharedData(keys: [ApplicationSpecificSharedDataKeys.stickerSettings]))
|
||||||
|> deliverOnMainQueue
|
|> deliverOnMainQueue
|
||||||
|> map { presentationData, state, view, featuredAndArchived, sharedData -> (ItemListControllerState, (ItemListNodeState, Any)) in
|
|> map { presentationData, state, view, temporaryPackOrder, featuredAndArchived, sharedData -> (ItemListControllerState, (ItemListNodeState, Any)) in
|
||||||
var stickerSettings = StickerSettings.defaultSettings
|
var stickerSettings = StickerSettings.defaultSettings
|
||||||
if let value = sharedData.entries[ApplicationSpecificSharedDataKeys.stickerSettings] as? StickerSettings {
|
if let value = sharedData.entries[ApplicationSpecificSharedDataKeys.stickerSettings] as? StickerSettings {
|
||||||
stickerSettings = value
|
stickerSettings = value
|
||||||
@ -632,7 +660,7 @@ public func installedStickerPacksController(context: AccountContext, mode: Insta
|
|||||||
|
|
||||||
let controllerState = ItemListControllerState(theme: presentationData.theme, title: .text(title), leftNavigationButton: leftNavigationButton, rightNavigationButton: rightNavigationButton, backNavigationButton: ItemListBackButton(title: presentationData.strings.Common_Back), animateChanges: true)
|
let controllerState = ItemListControllerState(theme: presentationData.theme, title: .text(title), leftNavigationButton: leftNavigationButton, rightNavigationButton: rightNavigationButton, backNavigationButton: ItemListBackButton(title: presentationData.strings.Common_Back), animateChanges: true)
|
||||||
|
|
||||||
let listState = ItemListNodeState(entries: installedStickerPacksControllerEntries(presentationData: presentationData, state: state, mode: mode, view: view, featured: featuredAndArchived.0, archived: featuredAndArchived.1, stickerSettings: stickerSettings), style: .blocks, ensureVisibleItemTag: focusOnItemTag, animateChanges: previous != nil && packCount != nil && (previous! != 0 && previous! >= packCount! - 10))
|
let listState = ItemListNodeState(entries: installedStickerPacksControllerEntries(presentationData: presentationData, state: state, mode: mode, view: view, temporaryPackOrder: temporaryPackOrder, featured: featuredAndArchived.0, archived: featuredAndArchived.1, stickerSettings: stickerSettings), style: .blocks, ensureVisibleItemTag: focusOnItemTag, animateChanges: previous != nil && packCount != nil && (previous! != 0 && previous! >= packCount! - 10))
|
||||||
return (controllerState, (listState, arguments))
|
return (controllerState, (listState, arguments))
|
||||||
}
|
}
|
||||||
|> afterDisposed {
|
|> afterDisposed {
|
||||||
@ -666,42 +694,81 @@ public func installedStickerPacksController(context: AccountContext, mode: Insta
|
|||||||
afterAll = true
|
afterAll = true
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = (context.account.postbox.transaction { transaction -> Void in
|
var currentIds: [ItemCollectionId] = []
|
||||||
var infos = transaction.getItemCollectionsInfos(namespace: namespaceForMode(mode))
|
for entry in entries {
|
||||||
var reorderInfo: ItemCollectionInfo?
|
switch entry {
|
||||||
for i in 0 ..< infos.count {
|
case let .pack(pack):
|
||||||
if infos[i].0 == fromPackInfo.id {
|
currentIds.append(pack.3.id)
|
||||||
reorderInfo = infos[i].1
|
default:
|
||||||
infos.remove(at: i)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let reorderInfo = reorderInfo {
|
|
||||||
|
for i in 0 ..< currentIds.count {
|
||||||
|
if currentIds[i] == fromPackInfo.id {
|
||||||
|
currentIds.remove(at: i)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let referenceId = referenceId {
|
if let referenceId = referenceId {
|
||||||
var inserted = false
|
var inserted = false
|
||||||
for i in 0 ..< infos.count {
|
for i in 0 ..< currentIds.count {
|
||||||
if infos[i].0 == referenceId {
|
if currentIds[i] == referenceId {
|
||||||
if fromIndex < toIndex {
|
if fromIndex < toIndex {
|
||||||
infos.insert((fromPackInfo.id, reorderInfo), at: i + 1)
|
currentIds.insert(fromPackInfo.id, at: i + 1)
|
||||||
} else {
|
} else {
|
||||||
infos.insert((fromPackInfo.id, reorderInfo), at: i)
|
currentIds.insert(fromPackInfo.id, at: i)
|
||||||
}
|
}
|
||||||
inserted = true
|
inserted = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !inserted {
|
if !inserted {
|
||||||
infos.append((fromPackInfo.id, reorderInfo))
|
currentIds.append(fromPackInfo.id)
|
||||||
}
|
}
|
||||||
} else if beforeAll {
|
} else if beforeAll {
|
||||||
infos.insert((fromPackInfo.id, reorderInfo), at: 0)
|
currentIds.insert(fromPackInfo.id, at: 0)
|
||||||
} else if afterAll {
|
} else if afterAll {
|
||||||
infos.append((fromPackInfo.id, reorderInfo))
|
currentIds.append(fromPackInfo.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
temporaryPackOrder.set(.single(currentIds))
|
||||||
|
})
|
||||||
|
|
||||||
|
controller.setReorderCompleted({ (entries: [InstalledStickerPacksEntry]) -> Void in
|
||||||
|
var currentIds: [ItemCollectionId] = []
|
||||||
|
for entry in entries {
|
||||||
|
switch entry {
|
||||||
|
case let .pack(pack):
|
||||||
|
currentIds.append(pack.3.id)
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = (context.account.postbox.transaction { transaction -> Void in
|
||||||
|
var infos = transaction.getItemCollectionsInfos(namespace: namespaceForMode(mode))
|
||||||
|
|
||||||
|
var packDict: [ItemCollectionId: Int] = [:]
|
||||||
|
for i in 0 ..< infos.count {
|
||||||
|
packDict[infos[i].0] = i
|
||||||
|
}
|
||||||
|
var tempSortedPacks: [(ItemCollectionId, ItemCollectionInfo)] = []
|
||||||
|
var processedPacks = Set<ItemCollectionId>()
|
||||||
|
for id in currentIds {
|
||||||
|
if let index = packDict[id] {
|
||||||
|
tempSortedPacks.append(infos[index])
|
||||||
|
processedPacks.insert(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let restPacks = infos.filter { !processedPacks.contains($0.0) }
|
||||||
|
let sortedPacks = restPacks + tempSortedPacks
|
||||||
addSynchronizeInstalledStickerPacksOperation(transaction: transaction, namespace: namespaceForMode(mode), content: .sync)
|
addSynchronizeInstalledStickerPacksOperation(transaction: transaction, namespace: namespaceForMode(mode), content: .sync)
|
||||||
transaction.replaceItemCollectionInfos(namespace: namespaceForMode(mode), itemCollectionInfos: infos)
|
transaction.replaceItemCollectionInfos(namespace: namespaceForMode(mode), itemCollectionInfos: sortedPacks)
|
||||||
}
|
}
|
||||||
}).start()
|
|> deliverOnMainQueue).start(completed: {
|
||||||
|
temporaryPackOrder.set(.single(nil))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
presentControllerImpl = { [weak controller] c, p in
|
presentControllerImpl = { [weak controller] c, p in
|
||||||
|
Loading…
x
Reference in New Issue
Block a user