DHBW-Service-App/DHBW-Service/Views/Other/FirstOpeningSettings.swift

73 lines
2.8 KiB
Swift
Raw Normal View History

2020-12-21 20:01:12 +00:00
//
// FirstOpeningSettings.swift
// DHBW-Service
//
// Created by Patrick Müller on 21.12.20.
//
import SwiftUI
2020-12-21 20:48:26 +00:00
import CoreData
2020-12-21 20:01:12 +00:00
struct FirstOpeningSettings: View {
@EnvironmentObject var settings: LocalSettings
2020-12-21 20:48:26 +00:00
@State private var name = ""
@State private var course = ""
@State private var director = ""
2020-12-21 20:01:12 +00:00
var body: some View {
2020-12-21 20:48:26 +00:00
VStack {
2020-12-22 10:47:40 +00:00
TextField("name".localized(tableName: "General", plural: false), text: self.$name)
2020-12-21 20:48:26 +00:00
.textContentType(.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
2020-12-22 10:47:40 +00:00
TextField("course".localized(tableName: "General", plural: false), text: self.$course)
2020-12-21 20:48:26 +00:00
.textContentType(.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
2020-12-22 10:47:40 +00:00
TextField("director".localized(tableName: "General", plural: false), text: self.$director)
2020-12-21 20:48:26 +00:00
.textContentType(.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
Button(action: {
self.settings.isFirstOpening = !self.settings.isFirstOpening
self.saveToCoreData()
2020-12-21 20:48:26 +00:00
}){
Text("Confirm")
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(15)
}
2020-12-21 20:01:12 +00:00
}
}
}
2020-12-21 20:48:26 +00:00
extension FirstOpeningSettings{
func saveToCoreData(){
// Delete old user data
let status = UtilityFunctions.deleteAllCoreDataEntitiesOfType(type: "User")
print("Deleting old user data status: \(status)")
// Insert new user data
2020-12-21 20:48:26 +00:00
let entity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)!
let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context)
user.setValue(name, forKey: "name")
user.setValue(course, forKey: "course")
user.setValue(director, forKey: "director")
PersistenceController.shared.save()
}
}
2020-12-21 20:01:12 +00:00
struct FirstOpeningSettings_Previews: PreviewProvider {
static var previews: some View {
FirstOpeningSettings()
2020-12-21 20:48:26 +00:00
.preferredColorScheme(.dark)
.environmentObject(LocalSettings())
2020-12-21 20:01:12 +00:00
}
}