wip
This commit is contained in:
parent
c7aaac02b8
commit
da9ba6ba0f
|
@ -39,6 +39,8 @@ final class ClientMartinMessagesManager {
|
||||||
var contentType: MessageContentType = .text
|
var contentType: MessageContentType = .text
|
||||||
if message.hints.contains(.noStore) {
|
if message.hints.contains(.noStore) {
|
||||||
contentType = .typing
|
contentType = .typing
|
||||||
|
// for now just skip it
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// From/To
|
// From/To
|
||||||
|
|
|
@ -12,23 +12,24 @@ final class ConversationStore: ObservableObject {
|
||||||
private let blockSize = Const.messagesPageSize
|
private let blockSize = Const.messagesPageSize
|
||||||
private let messagesMax = Const.messagesMaxSize
|
private let messagesMax = Const.messagesMaxSize
|
||||||
|
|
||||||
private var messagesObservation: AnyDatabaseCancellable
|
private var messagesCancellable: AnyCancellable?
|
||||||
|
|
||||||
init(roster: Roster, client: Client) {
|
init(roster: Roster, client: Client) {
|
||||||
self.client = client
|
self.client = client
|
||||||
self.roster = roster
|
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 {
|
extension ConversationStore {
|
||||||
func loadMoreBackward() async {
|
func loadMoreBackward() async {
|
||||||
|
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)
|
||||||
|
// }
|
||||||
|
|
||||||
// guard let lastMessage = messages.last else { return }
|
// guard let lastMessage = messages.last else { return }
|
||||||
// let messages = await fetchBlock(lastMessage.date, nil)
|
// let messages = await fetchBlock(lastMessage.date, nil)
|
||||||
// self.messages.append(contentsOf: messages)
|
// self.messages.append(contentsOf: messages)
|
||||||
|
@ -41,17 +42,66 @@ extension ConversationStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ConversationStore {
|
private extension ConversationStore {
|
||||||
private func fetchBlock(_ beforeDate: Date?, _ afterDate: Date?) async -> [Message] {
|
enum FetchDirection {
|
||||||
print(beforeDate, afterDate)
|
case before(Date)
|
||||||
return []
|
case after(Date)
|
||||||
// let messages = await client.fetchMessages()
|
|
||||||
// self.messages.append(contentsOf: messages)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func cutMessages() {
|
func resubscribe(_ side: FetchDirection) {
|
||||||
// if messages.count > messagesMax {
|
switch side {
|
||||||
// messages.removeFirst(messages.count - messagesMax)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,6 +101,9 @@ struct ConversationScreen: View {
|
||||||
.safeAreaInset(edge: .bottom, spacing: 0) {
|
.safeAreaInset(edge: .bottom, spacing: 0) {
|
||||||
ConversationTextInput(autoScroll: $autoScroll)
|
ConversationTextInput(autoScroll: $autoScroll)
|
||||||
}
|
}
|
||||||
|
.task {
|
||||||
|
await conversation.loadMoreBackward()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue