Merge pull request #4532 from oa-s/#4531

User shared realm for browser #4531
pull/4564/head
Crypto Pank 3 years ago committed by GitHub
commit a9bac3c34b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      AlphaWallet/ActiveWalletCoordinator.swift
  2. 5
      AlphaWallet/AppCoordinator.swift
  3. 9
      AlphaWallet/Core/Initializers/CleanupWallets.swift
  4. 37
      AlphaWallet/Core/Initializers/DatabaseMigration.swift
  5. 20
      AlphaWallet/Core/Types/RealmConfiguration.swift
  6. 31
      AlphaWallet/Core/Types/RealmStore.swift
  7. 26
      AlphaWallet/Tokens/Types/TokenObject.swift

@ -321,7 +321,7 @@ class ActiveWalletCoordinator: NSObject, Coordinator, DappRequestHandlerDelegate
}
private func createBrowserCoordinator(sessions: ServerDictionary<WalletSession>, browserOnly: Bool, analyticsCoordinator: AnalyticsCoordinator) -> DappBrowserCoordinator {
let coordinator = DappBrowserCoordinator(sessions: sessions, keystore: keystore, config: config, sharedRealm: realm, browserOnly: browserOnly, restartQueue: restartQueue, analyticsCoordinator: analyticsCoordinator)
let coordinator = DappBrowserCoordinator(sessions: sessions, keystore: keystore, config: config, sharedRealm: Realm.shared(), browserOnly: browserOnly, restartQueue: restartQueue, analyticsCoordinator: analyticsCoordinator)
coordinator.delegate = self
coordinator.start()
coordinator.rootViewController.tabBarItem = UITabBarController.Tabs.browser.tabBarItem

@ -109,7 +109,6 @@ class AppCoordinator: NSObject, Coordinator {
window.makeKeyAndVisible()
setupSplashViewController(on: navigationController)
bindWalletAddressesStore()
}
@ -129,7 +128,7 @@ class AppCoordinator: NSObject, Coordinator {
}.store(in: &cancelable)
}
func start() {
func start() {
initializers()
cleanPasscodeIfNeeded()
appTracker.start()
@ -207,7 +206,7 @@ class AppCoordinator: NSObject, Coordinator {
private func initializers() {
let initializers: [Initializer] = [
ConfigureApp(),
CleanupWallets(keystore: keystore),
CleanupWallets(keystore: keystore, walletAddressesStore: walletAddressesStore, config: config),
SkipBackupFiles(legacyFileBasedKeystore: legacyFileBasedKeystore),
]

@ -9,9 +9,13 @@ import Foundation
final class CleanupWallets: Initializer {
private let keystore: Keystore
init(keystore: Keystore) {
private let walletAddressesStore: WalletAddressesStore
private let config: Config
init(keystore: Keystore, walletAddressesStore: WalletAddressesStore, config: Config) {
self.keystore = keystore
self.config = config
self.walletAddressesStore = walletAddressesStore
}
func perform() {
@ -23,5 +27,6 @@ final class CleanupWallets: Initializer {
}
DatabaseMigration.removeWalletsIfRealmFilesMissed(keystore: keystore)
DatabaseMigration.oneTimeMigrationForBookmarksAndUrlHistoryToSharedRealm(walletAddressesStore: walletAddressesStore, config: config)
}
}

@ -137,6 +137,32 @@ extension DatabaseMigration {
}
}
static func oneTimeMigrationForBookmarksAndUrlHistoryToSharedRealm(walletAddressesStore: WalletAddressesStore, config: Config) {
guard !config.hasMigratedToSharedRealm() else { return }
for each in walletAddressesStore.wallets {
let migration = DatabaseMigration(account: each)
migration.perform()
migration.oneTimeMigrationForBookmarksAndUrlHistoryToSharedRealm()
}
config.markAsMigratedToSharedRealmDatabase()
}
private func oneTimeMigrationForBookmarksAndUrlHistoryToSharedRealm() {
let oldPerWalletDatabase = try! Realm(configuration: config)
let realm = Realm.shared()
try? realm.write {
for each in oldPerWalletDatabase.objects(History.self) {
realm.create(History.self, value: each)
}
for each in oldPerWalletDatabase.objects(Bookmark.self) {
realm.create(Bookmark.self, value: each)
}
}
}
func oneTimeCreationOfOneDatabaseToHoldAllChains(assetDefinitionStore: AssetDefinitionStore) {
let migration = self
@ -202,3 +228,14 @@ extension DatabaseMigration {
}
}
fileprivate extension Config {
private static var storageKey: String = "migrationsToSharedRealmForBookmarks"
func hasMigratedToSharedRealm() -> Bool {
defaults.bool(forKey: Config.storageKey)
}
func markAsMigratedToSharedRealmDatabase() {
defaults.set(true, forKey: Config.storageKey)
}
}

@ -5,12 +5,25 @@ import RealmSwift
struct RealmConfiguration {
private static let walletsFolderForTests = "testSuiteWalletsForRealm"
static func configuration(for account: Wallet) -> Realm.Configuration {
var config = realmConfiguration()
config.fileURL = defaultRealmFolderUrl.appendingPathComponent("\(account.address.eip55String.lowercased()).realm")
return config
}
static func configuration(for account: Wallet, server: RPCServer) -> Realm.Configuration {
var config = realmConfiguration()
config.fileURL = defaultRealmFolderUrl.appendingPathComponent("\(account.address.eip55String.lowercased())-\(server.chainID).realm")
return config
}
static func configuration(name: String) -> Realm.Configuration {
var config = realmConfiguration()
config.fileURL = defaultRealmFolderUrl.appendingPathComponent("\(name).realm")
return config
}
static var defaultRealmFolderUrl: URL {
return realmConfiguration().fileURL!.deletingLastPathComponent()
}
@ -51,13 +64,6 @@ struct RealmConfiguration {
try? fileManager.removeItem(atPath: directory.absoluteString)
}
static func configuration(for account: Wallet) -> Realm.Configuration {
var config = realmConfiguration()
config.fileURL = defaultRealmFolderUrl.appendingPathComponent("\(account.address.eip55String.lowercased()).realm")
return config
}
}
extension FileManager {

@ -35,7 +35,38 @@ final class RealmStore {
}
}
extension Wallet {
class functional {}
}
extension Wallet.functional {
static func realm(forAccount account: Wallet) -> Realm {
let migration = DatabaseMigration(account: account)
migration.perform()
let realm = try! Realm(configuration: migration.config)
let realmUrl = migration.config.fileURL!
let realmUrls = [
realmUrl,
realmUrl.appendingPathExtension("lock"),
realmUrl.appendingPathExtension("note"),
realmUrl.appendingPathExtension("management")
]
for each in realmUrls {
try? FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.none], ofItemAtPath: each.relativePath)
}
return realm
}
}
extension Realm {
static func shared(name: String = "Shared") -> Realm {
let configuration = RealmConfiguration.configuration(name: name)
return try! Realm(configuration: configuration)
}
public func safeWrite(_ block: (() throws -> Void)) throws {
if isInWriteTransaction {
try block()

@ -447,29 +447,3 @@ func compositeTokenName(forContract contract: AlphaWallet.Address, fromContractN
}
return compositeName
}
extension Wallet {
class functional {}
}
extension Wallet.functional {
static func realm(forAccount account: Wallet) -> Realm {
let migration = DatabaseMigration(account: account)
migration.perform()
let realm = try! Realm(configuration: migration.config)
let realmUrl = migration.config.fileURL!
let realmUrls = [
realmUrl,
realmUrl.appendingPathExtension("lock"),
realmUrl.appendingPathExtension("note"),
realmUrl.appendingPathExtension("management")
]
for each in realmUrls {
try? FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.none], ofItemAtPath: each.relativePath)
}
return realm
}
}

Loading…
Cancel
Save