-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1331 from novasamatech/feature/deleted-wallet-sta…
…te-cleaning Feature/deleted wallet state cleaning
- Loading branch information
Showing
18 changed files
with
647 additions
and
53 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
novawallet/Common/Storage/WalletStorageCleaning/Cleaners/DAppSettingsCleaner.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
import Operation_iOS | ||
|
||
final class DAppSettingsCleaner { | ||
private let authorizedDAppRepository: AnyDataProviderRepository<DAppSettings> | ||
|
||
init(authorizedDAppRepository: AnyDataProviderRepository<DAppSettings>) { | ||
self.authorizedDAppRepository = authorizedDAppRepository | ||
} | ||
} | ||
|
||
// MARK: WalletStorageCleaning | ||
|
||
extension DAppSettingsCleaner: WalletStorageCleaning { | ||
func cleanStorage( | ||
for removedItems: @escaping () throws -> [MetaAccountModel] | ||
) -> CompoundOperationWrapper<Void> { | ||
let fetchOptions = RepositoryFetchOptions() | ||
let fetchSettingsOperation = authorizedDAppRepository.fetchAllOperation(with: fetchOptions) | ||
|
||
let deletionBlock: () throws -> [String] = { | ||
let removedWallets = Set(try removedItems().map(\.metaId)) | ||
let dappSettingsIds = try fetchSettingsOperation.extractNoCancellableResultData() | ||
.compactMap(\.metaId) | ||
.filter { removedWallets.contains($0) } | ||
|
||
return dappSettingsIds | ||
} | ||
|
||
let removeSettingsOperation = authorizedDAppRepository.saveOperation( | ||
{ [] }, | ||
deletionBlock | ||
) | ||
|
||
removeSettingsOperation.addDependency(fetchSettingsOperation) | ||
|
||
return CompoundOperationWrapper( | ||
targetOperation: removeSettingsOperation, | ||
dependencies: [fetchSettingsOperation] | ||
) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
novawallet/Common/Storage/WalletStorageCleaning/Cleaners/WalletBrowserStateCleaner.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Foundation | ||
import Operation_iOS | ||
|
||
final class WalletBrowserStateCleaner { | ||
private let browserTabManager: DAppBrowserTabManagerProtocol | ||
private let webViewPoolEraser: WebViewPoolEraserProtocol | ||
private let operationQueue: OperationQueue | ||
|
||
init( | ||
browserTabManager: DAppBrowserTabManagerProtocol, | ||
webViewPoolEraser: WebViewPoolEraserProtocol, | ||
operationQueue: OperationQueue | ||
) { | ||
self.browserTabManager = browserTabManager | ||
self.webViewPoolEraser = webViewPoolEraser | ||
self.operationQueue = operationQueue | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private extension WalletBrowserStateCleaner { | ||
func createTabsCleaningWrapper( | ||
for removedItems: @escaping () throws -> [MetaAccountModel] | ||
) -> CompoundOperationWrapper<Set<UUID>> { | ||
OperationCombiningService.compoundNonOptionalWrapper( | ||
operationQueue: operationQueue | ||
) { [weak self] in | ||
guard let self else { | ||
throw BaseOperationError.parentOperationCancelled | ||
} | ||
|
||
let metaIds = try removedItems().map(\.metaId) | ||
|
||
return browserTabManager.removeAllWrapper(for: Set(metaIds)) | ||
} | ||
} | ||
|
||
func createWebViewCleaningOperation( | ||
dependingOn tabIdsOperation: BaseOperation<Set<UUID>> | ||
) -> ClosureOperation<Void> { | ||
ClosureOperation { [weak self] in | ||
let tabIds = try tabIdsOperation.extractNoCancellableResultData() | ||
|
||
tabIds.forEach { tabId in | ||
DispatchQueue.main.async { | ||
self?.webViewPoolEraser.removeWebView(for: tabId) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: WalletStorageCleaning | ||
|
||
extension WalletBrowserStateCleaner: WalletStorageCleaning { | ||
func cleanStorage( | ||
for removedItems: @escaping () throws -> [MetaAccountModel] | ||
) -> CompoundOperationWrapper<Void> { | ||
let tabsCleaningWrapper = createTabsCleaningWrapper(for: removedItems) | ||
|
||
let webViewPoolCleaningOperation = createWebViewCleaningOperation( | ||
dependingOn: tabsCleaningWrapper.targetOperation | ||
) | ||
|
||
webViewPoolCleaningOperation.addDependency(tabsCleaningWrapper.targetOperation) | ||
|
||
return tabsCleaningWrapper.insertingTail(operation: webViewPoolCleaningOperation) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
novawallet/Common/Storage/WalletStorageCleaning/RemovedWalletStorageCleaner.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
import Operation_iOS | ||
|
||
final class RemovedWalletStorageCleaner { | ||
private let cleanersCascade: [WalletStorageCleaning] | ||
|
||
init(cleanersCascade: [WalletStorageCleaning]) { | ||
self.cleanersCascade = cleanersCascade | ||
} | ||
} | ||
|
||
// MARK: WalletStorageCleaning | ||
|
||
extension RemovedWalletStorageCleaner: WalletStorageCleaning { | ||
func cleanStorage( | ||
for removedItems: @escaping () throws -> [MetaAccountModel] | ||
) -> CompoundOperationWrapper<Void> { | ||
let wrappers = cleanersCascade.map { $0.cleanStorage(for: removedItems) } | ||
|
||
let mergeOperation = ClosureOperation { | ||
_ = try wrappers.map { try $0.targetOperation.extractNoCancellableResultData() } | ||
|
||
return | ||
} | ||
|
||
wrappers.forEach { mergeOperation.addDependency($0.targetOperation) } | ||
|
||
let dependencies = wrappers.flatMap(\.allOperations) | ||
|
||
return CompoundOperationWrapper( | ||
targetOperation: mergeOperation, | ||
dependencies: dependencies | ||
) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
novawallet/Common/Storage/WalletStorageCleaning/WalletDeleteStorageCleanerFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Foundation | ||
import Operation_iOS | ||
|
||
final class WalletStorageCleanerFactory { | ||
static func createWalletStorageCleaner(using operationQueue: OperationQueue) -> WalletStorageCleaning { | ||
let browserStateCleaner = createBrowserStateCleaner(using: operationQueue) | ||
let dAppSettingsCleaner = createDAppSettingsCleaner() | ||
|
||
// Add every cleaner to the array | ||
// in the same order it should get called | ||
let cleaners = [ | ||
browserStateCleaner, | ||
dAppSettingsCleaner | ||
] | ||
|
||
let mainCleaner = RemovedWalletStorageCleaner(cleanersCascade: cleaners) | ||
|
||
return mainCleaner | ||
} | ||
|
||
private static func createBrowserStateCleaner( | ||
using operationQueue: OperationQueue | ||
) -> WalletStorageCleaning { | ||
let browserTabManager = DAppBrowserTabManager.shared | ||
let webViewPoolEraser = WebViewPool.shared | ||
|
||
let browserStateCleaner = WalletBrowserStateCleaner( | ||
browserTabManager: browserTabManager, | ||
webViewPoolEraser: webViewPoolEraser, | ||
operationQueue: operationQueue | ||
) | ||
|
||
return browserStateCleaner | ||
} | ||
|
||
private static func createDAppSettingsCleaner() -> WalletStorageCleaning { | ||
let mapper = DAppSettingsMapper() | ||
let storageFacade = UserDataStorageFacade.shared | ||
|
||
let repository = storageFacade.createRepository( | ||
filter: nil, | ||
sortDescriptors: [], | ||
mapper: AnyCoreDataMapper(mapper) | ||
) | ||
let authorizedDAppRepository = AnyDataProviderRepository(repository) | ||
|
||
let dappSettingsCleaner = DAppSettingsCleaner(authorizedDAppRepository: authorizedDAppRepository) | ||
|
||
return dappSettingsCleaner | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
novawallet/Common/Storage/WalletStorageCleaning/WalletStorageCleaning.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
import Operation_iOS | ||
|
||
protocol WalletStorageCleaning { | ||
func cleanStorage( | ||
for removedItems: @escaping () throws -> [MetaAccountModel] | ||
) -> CompoundOperationWrapper<Void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.