Skip to content

Commit

Permalink
Merge pull request #1287 from WalletConnect/feature/notify-messages-p…
Browse files Browse the repository at this point in the history
…agination

[Notify] Messages pagination
  • Loading branch information
flypaper0 committed Jan 27, 2024
2 parents 6504bdf + aa31a50 commit 320abfa
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Example/IntegrationTests/Push/NotifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ final class NotifyTests: XCTestCase {

await fulfillment(of: [subscribeExpectation], timeout: InputConfig.defaultTimeout)

try await walletNotifyClientA.fetchHistory(subscription: subscription)
XCTAssertTrue(walletNotifyClientA.getMessageHistory(topic: subscription.topic).count > 40)
let hasMore = try await walletNotifyClientA.fetchHistory(subscription: subscription, after: nil, limit: 20)
XCTAssertTrue(hasMore)
XCTAssertTrue(walletNotifyClientA.getMessageHistory(topic: subscription.topic).count == 20)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import WalletConnectNotify

final class SubscriptionPresenter: ObservableObject {

enum LoadingState {
case loading
case idle
}

private var subscription: NotifySubscription
private let interactor: SubscriptionInteractor
private let router: SubscriptionRouter
private var disposeBag = Set<AnyCancellable>()

@Published private var pushMessages: [NotifyMessageRecord] = []

@Published var loadingState: LoadingState = .idle
@Published var isMoreDataAvailable: Bool = true

var subscriptionViewModel: SubscriptionsViewModel {
return SubscriptionsViewModel(subscription: subscription)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -77,8 +98,6 @@ extension SubscriptionPresenter: SceneViewModel {
private extension SubscriptionPresenter {

func setupInitialState() {
interactor.fetchHistory()

pushMessages = interactor.getPushMessages()

interactor.messagesPublisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ struct SubscriptionView: View {
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
}

if presenter.isMoreDataAvailable {
lastRowView()
.listRowSeparator(.hidden)
}
}
.listStyle(PlainListStyle())
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,23 @@ 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
return NotifyMessageRecord(topic: subscription.topic, message: message, publishedAt: message.sentAt)
}

try notifyStorage.setMessages(records)

return messages.count == limit
}
}

Expand Down

0 comments on commit 320abfa

Please sign in to comment.