diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 3a595c501..0b8a898c9 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ A5629AE828772A0100094373 /* InviteViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5629AE728772A0100094373 /* InviteViewModel.swift */; }; A5629AEA2877F2D600094373 /* WalletConnectChat in Frameworks */ = {isa = PBXBuildFile; productRef = A5629AE92877F2D600094373 /* WalletConnectChat */; }; A5629AF22877F75100094373 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = A5629AF12877F75100094373 /* Starscream */; }; + A56AC8F22AD88A5A001C8FAA /* Sequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = A56AC8F12AD88A5A001C8FAA /* Sequence.swift */; }; A573C53729EC34A600E3CBFD /* SyncDerivationServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A573C53629EC34A600E3CBFD /* SyncDerivationServiceTests.swift */; }; A573C53929EC365000E3CBFD /* HDWalletKit in Frameworks */ = {isa = PBXBuildFile; productRef = A573C53829EC365000E3CBFD /* HDWalletKit */; }; A573C53B29EC365800E3CBFD /* HDWalletKit in Frameworks */ = {isa = PBXBuildFile; productRef = A573C53A29EC365800E3CBFD /* HDWalletKit */; }; @@ -478,6 +479,7 @@ A5629AE32876E6D200094373 /* ThreadViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadViewModel.swift; sourceTree = ""; }; A5629AE728772A0100094373 /* InviteViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InviteViewModel.swift; sourceTree = ""; }; A5629AEF2877F73000094373 /* DefaultSocketFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultSocketFactory.swift; sourceTree = ""; }; + A56AC8F12AD88A5A001C8FAA /* Sequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sequence.swift; sourceTree = ""; }; A573C53629EC34A600E3CBFD /* SyncDerivationServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncDerivationServiceTests.swift; sourceTree = ""; }; A57879702A4EDC8100F8D10B /* TextFieldView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldView.swift; sourceTree = ""; }; A578FA312873036400AA7720 /* InputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputView.swift; sourceTree = ""; }; @@ -1126,6 +1128,14 @@ path = Chat; sourceTree = ""; }; + A56AC8F02AD88A4B001C8FAA /* Foundation */ = { + isa = PBXGroup; + children = ( + A56AC8F12AD88A5A001C8FAA /* Sequence.swift */, + ); + path = Foundation; + sourceTree = ""; + }; A574B3592964570000C2BB91 /* Web3Inbox */ = { isa = PBXGroup; children = ( @@ -1664,6 +1674,7 @@ C56EE262293F56D6004840D1 /* Extensions */ = { isa = PBXGroup; children = ( + A56AC8F02AD88A4B001C8FAA /* Foundation */, 84F568C32795832A00D0A289 /* EthereumTransaction.swift */, C56EE26D293F56D6004840D1 /* SwiftUI */, C56EE269293F56D6004840D1 /* UIKit */, @@ -2438,6 +2449,7 @@ C5B2F6FB297055B0000DBA0E /* ETHSigner.swift in Sources */, C56EE274293F56D7004840D1 /* SceneViewController.swift in Sources */, A5D610D42AB35BED00C20083 /* FailableDecodable.swift in Sources */, + A56AC8F22AD88A5A001C8FAA /* Sequence.swift in Sources */, 847BD1E5298A806800076C90 /* NotificationsPresenter.swift in Sources */, A50D53C12ABA055700A4FD8B /* NotifyPreferencesModule.swift in Sources */, A5B4F7C52ABB20AE0099AF7C /* SubscriptionRouter.swift in Sources */, diff --git a/Example/WalletApp/Common/Extensions/Foundation/Sequence.swift b/Example/WalletApp/Common/Extensions/Foundation/Sequence.swift new file mode 100644 index 000000000..b1b86da7d --- /dev/null +++ b/Example/WalletApp/Common/Extensions/Foundation/Sequence.swift @@ -0,0 +1,21 @@ +import Foundation + +extension Sequence { + func sorted( + by firstPredicate: (Element, Element) -> Bool, + _ secondPredicate: (Element, Element) -> Bool, + _ otherPredicates: ((Element, Element) -> Bool)... + ) -> [Element] { + return sorted(by:) { lhs, rhs in + if firstPredicate(lhs, rhs) { return true } + if firstPredicate(rhs, lhs) { return false } + if secondPredicate(lhs, rhs) { return true } + if secondPredicate(rhs, lhs) { return false } + for predicate in otherPredicates { + if predicate(lhs, rhs) { return true } + if predicate(rhs, lhs) { return false } + } + return false + } + } +} diff --git a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift index c14bf62b9..91a573857 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/Notifications/NotificationsPresenter.swift @@ -14,15 +14,19 @@ final class NotificationsPresenter: ObservableObject { var subscriptionViewModels: [SubscriptionsViewModel] { return subscriptions .map { SubscriptionsViewModel(subscription: $0, messages: interactor.messages(for: $0)) } - .sorted { $0.messagesCount > $1.messagesCount } + .sorted(by: + { $0.messagesCount > $1.messagesCount }, + { $0.name < $1.name } + ) } var listingViewModels: [ListingViewModel] { return listings .map { ListingViewModel(listing: $0) } - .sorted { lhs, rhs in - return subscription(forListing: lhs) != nil && subscription(forListing: rhs) == nil - } + .sorted(by: + { subscription(forListing: $0) != nil && subscription(forListing: $1) == nil }, + { $0.title < $1.title } + ) } init(interactor: NotificationsInteractor, router: NotificationsRouter) { diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift index f08498f21..4b72d6026 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift @@ -64,10 +64,7 @@ final class NotifyStorage: NotifyStoring { } func replaceAllSubscriptions(_ subscriptions: [NotifySubscription], account: Account) { - subscriptionStore.deleteAll(for: account.absoluteString) - // todo - compare old with new = delete messages for removed subscriptions - //messages for new subscriptions are not required - subscriptionStore.set(elements: subscriptions, for: account.absoluteString) + subscriptionStore.replace(elements: subscriptions, for: account.absoluteString) subscriptionsSubject.send(subscriptions) } diff --git a/Sources/WalletConnectUtils/KeyedDatabase.swift b/Sources/WalletConnectUtils/KeyedDatabase.swift index c619d62d5..5677946ae 100644 --- a/Sources/WalletConnectUtils/KeyedDatabase.swift +++ b/Sources/WalletConnectUtils/KeyedDatabase.swift @@ -61,9 +61,20 @@ public class KeyedDatabase where Element: DatabaseObject { var map = index[key] ?? [:] for element in elements { - guard - map[element.databaseId] == nil else { continue } - map[element.databaseId] = element + map[element.databaseId] = element + } + + index[key] = map + + return true + } + + @discardableResult + public func replace(elements: [Element], for key: String) -> Bool { + var map: [String: Element] = [:] + + for element in elements { + map[element.databaseId] = element } index[key] = map