From 8dbdc359d8b47244341e8a21f328ea1d39a71d20 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Mon, 9 Oct 2023 14:25:25 +0800 Subject: [PATCH] Messages badge --- .../Models/SubscriptionsViewModel.swift | 14 ++++++++++++++ .../Notifications/NotificationsInteractor.swift | 4 ++-- .../Notifications/NotificationsPresenter.swift | 6 ++---- .../Wallet/Notifications/NotificationsView.swift | 12 ++++++++++++ .../NotifyPreferencesPresenter.swift | 2 +- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Example/WalletApp/PresentationLayer/Wallet/Notifications/Models/SubscriptionsViewModel.swift b/Example/WalletApp/PresentationLayer/Wallet/Notifications/Models/SubscriptionsViewModel.swift index 366ce4409..6e0f12f42 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/Notifications/Models/SubscriptionsViewModel.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/Notifications/Models/SubscriptionsViewModel.swift @@ -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 @@ -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 + } } diff --git a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsInteractor.swift b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsInteractor.swift index eacede968..ae29a80b5 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsInteractor.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsInteractor.swift @@ -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) } } diff --git a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift index ecb906cc4..c14bf62b9 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift @@ -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] { diff --git a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsView.swift b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsView.swift index a4e250e60..4ee4fd8c2 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsView.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsView.swift @@ -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) + } + } } } } diff --git a/Example/WalletApp/PresentationLayer/Wallet/NotifySettings/NotifyPreferencesPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/NotifySettings/NotifyPreferencesPresenter.swift index c811c4314..d2689effe 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/NotifySettings/NotifyPreferencesPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/NotifySettings/NotifyPreferencesPresenter.swift @@ -10,7 +10,7 @@ final class NotifyPreferencesPresenter: ObservableObject { private var disposeBag = Set() var subscriptionViewModel: SubscriptionsViewModel { - return SubscriptionsViewModel(subscription: subscription) + return SubscriptionsViewModel(subscription: subscription, messages: []) } var preferences: [String] {