From 52dbfa60ea9036048447d4af55ad0753e93f6d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Mu=CC=88ller?= Date: Mon, 28 Dec 2020 23:12:09 +0100 Subject: [PATCH] :sparkles: Fixing small issue, adding Settings, adding Acknowledgements, adding logout functionality --- DHBW-Service.xcodeproj/project.pbxproj | 12 ++++++ .../l10n/de.lproj/General.strings | 5 +++ .../l10n/en.lproj/General.strings | 5 +++ DHBW-Service/Utility/UtilityFunctions.swift | 21 ++++++++++ .../Views/Other/FirstOpeningSettings.swift | 4 +- DHBW-Service/Views/Tabs/SettingsMain.swift | 39 ++++++++++++++++++- .../SettingsAcknowledgements.swift | 36 +++++++++++++++++ 7 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 DHBW-Service/Views/Tabs/SettingsSubViews/SettingsAcknowledgements.swift diff --git a/DHBW-Service.xcodeproj/project.pbxproj b/DHBW-Service.xcodeproj/project.pbxproj index 1cc3178..5657cd8 100644 --- a/DHBW-Service.xcodeproj/project.pbxproj +++ b/DHBW-Service.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + CD730A35259A860E00E0BB69 /* SettingsAcknowledgements.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD730A34259A860E00E0BB69 /* SettingsAcknowledgements.swift */; }; CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9FAB80258EC60200D6D0C5 /* DHBW_ServiceApp.swift */; }; CD9FAB83258EC60200D6D0C5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9FAB82258EC60200D6D0C5 /* ContentView.swift */; }; CD9FAB85258EC60600D6D0C5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CD9FAB84258EC60600D6D0C5 /* Assets.xcassets */; }; @@ -43,6 +44,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + CD730A34259A860E00E0BB69 /* SettingsAcknowledgements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsAcknowledgements.swift; sourceTree = ""; }; CD9FAB7D258EC60200D6D0C5 /* DHBW-Service.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "DHBW-Service.app"; sourceTree = BUILT_PRODUCTS_DIR; }; CD9FAB80258EC60200D6D0C5 /* DHBW_ServiceApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DHBW_ServiceApp.swift; sourceTree = ""; }; CD9FAB82258EC60200D6D0C5 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -94,6 +96,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + CD730A33259A85F500E0BB69 /* SettingsSubViews */ = { + isa = PBXGroup; + children = ( + CD730A34259A860E00E0BB69 /* SettingsAcknowledgements.swift */, + ); + path = SettingsSubViews; + sourceTree = ""; + }; CD9FAB74258EC60100D6D0C5 = { isa = PBXGroup; children = ( @@ -196,6 +206,7 @@ children = ( CDCD721925912E1200FBF2F5 /* HomeView.swift */, CDD39B4A259A64150078D05F /* SettingsMain.swift */, + CD730A33259A85F500E0BB69 /* SettingsSubViews */, ); path = Tabs; sourceTree = ""; @@ -369,6 +380,7 @@ CDDCF4842592028A0027CDC5 /* Localizer.swift in Sources */, CDCD7230259135C500FBF2F5 /* FirstOpeningSettings.swift in Sources */, CD9FAB8A258EC60600D6D0C5 /* Persistence.swift in Sources */, + CD730A35259A860E00E0BB69 /* SettingsAcknowledgements.swift in Sources */, CD9FAB83258EC60200D6D0C5 /* ContentView.swift in Sources */, CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */, CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */, diff --git a/DHBW-Service/Supporting Files/l10n/de.lproj/General.strings b/DHBW-Service/Supporting Files/l10n/de.lproj/General.strings index 40f514b..20161af 100644 --- a/DHBW-Service/Supporting Files/l10n/de.lproj/General.strings +++ b/DHBW-Service/Supporting Files/l10n/de.lproj/General.strings @@ -10,3 +10,8 @@ "director" = "Studiengangsleiter"; "filledAuto" = "automatisch ausgefüllt"; "welcomeText" = "Bitte fülle die folgenden Felder aus:"; +"settings" = "Einstellungen"; +"other" = "Sonstiges"; +"logoutClearData" = "Abmelden und alle Daten löschen"; +"logout" = "Abmelden"; +"confirmLogoutMessage" = "Bist du dir sicher, dass du dich abmelden und alle Daten löschen möchtest? Diese Aktion kann nicht rückgängig gemacht werden."; diff --git a/DHBW-Service/Supporting Files/l10n/en.lproj/General.strings b/DHBW-Service/Supporting Files/l10n/en.lproj/General.strings index bf48c16..0535146 100644 --- a/DHBW-Service/Supporting Files/l10n/en.lproj/General.strings +++ b/DHBW-Service/Supporting Files/l10n/en.lproj/General.strings @@ -10,3 +10,8 @@ "director" = "Director"; "filledAuto" = "filled automatically"; "welcomeText" = "Please enter your data in the following fields:"; +"settings" = "Settings"; +"other" = "Other"; +"logoutClearData" = "Logout and clear all data"; +"logout" = "Logout"; +"confirmLogoutMessage" = "Are you sure you want to logout and clear all data? This cannot be undone."; diff --git a/DHBW-Service/Utility/UtilityFunctions.swift b/DHBW-Service/Utility/UtilityFunctions.swift index f9de9cb..330e1cf 100644 --- a/DHBW-Service/Utility/UtilityFunctions.swift +++ b/DHBW-Service/Utility/UtilityFunctions.swift @@ -39,4 +39,25 @@ class UtilityFunctions { return false } } + + public class func deleteAllData() -> Bool { + let entities = ["User", "Item"] + var allSuccessful = true + + for entityName in entities { + let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: entityName) + let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) + + do { + try PersistenceController.shared.context.execute(deleteRequest) + + PersistenceController.shared.save() + } catch let error as NSError { + print(error) + allSuccessful = false + } + } + + return allSuccessful + } } diff --git a/DHBW-Service/Views/Other/FirstOpeningSettings.swift b/DHBW-Service/Views/Other/FirstOpeningSettings.swift index abb6157..adc308e 100644 --- a/DHBW-Service/Views/Other/FirstOpeningSettings.swift +++ b/DHBW-Service/Views/Other/FirstOpeningSettings.swift @@ -19,8 +19,9 @@ struct FirstOpeningSettings: View { 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)) + .overlay(RoundedRectangle(cornerRadius: 10).stroke(invalidInputName ? Color.red : Color.secondary, lineWidth: 1)) .foregroundColor(invalidInputName ? .red : .primary) .textContentType(.name) .textFieldStyle(RoundedBorderTextFieldStyle()) @@ -56,6 +57,7 @@ struct FirstOpeningSettings: View { .background(Color.blue) .cornerRadius(15) } + //.disabled() //TODO: Check all inputs before enabling the button } } } diff --git a/DHBW-Service/Views/Tabs/SettingsMain.swift b/DHBW-Service/Views/Tabs/SettingsMain.swift index d102bf8..cc2c9af 100644 --- a/DHBW-Service/Views/Tabs/SettingsMain.swift +++ b/DHBW-Service/Views/Tabs/SettingsMain.swift @@ -8,8 +8,45 @@ import SwiftUI struct SettingsMain: View { + @EnvironmentObject var settings: LocalSettings + @State private var showLogoutConfirmationAlert = false + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + NavigationView { + List { + Section(header: Text("other".localized(tableName: "General", plural: false))) { + NavigationLink( + destination: SettingsAcknowledgements(), + label: { + Text("Acknowledgements") + }) + Button(action: { + self.showLogoutConfirmationAlert = true + }, label: { + Text("logoutClearData".localized(tableName: "General", plural: false)) + }) + } + } + .navigationTitle("settings".localized(tableName: "General", plural: false)) + .listStyle(GroupedListStyle()) + } + .alert(isPresented: $showLogoutConfirmationAlert, content: { + Alert( + title: Text("logout".localized(tableName: "General", plural: false)), + message: Text("confirmLogoutMessage".localized(tableName: "General", plural: false)), + primaryButton: .cancel(), + secondaryButton: .destructive(Text("Ok")){ + self.logoutAndClearData() + }) + }) + } +} + +extension SettingsMain { + private func logoutAndClearData() { + // TODO: Adjust before release! + UtilityFunctions.deleteAllData() + self.settings.isFirstOpening = true } } diff --git a/DHBW-Service/Views/Tabs/SettingsSubViews/SettingsAcknowledgements.swift b/DHBW-Service/Views/Tabs/SettingsSubViews/SettingsAcknowledgements.swift new file mode 100644 index 0000000..da8a7b5 --- /dev/null +++ b/DHBW-Service/Views/Tabs/SettingsSubViews/SettingsAcknowledgements.swift @@ -0,0 +1,36 @@ +// +// SettingsAcknowledgements.swift +// DHBW-Service +// +// Created by Patrick Müller on 28.12.20. +// + +import SwiftUI + +struct SettingsAcknowledgements: View { + var body: some View { + VStack { + Text("Contributors") + .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/) + Spacer() + Text("David Huh") + Text("Patrick Müller") + Spacer() + } + } +} + +struct SettingsAcknowledgements_Previews: PreviewProvider { + static var previews: some View { + SettingsAcknowledgements() + .preferredColorScheme(.dark) + .environmentObject(getFirstOpening()) + .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + } + + static func getFirstOpening() -> LocalSettings { + let settings = LocalSettings(); + settings.isFirstOpening = false; + return settings + } +}