Skip to content

Commit

Permalink
Refactor so reason is always needed for removal
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme committed Nov 8, 2024
1 parent 1ee048c commit f1bf0eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
19 changes: 8 additions & 11 deletions Sources/DDGSync/DDGSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public class DDGSync: DDGSyncing {
}
do {
try await disconnect(deviceId: deviceId)
try removeAccount()
dependencies.errorEvents.fire(.accountRemoved(.userTurnedOffSync))
try removeAccount(reason: .userTurnedOffSync)
} catch {
try handleUnauthenticated(error)
}
Expand Down Expand Up @@ -181,8 +180,7 @@ public class DDGSync: DDGSyncing {

do {
try await dependencies.account.deleteAccount(account)
try removeAccount()
dependencies.errorEvents.fire(.accountRemoved(.userDeletedAccount))
try removeAccount(reason: .userDeletedAccount)
} catch {
try handleUnauthenticated(error)
}
Expand All @@ -196,8 +194,7 @@ public class DDGSync: DDGSyncing {
}

public func updateServerEnvironment(_ serverEnvironment: ServerEnvironment) {
try? removeAccount()
dependencies.errorEvents.fire(.accountRemoved(.userDeletedAccount))
try? removeAccount(reason: .serverEnvironmentUpdated)
dependencies.updateServerEnvironment(serverEnvironment)
authState = .initializing
initializeIfNeeded()
Expand Down Expand Up @@ -247,8 +244,7 @@ public class DDGSync: DDGSyncing {

guard let account else {
do {
try removeAccount()
dependencies.errorEvents.fire(.accountRemoved(.syncEnabledNotSetOnKeyValueStore))
try removeAccount(reason: .notFoundInSecureStorage)
} catch {
dependencies.errorEvents.fire(.failedToRemoveAccount, error: error)
}
Expand All @@ -269,7 +265,7 @@ public class DDGSync: DDGSyncing {
}

guard account.state != .inactive else {
try removeAccount()
try removeAccount(reason: .authStateInactive)
return
}

Expand Down Expand Up @@ -337,7 +333,7 @@ public class DDGSync: DDGSyncing {
self.syncQueue = syncQueue
}

private func removeAccount() throws {
private func removeAccount(reason: SyncError.AccountRemovedReason) throws {
dependencies.scheduler.isEnabled = false
startSyncCancellable?.cancel()
syncQueueCancellable?.cancel()
Expand All @@ -347,6 +343,7 @@ public class DDGSync: DDGSyncing {
authState = .inactive
try dependencies.secureStore.removeAccount()
dependencies.keyValueStore.set(nil, forKey: Constants.syncEnabledKey)
dependencies.errorEvents.fire(.accountRemoved(reason))
}

private func handleUnauthenticated(_ error: Error) throws {
Expand All @@ -357,7 +354,7 @@ public class DDGSync: DDGSyncing {
}

do {
try removeAccount()
try removeAccount(reason: .unauthenticatedRequest)
throw SyncError.unauthenticatedWhileLoggedIn
} catch {
Logger.sync.error("Failed to delete account upon unauthenticated server response: \(error.localizedDescription, privacy: .public)")
Expand Down
13 changes: 8 additions & 5 deletions Sources/DDGSync/SyncError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import Foundation

public enum SyncError: Error, Equatable {

public enum AccountRemovedReason: String {
case syncEnabledNotSetOnKeyValueStore
case userTurnedOffSync
case userDeletedAccount
case serverEnvironmentUpdated
public enum AccountRemovedReason: String, Equatable {
case authStateInactive = "auth-state-inactive"
case syncEnabledNotSetOnKeyValueStore = "not-set-on-key-value-store"
case notFoundInSecureStorage = "not-found-in-secure-storage"
case userTurnedOffSync = "user-turned-off"
case userDeletedAccount = "user-deleted-account"
case unauthenticatedRequest = "unauthenticated-request"
case serverEnvironmentUpdated = "server-environment-updated"
}

case noToken
Expand Down

0 comments on commit f1bf0eb

Please sign in to comment.