wip
This commit is contained in:
parent
29349296b0
commit
cb9b21fc32
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,17 +74,17 @@ private extension MessagesStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Fetch archived messages
|
// MARK: - Archived messages
|
||||||
extension MessagesStore {
|
extension MessagesStore {
|
||||||
func fetchForward() {
|
func scrolledMessage(_ messageId: String) {
|
||||||
Task {
|
if messageId == messages.last?.id {
|
||||||
await archiver.fetchForward(roster, client)
|
Task {
|
||||||
}
|
await archiver.fetchBackward(roster, client)
|
||||||
}
|
}
|
||||||
|
} else if messageId == messages.first?.id {
|
||||||
func fetchBackward() {
|
Task {
|
||||||
Task {
|
await archiver.fetchForward(roster, client)
|
||||||
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
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue