I have a secondView witch I want to view when clicking on a button in ProfileView(). The button changes a variable in the ProfieModel(enum) and based on the variable the view changes. But when I make a change on any variable in the settingspage the view goes back to Profile.
ProfileView()
struct ProfileView: View {
@EnvironmentObject var authentication: AuthenticationModel
@ObservedObject var profile: ProfileModel = ProfileModel()
var body: some View {
ZStack {
switch profile.profileView {
case .settings:
SettingsView()
.environmentObject(profile)
.environmentObject(authentication)
.transition(.backslide)
case .profile:
etc...
ProfileModel:
import Foundation
class ProfileModel: ObservableObject {
@Published var profileView: profileViews = .profile
enum profileViews {
case profile, settings
}
}
The SettingsView:
When I Click on the button to change DarkMode the payload of the model seems te reset. Resulting in going back to the profileView.
import SwiftUI
//View
struct SettingsView: View {
@EnvironmentObject var authentication: AuthenticationModel
@EnvironmentObject var profile: ProfileModel
//Languages
@AppStorage("Language") private var language = LocalizationService.shared.language
//Darkmode
@Environment(\.colorScheme) private var currentColorScheme
@AppStorage("DarkMode") var isDarkMode: Bool = false
//Logout
@State var logoutMessage: Bool = false
var body: some View {
ZStack {
VStack(alignment: .leading) {
//MARK: - Header
HStack {
Button {
profile.profileView = .profile
} label: {
Image(systemName: "chevron.left")
.foregroundColor(Color("PrimaryColorTurned"))
.font(.system(size:18))
Text("Back".localized(language))
.foregroundColor(Color("PrimaryColorTurned"))
.font(.system(size:18))
}
Spacer()
} //header
//MARK: - Title
VStack(alignment: .leading) {
Text("Settings".localized(language))
.foregroundColor(Color("PrimaryColorTurned"))
.font(.title.bold())
Text("You are in control".localized(language))
.foregroundColor(Color("PrimaryColorTurned"))
.font(.title2)
} //Settingsname
.padding(10)
ScrollView {
VStack(alignment: .leading) {
//MARK: - Dark mode
Button {
isDarkMode.toggle() <-- Click results in reset of payload?
} label: {
HStack {
VStack(alignment: .leading) {
Image(systemName: "iphone")
.foregroundColor(currentColorScheme == .dark ? .white : Color("PrimarySingleColor"))
.padding(4)
.font(.system(size: 30))
Text("DarkMode".localized(language))
.foregroundColor(currentColorScheme == .dark ? .white : Color("PrimarySingleColor"))
.font(.title3.bold())
.padding(4)
Text("Manage how the app looks".localized(language))
.font(.caption)
.foregroundColor(.gray)
.multilineTextAlignment(.leading)
}
Spacer()
}
}
.padding(15)
.frame(width: 180, height: 160)
.background {
RoundedRectangle(cornerRadius: 30, style: .circular)
.fill(currentColorScheme == .light ? .white : Color("PrimarySingleColor"))
.shadow(color: Color("Black").opacity(0.06), radius: 5, x: 5, y: 5)
}
.padding(10)
}
I tried it with IF statements, ENUM(switch) one model instead of 2. But it all results in the same.