首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS- Chatto框架聊天删除

iOS- Chatto框架聊天删除
EN

Stack Overflow用户
提问于 2020-01-14 14:36:25
回答 1查看 74关注 0票数 0

我一直在使用Chatto框架在聊天应用程序中工作。一切都很好。现在我想从SlidingDataSource类的DataSource中删除一条聊天消息。数组的索引由windowOffsetitemsOffsetwindowCount等属性以及数组的计数处理。如果有人在Chatto中使用过delete操作,请解释一下属性或提供一个简单的delete函数会很有帮助。谢谢。我在这里附上了数据源代码..

代码语言:javascript
运行
复制
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)
    //    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-24 13:14:40

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

代码语言:javascript
运行
复制
when delete or add a new element in data source you need to -1 or +1 to offset. If offset is 0 - do nothing.

Originial answer link

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59728618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档