diff --git a/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift b/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift index 36f81605..bab897f9 100644 --- a/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift +++ b/Packages/Conversations/Sources/Conversations/List/ConversationsListView.swift @@ -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) } diff --git a/Packages/Conversations/Sources/Conversations/List/ConversationsListViewModel.swift b/Packages/Conversations/Sources/Conversations/List/ConversationsListViewModel.swift index 3799efaa..486798d3 100644 --- a/Packages/Conversations/Sources/Conversations/List/ConversationsListViewModel.swift +++ b/Packages/Conversations/Sources/Conversations/List/ConversationsListViewModel.swift @@ -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 } diff --git a/Packages/Network/Sources/Network/Endpoint/Conversations.swift b/Packages/Network/Sources/Network/Endpoint/Conversations.swift index 764f164b..36820cdd 100644 --- a/Packages/Network/Sources/Network/Endpoint/Conversations.swift +++ b/Packages/Network/Sources/Network/Endpoint/Conversations.swift @@ -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 + } } }