Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notify] Messages pagination #1287

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading