Private messages: Add pagination
This commit is contained in:
parent
2a31fd7ef5
commit
d0d969d52c
|
@ -51,6 +51,21 @@ public struct ConversationsListView: View {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if viewModel.nextPage != nil {
|
||||
HStack {
|
||||
Spacer()
|
||||
ProgressView()
|
||||
Spacer()
|
||||
}
|
||||
.onAppear {
|
||||
if !viewModel.isLoadingNextPage {
|
||||
Task {
|
||||
await viewModel.fetchNextPage()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.top, .layoutPadding)
|
||||
}
|
||||
|
|
|
@ -7,8 +7,11 @@ class ConversationsListViewModel: ObservableObject {
|
|||
var client: Client?
|
||||
|
||||
@Published var isLoadingFirstPage: Bool = true
|
||||
@Published var isLoadingNextPage: Bool = false
|
||||
@Published var conversations: [Conversation] = []
|
||||
@Published var isError: Bool = false
|
||||
|
||||
var nextPage: LinkHandler?
|
||||
|
||||
public init() {}
|
||||
|
||||
|
@ -18,13 +21,32 @@ class ConversationsListViewModel: ObservableObject {
|
|||
isLoadingFirstPage = true
|
||||
}
|
||||
do {
|
||||
conversations = try await client.get(endpoint: Conversations.conversations)
|
||||
(conversations, nextPage) = try await client.getWithLink(endpoint: Conversations.conversations(maxId: nil))
|
||||
if nextPage?.maxId == nil {
|
||||
nextPage = nil
|
||||
}
|
||||
isLoadingFirstPage = false
|
||||
} catch {
|
||||
isError = true
|
||||
isLoadingFirstPage = false
|
||||
}
|
||||
}
|
||||
|
||||
func fetchNextPage() async {
|
||||
if let maxId = nextPage?.maxId, let client {
|
||||
do {
|
||||
isLoadingNextPage = true
|
||||
var nextMessages: [Conversation] = []
|
||||
(nextMessages, nextPage) = try await client.getWithLink(endpoint: Conversations.conversations(maxId: maxId))
|
||||
conversations.append(contentsOf: nextMessages)
|
||||
if nextPage?.maxId == nil {
|
||||
nextPage = nil
|
||||
}
|
||||
isLoadingNextPage = false
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func markAsRead(conversation: Conversation) async {
|
||||
guard let client else { return }
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import Foundation
|
||||
|
||||
public enum Conversations: Endpoint {
|
||||
case conversations
|
||||
case conversations(maxId: String?)
|
||||
case delete(id: String)
|
||||
case read(id: String)
|
||||
|
||||
|
@ -17,6 +17,11 @@ public enum Conversations: Endpoint {
|
|||
}
|
||||
|
||||
public func queryItems() -> [URLQueryItem]? {
|
||||
return nil
|
||||
switch self {
|
||||
case let .conversations(maxId):
|
||||
return makePaginationParam(sinceId: nil, maxId: maxId, mindId: nil)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue