2024-08-14 13:48:30 +00:00
|
|
|
import Collections
|
|
|
|
import Combine
|
2024-08-13 08:40:27 +00:00
|
|
|
import Foundation
|
2024-08-14 13:48:30 +00:00
|
|
|
import GRDB
|
2024-08-13 08:40:27 +00:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
final class ConversationStore: ObservableObject {
|
2024-08-14 13:48:30 +00:00
|
|
|
@Published private(set) var messages: Deque<Message> = []
|
|
|
|
private(set) var roster: Roster
|
2024-08-13 08:40:27 +00:00
|
|
|
|
|
|
|
private let client: Client
|
2024-08-14 13:48:30 +00:00
|
|
|
private let blockSize = Const.messagesPageSize
|
|
|
|
private let messagesMax = Const.messagesMaxSize
|
|
|
|
|
2024-08-15 11:37:21 +00:00
|
|
|
private var messagesCancellable: AnyCancellable?
|
2024-08-13 08:40:27 +00:00
|
|
|
|
|
|
|
init(roster: Roster, client: Client) {
|
|
|
|
self.client = client
|
|
|
|
self.roster = roster
|
2024-08-14 13:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ConversationStore {
|
|
|
|
func loadMoreBackward() async {
|
2024-08-15 11:37:21 +00:00
|
|
|
let earliestDate = messages.last?.date ?? Date()
|
|
|
|
resubscribe(.before(earliestDate))
|
|
|
|
// let fetchedMessages = await fetchBlock(earliestDate, nil)
|
|
|
|
// messages.append(contentsOf: fetchedMessages)
|
|
|
|
// if messages.count > messagesMax {
|
|
|
|
// messages.removeFirst(messages.count - messagesMax)
|
|
|
|
// }
|
|
|
|
|
2024-08-14 13:48:30 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 11:37:21 +00:00
|
|
|
private extension ConversationStore {
|
|
|
|
enum FetchDirection {
|
|
|
|
case before(Date)
|
|
|
|
case after(Date)
|
2024-08-14 13:48:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 11:37:21 +00:00
|
|
|
func resubscribe(_ side: FetchDirection) {
|
|
|
|
switch side {
|
|
|
|
case .before(let date):
|
|
|
|
messagesCancellable = ValueObservation.tracking(Message
|
|
|
|
.filter(
|
|
|
|
(Column("to") == roster.bareJid && Column("from") == roster.contactBareJid) ||
|
|
|
|
(Column("from") == roster.bareJid && Column("to") == roster.contactBareJid)
|
|
|
|
)
|
|
|
|
.filter(Column("date") < date)
|
|
|
|
.limit(blockSize)
|
|
|
|
.order(Column("date").desc)
|
|
|
|
.fetchAll
|
|
|
|
)
|
|
|
|
.publisher(in: Database.shared.dbQueue, scheduling: .immediate)
|
|
|
|
.sink { _ in
|
|
|
|
} receiveValue: { [weak self] messages in
|
|
|
|
self?.processFetched(messages, side)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .after(let date):
|
|
|
|
messagesCancellable = ValueObservation.tracking(Message
|
|
|
|
.filter(
|
|
|
|
(Column("to") == roster.bareJid && Column("from") == roster.contactBareJid) ||
|
|
|
|
(Column("from") == roster.bareJid && Column("to") == roster.contactBareJid)
|
|
|
|
)
|
|
|
|
.filter(Column("date") > date)
|
|
|
|
.limit(blockSize)
|
|
|
|
.order(Column("date").desc)
|
|
|
|
.fetchAll
|
|
|
|
)
|
|
|
|
.publisher(in: Database.shared.dbQueue, scheduling: .immediate)
|
|
|
|
.sink { _ in
|
|
|
|
} receiveValue: { [weak self] messages in
|
|
|
|
self?.processFetched(messages, side)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processFetched(_ messages: [Message], _ side: FetchDirection) {
|
|
|
|
switch side {
|
|
|
|
case .before:
|
|
|
|
self.messages.append(contentsOf: messages)
|
|
|
|
|
|
|
|
case .after:
|
|
|
|
self.messages.insert(contentsOf: messages, at: 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
Task {
|
|
|
|
await processAttachments(messages)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processAttachments(_ messages: [Message]) async {
|
|
|
|
// load attachment here
|
|
|
|
print(messages.count)
|
2024-08-13 08:40:27 +00:00
|
|
|
}
|
|
|
|
}
|