Adding Observables class

This commit is contained in:
Patrick Müller 2020-12-21 20:54:59 +01:00 committed by Patrick Müller
parent bdc3560668
commit f47674cdbb
5 changed files with 72 additions and 9 deletions

View File

@ -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 = "<group>"; };
CD9FABA4258EC60600D6D0C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
CDCD721925912E1200FBF2F5 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
CDCD72232591316500FBF2F5 /* LocalSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalSettings.swift; sourceTree = "<group>"; };
/* 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 = "<group>";
};
CDCD72222591314000FBF2F5 /* Observables */ = {
isa = PBXGroup;
children = (
CDCD72232591316500FBF2F5 /* LocalSettings.swift */,
);
path = Observables;
sourceTree = "<group>";
};
/* 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 */,

View File

@ -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)
}
}
}

View File

@ -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
}

View File

@ -9,9 +9,12 @@ import SwiftUI
import CoreData
struct ContentView: View {
@EnvironmentObject var settings: LocalSettings
@State private var selection = 0
var body: some View {
Group {
if(!settings.isFirstOpening) {
TabView(selection: $selection) {
HomeView()
.tabItem {
@ -22,7 +25,18 @@ struct ContentView: View {
}
.tag(0)
}
} else {
Button(action: {
self.settings.isFirstOpening = !self.settings.isFirstOpening
}){
Text("First opening toggle")
}
}
}
}
}
extension ContentView{
}
struct ContentView_Previews: PreviewProvider {

View File

@ -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)
}
}