mv-experiment #1

Merged
fmodf merged 88 commits from mv-experiment into develop 2024-09-03 15:13:59 +00:00
6 changed files with 47 additions and 135 deletions
Showing only changes of commit cb9b21fc32 - Show all commits

View file

@ -36,7 +36,7 @@ private actor ArchiveMessageProcessor {
func append(_ msg: ArchMsg) async { func append(_ msg: ArchMsg) async {
accumulator.append(msg) accumulator.append(msg)
if accumulator.count >= Const.mamRequestLimit { if accumulator.count >= Const.mamRequestPageSize {
await process() await process()
} }
} }

View file

@ -218,19 +218,19 @@ private extension AttachmentsStore {
if attachment.localPath != nil, attachment.remotePath == nil { if attachment.localPath != nil, attachment.remotePath == nil {
// Uploading // Uploading
self?.processing.insert(message.id) self?.processing.insert(message.id)
Task(priority: .background) { Task {
await self?.uploadAttachment(message) await self?.uploadAttachment(message)
} }
} else if attachment.localPath == nil, attachment.remotePath != nil { } else if attachment.localPath == nil, attachment.remotePath != nil {
// Downloading // Downloading
self?.processing.insert(message.id) self?.processing.insert(message.id)
Task(priority: .background) { Task {
await self?.downloadAttachment(message) await self?.downloadAttachment(message)
} }
} else if attachment.localPath != nil, attachment.remotePath != nil, attachment.thumbnailName == nil, attachment.type == .image { } else if attachment.localPath != nil, attachment.remotePath != nil, attachment.thumbnailName == nil, attachment.type == .image {
// Generate thumbnail // Generate thumbnail
self?.processing.insert(message.id) self?.processing.insert(message.id)
Task(priority: .background) { Task {
await self?.generateThumbnail(message) await self?.generateThumbnail(message)
} }
} }

View file

@ -74,18 +74,18 @@ private extension MessagesStore {
} }
} }
// MARK: - Fetch archived messages // MARK: - Archived messages
extension MessagesStore { extension MessagesStore {
func fetchForward() { func scrolledMessage(_ messageId: String) {
if messageId == messages.last?.id {
Task {
await archiver.fetchBackward(roster, client)
}
} else if messageId == messages.first?.id {
Task { Task {
await archiver.fetchForward(roster, client) await archiver.fetchForward(roster, client)
} }
} }
func fetchBackward() {
Task {
await archiver.fetchBackward(roster, client)
}
} }
} }
@ -93,142 +93,57 @@ private actor ArchiveMessageFetcher {
private var initFetchStarted = false private var initFetchStarted = false
private var forwardRsm: RSM.Query? private var forwardRsm: RSM.Query?
private var backwardRsm: RSM.Query? private var backwardRsm: RSM.Query?
private var fetchInProgress = false
func initialFetch(_ messages: [Message], _ roster: Roster, _ client: Client) async { func initialFetch(_ messages: [Message], _ roster: Roster, _ client: Client) async {
if initFetchStarted { return } if initFetchStarted { return }
initFetchStarted = true initFetchStarted = true
fetchInProgress = true
do { do {
if let firstExistId = messages.first?.id { if let firstExistId = messages.first?.id {
let result = try await client.fetchArchiveMessages(for: roster, query: .init(before: firstExistId, max: Const.mamRequestLimit)) let result = try await client.fetchArchiveMessages(for: roster, query: .init(before: firstExistId, max: Const.mamRequestPageSize))
result.complete ? forwardRsm = nil : (forwardRsm = .init(after: result.rsm?.last, max: Const.mamRequestLimit)) result.complete ? forwardRsm = nil : (forwardRsm = .init(after: result.rsm?.last, max: Const.mamRequestPageSize))
result.complete ? backwardRsm = nil : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit)) result.complete ? backwardRsm = nil : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestPageSize))
} else { } else {
let result = try await client.fetchArchiveMessages(for: roster, query: .init(lastItems: Const.mamRequestLimit)) let result = try await client.fetchArchiveMessages(for: roster, query: .init(lastItems: Const.mamRequestPageSize))
result.complete ? backwardRsm = nil : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit)) result.complete ? backwardRsm = nil : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestPageSize))
} }
} catch { } catch {
logIt(.error, "Error requesting archived messages: \(error)") logIt(.error, "Error requesting archived messages: \(error)")
initFetchStarted = false initFetchStarted = false
} }
fetchInProgress = false
} }
func fetchForward(_ roster: Roster, _ client: Client) { func fetchForward(_ roster: Roster, _ client: Client) async {
while !initFetchStarted {
await Task.yield()
}
guard let rsm = forwardRsm else { return } guard let rsm = forwardRsm else { return }
if fetchInProgress { return }
fetchInProgress = true
Task { Task {
let result = try await client.fetchArchiveMessages(for: roster, query: rsm) let result = try await client.fetchArchiveMessages(for: roster, query: rsm)
result.complete ? (forwardRsm = nil) : (forwardRsm = .init(after: result.rsm?.last, max: Const.mamRequestLimit)) result.complete ? (forwardRsm = nil) : (forwardRsm = .init(after: result.rsm?.last, max: Const.mamRequestPageSize))
fetchInProgress = false
} }
} }
func fetchBackward(_ roster: Roster, _ client: Client) { func fetchBackward(_ roster: Roster, _ client: Client) async {
while !initFetchStarted {
await Task.yield()
}
guard let rsm = backwardRsm else { return } guard let rsm = backwardRsm else { return }
if fetchInProgress { return }
fetchInProgress = true
Task { Task {
let result = try await client.fetchArchiveMessages(for: roster, query: rsm) let result = try await client.fetchArchiveMessages(for: roster, query: rsm)
result.complete ? (backwardRsm = nil) : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit)) result.complete ? (backwardRsm = nil) : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestPageSize))
fetchInProgress = false
} }
} }
// func fetchBackward(_ roster: Roster, _ client: Client) {
// guard let rsm = backwardRsm else { return }
// Task {
// let result = try await client.fetchArchiveMessages(for: roster, query: rsm)
// result.complete ? (backwardRsm = nil) : (backwardRsm = .init(before: result.rsm?.first, max: Const.mamRequestLimit))
// }
// }
} }
// MARK: - Archived messages
// extension MessagesStore {
// func requestEarliestArchivedMessages() {
// guard let beforeId = messages.first?.id else { return }
// Task {
// await archiveMessageFetcher.fetchAfterMessages(roster, client, afterId: beforeId)
// // await archiveMessageFetcher.fetchBeforeMessages(roster, client, beforeId: beforeId)
// }
// }
//
// func requestLatestArchivedMessages() {
// guard let afterId = messages.last?.id else { return }
// Task {
// await archiveMessageFetcher.fetchBeforeMessages(roster, client, beforeId: afterId)
// // await archiveMessageFetcher.fetchAfterMessages(roster, client, afterId: afterId)
// }
// }
//
// private func requestLastArchivedMessages() {
// Task {
// await archiveMessageFetcher.fetchLastMessages(roster, client)
// }
// }
// }
// private actor ArchiveMessageFetcher {
// private var afterAvailable = true
// private var beforeAvailable = true
// private var isFetching = false
// private var fetchingIsPossinle = true
//
// func fetchLastMessages(_ roster: Roster, _ client: Client) async {
// if !fetchingIsPossinle { return }
// while isFetching {
// await Task.yield()
// }
// isFetching = true
//
// let query: RSM.Query = .init(lastItems: Const.mamRequestLimit)
// do {
// _ = try await client.fetchArchiveMessages(for: roster, query: query)
// } catch AppError.featureNotSupported {
// fetchingIsPossinle = false
// } catch {
// logIt(.error, "Error requesting archived messages: \(error)")
// }
//
// isFetching = false
// }
//
// func fetchBeforeMessages(_ roster: Roster, _ client: Client, beforeId: String) async {
// if !fetchingIsPossinle || !beforeAvailable { return }
// while isFetching {
// await Task.yield()
// }
// isFetching = true
//
// let query: RSM.Query = .init(before: beforeId, max: Const.mamRequestLimit)
// do {
// let result = try await client.fetchArchiveMessages(for: roster, query: query)
// if result.complete {
// beforeAvailable = false
// }
// } catch AppError.featureNotSupported {
// fetchingIsPossinle = false
// } catch {
// logIt(.error, "Error requesting archived messages: \(error)")
// }
//
// isFetching = false
// }
//
// func fetchAfterMessages(_ roster: Roster, _ client: Client, afterId: String) async {
// if !fetchingIsPossinle || !afterAvailable { return }
// while isFetching {
// await Task.yield()
// }
// isFetching = true
//
// let query: RSM.Query = .init(after: afterId, max: Const.mamRequestLimit)
// do {
// let result = try await client.fetchArchiveMessages(for: roster, query: query)
// if result.complete {
// afterAvailable = false
// }
// } catch AppError.featureNotSupported {
// fetchingIsPossinle = false
// } catch {
// logIt(.error, "Error requesting archived messages: \(error)")
// }
//
// isFetching = false
// }
// }

View file

@ -44,5 +44,5 @@ enum Const {
static let attachmentPreviewSize = UIScreen.main.bounds.width * 0.5 static let attachmentPreviewSize = UIScreen.main.bounds.width * 0.5
// MAM request page size // MAM request page size
static let mamRequestLimit = 10 static let mamRequestPageSize = 50
} }

View file

@ -75,7 +75,10 @@ struct LoginScreen: View {
) )
Button { Button {
Task { router.showModal {
LoadingScreen()
}
Task(priority: .background) {
await tryLogin() await tryLogin()
} }
} label: { } label: {
@ -102,10 +105,6 @@ struct LoginScreen: View {
} }
private func tryLogin() async { private func tryLogin() async {
router.showModal {
LoadingScreen()
}
defer { defer {
router.dismissModal() router.dismissModal()
} }

View file

@ -45,9 +45,7 @@ struct ConversationScreen: View {
firstIsVisible = true firstIsVisible = true
autoScroll = true autoScroll = true
} }
if message.id == messages.last?.id { messagesStore.scrolledMessage(message.id)
messagesStore.fetchBackward()
}
} }
.onDisappear { .onDisappear {
if message.id == messages.first?.id { if message.id == messages.first?.id {