switcher theme in BM
This commit is contained in:
		
							parent
							
								
									27a6ce7ce3
								
							
						
					
					
						commit
						c5906f42b7
					
				
							
								
								
									
										53
									
								
								Shared/Services/ThemeManager.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								Shared/Services/ThemeManager.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,53 @@
 | 
				
			|||||||
 | 
					import SwiftUI
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Enum to represent the three theme options
 | 
				
			||||||
 | 
					enum Theme: String, CaseIterable {
 | 
				
			||||||
 | 
					    case system = "System"
 | 
				
			||||||
 | 
					    case light = "Light"
 | 
				
			||||||
 | 
					    case dark = "Dark"
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    var colorScheme: ColorScheme? {
 | 
				
			||||||
 | 
					        switch self {
 | 
				
			||||||
 | 
					        case .system:
 | 
				
			||||||
 | 
					            return nil
 | 
				
			||||||
 | 
					        case .light:
 | 
				
			||||||
 | 
					            return .light
 | 
				
			||||||
 | 
					        case .dark:
 | 
				
			||||||
 | 
					            return .dark
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Observable class to manage the theme state
 | 
				
			||||||
 | 
					class ThemeManager: ObservableObject {
 | 
				
			||||||
 | 
					    @AppStorage("selectedTheme") private var selectedThemeValue: String = Theme.system.rawValue
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    @Published var theme: Theme
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    init() {
 | 
				
			||||||
 | 
					        // Read directly from UserDefaults to avoid using self before initialization is complete.
 | 
				
			||||||
 | 
					        let storedThemeValue = UserDefaults.standard.string(forKey: "selectedTheme") ?? ""
 | 
				
			||||||
 | 
					        self.theme = Theme(rawValue: storedThemeValue) ?? .system
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    func setTheme(_ theme: Theme) {
 | 
				
			||||||
 | 
					        self.theme = theme
 | 
				
			||||||
 | 
					        selectedThemeValue = theme.rawValue
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // This will be called from the button
 | 
				
			||||||
 | 
					    func toggleTheme(from currentSystemScheme: ColorScheme) {
 | 
				
			||||||
 | 
					        let newTheme: Theme
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        switch theme {
 | 
				
			||||||
 | 
					        case .system:
 | 
				
			||||||
 | 
					            // If system is active, toggle to the opposite of the current system theme
 | 
				
			||||||
 | 
					            newTheme = currentSystemScheme == .dark ? .light : .dark
 | 
				
			||||||
 | 
					        case .light:
 | 
				
			||||||
 | 
					            newTheme = .dark
 | 
				
			||||||
 | 
					        case .dark:
 | 
				
			||||||
 | 
					            newTheme = .light
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        setTheme(newTheme)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -62,6 +62,8 @@ struct SideMenuFooterButton: View {
 | 
				
			|||||||
// --- MAIN VIEW ---
 | 
					// --- MAIN VIEW ---
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct SideMenuView: View {
 | 
					struct SideMenuView: View {
 | 
				
			||||||
 | 
					    @EnvironmentObject var themeManager: ThemeManager
 | 
				
			||||||
 | 
					    @Environment(\.colorScheme) var colorScheme
 | 
				
			||||||
    @Binding var isPresented: Bool
 | 
					    @Binding var isPresented: Bool
 | 
				
			||||||
    @State private var isAccountListExpanded = false
 | 
					    @State private var isAccountListExpanded = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -78,21 +80,49 @@ struct SideMenuView: View {
 | 
				
			|||||||
        Account(name: "Creative Profile", username: "@creative", isCurrent: false)
 | 
					        Account(name: "Creative Profile", username: "@creative", isCurrent: false)
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    private var themeToggleButton: some View {
 | 
				
			||||||
 | 
					        Button(action: {
 | 
				
			||||||
 | 
					            themeManager.toggleTheme(from: colorScheme)
 | 
				
			||||||
 | 
					        }) {
 | 
				
			||||||
 | 
					            Image(systemName: iconName)
 | 
				
			||||||
 | 
					                .font(.title2)
 | 
				
			||||||
 | 
					                .foregroundColor(.primary)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private var iconName: String {
 | 
				
			||||||
 | 
					        let effectiveScheme: ColorScheme
 | 
				
			||||||
 | 
					        switch themeManager.theme {
 | 
				
			||||||
 | 
					        case .system:
 | 
				
			||||||
 | 
					            effectiveScheme = colorScheme
 | 
				
			||||||
 | 
					        case .light:
 | 
				
			||||||
 | 
					            effectiveScheme = .light
 | 
				
			||||||
 | 
					        case .dark:
 | 
				
			||||||
 | 
					            effectiveScheme = .dark
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        return effectiveScheme == .dark ? "moon.fill" : "sun.max.fill"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var body: some View {
 | 
					    var body: some View {
 | 
				
			||||||
        VStack(alignment: .leading, spacing: 0) {
 | 
					        VStack(alignment: .leading, spacing: 0) {
 | 
				
			||||||
            ScrollView {
 | 
					            ScrollView {
 | 
				
			||||||
                VStack(alignment: .leading, spacing: 0) { // Parent VStack
 | 
					                VStack(alignment: .leading, spacing: 0) { // Parent VStack
 | 
				
			||||||
                    
 | 
					                    
 | 
				
			||||||
                    // --- Header
 | 
					                    // --- Header ---
 | 
				
			||||||
                    Button(action: {  }) {
 | 
					                    HStack {
 | 
				
			||||||
                        Image(systemName: "person.circle.fill")
 | 
					                        Button(action: {  }) {
 | 
				
			||||||
                            .resizable()
 | 
					                            Image(systemName: "person.circle.fill")
 | 
				
			||||||
                            .frame(width: 60, height: 60)
 | 
					                                .resizable()
 | 
				
			||||||
                            .foregroundColor(.gray)
 | 
					                                .frame(width: 60, height: 60)
 | 
				
			||||||
                            .padding(.horizontal, 20)
 | 
					                                .foregroundColor(.gray)
 | 
				
			||||||
                            .padding(.top, topPadding)
 | 
					                        }
 | 
				
			||||||
                            .padding(.bottom, 10)
 | 
					                        Spacer()
 | 
				
			||||||
 | 
					                        themeToggleButton
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					                    .padding(.horizontal, 20)
 | 
				
			||||||
 | 
					                    .padding(.top, topPadding)
 | 
				
			||||||
 | 
					                    .padding(.bottom, 10)
 | 
				
			||||||
                    
 | 
					                    
 | 
				
			||||||
                    // --- Header Button ---
 | 
					                    // --- Header Button ---
 | 
				
			||||||
                    Button(action: {
 | 
					                    Button(action: {
 | 
				
			||||||
 | 
				
			|||||||
@ -7,31 +7,24 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import SwiftUI
 | 
					import SwiftUI
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//@AppStorage("isLoggedIn") var isLoggedIn: Bool = false
 | 
					 | 
				
			||||||
//@AppStorage("isDarkMode") private var isDarkMode: Bool = false
 | 
					 | 
				
			||||||
//@AppStorage("currentUser") var currentUser: String = ""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@main
 | 
					@main
 | 
				
			||||||
struct volnahubApp: App {
 | 
					struct volnahubApp: App {
 | 
				
			||||||
    @AppStorage("isDarkMode") private var isDarkMode: Bool = true
 | 
					    @StateObject private var themeManager = ThemeManager()
 | 
				
			||||||
    @StateObject private var viewModel = LoginViewModel()
 | 
					    @StateObject private var viewModel = LoginViewModel()
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    var body: some Scene {
 | 
					    var body: some Scene {
 | 
				
			||||||
        WindowGroup {
 | 
					        WindowGroup {
 | 
				
			||||||
            ZStack {
 | 
					            Group {
 | 
				
			||||||
                Color(isDarkMode ? .black : .white) // ✅ Фон в зависимости от темы
 | 
					                if viewModel.isLoading {
 | 
				
			||||||
 | 
					 | 
				
			||||||
                if viewModel.isLoading{
 | 
					 | 
				
			||||||
                    SplashScreenView()
 | 
					                    SplashScreenView()
 | 
				
			||||||
                }else{
 | 
					                } else if viewModel.isLoggedIn {
 | 
				
			||||||
                    if viewModel.isLoggedIn {
 | 
					                    MainView(viewModel: viewModel)
 | 
				
			||||||
                        MainView(viewModel: viewModel)
 | 
					                } else {
 | 
				
			||||||
                    } else {
 | 
					                    LoginView(viewModel: viewModel)
 | 
				
			||||||
                        LoginView(viewModel: viewModel)
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            .preferredColorScheme(isDarkMode ? .dark : .light)
 | 
					            .environmentObject(themeManager)
 | 
				
			||||||
 | 
					            .preferredColorScheme(themeManager.theme.colorScheme)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -36,6 +36,7 @@
 | 
				
			|||||||
		1A9B01662E4BFA3600887E0B /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1A9B01652E4BFA3600887E0B /* Media.xcassets */; };
 | 
							1A9B01662E4BFA3600887E0B /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1A9B01652E4BFA3600887E0B /* Media.xcassets */; };
 | 
				
			||||||
		1A9B016E2E4BFB9000887E0B /* NewHomeTabViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A9B016D2E4BFB9000887E0B /* NewHomeTabViewModel.swift */; };
 | 
							1A9B016E2E4BFB9000887E0B /* NewHomeTabViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A9B016D2E4BFB9000887E0B /* NewHomeTabViewModel.swift */; };
 | 
				
			||||||
		1A9B017C2E4C087F00887E0B /* SideMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A9B017B2E4C087F00887E0B /* SideMenuView.swift */; };
 | 
							1A9B017C2E4C087F00887E0B /* SideMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A9B017B2E4C087F00887E0B /* SideMenuView.swift */; };
 | 
				
			||||||
 | 
							1A9E4FB32E4D6A67002249D6 /* ThemeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A9E4FB22E4D6A67002249D6 /* ThemeManager.swift */; };
 | 
				
			||||||
		1AB4F8CD2E22E341002B6E40 /* AccountShareSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB4F8CC2E22E341002B6E40 /* AccountShareSheet.swift */; };
 | 
							1AB4F8CD2E22E341002B6E40 /* AccountShareSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB4F8CC2E22E341002B6E40 /* AccountShareSheet.swift */; };
 | 
				
			||||||
		1AB4F8F32E22EC9F002B6E40 /* FollowersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB4F8F22E22EC9F002B6E40 /* FollowersView.swift */; };
 | 
							1AB4F8F32E22EC9F002B6E40 /* FollowersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB4F8F22E22EC9F002B6E40 /* FollowersView.swift */; };
 | 
				
			||||||
		1AB4F8F72E22ECAC002B6E40 /* FollowingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB4F8F62E22ECAC002B6E40 /* FollowingView.swift */; };
 | 
							1AB4F8F72E22ECAC002B6E40 /* FollowingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB4F8F62E22ECAC002B6E40 /* FollowingView.swift */; };
 | 
				
			||||||
@ -90,6 +91,7 @@
 | 
				
			|||||||
		1A9B01652E4BFA3600887E0B /* Media.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Media.xcassets; sourceTree = "<group>"; };
 | 
							1A9B01652E4BFA3600887E0B /* Media.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Media.xcassets; sourceTree = "<group>"; };
 | 
				
			||||||
		1A9B016D2E4BFB9000887E0B /* NewHomeTabViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NewHomeTabViewModel.swift; sourceTree = "<group>"; };
 | 
							1A9B016D2E4BFB9000887E0B /* NewHomeTabViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NewHomeTabViewModel.swift; sourceTree = "<group>"; };
 | 
				
			||||||
		1A9B017B2E4C087F00887E0B /* SideMenuView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SideMenuView.swift; sourceTree = "<group>"; };
 | 
							1A9B017B2E4C087F00887E0B /* SideMenuView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SideMenuView.swift; sourceTree = "<group>"; };
 | 
				
			||||||
 | 
							1A9E4FB22E4D6A67002249D6 /* ThemeManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThemeManager.swift; sourceTree = "<group>"; };
 | 
				
			||||||
		1AB4F8CC2E22E341002B6E40 /* AccountShareSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountShareSheet.swift; sourceTree = "<group>"; };
 | 
							1AB4F8CC2E22E341002B6E40 /* AccountShareSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountShareSheet.swift; sourceTree = "<group>"; };
 | 
				
			||||||
		1AB4F8F22E22EC9F002B6E40 /* FollowersView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowersView.swift; sourceTree = "<group>"; };
 | 
							1AB4F8F22E22EC9F002B6E40 /* FollowersView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowersView.swift; sourceTree = "<group>"; };
 | 
				
			||||||
		1AB4F8F62E22ECAC002B6E40 /* FollowingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowingView.swift; sourceTree = "<group>"; };
 | 
							1AB4F8F62E22ECAC002B6E40 /* FollowingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowingView.swift; sourceTree = "<group>"; };
 | 
				
			||||||
@ -227,6 +229,7 @@
 | 
				
			|||||||
		1A7940E52DF7B341002569DA /* Services */ = {
 | 
							1A7940E52DF7B341002569DA /* Services */ = {
 | 
				
			||||||
			isa = PBXGroup;
 | 
								isa = PBXGroup;
 | 
				
			||||||
			children = (
 | 
								children = (
 | 
				
			||||||
 | 
									1A9E4FB22E4D6A67002249D6 /* ThemeManager.swift */,
 | 
				
			||||||
				1A7940E12DF7B1C5002569DA /* KeychainService.swift */,
 | 
									1A7940E12DF7B1C5002569DA /* KeychainService.swift */,
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
			path = Services;
 | 
								path = Services;
 | 
				
			||||||
@ -450,6 +453,7 @@
 | 
				
			|||||||
				1A7940E22DF7B1C5002569DA /* KeychainService.swift in Sources */,
 | 
									1A7940E22DF7B1C5002569DA /* KeychainService.swift in Sources */,
 | 
				
			||||||
				1A7940A62DF77DF5002569DA /* User.swift in Sources */,
 | 
									1A7940A62DF77DF5002569DA /* User.swift in Sources */,
 | 
				
			||||||
				1A7940A22DF77DE9002569DA /* AuthService.swift in Sources */,
 | 
									1A7940A22DF77DE9002569DA /* AuthService.swift in Sources */,
 | 
				
			||||||
 | 
									1A9E4FB32E4D6A67002249D6 /* ThemeManager.swift in Sources */,
 | 
				
			||||||
				1AEE5EB32E21A85800A3DCA3 /* SearchTab.swift in Sources */,
 | 
									1AEE5EB32E21A85800A3DCA3 /* SearchTab.swift in Sources */,
 | 
				
			||||||
				1ACE61152E22FE2000B37AC5 /* PostDetailView.swift in Sources */,
 | 
									1ACE61152E22FE2000B37AC5 /* PostDetailView.swift in Sources */,
 | 
				
			||||||
				1ACE61192E22FF1400B37AC5 /* Post.swift in Sources */,
 | 
									1ACE61192E22FF1400B37AC5 /* Post.swift in Sources */,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user