Add GeneralSettingsModel and concept of active profile
This commit is contained in:
16
Yeti/Models/GeneralSettingsModel.swift
Normal file
16
Yeti/Models/GeneralSettingsModel.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// GeneralSettingsModel.swift
|
||||
// Yeti
|
||||
//
|
||||
// Created by Terry Yiu on 1/21/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class GeneralSettingsModel {
|
||||
var activePublicKey: String?
|
||||
|
||||
init() { }
|
||||
}
|
||||
@@ -13,6 +13,7 @@ final class ProfileSettingsModel {
|
||||
@Attribute(.unique) var publicKey: String
|
||||
|
||||
var nostrClientModels: [NostrClientModel] = []
|
||||
var signingPolicy: SigningPolicy?
|
||||
|
||||
init(publicKey: String) {
|
||||
self.publicKey = publicKey
|
||||
|
||||
@@ -10,11 +10,17 @@ import SwiftData
|
||||
|
||||
struct ContentView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query var generalSettingsModels: [GeneralSettingsModel]
|
||||
|
||||
var body: some View {
|
||||
// OnboardingView()
|
||||
Group {
|
||||
if generalSettingsModels.first!.activePublicKey == nil {
|
||||
OnboardingView()
|
||||
} else {
|
||||
LoggedInView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
|
||||
@@ -5,11 +5,18 @@
|
||||
// Created by Terry Yiu on 1/20/25.
|
||||
//
|
||||
|
||||
import NostrSDK
|
||||
import SwiftData
|
||||
import SwiftUI
|
||||
|
||||
struct SigningPolicyConfirmationView: View {
|
||||
let keypair: Keypair
|
||||
let signingPolicy: SigningPolicy
|
||||
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
@Query private var generalSettingsModels: [GeneralSettingsModel]
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Hooo-raaaayyy!", comment: "Title of view that confirms the user’s selection of the signing policy.")
|
||||
@@ -34,11 +41,25 @@ You’re set for now. You’ll need to come back here with every new app and app
|
||||
)
|
||||
.font(.caption)
|
||||
}
|
||||
|
||||
Button("Done", action: {
|
||||
PrivateKeySecureStorage.shared.store(for: keypair)
|
||||
|
||||
let profileSettingsModel = ProfileSettingsModel(publicKey: keypair.publicKey.hex)
|
||||
profileSettingsModel.signingPolicy = signingPolicy
|
||||
modelContext.insert(profileSettingsModel)
|
||||
|
||||
generalSettingsModels.first!.activePublicKey = keypair.publicKey.hex
|
||||
})
|
||||
.buttonStyle(.borderedProminent)
|
||||
}
|
||||
.toolbar(.hidden)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SigningPolicyConfirmationView(signingPolicy: .basic)
|
||||
SigningPolicyConfirmationView(
|
||||
keypair: Keypair()!,
|
||||
signingPolicy: .basic
|
||||
)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ struct SigningPolicySelectionView: View {
|
||||
)
|
||||
|
||||
Button("Done", action: {
|
||||
PrivateKeySecureStorage.shared.store(for: keypair)
|
||||
navigationDestinationPresented = true
|
||||
})
|
||||
.buttonStyle(.borderedProminent)
|
||||
@@ -64,7 +63,7 @@ struct SigningPolicySelectionView: View {
|
||||
.background(Color(UIColor.systemGroupedBackground))
|
||||
}
|
||||
.navigationDestination(isPresented: $navigationDestinationPresented) {
|
||||
SigningPolicyConfirmationView(signingPolicy: signingPolicy)
|
||||
SigningPolicyConfirmationView(keypair: keypair, signingPolicy: signingPolicy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,23 +12,39 @@ import SwiftData
|
||||
struct YetiApp: App {
|
||||
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
|
||||
|
||||
var sharedModelContainer: ModelContainer = {
|
||||
private let modelContainer: ModelContainer
|
||||
|
||||
init() {
|
||||
let schema = Schema([
|
||||
GeneralSettingsModel.self,
|
||||
ProfileSettingsModel.self
|
||||
])
|
||||
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
||||
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: [modelConfiguration])
|
||||
modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
|
||||
} catch {
|
||||
fatalError("Could not create ModelContainer: \(error)")
|
||||
}
|
||||
}()
|
||||
|
||||
var descriptor = FetchDescriptor<GeneralSettingsModel>()
|
||||
descriptor.fetchLimit = 1
|
||||
|
||||
if (try? modelContainer.mainContext.fetch(descriptor))?.first == nil {
|
||||
let newGeneralSettingsModel = GeneralSettingsModel()
|
||||
modelContainer.mainContext.insert(newGeneralSettingsModel)
|
||||
do {
|
||||
try modelContainer.mainContext.save()
|
||||
} catch {
|
||||
fatalError("Unable to save initial GeneralSettingsModel.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
.modelContainer(sharedModelContainer)
|
||||
.modelContainer(modelContainer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user