iOS-聊天框架聊天删除



我一直在使用Chatto框架在聊天应用程序中工作。一切正常。现在我想从SlidingDataSource类中删除DataSource聊天消息。数组的索引使用一些属性(如windowOffsetitemsOffsetwindowCount(以及数组的计数进行处理。如果有人在Chatto中使用过删除操作,请解释属性或提供简单的删除功能会有所帮助。 谢谢。我在这里附加数据源代码。.

import Foundation
import Chatto
public enum InsertPosition {
case top
case bottom
}
public class SlidingDataSource<Element> {
private var pageSize: Int
private var windowOffset: Int
private var windowCount: Int
private var itemGenerator: (() -> Element)?
private var items = [Element]()
private var itemsOffset: Int
public var itemsInWindow: [Element] {
let offset = self.windowOffset - self.itemsOffset
return Array(items[offset..<offset+self.windowCount])
}
public init(count: Int, pageSize: Int, itemGenerator: (() -> Element)?) {
self.windowOffset = count
self.itemsOffset = count
self.windowCount = 0
self.pageSize = pageSize
self.itemGenerator = itemGenerator
self.generateItems(min(pageSize, count), position: .top)
}
public convenience init(items: [Element], pageSize: Int) {
var iterator = items.makeIterator()
self.init(count: items.count, pageSize: pageSize) { iterator.next()! }
}
private func generateItems(_ count: Int, position: InsertPosition) {
// swiftlint:disable:next empty_count
guard count > 0 else { return }
guard let itemGenerator = self.itemGenerator else {
fatalError("Can't create messages without a generator")
}
for _ in 0..<count {
self.insertItem(itemGenerator(), position: .top)
}
}
public func insertItem(_ item: Element, position: InsertPosition) {
if position == .top {
self.items.insert(item, at: 0)
let shouldExpandWindow = self.itemsOffset == self.windowOffset
self.itemsOffset -= 1
if shouldExpandWindow {
self.windowOffset -= 1
self.windowCount += 1
}
} else {
let shouldExpandWindow = self.itemsOffset + self.items.count == self.windowOffset + self.windowCount
if shouldExpandWindow {
self.windowCount += 1
}
self.items.append(item)
}
}
public func hasPrevious() -> Bool {
return self.windowOffset > 0
}
public func hasMore() -> Bool {
return self.windowOffset + self.windowCount < self.itemsOffset + self.items.count
}
public func loadPrevious() {
let previousWindowOffset = self.windowOffset
let previousWindowCount = self.windowCount
let nextWindowOffset = max(0, self.windowOffset - self.pageSize)
let messagesNeeded = self.itemsOffset - nextWindowOffset
if messagesNeeded > 0 {
self.generateItems(messagesNeeded, position: .top)
}
let newItemsCount = previousWindowOffset - nextWindowOffset
self.windowOffset = nextWindowOffset
self.windowCount = previousWindowCount + newItemsCount
}
public func loadNext() {
guard !self.items.isEmpty else { return }
let itemCountAfterWindow = self.itemsOffset + self.items.count - self.windowOffset - self.windowCount
self.windowCount += min(self.pageSize, itemCountAfterWindow)
}
@discardableResult
public func adjustWindow(focusPosition: Double, maxWindowSize: Int) -> Bool {
assert(0 <= focusPosition && focusPosition <= 1, "")
guard 0 <= focusPosition && focusPosition <= 1 else {
assert(false, "focus should be in the [0, 1] interval")
return false
}
let sizeDiff = self.windowCount - maxWindowSize
guard sizeDiff > 0 else { return false }
self.windowOffset +=  Int(focusPosition * Double(sizeDiff))
self.windowCount = maxWindowSize
return true
}
@discardableResult
func replaceItem(withNewItem item: Element, where predicate: (Element) -> Bool) -> Bool {
guard let index = self.items.firstIndex(where: predicate) else { return false }
self.items[index] = item
return true
}
func contains(item: Element, where predicate: (Element) -> Bool ) -> Bool {
guard let _ = self.items.firstIndex(where: predicate) else { return false }
return true
}
//FIXME: Add delete method here, don't know the use off itemOffset, windowOffset, windowCount
//    func delete(item: Element, where predicate: (Element) -> Bool ) {
//        self.items.removeAll(where: predicate)
//    }
}

我在他们的Github存储库中提出了一个问题。这是他们回答的解决方案。

when delete or add a new element in data source you need to -1 or +1 to offset. If offset is 0 - do nothing.

原始答案链接

最新更新