From f47674cdbb23aa7296b96cc6a665e4a51f9808f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Mu=CC=88ller?= Date: Mon, 21 Dec 2020 20:54:59 +0100 Subject: [PATCH] Adding Observables class --- DHBW-Service.xcodeproj/project.pbxproj | 12 ++++++++ DHBW-Service/App/DHBW_ServiceApp.swift | 2 ++ DHBW-Service/Observables/LocalSettings.swift | 25 ++++++++++++++++ DHBW-Service/Views/ContentView.swift | 30 ++++++++++++++------ DHBW-Service/Views/Tabs/HomeView.swift | 12 +++++++- 5 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 DHBW-Service/Observables/LocalSettings.swift diff --git a/DHBW-Service.xcodeproj/project.pbxproj b/DHBW-Service.xcodeproj/project.pbxproj index 4f5f386..be08bcb 100644 --- a/DHBW-Service.xcodeproj/project.pbxproj +++ b/DHBW-Service.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ CD9FAB98258EC60600D6D0C5 /* DHBW_ServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9FAB97258EC60600D6D0C5 /* DHBW_ServiceTests.swift */; }; CD9FABA3258EC60600D6D0C5 /* DHBW_ServiceUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9FABA2258EC60600D6D0C5 /* DHBW_ServiceUITests.swift */; }; CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD721925912E1200FBF2F5 /* HomeView.swift */; }; + CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD72232591316500FBF2F5 /* LocalSettings.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -51,6 +52,7 @@ CD9FABA2258EC60600D6D0C5 /* DHBW_ServiceUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DHBW_ServiceUITests.swift; sourceTree = ""; }; CD9FABA4258EC60600D6D0C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; CDCD721925912E1200FBF2F5 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = ""; }; + CDCD72232591316500FBF2F5 /* LocalSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalSettings.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -103,6 +105,7 @@ children = ( CDCD720F25912D3C00FBF2F5 /* App */, CDCD721025912D4900FBF2F5 /* Views */, + CDCD72222591314000FBF2F5 /* Observables */, CDCD721125912D5400FBF2F5 /* CoreData */, CDCD721225912D6300FBF2F5 /* Supporting Files */, CD9FAB86258EC60600D6D0C5 /* Preview Content */, @@ -179,6 +182,14 @@ path = Tabs; sourceTree = ""; }; + CDCD72222591314000FBF2F5 /* Observables */ = { + isa = PBXGroup; + children = ( + CDCD72232591316500FBF2F5 /* LocalSettings.swift */, + ); + path = Observables; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -310,6 +321,7 @@ files = ( CD9FAB8A258EC60600D6D0C5 /* Persistence.swift in Sources */, CD9FAB83258EC60200D6D0C5 /* ContentView.swift in Sources */, + CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */, CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */, CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */, CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */, diff --git a/DHBW-Service/App/DHBW_ServiceApp.swift b/DHBW-Service/App/DHBW_ServiceApp.swift index de336c7..7ca1eab 100644 --- a/DHBW-Service/App/DHBW_ServiceApp.swift +++ b/DHBW-Service/App/DHBW_ServiceApp.swift @@ -10,11 +10,13 @@ import SwiftUI @main struct DHBW_ServiceApp: App { let persistenceController = PersistenceController.shared + let settings = LocalSettings() var body: some Scene { WindowGroup { ContentView() .environment(\.managedObjectContext, persistenceController.context) + .environmentObject(settings) } } } diff --git a/DHBW-Service/Observables/LocalSettings.swift b/DHBW-Service/Observables/LocalSettings.swift new file mode 100644 index 0000000..f02690a --- /dev/null +++ b/DHBW-Service/Observables/LocalSettings.swift @@ -0,0 +1,25 @@ +// +// LocalSettings.swift +// DHBW-Service +// +// Created by Patrick Müller on 21.12.20. +// + +import Foundation +import Combine + +private var cancellables = [String:AnyCancellable]() + +extension Published { + init(wrappedValue defaultValue: Value, key: String) { + let value = UserDefaults.standard.object(forKey: key) as? Value ?? defaultValue + self.init(initialValue: value) + cancellables[key] = projectedValue.sink { val in + UserDefaults.standard.set(val, forKey: key) + } + } +} + +class LocalSettings: ObservableObject { + @Published(wrappedValue: true, key: "IsFirstOpening") var isFirstOpening: Bool // To determine if the user is logged in +} diff --git a/DHBW-Service/Views/ContentView.swift b/DHBW-Service/Views/ContentView.swift index 8a4fbba..7f4a4b0 100644 --- a/DHBW-Service/Views/ContentView.swift +++ b/DHBW-Service/Views/ContentView.swift @@ -9,22 +9,36 @@ import SwiftUI import CoreData struct ContentView: View { + @EnvironmentObject var settings: LocalSettings @State private var selection = 0 var body: some View { - TabView(selection: $selection) { - HomeView() - .tabItem { - VStack { - Image(systemName: "house.fill") - Text("Home") - } + Group { + if(!settings.isFirstOpening) { + TabView(selection: $selection) { + HomeView() + .tabItem { + VStack { + Image(systemName: "house.fill") + Text("Home") + } + } + .tag(0) } - .tag(0) + } else { + Button(action: { + self.settings.isFirstOpening = !self.settings.isFirstOpening + }){ + Text("First opening toggle") + } + } } } } +extension ContentView{ +} + struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() diff --git a/DHBW-Service/Views/Tabs/HomeView.swift b/DHBW-Service/Views/Tabs/HomeView.swift index d51f9d0..482691f 100644 --- a/DHBW-Service/Views/Tabs/HomeView.swift +++ b/DHBW-Service/Views/Tabs/HomeView.swift @@ -8,13 +8,23 @@ import SwiftUI struct HomeView: View { + @EnvironmentObject var settings: LocalSettings + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + VStack { + Button(action: { + self.settings.isFirstOpening = !self.settings.isFirstOpening + }){ + Text("First opening toggle") + } + Text("Test") + } } } struct HomeView_Previews: PreviewProvider { static var previews: some View { HomeView() + .preferredColorScheme(.dark) } }