diff --git a/Artemis/Artemis/DAL/ProfileProvider.cs b/Artemis/Artemis/DAL/ProfileProvider.cs index 09a60f6e3..2aa08226f 100644 --- a/Artemis/Artemis/DAL/ProfileProvider.cs +++ b/Artemis/Artemis/DAL/ProfileProvider.cs @@ -8,18 +8,18 @@ using Newtonsoft.Json; namespace Artemis.DAL { - internal class ProfileProvider + public static class ProfileProvider { - private List _profiles; + private static readonly string ProfileFolder = + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\profiles"; /// /// Get all profiles /// /// All profiles - public List GetAll() + public static List GetAll() { - ReadProfiles(); - return new List(); + return ReadProfiles(); } /// @@ -27,7 +27,7 @@ namespace Artemis.DAL /// /// The game to match /// All profiles matching the provided game - public List GetAll(GameModel game) + public static List GetAll(GameModel game) { return GetAll().Where(g => g.GameName.Equals(game.Name)).ToList(); } @@ -36,37 +36,43 @@ namespace Artemis.DAL /// Adds or update the given profile. /// Updates occur when a profile with the same name and game exist. /// - /// The profile to add or update - public void AddOrUpdate(ProfileModel profile) + /// The profile to add or update + public static void AddOrUpdate(ProfileModel prof) { + 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); } - private void ReadProfiles() + private static List ReadProfiles() { CheckProfiles(); - _profiles = new List(); + var profiles = new List(); // Create the directory structure - var profileFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\profiles"; - var profileFiles = Directory.GetFiles(profileFolder, "*.json", SearchOption.AllDirectories); + var profileFiles = Directory.GetFiles(ProfileFolder, "*.json", SearchOption.AllDirectories); // Parse the JSON files into objects and add them if they are valid foreach (var file in profileFiles) { var prof = JsonConvert.DeserializeObject(File.ReadAllText(file)); if (prof.GameName?.Length > 1 && prof.KeyboardName?.Length > 1 && prof.Name?.Length > 1) - _profiles.Add(prof); + profiles.Add(prof); } + + return profiles; } - private void CheckProfiles() + private static void CheckProfiles() { // Create the directory structure - var profileFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artemis\profiles"; - if (Directory.Exists(profileFolder)) + if (Directory.Exists(ProfileFolder)) return; - Directory.CreateDirectory(profileFolder); + Directory.CreateDirectory(ProfileFolder); Debug.WriteLine("Place presets"); } } diff --git a/Artemis/Artemis/Modules/Games/TheDivision/TheDivisionViewModel.cs b/Artemis/Artemis/Modules/Games/TheDivision/TheDivisionViewModel.cs index 48b392456..314b9e74a 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(MainManager.KeyboardManager.ActiveKeyboard); + ProfileEditor = new ProfileEditorViewModel(GameModel, MainManager.KeyboardManager.ActiveKeyboard); } public ProfileEditorViewModel ProfileEditor { get; set; } diff --git a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs index dafad9215..c09f4655a 100644 --- a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs +++ b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs @@ -1,18 +1,29 @@ using System.Collections.Generic; using System.Windows.Media; -using Artemis.Components; +using Artemis.DAL; using Artemis.KeyboardProviders; +using Artemis.Models; using Caliburn.Micro; namespace Artemis.ViewModels { public class ProfileEditorViewModel : Screen { + private readonly GameModel _gameModel; private readonly KeyboardProvider _keyboard; + private List _profiles; - public ProfileEditorViewModel(KeyboardProvider keyboard) + public ProfileEditorViewModel(GameModel gameModel, KeyboardProvider keyboard) { + _gameModel = gameModel; _keyboard = keyboard; + + GetProfiles(); + } + + public void GetProfiles() + { + _profiles = ProfileProvider.GetAll(_gameModel); } private ImageSource GenerateKeyboardImage()