From fa30bd657baee04165b8d24581e759016ed50dc9 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Mon, 21 Mar 2016 00:42:29 +0100 Subject: [PATCH] Implemented profile selection and creation --- Artemis/Artemis/DAL/ProfileProvider.cs | 9 +- Artemis/Artemis/Models/ProfileModel.cs | 35 +++- .../Games/TheDivision/TheDivisionViewModel.cs | 2 +- .../Artemis/Services/MetroDialogService.cs | 42 ++--- .../ViewModels/ProfileEditorViewModel.cs | 74 +++++++-- Artemis/Artemis/Views/ProfileEditorView.xaml | 151 +++--------------- Artemis/Artemis/Views/ShellView.xaml | 2 +- 7 files changed, 148 insertions(+), 167 deletions(-) diff --git a/Artemis/Artemis/DAL/ProfileProvider.cs b/Artemis/Artemis/DAL/ProfileProvider.cs index 2aa08226f..3c847ce8b 100644 --- a/Artemis/Artemis/DAL/ProfileProvider.cs +++ b/Artemis/Artemis/DAL/ProfileProvider.cs @@ -42,9 +42,12 @@ namespace Artemis.DAL if (!(prof.GameName?.Length > 1) || !(prof.KeyboardName?.Length > 1) || !(prof.Name?.Length > 1)) throw new ArgumentException("Profile is invalid. Name, GameName and KeyboardName are required"); - var serialized = JsonConvert.SerializeObject(prof); - var path = ProfileFolder + $@"\{prof.KeyboardName}\{prof.GameName}\{prof.Name}.json"; - File.WriteAllText(path, serialized); + var path = ProfileFolder + $@"\{prof.KeyboardName}\{prof.GameName}"; + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + + var serialized = JsonConvert.SerializeObject(prof, Formatting.Indented); + File.WriteAllText(path + $@"\{prof.Name}.json", serialized); } private static List ReadProfiles() diff --git a/Artemis/Artemis/Models/ProfileModel.cs b/Artemis/Artemis/Models/ProfileModel.cs index 6e4471686..769013786 100644 --- a/Artemis/Artemis/Models/ProfileModel.cs +++ b/Artemis/Artemis/Models/ProfileModel.cs @@ -1,14 +1,45 @@ -using Artemis.Components; +using System.Collections.Generic; +using Artemis.Components; namespace Artemis.Models { public class ProfileModel { - public string Name { get; set; } + public ProfileModel(string name, string keyboardName, string gameName) + { + Name = name; + KeyboardName = keyboardName; + GameName = gameName; + } + public string Name { get; set; } public string KeyboardName { get; set; } public string GameName { get; set; } public LayerComposite Layers { get; set; } + + protected bool Equals(ProfileModel other) + { + return string.Equals(Name, other.Name) && string.Equals(KeyboardName, other.KeyboardName) && string.Equals(GameName, other.GameName); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((ProfileModel) obj); + } + + public override int GetHashCode() + { + unchecked + { + var hashCode = (Name != null ? Name.GetHashCode() : 0); + hashCode = (hashCode*397) ^ (KeyboardName != null ? KeyboardName.GetHashCode() : 0); + hashCode = (hashCode*397) ^ (GameName != null ? GameName.GetHashCode() : 0); + return hashCode; + } + } } } \ No newline at end of file diff --git a/Artemis/Artemis/Modules/Games/TheDivision/TheDivisionViewModel.cs b/Artemis/Artemis/Modules/Games/TheDivision/TheDivisionViewModel.cs index 314b9e74a..01f98cc6d 100644 --- a/Artemis/Artemis/Modules/Games/TheDivision/TheDivisionViewModel.cs +++ b/Artemis/Artemis/Modules/Games/TheDivision/TheDivisionViewModel.cs @@ -17,7 +17,7 @@ namespace Artemis.Modules.Games.TheDivision GameModel = new TheDivisionModel(mainManager, (TheDivisionSettings) GameSettings); MainManager.EffectManager.EffectModels.Add(GameModel); - ProfileEditor = new ProfileEditorViewModel(GameModel, MainManager.KeyboardManager.ActiveKeyboard); + ProfileEditor = new ProfileEditorViewModel(MainManager, GameModel); } public ProfileEditorViewModel ProfileEditor { get; set; } diff --git a/Artemis/Artemis/Services/MetroDialogService.cs b/Artemis/Artemis/Services/MetroDialogService.cs index e8ef0b398..b63d5cad9 100644 --- a/Artemis/Artemis/Services/MetroDialogService.cs +++ b/Artemis/Artemis/Services/MetroDialogService.cs @@ -1,24 +1,24 @@ -//The MIT License(MIT) - -//Copyright(c) 2015 ihtfw - -//Permission is hereby granted, free of charge, to any person obtaining a copy -//of this software and associated documentation files (the "Software"), to deal -//in the Software without restriction, including without limitation the rights -//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -//copies of the Software, and to permit persons to whom the Software is -//furnished to do so, subject to the following conditions: - -//The above copyright notice and this permission notice shall be included in all -//copies or substantial portions of the Software. - -//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -//SOFTWARE. +// The MIT License(MIT) + +// Copyright(c) 2015 ihtfw + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. using System.Linq; using System.Threading.Tasks; diff --git a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs index c09f4655a..51dda4530 100644 --- a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs +++ b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs @@ -1,7 +1,7 @@ -using System.Collections.Generic; +using System.Linq; using System.Windows.Media; using Artemis.DAL; -using Artemis.KeyboardProviders; +using Artemis.Managers; using Artemis.Models; using Caliburn.Micro; @@ -10,20 +10,76 @@ namespace Artemis.ViewModels public class ProfileEditorViewModel : Screen { private readonly GameModel _gameModel; - private readonly KeyboardProvider _keyboard; - private List _profiles; + private readonly MainManager _mainManager; + private BindableCollection _profileModels; + private ProfileModel _selectedProfileModel; - public ProfileEditorViewModel(GameModel gameModel, KeyboardProvider keyboard) + public ProfileEditorViewModel(MainManager mainManager, GameModel gameModel) { + _mainManager = mainManager; _gameModel = gameModel; - _keyboard = keyboard; - GetProfiles(); + ProfileModels = new BindableCollection(); + LoadProfiles(); } - public void GetProfiles() + public BindableCollection ProfileModels { - _profiles = ProfileProvider.GetAll(_gameModel); + get { return _profileModels; } + set + { + if (Equals(value, _profileModels)) return; + _profileModels = value; + NotifyOfPropertyChange(() => ProfileModels); + } + } + + public ProfileModel SelectedProfileModel + { + get { return _selectedProfileModel; } + set + { + if (Equals(value, _selectedProfileModel)) return; + _selectedProfileModel = value; + NotifyOfPropertyChange(); + } + } + + private void LoadProfiles() + { + ProfileModels.Clear(); + ProfileModels.AddRange(ProfileProvider.GetAll(_gameModel)); + SelectedProfileModel = ProfileModels.FirstOrDefault(); + } + + public async void AddProfile() + { + var name = + await + _mainManager.DialogService.ShowInputDialog("Add new profile", + "Please provide a profile name unique to this game and keyboard."); + if (name.Length < 1) + { + _mainManager.DialogService.ShowMessageBox("Invalid profile name", "Please provide a valid profile name"); + return; + } + + var profile = new ProfileModel(name, _mainManager.KeyboardManager.ActiveKeyboard.Name, _gameModel.Name); + var test = ProfileProvider.GetAll(); + if (test.Contains(profile)) + { + var overwrite = + await + _mainManager.DialogService.ShowQuestionMessageBox("Overwrite existing profile", + "A profile with this name already exists for this game. Would you like to overwrite it?"); + if (!overwrite.Value) + return; + } + + ProfileProvider.AddOrUpdate(profile); + + LoadProfiles(); + SelectedProfileModel = profile; } private ImageSource GenerateKeyboardImage() diff --git a/Artemis/Artemis/Views/ProfileEditorView.xaml b/Artemis/Artemis/Views/ProfileEditorView.xaml index eaf042c15..440d24e28 100644 --- a/Artemis/Artemis/Views/ProfileEditorView.xaml +++ b/Artemis/Artemis/Views/ProfileEditorView.xaml @@ -32,129 +32,20 @@ + + + + +