diff --git a/Session/Meta/AppDelegate.swift b/Session/Meta/AppDelegate.swift index f501d4fbe..a97b8aaf2 100644 --- a/Session/Meta/AppDelegate.swift +++ b/Session/Meta/AppDelegate.swift @@ -1,7 +1,9 @@ +import PromiseKit extension AppDelegate { - @objc func syncConfigurationIfNeeded() { + @objc(syncConfigurationIfNeeded) + func syncConfigurationIfNeeded() { let userDefaults = UserDefaults.standard guard userDefaults[.isUsingMultiDevice] else { return } let lastSync = userDefaults[.lastConfigurationSync] ?? .distantPast @@ -13,4 +15,20 @@ extension AppDelegate { JobQueue.shared.add(job, using: transaction) } } + + func forceSyncConfigurationNowIfNeeded() -> Promise { + let userDefaults = UserDefaults.standard + guard userDefaults[.isUsingMultiDevice] else { return Promise.value(()) } + let configurationMessage = ConfigurationMessage.getCurrent() + let destination = Message.Destination.contact(publicKey: getUserHexEncodedPublicKey()) + let (promise, seal) = Promise.pending() + Storage.write { transaction in + MessageSender.send(configurationMessage, to: destination, using: transaction).done { + seal.fulfill(()) + }.catch { _ in + seal.fulfill(()) // Fulfill even if this failed; the configuration in the swarm should be at most 2 days old + }.retainUntilComplete() + } + return promise + } } diff --git a/Session/Settings/MultiDeviceVC.swift b/Session/Settings/MultiDeviceVC.swift index 8efc9bca8..0ab5da706 100644 --- a/Session/Settings/MultiDeviceVC.swift +++ b/Session/Settings/MultiDeviceVC.swift @@ -148,6 +148,9 @@ final class MultiDeviceVC : BaseVC { // MARK: Interaction @objc private func handleToggle() { stepsRow.isHidden = !toggle.isOn + UserDefaults.standard[.isUsingMultiDevice] = toggle.isOn + let appDelegate = UIApplication.shared.delegate as! AppDelegate + appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete() } @objc private func copyMnemonic() { diff --git a/Session/Settings/NukeDataModal.swift b/Session/Settings/NukeDataModal.swift index 2ae012e10..8eaf5e24a 100644 --- a/Session/Settings/NukeDataModal.swift +++ b/Session/Settings/NukeDataModal.swift @@ -50,8 +50,14 @@ final class NukeDataModal : Modal { // MARK: Interaction @objc private func nuke() { func proceed() { - UserDefaults.removeAll() // Not done in the nuke data implementation as unlinking requires this to happen later - NotificationCenter.default.post(name: .dataNukeRequested, object: nil) + let appDelegate = UIApplication.shared.delegate as! AppDelegate + ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in + appDelegate.forceSyncConfigurationNowIfNeeded().done(on: DispatchQueue.main) { + self?.dismiss(animated: true, completion: nil) // Dismiss the loader + UserDefaults.removeAll() // Not done in the nuke data implementation as unlinking requires this to happen later + NotificationCenter.default.post(name: .dataNukeRequested, object: nil) + }.retainUntilComplete() + } } if KeyPairUtilities.hasV2KeyPair() { proceed()