Added Input Validation.

This commit is contained in:
David Huh 2020-12-23 16:41:04 +01:00
parent cacf1daf8e
commit 5b075dd90d
4 changed files with 54 additions and 16 deletions

View File

@ -551,7 +551,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\"";
DEVELOPMENT_TEAM = HS7KNT4MZ2;
DEVELOPMENT_TEAM = G3TRS8UHWN;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
@ -573,7 +573,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\"";
DEVELOPMENT_TEAM = HS7KNT4MZ2;
DEVELOPMENT_TEAM = G3TRS8UHWN;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;

View File

@ -8,3 +8,5 @@
"name" = "Name";
"course" = "Course";
"director" = "Director";
"filledAuto" = "filled automatically";
"welcomeText" = "Please enter your data in the following fields.";

View File

@ -13,27 +13,44 @@ struct FirstOpeningSettings: View {
@State private var name = ""
@State private var course = ""
@State private var director = ""
@State private var invalidInputName = false
@State private var invalidInputCourse = false
var body: some View {
VStack {
Text("welcomeText".localized(tableName: "General", plural: false))
TextField("name".localized(tableName: "General", plural: false), text: self.$name)
.overlay(RoundedRectangle(cornerRadius: 10).stroke(invalidInputCourse ? Color.red : Color.secondary, lineWidth: 1))
.foregroundColor(invalidInputName ? .red : .primary)
.textContentType(.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
TextField("course".localized(tableName: "General", plural: false), text: self.$course)
.textContentType(.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
TextField("director".localized(tableName: "General", plural: false), text: self.$director)
TextField("course".localized(tableName: "General", plural: false),
text: self.$course)
.overlay(RoundedRectangle(cornerRadius: 10).stroke(invalidInputCourse ? Color.red : Color.secondary, lineWidth: 1))
.onChange(of: course, perform: { value in
self.setDirector()
})
.foregroundColor(invalidInputCourse ? .red : .primary)
.textContentType(.name)
.disableAutocorrection(true)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
TextField("director".localized(tableName: "General", plural: false) + " (" + "filledAuto".localized(tableName: "General", plural: false) + ")", text: self.$director)
.foregroundColor(.primary)
.textContentType(.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(minWidth: 200, idealWidth: nil, maxWidth: 500, minHeight: nil, idealHeight: nil, maxHeight: nil, alignment: .center)
.padding(.horizontal)
.disabled(true)
Button(action: {
self.settings.isFirstOpening = !self.settings.isFirstOpening
self.saveToCoreData()
}){
Text("Confirm")
@ -47,7 +64,32 @@ struct FirstOpeningSettings: View {
}
extension FirstOpeningSettings{
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
}
func saveToCoreData(){
if (!self.checkInput()) {
print("Input invalid")
return
}
// Delete old user data
let status = UtilityFunctions.deleteAllCoreDataEntitiesOfType(type: "User")
print("Deleting old user data status: \(status)")
@ -59,6 +101,7 @@ extension FirstOpeningSettings{
user.setValue(course, forKey: "course")
user.setValue(director, forKey: "director")
self.settings.isFirstOpening = !self.settings.isFirstOpening
PersistenceController.shared.save()
}
}

View File

@ -16,13 +16,6 @@ struct HomeView: View {
var body: some View {
VStack {
Button(action: {
self.settings.isFirstOpening = !self.settings.isFirstOpening
}){
Text("First opening toggle")
}
Text("Test")
HStack {
Text("name".localized(tableName: "General", plural: false) + ": ")
Text(self.name)