Skip to content

Commit

Permalink
cleanup vaultservice
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldavidw committed Jun 18, 2024
1 parent 2619c97 commit de6d0f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Chronos/Services/CryptoService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class CryptoService {
}
}

func unwrapMasterKeyWithUserPassword(password: [UInt8], isRestore: Bool = false) async -> Bool {
guard let vault = vaultService.getVault(context: nil, isRestore: isRestore) else {
func unwrapMasterKeyWithUserPassword(password: [UInt8], isRestore _: Bool = false) async -> Bool {
guard let vault = vaultService.getVaultFromCloudContainer() else {
return false
}

Expand Down
29 changes: 26 additions & 3 deletions Chronos/Services/VaultService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public class VaultService {
}
}

func getVault(context: ModelContext? = nil, isRestore: Bool = false) -> Vault? {
func getVaultFromCloudContainer() -> Vault? {
guard let vaultId: UUID = stateService.getVaultId() else {
logger.error("vaultId not found in AppStorage")
return nil
}

let predicate = #Predicate<Vault> { $0.vaultId == vaultId }
let _context = context ?? ModelContext(swiftDataService.getModelContainer(isRestore: isRestore))
let context = ModelContext(swiftDataService.getCloudModelContainer())

do {
let vaultArr = try _context.fetch(FetchDescriptor<Vault>(predicate: predicate))
let vaultArr = try context.fetch(FetchDescriptor<Vault>(predicate: predicate))
if let vault = vaultArr.first {
logger.info("Returning vault \(vault.name)")
return vault
Expand All @@ -53,6 +53,29 @@ public class VaultService {
}

extension VaultService {
private func getVault(context: ModelContext) -> Vault? {
guard let vaultId: UUID = stateService.getVaultId() else {
logger.error("vaultId not found in AppStorage")
return nil
}

let predicate = #Predicate<Vault> { $0.vaultId == vaultId }

do {
let vaultArr = try context.fetch(FetchDescriptor<Vault>(predicate: predicate))
if let vault = vaultArr.first {
logger.info("Returning vault \(vault.name)")
return vault
} else {
logger.error("No vaults found with the given vaultId")
return nil
}
} catch {
logger.error("Failed to fetch vaults: \(error.localizedDescription)")
return nil
}
}

func insertEncryptedToken(_ encryptedToken: EncryptedToken) {
let context = ModelContext(swiftDataService.getModelContainer())

Expand Down

0 comments on commit de6d0f3

Please sign in to comment.