Skip to content

Commit

Permalink
implement options visibility management (#1750)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/0/1205692136545021/f

**Description**: Make option visible in sync set up only for second
active device
  • Loading branch information
SabrinaTardio authored Oct 11, 2023
1 parent 00d891a commit 7ca4732
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
26 changes: 21 additions & 5 deletions DuckDuckGo/Preferences/Model/SyncPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,16 @@ extension SyncPreferences: ManagementDialogModelDelegate {
}

@MainActor
private func loginAndShowPresentedDialog(_ recoveryKey: SyncCode.RecoveryKey, shouldShowOptions: Bool) async throws {
private func loginAndShowPresentedDialog(_ recoveryKey: SyncCode.RecoveryKey, isActiveDevice: Bool) async throws {
let device = deviceInfo()
let knownDevices = Set(self.devices.map { $0.id })
let devices = try await syncService.login(recoveryKey, deviceName: device.name, deviceType: device.type)
mapDevices(devices)
let syncedDevices = self.devices.filter { !knownDevices.contains($0.id) && !$0.isCurrent }
let isSecondDevice = syncedDevices.count == 1

managementDialogModel.endFlow()
presentDialog(for: .deviceSynced(syncedDevices, shouldShowOptions: shouldShowOptions))
presentDialog(for: .deviceSynced(syncedDevices, shouldShowOptions: isActiveDevice && isSecondDevice))
}

func turnOnSync() {
Expand All @@ -300,7 +301,7 @@ extension SyncPreferences: ManagementDialogModelDelegate {
self.connector = try syncService.remoteConnect()
self.codeToDisplay = connector?.code
if let recoveryKey = try await connector?.pollForRecoveryKey() {
try await loginAndShowPresentedDialog(recoveryKey, shouldShowOptions: false)
try await loginAndShowPresentedDialog(recoveryKey, isActiveDevice: false)
} else {
// Polling was likeley cancelled elsewhere (e.g. dialog closed)
return
Expand All @@ -318,7 +319,7 @@ extension SyncPreferences: ManagementDialogModelDelegate {
self.connector = nil
}

func recoverDevice(using recoveryCode: String) {
func recoverDevice(using recoveryCode: String, isActiveDevice: Bool) {
Task { @MainActor in
do {
guard let syncCode = try? SyncCode.decodeBase64String(recoveryCode) else {
Expand All @@ -327,14 +328,29 @@ extension SyncPreferences: ManagementDialogModelDelegate {
}
if let recoveryKey = syncCode.recovery {
// This will error if the account already exists, we don't have good UI for this just now
try await loginAndShowPresentedDialog(recoveryKey, shouldShowOptions: true)
try await loginAndShowPresentedDialog(recoveryKey, isActiveDevice: isActiveDevice)
} else if let connectKey = syncCode.connect {
if syncService.account == nil {
let device = deviceInfo()
try await syncService.createAccount(deviceName: device.name, deviceType: device.type)
}

try await syncService.transmitRecoveryKey(connectKey)
self.$devices
.removeDuplicates()
.dropFirst()
.prefix(1)
.sink { [weak self] devices in
guard let self else { return }
let thisDeviceName = deviceInfo().name
var syncedDevices: [SyncDevice] = []
for device in devices where device.name != thisDeviceName {
syncedDevices.append(device)
}

self.managementDialogModel.endFlow()
presentDialog(for: .deviceSynced(syncedDevices, shouldShowOptions: devices.count == 2))
}.store(in: &cancellables)

// The UI will update when the devices list changes.
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Combine
public protocol ManagementDialogModelDelegate: AnyObject {
var isUnifiedFavoritesEnabled: Bool { get set }

func recoverDevice(using recoveryCode: String)
func recoverDevice(using recoveryCode: String, isActiveDevice: Bool)
func saveRecoveryPDF()
func turnOffSync()
func updateDeviceName(_ name: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct RecoverAccountView: View {
@EnvironmentObject var model: ManagementDialogModel
@EnvironmentObject var recoveryCodeModel: RecoveryCodeViewModel
let isRecovery: Bool
let isActiveDevice: Bool
var instructionText: String {
if isRecovery {
return UserText.recoverSyncedDataExplanation
Expand All @@ -38,7 +39,7 @@ struct RecoverAccountView: View {
}

func submitRecoveryCode() {
model.delegate?.recoverDevice(using: recoveryCodeModel.recoveryCode)
model.delegate?.recoverDevice(using: recoveryCodeModel.recoveryCode, isActiveDevice: isActiveDevice)
}

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public struct ManagementDialog: View {
Group {
switch model.currentDialog {
case .recoverAccount:
RecoverAccountView(isRecovery: true)
RecoverAccountView(isRecovery: true, isActiveDevice: false)
case .manuallyEnterCode:
RecoverAccountView(isRecovery: false)
RecoverAccountView(isRecovery: false, isActiveDevice: true)
case .deviceSynced(let devices, let shouldShowOptions):
DeviceSyncedView(devices: devices, shouldShowOptions: shouldShowOptions, isSingleDevice: false)
case .firstDeviceSetup:
Expand Down

0 comments on commit 7ca4732

Please sign in to comment.