Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 6a631ff

Browse files
authored
Fix sync connect flow (#1195)
Task/Issue URL: https://app.asana.com/0/0/1204398270301335/f Tech Design URL: CC: **Description**: Fixes the connect flow. No BSK changes. **Steps to test this PR**: 1. On iOS (or "another" device) turn off sync, then turn on sync, choose sync another device and have it show a code 2. On macOS turn off sync, then turn on sync, choose sync another device and paste in the connect code from the other device 3. The UI will close and the other device will connect. Eventually the new device will appear in the UI. 4. Smoke test sync <!-- Tagging instructions If this PR isn't ready to be merged for whatever reason it should be marked with the `DO NOT MERGE` label (particularly if it's a draft) If it's pending Product Review/PFR, please add the `Pending Product Review` label. If at any point it isn't actively being worked on/ready for review/otherwise moving forward (besides the above PR/PFR exception) strongly consider closing it (or not opening it in the first place). If you decide not to close it, make sure it's labelled to make it clear the PRs state and comment with more information. --> --- ###### Internal references: [Pull Request Review Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f) [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943) [Pull Request Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f)
1 parent c1eaa24 commit 6a631ff

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

DuckDuckGo/Preferences/Model/SyncPreferences.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ final class SyncPreferences: ObservableObject, SyncUI.ManagementViewModel {
7373
@MainActor
7474
func presentShowOrEnterCodeDialog() {
7575
Task { @MainActor in
76+
let devicesAtStart = self.devices
77+
self.$devices
78+
.removeDuplicates()
79+
.dropFirst()
80+
.prefix(1)
81+
.sink { [weak self] _ in
82+
self?.managementDialogModel.endFlow()
83+
self?.objectWillChange.send()
84+
}.store(in: &cancellables)
7685
managementDialogModel.codeToDisplay = syncService.account?.recoveryCode
7786
presentDialog(for: .syncAnotherDevice)
7887
}
@@ -152,7 +161,6 @@ final class SyncPreferences: ObservableObject, SyncUI.ManagementViewModel {
152161
do {
153162
let registeredDevices = try await syncService.fetchDevices()
154163
mapDevices(registeredDevices)
155-
print("devices", self.devices)
156164
} catch {
157165
print("error", error.localizedDescription)
158166
}
@@ -207,7 +215,11 @@ extension SyncPreferences: ManagementDialogModelDelegate {
207215
func deleteAccount() {
208216
Task { @MainActor in
209217
managementDialogModel.endFlow()
210-
try await syncService.deleteAccount()
218+
do {
219+
try await syncService.deleteAccount()
220+
} catch {
221+
managementDialogModel.errorMessage = String(describing: error)
222+
}
211223
}
212224
}
213225

@@ -268,8 +280,21 @@ extension SyncPreferences: ManagementDialogModelDelegate {
268280
try await login(recoveryKey)
269281
presentDialog(for: .deviceSynced)
270282
} else if let connectKey = syncCode.connect {
271-
// Unclear what the UX is supposed to be here given everything is happening on the other device
283+
var isNewAccount = false
284+
if syncService.account == nil {
285+
let device = deviceInfo()
286+
try await syncService.createAccount(deviceName: device.name, deviceType: device.type)
287+
isNewAccount = true
288+
}
289+
272290
try await syncService.transmitRecoveryKey(connectKey)
291+
292+
if isNewAccount {
293+
presentDialog(for: .deviceSynced)
294+
} else {
295+
managementDialogModel.endFlow()
296+
}
297+
273298
} else {
274299
managementDialogModel.errorMessage = "Invalid code"
275300
return
@@ -327,7 +352,11 @@ extension SyncPreferences: ManagementDialogModelDelegate {
327352
guard response == .OK,
328353
let location = panel.url else { return }
329354

330-
try data.writeFileWithProgress(to: location)
355+
do {
356+
try data.writeFileWithProgress(to: location)
357+
} catch {
358+
managementDialogModel.errorMessage = String(describing: error)
359+
}
331360
}
332361

333362
}

0 commit comments

Comments
 (0)