Various fixes

This commit is contained in:
Ilya Laktyushin 2025-08-24 17:39:47 +04:00
parent 83ef360953
commit 5749548ac8
4 changed files with 108 additions and 42 deletions

View File

@ -669,12 +669,14 @@ open class ListView: ASDisplayNode, ASScrollViewDelegate, ASGestureRecognizerDel
private func updateReordering(offset: CGFloat) { private func updateReordering(offset: CGFloat) {
if let reorderNode = self.reorderNode { if let reorderNode = self.reorderNode {
let updatedLocation = reorderNode.initialLocation.y + offset if !self.autoScrollWhenReordering, case let .known(contentOffset) = self.visibleContentOffset() {
if updatedLocation < self.insets.top { let updatedLocation = reorderNode.initialLocation.y + offset
} else { if updatedLocation < self.insets.top - contentOffset {
reorderNode.updateOffset(offset: offset) return
self.checkItemReordering() }
} }
reorderNode.updateOffset(offset: offset)
self.checkItemReordering()
} }
} }
@ -1525,10 +1527,14 @@ open class ListView: ASDisplayNode, ASScrollViewDelegate, ASGestureRecognizerDel
topItemFound = true topItemFound = true
} }
if !topItemFound, self.stackFromBottom && !self.autoScrollWhenReordering, let itemNode = self.itemNodes.first, itemNode.apparentFrame.minY > 0.0 {
topItemFound = true
}
var topOffset: CGFloat var topOffset: CGFloat
if topItemFound { if topItemFound {
let realTopItemEdge = itemNodes.first!.apparentFrame.origin.y let realTopItemEdge = self.itemNodes.first!.apparentFrame.origin.y
let realTopItemEdgeOffset = max(0.0, realTopItemEdge) let realTopItemEdgeOffset = max(0.0, realTopItemEdge)
topOffset = realTopItemEdgeOffset topOffset = realTopItemEdgeOffset

View File

@ -818,7 +818,7 @@ struct ListViewState {
} }
operations.append(.Remove(index: index, offsetDirection: offsetDirection)) operations.append(.Remove(index: index, offsetDirection: offsetDirection))
if let referenceNode = referenceNode , animated { if let referenceNode = referenceNode, animated {
self.nodes.insert(.Placeholder(frame: nodeFrame), at: index) self.nodes.insert(.Placeholder(frame: nodeFrame), at: index)
operations.append(.InsertDisappearingPlaceholder(index: index, referenceNode: referenceNode, offsetDirection: offsetDirection.inverted())) operations.append(.InsertDisappearingPlaceholder(index: index, referenceNode: referenceNode, offsetDirection: offsetDirection.inverted()))
} else { } else {

View File

@ -127,13 +127,39 @@ func _internal_addSavedMusic(account: Account, file: FileMediaReference, afterFi
return account.postbox.transaction { transaction in return account.postbox.transaction { transaction in
if let cachedSavedMusic = transaction.retrieveItemCacheEntry(id: entryId(peerId: account.peerId))?.get(CachedProfileSavedMusic.self) { if let cachedSavedMusic = transaction.retrieveItemCacheEntry(id: entryId(peerId: account.peerId))?.get(CachedProfileSavedMusic.self) {
var updatedFiles = cachedSavedMusic.files var updatedFiles = cachedSavedMusic.files
updatedFiles.removeAll(where: { $0.fileId == file.media.fileId }) var updatedCount = cachedSavedMusic.count
if let afterFile, let index = updatedFiles.firstIndex(where: { $0.fileId == afterFile.media.fileId }) {
updatedFiles.insert(file.media, at: index + 1) if let fromIndex = updatedFiles.firstIndex(where: { $0.fileId == file.media.fileId }) {
let anchorIdxOpt: Int? = afterFile.flatMap { af in
updatedFiles.firstIndex(where: { $0.fileId == af.media.fileId })
}
updatedFiles.remove(at: fromIndex)
let insertIndex: Int
if let anchorIndex = anchorIdxOpt {
if anchorIndex == fromIndex {
insertIndex = min(fromIndex + 1, updatedFiles.count)
} else {
let adjustedAnchor = anchorIndex > fromIndex ? (anchorIndex - 1) : anchorIndex
insertIndex = updatedFiles.index(after: adjustedAnchor)
}
} else if afterFile != nil {
insertIndex = 0
} else {
insertIndex = 0
}
updatedFiles.insert(file.media, at: insertIndex)
} else { } else {
updatedFiles.insert(file.media, at: 0) if let afterFile, let anchor = updatedFiles.firstIndex(where: { $0.fileId == afterFile.media.fileId }) {
updatedFiles.insert(file.media, at: updatedFiles.index(after: anchor))
} else if afterFile != nil {
updatedFiles.append(file.media)
} else {
updatedFiles.insert(file.media, at: 0)
}
updatedCount = updatedCount + 1
} }
let updatedCount = max(0, cachedSavedMusic.count + 1)
if let entry = CodableEntry(CachedProfileSavedMusic(files: updatedFiles, count: updatedCount)) { if let entry = CodableEntry(CachedProfileSavedMusic(files: updatedFiles, count: updatedCount)) {
transaction.putItemCacheEntry(id: entryId(peerId: account.peerId), entry: entry) transaction.putItemCacheEntry(id: entryId(peerId: account.peerId), entry: entry)
} }
@ -371,15 +397,44 @@ public final class ProfileSavedMusicContext {
} }
public func addMusic(file: FileMediaReference, afterFile: FileMediaReference? = nil, apply: Bool = true) -> Signal<Never, AddSavedMusicError> { public func addMusic(file: FileMediaReference, afterFile: FileMediaReference? = nil, apply: Bool = true) -> Signal<Never, AddSavedMusicError> {
self.files.removeAll(where: { $0.fileId == file.media.fileId }) var updatedFiles = self.files
if let afterFile, let index = self.files.firstIndex(where: { $0.fileId == afterFile.media.fileId }) {
self.files.insert(file.media, at: index + 1) let fromIdx = updatedFiles.firstIndex { $0.fileId == file.media.fileId }
let anchorIdxOpt = afterFile.flatMap { af in
updatedFiles.firstIndex { $0.fileId == af.media.fileId }
}
if let fromIdx = fromIdx {
updatedFiles.remove(at: fromIdx)
let insertIdx: Int
if let anchorIdx = anchorIdxOpt {
if anchorIdx == fromIdx {
insertIdx = min(fromIdx + 1, updatedFiles.count)
} else {
let adjustedAnchor = anchorIdx > fromIdx ? (anchorIdx - 1) : anchorIdx
insertIdx = updatedFiles.index(after: adjustedAnchor)
}
} else if afterFile != nil {
insertIdx = updatedFiles.count
} else {
insertIdx = 0
}
updatedFiles.insert(file.media, at: insertIdx)
} else { } else {
self.files.insert(file.media, at: 0) if let anchorIdx = anchorIdxOpt {
} updatedFiles.insert(file.media, at: updatedFiles.index(after: anchorIdx))
if let count = self.count { } else if afterFile != nil {
self.count = count + 1 updatedFiles.append(file.media)
} else {
updatedFiles.insert(file.media, at: 0)
}
if let count = self.count {
self.count = count + 1
}
} }
self.files = updatedFiles
self.pushState() self.pushState()
if apply { if apply {

View File

@ -268,6 +268,7 @@ final class OverlayAudioPlayerControllerNode: ViewControllerTracingNode, ASGestu
self.historyNode.preloadPages = true self.historyNode.preloadPages = true
self.historyNode.stackFromBottom = true self.historyNode.stackFromBottom = true
self.historyNode.areContentAnimationsEnabled = true
self.historyNode.updateFloatingHeaderOffset = { [weak self] offset, transition in self.historyNode.updateFloatingHeaderOffset = { [weak self] offset, transition in
if let strongSelf = self { if let strongSelf = self {
strongSelf.updateFloatingHeaderOffset(offset: offset, transition: transition) strongSelf.updateFloatingHeaderOffset(offset: offset, transition: transition)
@ -461,40 +462,43 @@ final class OverlayAudioPlayerControllerNode: ViewControllerTracingNode, ASGestu
guard let self, let peer = peer.flatMap({ PeerReference($0._asPeer()) }) else { guard let self, let peer = peer.flatMap({ PeerReference($0._asPeer()) }) else {
return return
} }
self.historyNode.reorderItem = { fromIndex, toIndex, transactionOpaqueState -> Signal<Bool, NoError> in self.historyNode.reorderItem = { fromIndex, toIndex, transactionOpaqueState -> Signal<Bool, NoError> in
guard let filteredEntries = (transactionOpaqueState as? ChatHistoryTransactionOpaqueState)?.historyView.filteredEntries else { guard let filteredEntries = (transactionOpaqueState as? ChatHistoryTransactionOpaqueState)?.historyView.filteredEntries, !filteredEntries.isEmpty else {
return .single(false) return .single(false)
} }
let fromEntry = filteredEntries[filteredEntries.count - 1 - fromIndex]
let toEntry: ChatHistoryEntry? func mapIndex(_ uiIndex: Int) -> Int {
if toIndex == 0 { return filteredEntries.count - 1 - uiIndex
toEntry = nil }
let mappedFromIndex = mapIndex(fromIndex)
guard filteredEntries.indices.contains(mappedFromIndex), case let .MessageEntry(fromMessage, _, _, _, _, _) = filteredEntries[mappedFromIndex], let fromFile = fromMessage.media.first(where: { $0 is TelegramMediaFile }) as? TelegramMediaFile else {
return .single(false)
}
var afterFile: TelegramMediaFile?
if toIndex > 0 {
let afterMappedIndex = mapIndex(toIndex - 1)
if filteredEntries.indices.contains(afterMappedIndex), case let .MessageEntry(afterMessage, _, _, _, _, _) = filteredEntries[afterMappedIndex], let file = afterMessage.media.first(where: { $0 is TelegramMediaFile }) as? TelegramMediaFile {
afterFile = file
}
} else { } else {
toEntry = filteredEntries[filteredEntries.count - 1 - toIndex] afterFile = nil
}
guard case let .MessageEntry(fromMessage, _, _, _, _, _) = fromEntry else {
return .single(false)
}
guard let fromFile = fromMessage.media.first(where: { $0 is TelegramMediaFile }) as? TelegramMediaFile else {
return .single(false)
} }
var toFile: TelegramMediaFile? let _ = savedMusicContext.addMusic(
if let toEntry, case let .MessageEntry(toMessage, _, _, _, _, _) = toEntry, let file = toMessage.media.first(where: { $0 is TelegramMediaFile }) as? TelegramMediaFile { file: .savedMusic(peer: peer, media: fromFile),
toFile = file afterFile: afterFile.flatMap { .savedMusic(peer: peer, media: $0) }
} ).start()
if fromFile.id == toFile?.id {
return .single(false)
}
let _ = savedMusicContext.addMusic(file: .savedMusic(peer: peer, media: fromFile), afterFile: toFile.flatMap { .savedMusic(peer: peer, media: $0) }).start()
return .single(true) return .single(true)
} }
}) })
self.historyNode.useMainQueueTransactions = true
self.historyNode.autoScrollWhenReordering = false self.historyNode.autoScrollWhenReordering = false
} }
func updatePresentationData(_ presentationData: PresentationData) { func updatePresentationData(_ presentationData: PresentationData) {
self.presentationData = presentationData self.presentationData = presentationData
@ -849,6 +853,7 @@ final class OverlayAudioPlayerControllerNode: ViewControllerTracingNode, ASGestu
historyNode.clipsToBounds = true historyNode.clipsToBounds = true
historyNode.preloadPages = true historyNode.preloadPages = true
historyNode.stackFromBottom = true historyNode.stackFromBottom = true
historyNode.areContentAnimationsEnabled = true
historyNode.updateFloatingHeaderOffset = { [weak self] offset, _ in historyNode.updateFloatingHeaderOffset = { [weak self] offset, _ in
self?.replacementHistoryNodeFloatingOffset = offset self?.replacementHistoryNodeFloatingOffset = offset
} }