-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16f90c5
commit 53d4e1e
Showing
6 changed files
with
77 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Factory | ||
import Foundation | ||
import SwiftData | ||
import Logging | ||
|
||
public class VaultService { | ||
private let logger = Logger(label: "VaultService") | ||
|
||
private let stateService = Container.shared.stateService() | ||
private let swiftDataService = Container.shared.swiftDataService() | ||
|
||
// TODO(joeldavidw): Selects first vault for now. Selection page should be shown if there are more than one vault. | ||
func getFirstVault() -> Vault? { | ||
let context = ModelContext(swiftDataService.getCloudModelContainer()) | ||
|
||
guard let vaultArr = try? context.fetch(FetchDescriptor<Vault>(sortBy: [SortDescriptor(\.createdAt)])) else { | ||
logger.error("No vaults found") | ||
return nil | ||
} | ||
|
||
guard let vault = vaultArr.first else { | ||
logger.error("Empty vaultArr") | ||
return nil | ||
} | ||
|
||
stateService.setVaultId(vaultId: vault.vaultId!) | ||
|
||
return vault | ||
} | ||
|
||
func getVault() -> Vault? { | ||
guard let vaultId: UUID = stateService.getVaultId() else { | ||
logger.error("vaultId not found in AppStorage") | ||
return nil | ||
} | ||
|
||
let context = ModelContext(swiftDataService.getModelContainer()) | ||
|
||
let predicate = #Predicate<Vault> { $0.vaultId == vaultId } | ||
|
||
guard let vaultArr = try? context.fetch(FetchDescriptor<Vault>(predicate: predicate)) else { | ||
logger.error("No vaults found") | ||
return nil | ||
} | ||
|
||
return vaultArr.first | ||
} | ||
} |