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

Feature/tabs for wallet #1327

Merged
merged 2 commits into from
Jan 9, 2025
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,6 +9,33 @@ protocol WalletListLocalStorageSubscriber where Self: AnyObject {
func subscribeAllWalletsProvider() -> StreamableProvider<ManagedMetaAccountModel>?

func subscribeWallet(by walletId: String) -> StreamableProvider<ManagedMetaAccountModel>?

func subscribeSelectedWalletProvider() -> StreamableProvider<ManagedMetaAccountModel>?
}

private extension WalletListLocalStorageSubscriber {
func subscribeWallets(
provider: StreamableProvider<ManagedMetaAccountModel>,
updateClosure: @escaping ([DataProviderChange<ManagedMetaAccountModel>]) -> Void,
failureClosure: @escaping (Error) -> Void
) -> StreamableProvider<ManagedMetaAccountModel>? {
let options = StreamableProviderObserverOptions(
alwaysNotifyOnRefresh: false,
waitsInProgressSyncOnAdd: false,
initialSize: 0,
refreshWhenEmpty: false
)

provider.addObserver(
self,
deliverOn: .main,
executing: updateClosure,
failing: failureClosure,
options: options
)

return provider
}
}

extension WalletListLocalStorageSubscriber {
Expand All @@ -27,22 +54,11 @@ extension WalletListLocalStorageSubscriber {
return
}

let options = StreamableProviderObserverOptions(
alwaysNotifyOnRefresh: false,
waitsInProgressSyncOnAdd: false,
initialSize: 0,
refreshWhenEmpty: false
return subscribeWallets(
provider: provider,
updateClosure: updateClosure,
failureClosure: failureClosure
)

provider.addObserver(
self,
deliverOn: .main,
executing: updateClosure,
failing: failureClosure,
options: options
)

return provider
}

func subscribeWallet(by walletId: String) -> StreamableProvider<ManagedMetaAccountModel>? {
Expand All @@ -58,26 +74,39 @@ extension WalletListLocalStorageSubscriber {
}

let failureClosure = { [weak self] (error: Error) in
self?.walletListLocalSubscriptionHandler.handleAllWallets(result: .failure(error))
self?.walletListLocalSubscriptionHandler.handleWallet(result: .failure(error), for: walletId)
return
}

let options = StreamableProviderObserverOptions(
alwaysNotifyOnRefresh: false,
waitsInProgressSyncOnAdd: false,
initialSize: 0,
refreshWhenEmpty: false
return subscribeWallets(
provider: provider,
updateClosure: updateClosure,
failureClosure: failureClosure
)
}

provider.addObserver(
self,
deliverOn: .main,
executing: updateClosure,
failing: failureClosure,
options: options
)
func subscribeSelectedWalletProvider() -> StreamableProvider<ManagedMetaAccountModel>? {
guard let provider = try? walletListLocalSubscriptionFactory.getSelectedWalletProvider() else {
return nil
}

return provider
let updateClosure = { [weak self] (changes: [DataProviderChange<ManagedMetaAccountModel>]) in
let wallet = changes.reduceToLastChange()

self?.walletListLocalSubscriptionHandler.handleSelectedWallet(result: .success(wallet))
return
}

let failureClosure = { [weak self] (error: Error) in
self?.walletListLocalSubscriptionHandler.handleSelectedWallet(result: .failure(error))
return
}

return subscribeWallets(
provider: provider,
updateClosure: updateClosure,
failureClosure: failureClosure
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import Operation_iOS
protocol WalletListLocalSubscriptionHandler {
func handleAllWallets(result: Result<[DataProviderChange<ManagedMetaAccountModel>], Error>)
func handleWallet(result: Result<ManagedMetaAccountModel?, Error>, for walletId: String)
func handleSelectedWallet(result: Result<ManagedMetaAccountModel?, Error>)
}

extension WalletListLocalSubscriptionHandler {
func handleAllWallets(result _: Result<[DataProviderChange<ManagedMetaAccountModel>], Error>) {}
func handleWallet(result _: Result<ManagedMetaAccountModel?, Error>, for _: String) {}
func handleSelectedWallet(result _: Result<ManagedMetaAccountModel?, Error>) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Operation_iOS

protocol WalletListLocalSubscriptionFactoryProtocol {
func getWalletProvider(for walletId: String) throws -> StreamableProvider<ManagedMetaAccountModel>
func getSelectedWalletProvider() throws -> StreamableProvider<ManagedMetaAccountModel>
func getWalletsProvider() throws -> StreamableProvider<ManagedMetaAccountModel>
}

Expand All @@ -28,11 +29,15 @@ final class WalletListLocalSubscriptionFactory: BaseLocalSubscriptionFactory {
}
}

extension WalletListLocalSubscriptionFactory: WalletListLocalSubscriptionFactoryProtocol {
func getWalletProvider(for walletId: String) throws -> StreamableProvider<ManagedMetaAccountModel> {
clearIfNeeded()
// MARK: Private

let cacheKey = "wallet-\(walletId)"
private extension WalletListLocalSubscriptionFactory {
func getWalletProvider(
cacheKey: String,
with filter: NSPredicate?,
predicateClosure: @escaping (ManagedMetaAccountMapper.CoreDataEntity) -> Bool
) throws -> StreamableProvider<ManagedMetaAccountModel> {
clearIfNeeded()

if let provider = getProvider(for: cacheKey) as? StreamableProvider<ManagedMetaAccountModel> {
return provider
Expand All @@ -42,7 +47,6 @@ extension WalletListLocalSubscriptionFactory: WalletListLocalSubscriptionFactory

let mapper = ManagedMetaAccountMapper()

let filter = NSPredicate.metaAccountById(walletId)
let repository = storageFacade.createRepository(
filter: filter,
sortDescriptors: [],
Expand All @@ -52,7 +56,7 @@ extension WalletListLocalSubscriptionFactory: WalletListLocalSubscriptionFactory
let observable = CoreDataContextObservable(
service: storageFacade.databaseService,
mapper: AnyCoreDataMapper(mapper),
predicate: { entity in entity.metaId == walletId }
predicate: predicateClosure
)

observable.start { [weak self] error in
Expand All @@ -72,42 +76,50 @@ extension WalletListLocalSubscriptionFactory: WalletListLocalSubscriptionFactory

return provider
}
}

func getWalletsProvider() throws -> StreamableProvider<ManagedMetaAccountModel> {
clearIfNeeded()

let cacheKey = "all-wallets"
// MARK: WalletListLocalSubscriptionFactoryProtocol

if let provider = getProvider(for: cacheKey) as? StreamableProvider<ManagedMetaAccountModel> {
return provider
}

let source = EmptyStreamableSource<ManagedMetaAccountModel>()
extension WalletListLocalSubscriptionFactory: WalletListLocalSubscriptionFactoryProtocol {
func getWalletProvider(for walletId: String) throws -> StreamableProvider<ManagedMetaAccountModel> {
try getWalletProvider(
cacheKey: CacheKeys.wallet(with: walletId),
with: NSPredicate.metaAccountById(walletId),
predicateClosure: { entity in entity.metaId == walletId }
)
}

let mapper = ManagedMetaAccountMapper()
let repository = storageFacade.createRepository(mapper: AnyCoreDataMapper(mapper))
func getSelectedWalletProvider() throws -> StreamableProvider<ManagedMetaAccountModel> {
try getWalletProvider(
cacheKey: CacheKeys.selectedWallet,
with: NSPredicate.selectedMetaAccount(),
predicateClosure: { $0.isSelected }
)
}

let observable = CoreDataContextObservable(
service: storageFacade.databaseService,
mapper: AnyCoreDataMapper(mapper),
predicate: { _ in true }
func getWalletsProvider() throws -> StreamableProvider<ManagedMetaAccountModel> {
try getWalletProvider(
cacheKey: CacheKeys.allWallets,
with: nil,
predicateClosure: { _ in true }
)
}
}

observable.start { [weak self] error in
if let error = error {
self?.logger.error("Did receive error: \(error)")
}
}
// MARK: CacheKeys

let provider = StreamableProvider(
source: AnyStreamableSource(source),
repository: AnyDataProviderRepository(repository),
observable: AnyDataProviderRepositoryObservable(observable),
operationManager: operationManager
)
private extension WalletListLocalSubscriptionFactory {
enum CacheKeys {
static var allWallets: String {
"all-wallets"
}

saveProvider(provider, for: cacheKey)
static var selectedWallet: String {
"selected-wallet"
}

return provider
static func wallet(with id: String) -> String {
"wallet-\(id)"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ extension NSPredicate {
NSPredicate(format: "%K == %@", #keyPath(CDDAppFavorite.identifier), identifier)
}

static func filterDAppBrowserTabs(by metaId: String) -> NSPredicate {
NSPredicate(format: "%K == %@", #keyPath(CDDAppBrowserTab.metaId), metaId)
}

static func filterAuthorizedBrowserDApps(by metaId: String) -> NSPredicate {
let metaId = NSPredicate(format: "%K == %@", #keyPath(CDDAppSettings.metaId), metaId)
let source = NSPredicate(format: "%K = nil", #keyPath(CDDAppSettings.source))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extension DAppBrowserTabMapper: CoreDataMapperProtocol {
uuid: UUID(uuidString: entity.identifier!)!,
name: entity.label,
url: entity.url!,
metaId: entity.metaId!,
createdAt: entity.createdAt!,
renderModifiedAt: entity.renderModifiedAt,
icon: entity.icon,
Expand All @@ -31,6 +32,7 @@ extension DAppBrowserTabMapper: CoreDataMapperProtocol {
entity.identifier = model.identifier
entity.label = model.name
entity.url = model.url
entity.metaId = model.metaId
entity.createdAt = model.createdAt
entity.renderModifiedAt = model.renderModifiedAt
entity.icon = model.icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<attribute name="icon" optional="YES" attributeType="String"/>
<attribute name="identifier" optional="YES" attributeType="String"/>
<attribute name="label" optional="YES" attributeType="String"/>
<attribute name="metaId" attributeType="String"/>
<attribute name="renderModifiedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="url" optional="YES" attributeType="URI"/>
</entity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ private extension DAppBrowserInteractor {
}

func proceedWithNewTab(opening dApp: DApp) {
let newTab = DAppBrowserTab(from: dApp)
let newTab = DAppBrowserTab(
from: dApp,
metaId: dataSource.wallet.metaId
)

let states = transports.compactMap { $0.makeOpaqueState() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ final class DAppBrowserTabListPresenter {
let interactor: DAppBrowserTabListInteractorInputProtocol
let localizationManager: LocalizationManagerProtocol

let metaId: MetaAccountModel.Id

private let viewModelFactory: DAppBrowserTabListViewModelFactoryProtocol

private var tabs: [DAppBrowserTab] = []
Expand All @@ -15,11 +17,13 @@ final class DAppBrowserTabListPresenter {
interactor: DAppBrowserTabListInteractorInputProtocol,
wireframe: DAppBrowserTabListWireframeProtocol,
viewModelFactory: DAppBrowserTabListViewModelFactoryProtocol,
metaId: MetaAccountModel.Id,
localizationManager: LocalizationManagerProtocol
) {
self.interactor = interactor
self.wireframe = wireframe
self.viewModelFactory = viewModelFactory
self.metaId = metaId
self.localizationManager = localizationManager
}
}
Expand Down Expand Up @@ -98,7 +102,7 @@ extension DAppBrowserTabListPresenter: DAppBrowserTabListInteractorOutputProtoco

extension DAppBrowserTabListPresenter: DAppSearchDelegate {
func didCompleteDAppSearchResult(_ result: DAppSearchResult) {
guard let tab = DAppBrowserTab(from: result) else {
guard let tab = DAppBrowserTab(from: result, metaId: metaId) else {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ struct DAppBrowserTabListViewFactory {
)
let viewModelFactory = DAppBrowserTabListViewModelFactory(imageViewModelFactory: imageViewModelFactory)

let wallet: MetaAccountModel = SelectedWalletSettings.shared.value

let presenter = DAppBrowserTabListPresenter(
interactor: interactor,
wireframe: wireframe,
viewModelFactory: viewModelFactory,
metaId: wallet.metaId,
localizationManager: localizationManager
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ final class DAppBrowserWidgetInteractor {
weak var presenter: DAppBrowserWidgetInteractorOutputProtocol?

private let tabManager: DAppBrowserTabManagerProtocol
private let eventCenter: EventCenterProtocol

init(
tabManager: DAppBrowserTabManagerProtocol,
eventCenter: EventCenterProtocol
) {
init(tabManager: DAppBrowserTabManagerProtocol) {
self.tabManager = tabManager
self.eventCenter = eventCenter
}
}

Expand All @@ -24,10 +19,6 @@ extension DAppBrowserWidgetInteractor: DAppBrowserWidgetInteractorInputProtocol
self,
sendOnSubscription: false
)
eventCenter.add(
observer: self,
dispatchIn: .main
)
}

func closeTabs() {
Expand All @@ -44,11 +35,3 @@ extension DAppBrowserWidgetInteractor: DAppBrowserTabsObserver {
presenter?.didReceive(tabsById)
}
}

// MARK: EventVisitorProtocol

extension DAppBrowserWidgetInteractor: EventVisitorProtocol {
func processSelectedWalletChanged(event _: SelectedWalletSwitched) {
presenter?.didReceiveWalletChanged()
}
}
Loading
Loading