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

113 lines
4.5 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-23 15:41:04 +00:00
@State private var invalidInputName = false
@State private var invalidInputCourse = false
2020-12-21 20:01:12 +00:00
var body: some View {
2020-12-21 20:48:26 +00:00
VStack {
2020-12-23 15:41:04 +00:00
Text("welcomeText".localized(tableName: "General", plural: false))
2020-12-22 10:47:40 +00:00
TextField("name".localized(tableName: "General", plural: false), text: self.$name)
2020-12-23 15:41:04 +00:00
.overlay(RoundedRectangle(cornerRadius: 10).stroke(invalidInputCourse ? Color.red : Color.secondary, lineWidth: 1))
.foregroundColor(invalidInputName ? .red : .primary)
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-23 15:41:04 +00:00
2020-12-23 16:17:26 +00:00
TextField("course".localized(tableName: "General", plural: false), text: self.$course)
2020-12-23 15:41:04 +00:00
.overlay(RoundedRectangle(cornerRadius: 10).stroke(invalidInputCourse ? Color.red : Color.secondary, lineWidth: 1))
.onChange(of: course, perform: { value in
self.setDirector()
})
.foregroundColor(invalidInputCourse ? .red : .primary)
2020-12-21 20:48:26 +00:00
.textContentType(.name)
2020-12-23 15:41:04 +00:00
.disableAutocorrection(true)
2020-12-21 20:48:26 +00:00
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
2020-12-23 15:41:04 +00:00
TextField("director".localized(tableName: "General", plural: false) + " (" + "filledAuto".localized(tableName: "General", plural: false) + ")", text: self.$director)
.foregroundColor(.primary)
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-23 15:41:04 +00:00
.disabled(true)
2020-12-23 16:17:26 +00:00
2020-12-21 20:48:26 +00:00
Button(action: {
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{
2020-12-23 15:41:04 +00:00
func setDirector() {
if (course == "TINF19B4") {
director = "Jörn Eisenbiegler"
} else {
director = ""
}
}
func checkInput() -> Bool {
let nameRegex = try! NSRegularExpression(pattern: "[a-zA-ZÄÖÜäöüß]+")
let courseRegex = try! NSRegularExpression(pattern: "[T|G|W][A-Z]{2,3}[0-9]{2}B[1-9]")
let nameRange = NSRange(location: 0, length: name.utf16.count)
let courseRange = NSRange(location: 0, length: course.utf16.count)
invalidInputName = nameRegex.firstMatch(in: name, options: [], range: nameRange) == nil
invalidInputCourse = courseRegex.firstMatch(in: course, options: [], range: courseRange) == nil
return !invalidInputName && !invalidInputCourse
}
2020-12-21 20:48:26 +00:00
func saveToCoreData(){
2020-12-23 15:41:04 +00:00
if (!self.checkInput()) {
print("Input invalid")
return
}
// 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")
2020-12-23 15:41:04 +00:00
self.settings.isFirstOpening = !self.settings.isFirstOpening
2020-12-21 20:48:26 +00:00
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
}
}