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 {
|
2021-02-02 21:32:32 +00:00
|
|
|
Text("welcomeText".localized(tableName: "General"))
|
2020-12-28 22:12:09 +00:00
|
|
|
|
2021-02-02 21:32:32 +00:00
|
|
|
TextField("name".localized(tableName: "General"), text: self.$name)
|
2020-12-28 22:12:09 +00:00
|
|
|
.overlay(RoundedRectangle(cornerRadius: 10).stroke(invalidInputName ? Color.red : Color.secondary, lineWidth: 1))
|
2020-12-23 15:41:04 +00:00
|
|
|
.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
|
|
|
|
2021-02-02 21:32:32 +00:00
|
|
|
TextField("course".localized(tableName: "General"), 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()
|
2021-02-07 13:57:56 +00:00
|
|
|
self.course = self.course.uppercased()
|
2020-12-23 15:41:04 +00:00
|
|
|
})
|
|
|
|
.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
|
|
|
|
2021-02-02 21:32:32 +00:00
|
|
|
TextField("director".localized(tableName: "General") + " (" + "filledAuto".localized(tableName: "General") + ")", text: self.$director)
|
2020-12-23 15:41:04 +00:00
|
|
|
.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: {
|
2020-12-22 10:30:41 +00:00
|
|
|
self.saveToCoreData()
|
2021-01-08 10:30:07 +00:00
|
|
|
self.checkAppIcon()
|
2020-12-21 20:48:26 +00:00
|
|
|
}){
|
|
|
|
Text("Confirm")
|
|
|
|
.padding()
|
|
|
|
.foregroundColor(.white)
|
|
|
|
.background(Color.blue)
|
|
|
|
.cornerRadius(15)
|
|
|
|
}
|
2020-12-28 22:12:09 +00:00
|
|
|
//.disabled() //TODO: Check all inputs before enabling the button
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:30:41 +00:00
|
|
|
// 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()
|
|
|
|
}
|
2021-01-08 10:30:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check the input and change app icon if necessary
|
|
|
|
*/
|
|
|
|
func checkAppIcon() {
|
|
|
|
if(self.name.lowercased().contains("alpaca")) {
|
|
|
|
UIApplication.shared.setAlternateIconName("Alpaca-Alt-Icon") { error in
|
|
|
|
if let error = error {
|
|
|
|
print("Error changing app icon:")
|
|
|
|
print(error.localizedDescription)
|
|
|
|
} else {
|
|
|
|
print("Successfully changed app icon!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(UIApplication.shared.alternateIconName == "Alpaca-Alt-Icon" && !self.name.lowercased().contains("alpaca")) {
|
|
|
|
UIApplication.shared.setAlternateIconName(nil) { error in
|
|
|
|
if let error = error {
|
|
|
|
print("Error changing app icon:")
|
|
|
|
print(error.localizedDescription)
|
|
|
|
} else {
|
|
|
|
print("Successfully changed app icon!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 20:48:26 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2021-02-07 14:51:01 +00:00
|
|
|
.previewDevice("iPhone 12")
|
2020-12-28 19:09:28 +00:00
|
|
|
.environmentObject(getFirstOpening())
|
|
|
|
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getFirstOpening() -> LocalSettings {
|
|
|
|
let settings = LocalSettings();
|
|
|
|
settings.isFirstOpening = false;
|
|
|
|
return settings
|
2020-12-21 20:01:12 +00:00
|
|
|
}
|
|
|
|
}
|