From 657f92c23b8f35f24f4e19b61f8c79a962c3e73b Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Thu, 25 Jan 2024 16:05:46 +0300 Subject: [PATCH 1/3] Pagination implemented --- .../PushMessages/SubscriptionInteractor.swift | 6 ++--- .../PushMessages/SubscriptionPresenter.swift | 23 +++++++++++++++-- .../PushMessages/SubscriptionView.swift | 25 +++++++++++++++++++ .../Client/Wallet/HistoryService.swift | 6 ++--- .../Client/Wallet/NotifyClient.swift | 8 ++++-- 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift index e3d1a14e4..37ea5fcd0 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift @@ -31,9 +31,7 @@ final class SubscriptionInteractor { } } - func fetchHistory() { - Task(priority: .high) { - try await Notify.instance.fetchHistory(subscription: subscription) - } + func fetchHistory(after: String?, limit: Int) async throws -> Bool { + return try await Notify.instance.fetchHistory(subscription: subscription, after: after, limit: limit) } } diff --git a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift index c0527399e..e5b69d57c 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift @@ -4,6 +4,11 @@ import WalletConnectNotify final class SubscriptionPresenter: ObservableObject { + enum LoadingState { + case loading + case idle + } + private var subscription: NotifySubscription private let interactor: SubscriptionInteractor private let router: SubscriptionRouter @@ -11,6 +16,9 @@ final class SubscriptionPresenter: ObservableObject { @Published private var pushMessages: [NotifyMessageRecord] = [] + @Published var loadingState: LoadingState = .idle + @Published var isMoreDataAvailable: Bool = true + var subscriptionViewModel: SubscriptionsViewModel { return SubscriptionsViewModel(subscription: subscription) } @@ -49,6 +57,19 @@ final class SubscriptionPresenter: ObservableObject { router.dismiss() } + func loadMoreMessages() { + switch loadingState { + case .loading: + break + case .idle: + Task(priority: .high) { @MainActor in + loadingState = .loading + isMoreDataAvailable = try await interactor.fetchHistory(after: messages.last?.id, limit: 50) + loadingState = .idle + } + } + } + @objc func preferencesDidPress() { router.presentPreferences(subscription: subscription) } @@ -77,8 +98,6 @@ extension SubscriptionPresenter: SceneViewModel { private extension SubscriptionPresenter { func setupInitialState() { - interactor.fetchHistory() - pushMessages = interactor.getPushMessages() interactor.messagesPublisher diff --git a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionView.swift b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionView.swift index 358affe2b..890629513 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionView.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionView.swift @@ -34,6 +34,11 @@ struct SubscriptionView: View { .listRowSeparator(.hidden) .listRowBackground(Color.clear) } + + if presenter.isMoreDataAvailable { + lastRowView() + .listRowSeparator(.hidden) + } } .listStyle(PlainListStyle()) } @@ -161,6 +166,26 @@ struct SubscriptionView: View { .frame(maxWidth: .infinity) .frame(height: 410) } + + func lastRowView() -> some View { + VStack { + switch presenter.loadingState { + case .loading: + HStack { + Spacer() + ProgressView() + Spacer() + } + .padding(.bottom, 24) + case .idle: + EmptyView() + } + } + .frame(height: 50) + .onAppear { + presenter.loadMoreMessages() + } + } } #if DEBUG diff --git a/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift b/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift index 21574e569..7cece784e 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift @@ -12,7 +12,7 @@ public final class HistoryService { self.identityClient = identityClient } - public func fetchHistory(account: Account, topic: String, appAuthenticationKey: String, host: String) async throws -> [NotifyMessage] { + public func fetchHistory(account: Account, topic: String, appAuthenticationKey: String, host: String, after: String?, limit: Int) async throws -> [NotifyMessage] { let dappAuthKey = try DIDKey(did: appAuthenticationKey) let app = DIDWeb(host: host) @@ -21,8 +21,8 @@ public final class HistoryService { keyserver: keyserver.absoluteString, dappAuthKey: dappAuthKey, app: app, - limit: 50, - after: nil + limit: UInt64(limit), + after: after ) let wrapper = try identityClient.signAndCreateWrapper(payload: requestPayload, account: account) diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift index e4eab7e08..d806dba04 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift @@ -148,12 +148,14 @@ public class NotifyClient { return notifyStorage.messagesPublisher(topic: topic) } - public func fetchHistory(subscription: NotifySubscription) async throws { + public func fetchHistory(subscription: NotifySubscription, after: String?, limit: Int) async throws -> Bool { let messages = try await historyService.fetchHistory( account: subscription.account, topic: subscription.topic, appAuthenticationKey: subscription.appAuthenticationKey, - host: subscription.metadata.url + host: subscription.metadata.url, + after: after, + limit: limit ) let records = messages.map { message in @@ -161,6 +163,8 @@ public class NotifyClient { } try notifyStorage.setMessages(records) + + return messages.count == limit } } From b9c819a8cf27195d333d03be6701cdfcbde9e8e4 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Thu, 25 Jan 2024 16:07:43 +0300 Subject: [PATCH 2/3] Integration tests hasMore --- Example/IntegrationTests/Push/NotifyTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 4673fe55d..742fd8d9e 100644 --- a/Example/IntegrationTests/Push/NotifyTests.swift +++ b/Example/IntegrationTests/Push/NotifyTests.swift @@ -264,7 +264,8 @@ final class NotifyTests: XCTestCase { await fulfillment(of: [subscribeExpectation], timeout: InputConfig.defaultTimeout) - try await walletNotifyClientA.fetchHistory(subscription: subscription) + let hasMore = try await walletNotifyClientA.fetchHistory(subscription: subscription, after: nil, limit: 50) + XCTAssertTrue(hasMore) XCTAssertTrue(walletNotifyClientA.getMessageHistory(topic: subscription.topic).count > 40) } } From aa31a50679b392e566f053394232bd3703e7640a Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Thu, 25 Jan 2024 16:09:31 +0300 Subject: [PATCH 3/3] testFetchHistory updated --- Example/IntegrationTests/Push/NotifyTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 742fd8d9e..1618881e6 100644 --- a/Example/IntegrationTests/Push/NotifyTests.swift +++ b/Example/IntegrationTests/Push/NotifyTests.swift @@ -264,9 +264,9 @@ final class NotifyTests: XCTestCase { await fulfillment(of: [subscribeExpectation], timeout: InputConfig.defaultTimeout) - let hasMore = try await walletNotifyClientA.fetchHistory(subscription: subscription, after: nil, limit: 50) + let hasMore = try await walletNotifyClientA.fetchHistory(subscription: subscription, after: nil, limit: 20) XCTAssertTrue(hasMore) - XCTAssertTrue(walletNotifyClientA.getMessageHistory(topic: subscription.topic).count > 40) + XCTAssertTrue(walletNotifyClientA.getMessageHistory(topic: subscription.topic).count == 20) } }