From a174135ebde7ccd3ac95ba16b3242bf06776a5b7 Mon Sep 17 00:00:00 2001 From: "remi.regnault" Date: Thu, 30 May 2024 16:26:29 +0200 Subject: [PATCH] :construction: added SettingsVM, rework some views and components to support the SettingsVM --- .../DouShouQi_App.xcodeproj/project.pbxproj | 12 ++++++ .../ViewModel/Settings/SettingsVM.swift | 42 +++++++++++++++++++ .../Views/Menu/MainMenuView.swift | 2 +- .../Views/Menu/SettingsView.swift | 22 ++++------ 4 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 DouShouQi_App/DouShouQi_App/ViewModel/Settings/SettingsVM.swift diff --git a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj index 593898a..2d3a99b 100644 --- a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj +++ b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ 649B59B22BF65392002BAE38 /* TextStyles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649B59B12BF65392002BAE38 /* TextStyles.swift */; }; 649B59B42BF653E1002BAE38 /* ViewTitleTextStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649B59B32BF653E1002BAE38 /* ViewTitleTextStyle.swift */; }; 64D992722C06281B002ACBC6 /* SystemIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64D992712C06281B002ACBC6 /* SystemIcons.swift */; }; + 64E00BE32C08B1D500E90544 /* SettingsVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64E00BE22C08B1D500E90544 /* SettingsVM.swift */; }; EC05BFC42C04C3C4000F7B19 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC05BFC32C04C3C4000F7B19 /* SettingsView.swift */; }; EC05BFC82C04D832000F7B19 /* CustomSwitchButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC05BFC72C04D832000F7B19 /* CustomSwitchButton.swift */; }; EC62C4F92C0371660048CD0B /* MusicPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC62C4F82C0371660048CD0B /* MusicPlayer.swift */; }; @@ -110,6 +111,7 @@ 649B59B12BF65392002BAE38 /* TextStyles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextStyles.swift; sourceTree = ""; }; 649B59B32BF653E1002BAE38 /* ViewTitleTextStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewTitleTextStyle.swift; sourceTree = ""; }; 64D992712C06281B002ACBC6 /* SystemIcons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemIcons.swift; sourceTree = ""; }; + 64E00BE22C08B1D500E90544 /* SettingsVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsVM.swift; sourceTree = ""; }; EC05BFC32C04C3C4000F7B19 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = ""; }; EC05BFC72C04D832000F7B19 /* CustomSwitchButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSwitchButton.swift; sourceTree = ""; }; EC62C4F82C0371660048CD0B /* MusicPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicPlayer.swift; sourceTree = ""; }; @@ -259,6 +261,7 @@ 646FA83C2C072284001466BA /* ViewModel */ = { isa = PBXGroup; children = ( + 64E00BE12C08B1BF00E90544 /* Settings */, 646FA83F2C0730B4001466BA /* Game */, ); path = ViewModel; @@ -347,6 +350,14 @@ path = TextStyles; sourceTree = ""; }; + 64E00BE12C08B1BF00E90544 /* Settings */ = { + isa = PBXGroup; + children = ( + 64E00BE22C08B1D500E90544 /* SettingsVM.swift */, + ); + path = Settings; + sourceTree = ""; + }; EC05BFC62C04D7C2000F7B19 /* Settings */ = { isa = PBXGroup; children = ( @@ -531,6 +542,7 @@ EC62C5012C045B590048CD0B /* SoundPlayer.swift in Sources */, EC05BFC82C04D832000F7B19 /* CustomSwitchButton.swift in Sources */, 64D992722C06281B002ACBC6 /* SystemIcons.swift in Sources */, + 64E00BE32C08B1D500E90544 /* SettingsVM.swift in Sources */, 645B4C252BFCD3C600FD658A /* ScoreBoardView.swift in Sources */, 649B59AE2BF64EAB002BAE38 /* AppImages.swift in Sources */, 649ABF602BF60F2D002E8894 /* MainMenuButton.swift in Sources */, diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Settings/SettingsVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Settings/SettingsVM.swift new file mode 100644 index 0000000..c92ec49 --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Settings/SettingsVM.swift @@ -0,0 +1,42 @@ +// +// SettingsVM.swift +// DouShouQi_App +// +// Created by Rémi REGNAULT on 30/05/2024. +// + +import Foundation +import SwiftUI + +public enum Language: String, Identifiable { + + case french, english, spanish + + public var id: String { + return self.rawValue + } + + public var litteral: String { + switch self { + case .french: + return "Français" + case .english: + return "English" + case .spanish: + return "Español" + } + } + + static var list: [Language] { + return [.french, .english, .spanish] + } +} + +class SettingsVM: ObservableObject { + // Properties + @Published var darkTheme: Bool = false + @Published var language: Language = Language.english + @Published var soundOn: Bool = true + + init() {} +} diff --git a/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift b/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift index 133218e..ba5aeaa 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift @@ -36,7 +36,7 @@ struct MainMenuView: View { MainMenuButton(text: "Historique", destination: HistoricView(historicVM: HistoricVM()), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Best Scores", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Players", destination: PlayersView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) - MainMenuButton(text: "Settings", destination: SettingsView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) + MainMenuButton(text: "Settings", destination: SettingsView(settingsVM: SettingsVM()), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) } Image(AppImages.SemiLion) .resizable() diff --git a/DouShouQi_App/DouShouQi_App/Views/Menu/SettingsView.swift b/DouShouQi_App/DouShouQi_App/Views/Menu/SettingsView.swift index dcdb387..aec6428 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Menu/SettingsView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Menu/SettingsView.swift @@ -7,15 +7,9 @@ import SwiftUI -extension String : Identifiable { - public var id: Self { return self } -} - struct SettingsView: View { - @State private var theme = false - @State private var choice = "English" - @State private var sound = true + @ObservedObject var settingsVM: SettingsVM var body: some View { VStack { @@ -30,7 +24,7 @@ struct SettingsView: View { Spacer() - CustomSwitchButton(IsOn: $theme, imgSystemNameIsOff: SystemIcons.LightTheme, imgSystemNameIsOn: SystemIcons.DarkTheme) + CustomSwitchButton(IsOn: $settingsVM.darkTheme, imgSystemNameIsOff: SystemIcons.LightTheme, imgSystemNameIsOn: SystemIcons.DarkTheme) } Divider() @@ -41,9 +35,9 @@ struct SettingsView: View { Spacer() - Picker("Sort", selection: $choice) { - ForEach(["English", "Français", "Español"]) { - Text($0) + Picker("Language", selection: $settingsVM.language) { + ForEach(Language.list) { language in + Text(language.litteral).tag(language) } } .pickerStyle(.menu) @@ -52,12 +46,12 @@ struct SettingsView: View { Divider() HStack { - Text("Theme") + Text("Sound") .font(.headline) Spacer() - CustomSwitchButton(IsOn: $sound, imgSystemNameIsOff: SystemIcons.SoundOff, imgSystemNameIsOn: SystemIcons.SoundOn) + CustomSwitchButton(IsOn: $settingsVM.soundOn, imgSystemNameIsOff: SystemIcons.SoundOff, imgSystemNameIsOn: SystemIcons.SoundOn) } Divider() @@ -71,6 +65,6 @@ struct SettingsView: View { struct SettingsView_Previews: PreviewProvider { static var previews: some View { - SettingsView() + SettingsView(settingsVM: SettingsVM()) } }