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
|
@Attribute(.unique) var publicKey: String
|
||||||
|
|
||||||
var nostrClientModels: [NostrClientModel] = []
|
var nostrClientModels: [NostrClientModel] = []
|
||||||
|
var signingPolicy: SigningPolicy?
|
||||||
|
|
||||||
init(publicKey: String) {
|
init(publicKey: String) {
|
||||||
self.publicKey = publicKey
|
self.publicKey = publicKey
|
||||||
|
|||||||
@@ -10,12 +10,18 @@ import SwiftData
|
|||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
@Environment(\.modelContext) private var modelContext
|
@Environment(\.modelContext) private var modelContext
|
||||||
|
@Query var generalSettingsModels: [GeneralSettingsModel]
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
// OnboardingView()
|
Group {
|
||||||
|
if generalSettingsModels.first!.activePublicKey == nil {
|
||||||
|
OnboardingView()
|
||||||
|
} else {
|
||||||
LoggedInView()
|
LoggedInView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
ContentView()
|
ContentView()
|
||||||
|
|||||||
@@ -5,11 +5,18 @@
|
|||||||
// Created by Terry Yiu on 1/20/25.
|
// Created by Terry Yiu on 1/20/25.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import NostrSDK
|
||||||
|
import SwiftData
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct SigningPolicyConfirmationView: View {
|
struct SigningPolicyConfirmationView: View {
|
||||||
|
let keypair: Keypair
|
||||||
let signingPolicy: SigningPolicy
|
let signingPolicy: SigningPolicy
|
||||||
|
|
||||||
|
@Environment(\.modelContext) private var modelContext
|
||||||
|
|
||||||
|
@Query private var generalSettingsModels: [GeneralSettingsModel]
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
Text("Hooo-raaaayyy!", comment: "Title of view that confirms the user’s selection of the signing policy.")
|
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)
|
.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)
|
.toolbar(.hidden)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
SigningPolicyConfirmationView(signingPolicy: .basic)
|
SigningPolicyConfirmationView(
|
||||||
|
keypair: Keypair()!,
|
||||||
|
signingPolicy: .basic
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ struct SigningPolicySelectionView: View {
|
|||||||
)
|
)
|
||||||
|
|
||||||
Button("Done", action: {
|
Button("Done", action: {
|
||||||
PrivateKeySecureStorage.shared.store(for: keypair)
|
|
||||||
navigationDestinationPresented = true
|
navigationDestinationPresented = true
|
||||||
})
|
})
|
||||||
.buttonStyle(.borderedProminent)
|
.buttonStyle(.borderedProminent)
|
||||||
@@ -64,7 +63,7 @@ struct SigningPolicySelectionView: View {
|
|||||||
.background(Color(UIColor.systemGroupedBackground))
|
.background(Color(UIColor.systemGroupedBackground))
|
||||||
}
|
}
|
||||||
.navigationDestination(isPresented: $navigationDestinationPresented) {
|
.navigationDestination(isPresented: $navigationDestinationPresented) {
|
||||||
SigningPolicyConfirmationView(signingPolicy: signingPolicy)
|
SigningPolicyConfirmationView(keypair: keypair, signingPolicy: signingPolicy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,23 +12,39 @@ import SwiftData
|
|||||||
struct YetiApp: App {
|
struct YetiApp: App {
|
||||||
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
|
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
|
||||||
|
|
||||||
var sharedModelContainer: ModelContainer = {
|
private let modelContainer: ModelContainer
|
||||||
|
|
||||||
|
init() {
|
||||||
let schema = Schema([
|
let schema = Schema([
|
||||||
|
GeneralSettingsModel.self,
|
||||||
ProfileSettingsModel.self
|
ProfileSettingsModel.self
|
||||||
])
|
])
|
||||||
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
return try ModelContainer(for: schema, configurations: [modelConfiguration])
|
modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
|
||||||
} catch {
|
} catch {
|
||||||
fatalError("Could not create ModelContainer: \(error)")
|
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 {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView()
|
ContentView()
|
||||||
}
|
}
|
||||||
.modelContainer(sharedModelContainer)
|
.modelContainer(modelContainer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user