another.im-ios/ConversationsClassic/AppData/Store/ConversationStore.swift
2024-08-14 15:48:30 +02:00

58 lines
1.7 KiB
Swift

import Collections
import Combine
import Foundation
import GRDB
@MainActor
final class ConversationStore: ObservableObject {
@Published private(set) var messages: Deque<Message> = []
private(set) var roster: Roster
private let client: Client
private let blockSize = Const.messagesPageSize
private let messagesMax = Const.messagesMaxSize
private var messagesObservation: AnyDatabaseCancellable
init(roster: Roster, client: Client) {
self.client = client
self.roster = roster
// observe change messages in database
messagesObservation = DatabaseRegionObservation(tracking: Message.all()).start(in: Database.shared.dbQueue) { _ in
// Handle error
} onChange: { _ in
print("Messages were changed")
}
}
}
extension ConversationStore {
func loadMoreBackward() async {
// guard let lastMessage = messages.last else { return }
// let messages = await fetchBlock(lastMessage.date, nil)
// self.messages.append(contentsOf: messages)
}
func loadMoreForward() async {
// guard let firstMessage = messages.first else { return }
// let messages = await fetchBlock(nil, firstMessage.date)
// self.messages.insert(contentsOf: messages, at: 0)
}
}
extension ConversationStore {
private func fetchBlock(_ beforeDate: Date?, _ afterDate: Date?) async -> [Message] {
print(beforeDate, afterDate)
return []
// let messages = await client.fetchMessages()
// self.messages.append(contentsOf: messages)
}
private func cutMessages() {
// if messages.count > messagesMax {
// messages.removeFirst(messages.count - messagesMax)
// }
}
}