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 publisher #1067

Merged
merged 3 commits into from
Aug 30, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ final class PushMessagesInteractor {
self.subscription = subscription
}

var notifyMessagePublisher: AnyPublisher<NotifyMessageRecord, Never> {
return Notify.instance.notifyMessagePublisher
var messagesPublisher: AnyPublisher<[NotifyMessageRecord], Never> {
return Notify.instance.messagesPublisher(topic: subscription.topic)
}

func getPushMessages() -> [NotifyMessageRecord] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ final class PushMessagesPresenter: ObservableObject {
private let router: PushMessagesRouter
private var disposeBag = Set<AnyCancellable>()

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

var messages: [PushMessageViewModel] {
return pushMessages
.sorted { $0.publishedAt > $1.publishedAt }
.map { PushMessageViewModel(pushMessageRecord: $0) }
}

init(interactor: PushMessagesInteractor, router: PushMessagesRouter) {
defer { reloadPushMessages() }
defer { setupInitialState() }
self.interactor = interactor
self.router = router
}
Expand All @@ -20,7 +26,6 @@ final class PushMessagesPresenter: ObservableObject {
if let index = indexSet.first {
interactor.deletePushMessage(id: pushMessages[index].id)
}
reloadPushMessages()
}
}

Expand All @@ -40,27 +45,13 @@ extension PushMessagesPresenter: SceneViewModel {

private extension PushMessagesPresenter {

func reloadPushMessages() {
self.pushMessages = interactor.getPushMessages()
.sorted {
// Most recent first
$0.publishedAt > $1.publishedAt
}
.map { pushMessageRecord in
PushMessageViewModel(pushMessageRecord: pushMessageRecord)
}

interactor.notifyMessagePublisher
func setupInitialState() {
pushMessages = interactor.getPushMessages()

interactor.messagesPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] newPushMessage in
let newMessageViewModel = PushMessageViewModel(pushMessageRecord: newPushMessage)
guard let index = self?.pushMessages.firstIndex(
where: { $0.pushMessageRecord.publishedAt > newPushMessage.publishedAt }
) else {
self?.pushMessages.append(newMessageViewModel)
return
}
self?.pushMessages.insert(newMessageViewModel, at: index)
.sink { [unowned self] messages in
pushMessages = interactor.getPushMessages()
}
.store(in: &disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct PushMessagesView: View {

VStack(alignment: .leading, spacing: 16) {
ZStack {
if presenter.pushMessages.isEmpty {
if presenter.messages.isEmpty {
VStack(spacing: 10) {
Image(systemName: "bell.badge.fill")
.resizable()
Expand All @@ -29,9 +29,9 @@ struct PushMessagesView: View {
}

VStack {
if !presenter.pushMessages.isEmpty {
if !presenter.messages.isEmpty {
List {
ForEach(presenter.pushMessages, id: \.id) { pm in
ForEach(presenter.messages, id: \.id) { pm in
notificationView(pushMessage: pm)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 16, trailing: 0))
Expand Down
6 changes: 5 additions & 1 deletion Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class NotifyClient {
}

public var notifyMessagePublisher: AnyPublisher<NotifyMessageRecord, Never> {
notifyMessageSubscriber.notifyMessagePublisher
return notifyMessageSubscriber.notifyMessagePublisher
}

public var updateSubscriptionPublisher: AnyPublisher<Result<NotifySubscription, Error>, Never> {
Expand Down Expand Up @@ -119,6 +119,10 @@ public class NotifyClient {
public func isSyncRegistered(account: Account) -> Bool {
return notifySyncService.isSyncRegistered(account: account)
}

public func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> {
return notifyStorage.messagesPublisher(topic: topic)
}
}

#if targetEnvironment(simulator)
Expand Down
17 changes: 16 additions & 1 deletion Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class NotifyStorage: NotifyStoring {
private let newSubscriptionSubject = PassthroughSubject<NotifySubscription, Never>()
private let updateSubscriptionSubject = PassthroughSubject<NotifySubscription, Never>()
private let deleteSubscriptionSubject = PassthroughSubject<String, Never>()
private let messagesSubject = PassthroughSubject<[NotifyMessageRecord], Never>()

private let subscriptionStoreDelegate: NotifySubscriptionStoreDelegate

Expand All @@ -26,7 +27,7 @@ final class NotifyStorage: NotifyStoring {
}

var updateSubscriptionPublisher: AnyPublisher<NotifySubscription, Never> {
return newSubscriptionSubject.eraseToAnyPublisher()
return updateSubscriptionSubject.eraseToAnyPublisher()
}

var deleteSubscriptionPublisher: AnyPublisher<String, Never> {
Expand All @@ -37,6 +38,10 @@ final class NotifyStorage: NotifyStoring {
return subscriptionStore.dataUpdatePublisher
}

var messagesPublisher: AnyPublisher<[NotifyMessageRecord], Never> {
return messagesSubject.eraseToAnyPublisher()
}

init(
subscriptionStore: SyncStore<NotifySubscription>,
messagesStore: KeyedDatabase<NotifyMessageRecord>,
Expand Down Expand Up @@ -88,6 +93,12 @@ final class NotifyStorage: NotifyStoring {

// MARK: Messages

func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> {
return messagesPublisher
.map { $0.filter { $0.topic == topic } }
.eraseToAnyPublisher()
}

func getMessages(topic: String) -> [NotifyMessageRecord] {
return messagesStore.getAll(for: topic)
.sorted{$0.publishedAt > $1.publishedAt}
Expand All @@ -110,6 +121,10 @@ final class NotifyStorage: NotifyStoring {
private extension NotifyStorage {

func setupSubscriptions() {
messagesStore.onUpdate = { [unowned self] in
messagesSubject.send(messagesStore.getAll())
}

subscriptionStore.syncUpdatePublisher.sink { [unowned self] (_, _, update) in
switch update {
case .set(let subscription):
Expand Down
10 changes: 10 additions & 0 deletions Tests/WalletConnectUtilsTests/KeyedDatabaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ final class KeyedDatabaseTests: XCTestCase {

XCTAssertEqual(value, updated)
}

func testOnUpdate() {
let new = Object(key: "key1", value: "value1")

var onUpdateCalled = false
sut.onUpdate = { onUpdateCalled = true }
sut.set(element: new, for: storageKey)

XCTAssertTrue(onUpdateCalled)
}
}