Force sync configuration upon enabling multi device or clearing data

pull/333/head
nielsandriesse 5 years ago
parent 9eefc21eb9
commit 3ec4e0ae52

@ -1,7 +1,9 @@
import PromiseKit
extension AppDelegate { extension AppDelegate {
@objc func syncConfigurationIfNeeded() { @objc(syncConfigurationIfNeeded)
func syncConfigurationIfNeeded() {
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
guard userDefaults[.isUsingMultiDevice] else { return } guard userDefaults[.isUsingMultiDevice] else { return }
let lastSync = userDefaults[.lastConfigurationSync] ?? .distantPast let lastSync = userDefaults[.lastConfigurationSync] ?? .distantPast
@ -13,4 +15,20 @@ extension AppDelegate {
JobQueue.shared.add(job, using: transaction) JobQueue.shared.add(job, using: transaction)
} }
} }
func forceSyncConfigurationNowIfNeeded() -> Promise<Void> {
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<Void>.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
}
} }

@ -148,6 +148,9 @@ final class MultiDeviceVC : BaseVC {
// MARK: Interaction // MARK: Interaction
@objc private func handleToggle() { @objc private func handleToggle() {
stepsRow.isHidden = !toggle.isOn stepsRow.isHidden = !toggle.isOn
UserDefaults.standard[.isUsingMultiDevice] = toggle.isOn
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete()
} }
@objc private func copyMnemonic() { @objc private func copyMnemonic() {

@ -50,8 +50,14 @@ final class NukeDataModal : Modal {
// MARK: Interaction // MARK: Interaction
@objc private func nuke() { @objc private func nuke() {
func proceed() { func proceed() {
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 UserDefaults.removeAll() // Not done in the nuke data implementation as unlinking requires this to happen later
NotificationCenter.default.post(name: .dataNukeRequested, object: nil) NotificationCenter.default.post(name: .dataNukeRequested, object: nil)
}.retainUntilComplete()
}
} }
if KeyPairUtilities.hasV2KeyPair() { if KeyPairUtilities.hasV2KeyPair() {
proceed() proceed()

Loading…
Cancel
Save