mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2024-11-22 09:23:57 +00:00
Adding Observables class
This commit is contained in:
parent
bdc3560668
commit
f47674cdbb
|
@ -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 */,
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
25
DHBW-Service/Observables/LocalSettings.swift
Normal file
25
DHBW-Service/Observables/LocalSettings.swift
Normal 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
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user