Skip to content

Commit

Permalink
WIP. Having issues with container
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldavidw committed Jun 14, 2024
1 parent 67b0622 commit b95d726
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Chronos/App/Onboarding/PasswordSetupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ extension PasswordSetupView {

let vault = Vault(vaultId: UUID(), createdAt: Date(), chronosCryptos: [], encryptedTokens: [])
modelContext.insert(vault)
try? modelContext.save()

stateService.setVaultId(vaultId: vault.vaultId!)

await cryptoService.wrapMasterKeyWithUserPassword(password: Array(password.utf8))
nextBtnPressed = true
Expand Down
8 changes: 6 additions & 2 deletions Chronos/App/Onboarding/RestoreBackupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct RestoreBackupView: View {
@FocusState private var focusedField: FocusedField?

let cryptoService = Container.shared.cryptoService()
let vaultService = Container.shared.vaultService()
let stateService = Container.shared.stateService()

var body: some View {
VStack {
Expand Down Expand Up @@ -58,11 +60,13 @@ struct RestoreBackupView: View {
restoreBtnPressed = false

if passwordVerified {
let vault = vaultService.getFirstVault()
stateService.setVaultId(vaultId: vault!.vaultId!)

isICloudEnabled = true
} else {
passwordInvalid = true
let notificationGenerator = UINotificationFeedbackGenerator()
notificationGenerator.notificationOccurred(.error)
UINotificationFeedbackGenerator().notificationOccurred(.error)
}
}
} label: {
Expand Down
4 changes: 0 additions & 4 deletions Chronos/App/Privacy/PrivacyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ struct PrivacyView: View {
.background(Color(red: 0.04, green: 0, blue: 0.11))
}
}

#Preview {
PrivacyView()
}
14 changes: 12 additions & 2 deletions Chronos/App/Tabs/Tokens/TokensTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI

struct TokensTab: View {
@Query(sort: \EncryptedToken.createdAt) private var encyptedTokens: [EncryptedToken]

@State private var showTokenAddSheet = false
@State private var showTokenUpdateSheet = false
@State private var showTokenDeleteSheet = false
Expand All @@ -13,11 +13,19 @@ struct TokensTab: View {
@State private var selectedTokenForUpdate: Token? = nil
@State var detentHeight: CGFloat = 0

let stateService = Container.shared.stateService()

private var filteredEncyptedTokens: [EncryptedToken] {
return encyptedTokens.compactMap { encToken in
return encToken.vault?.vaultId == stateService.getVaultId() ? encToken : nil
}
}

var body: some View {
ZStack {
NavigationStack {
ScrollViewReader { _ in
List(encyptedTokens) { encyptedToken in
List(filteredEncyptedTokens) { encyptedToken in
TokenRowView(tokenRowViewModel: TokenRowViewModel(encyptedToken: encyptedToken))
}
.listStyle(.plain)
Expand All @@ -40,6 +48,8 @@ struct TokensTab: View {
.presentationDetents([.height(self.detentHeight)])
}
}
}.onAppear {
stateService.getAllStates()
}
}
}
Expand Down
34 changes: 22 additions & 12 deletions Chronos/Services/StateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,53 @@ import Foundation

enum StateEnum: String {
case VAULT_ID

Check warning on line 6 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
case ICLOUD_BACKUP_ENABLED
case BIOMETRICS_AUTH_ENABLED
case ONBOARDING_COMPLETED

Check warning on line 10 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
case LAST_BIOMETRICS_AUTH_ATTEMPT
}

public class StateService {
private let defaults = UserDefaults.standard

Check warning on line 16 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
var masterKey: SecureBytes = .init(bytes: [])

Check warning on line 18 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
func setVaultId(vaultId: UUID) {
defaults.setValue(vaultId.uuidString, forKey: StateEnum.VAULT_ID.rawValue)
}

Check warning on line 22 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
func getVaultId() -> UUID? {
guard let vaultIdStr = defaults.string(forKey: StateEnum.VAULT_ID.rawValue) else {
return nil
}

Check warning on line 27 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
return UUID(uuidString: vaultIdStr)
}

Check warning on line 30 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
func resetAllStates() {
defaults.setValue(false, forKey: StateEnum.VAULT_ID.rawValue)
defaults.setValue(nil, forKey: StateEnum.VAULT_ID.rawValue)

Check warning on line 33 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
defaults.setValue(false, forKey: StateEnum.ICLOUD_BACKUP_ENABLED.rawValue)
defaults.setValue(false, forKey: StateEnum.BIOMETRICS_AUTH_ENABLED.rawValue)
defaults.setValue(false, forKey: StateEnum.ONBOARDING_COMPLETED.rawValue)

defaults.setValue(0, forKey: StateEnum.LAST_BIOMETRICS_AUTH_ATTEMPT.rawValue)

Check warning on line 37 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
defaults.setValue(Date().timeIntervalSince1970, forKey: StateEnum.LAST_BIOMETRICS_AUTH_ATTEMPT.rawValue)

Check warning on line 39 in Chronos/Services/StateService.swift

View workflow job for this annotation

GitHub Actions / Lint

Remove trailing space at end of a line. (trailingSpace)
masterKey.clear()
}

func clearMasterKey() {
masterKey.clear()
}

func getAllStates() {
print("DUCK")
print(defaults.value(forKey: StateEnum.VAULT_ID.rawValue))
print(defaults.value(forKey: StateEnum.ICLOUD_BACKUP_ENABLED.rawValue))
print(defaults.value(forKey: StateEnum.BIOMETRICS_AUTH_ENABLED.rawValue))
print(defaults.value(forKey: StateEnum.ONBOARDING_COMPLETED.rawValue))
print(defaults.value(forKey: StateEnum.LAST_BIOMETRICS_AUTH_ATTEMPT.rawValue))
}
}
11 changes: 6 additions & 5 deletions Chronos/Services/SwiftDataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Logging
import SwiftData

public class SwiftDataService {
let logger = Logger(label: "SwiftDataModelSingleton")
private let logger = Logger(label: "SwiftDataModelSingleton")

let defaults = UserDefaults.standard
private let defaults = UserDefaults.standard

private lazy var localModelContainer: ModelContainer = setupModelContainer(storeName: "localChronos.sqlite", cloudKitDatabase: .none)

private lazy var cloudModelContainer: ModelContainer = setupModelContainer(storeName: "onlineChronos.sqlite", cloudKitDatabase: .automatic)

let schema = Schema([Vault.self, ChronosCrypto.self, EncryptedToken.self])
private let schema = Schema([Vault.self, ChronosCrypto.self, EncryptedToken.self])

init() {
_ = localModelContainer
Expand Down Expand Up @@ -71,8 +71,9 @@ extension SwiftDataService {
}

func deleteLocalChronosCryptoData() {
let container = getLocalModelContainer()
container.deleteAllData()
getLocalModelContainer().deleteAllData()
getCloudModelContainer().deleteAllData()
resetModelContainers()
}

func deleteCloudChronosCryptoData() -> Bool {
Expand Down
4 changes: 1 addition & 3 deletions Chronos/Services/VaultService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class VaultService {

// 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())
let context = ModelContext(swiftDataService.getModelContainer())

guard let vaultArr = try? context.fetch(FetchDescriptor<Vault>(sortBy: [SortDescriptor(\.createdAt)])) else {
logger.error("No vaults found")
Expand All @@ -23,8 +23,6 @@ public class VaultService {
return nil
}

stateService.setVaultId(vaultId: vault.vaultId!)

return vault
}

Expand Down

0 comments on commit b95d726

Please sign in to comment.