Skip to content

Commit

Permalink
Merge pull request #1169 from WalletConnect/feature/notify-messages-b…
Browse files Browse the repository at this point in the history
…adge

[Notify] Messages badge
  • Loading branch information
flypaper0 authored Oct 9, 2023
2 parents c17686e + 8dbdc35 commit 1a03973
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ typealias SubscriptionScope = [String: ScopeValue]

struct SubscriptionsViewModel: Identifiable {
let subscription: NotifySubscription
let messages: [NotifyMessageRecord]?

init(subscription: NotifySubscription, messages: [NotifyMessageRecord]? = nil) {
self.subscription = subscription
self.messages = messages
}

var id: String {
return subscription.topic
Expand Down Expand Up @@ -33,4 +39,12 @@ struct SubscriptionsViewModel: Identifiable {
var scope: SubscriptionScope {
return subscription.scope
}

var messagesCount: Int {
return messages?.count ?? 0
}

var hasMessage: Bool {
return messagesCount != 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class NotificationsInteractor {
try await Notify.instance.deleteSubscription(topic: topic)
}

func messagesCount(subscription: NotifySubscription) -> Int {
return Notify.instance.getMessageHistory(topic: subscription.topic).count
func messages(for subscription: NotifySubscription) -> [NotifyMessageRecord] {
return Notify.instance.getMessageHistory(topic: subscription.topic)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ final class NotificationsPresenter: ObservableObject {

var subscriptionViewModels: [SubscriptionsViewModel] {
return subscriptions
.map { SubscriptionsViewModel(subscription: $0) }
.sorted { lhs, rhs in
return interactor.messagesCount(subscription: lhs.subscription) > interactor.messagesCount(subscription: rhs.subscription)
}
.map { SubscriptionsViewModel(subscription: $0, messages: interactor.messages(for: $0)) }
.sorted { $0.messagesCount > $1.messagesCount }
}

var listingViewModels: [ListingViewModel] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ struct NotificationsView: View {
.foregroundColor(.Foreground150)
.font(.system(size: 14, weight: .regular, design: .rounded))
}

Spacer()

if subscription.hasMessage {
Text(String(subscription.messagesCount))
.foregroundColor(.Inverse100)
.font(.system(size: 13, weight: .medium))
.frame(width: 20, height: 20)
.background {
Circle().foregroundColor(.blue100)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class NotifyPreferencesPresenter: ObservableObject {
private var disposeBag = Set<AnyCancellable>()

var subscriptionViewModel: SubscriptionsViewModel {
return SubscriptionsViewModel(subscription: subscription)
return SubscriptionsViewModel(subscription: subscription, messages: [])
}

var preferences: [String] {
Expand Down

0 comments on commit 1a03973

Please sign in to comment.