1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 13:50:14 +00:00
msfs-popout-panel-manager/Provider/UserProfileManager.cs

140 lines
5.2 KiB
C#
Raw Normal View History

2022-01-27 13:40:04 +00:00
using MSFSPopoutPanelManager.Model;
using MSFSPopoutPanelManager.Shared;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace MSFSPopoutPanelManager.Provider
{
public class UserProfileManager
{
private const string USER_PROFILE_DATA_FILENAME = "userprofiledata.json";
public ObservableCollection<UserProfile> UserProfiles { get; set; }
public int AddUserProfile(string newProfileName)
{
return AddProfile(new UserProfile(), newProfileName);
}
public int AddUserProfileByCopyingProfile(string newProfileName, int copyProfileId)
{
if (UserProfiles == null)
throw new Exception("User Profiles is null.");
var matchedProfile = UserProfiles.FirstOrDefault(p => p.ProfileId == copyProfileId);
var copiedProfile = matchedProfile.Copy<UserProfile>(); // Using Shared/ObjectExtensions.cs extension method
2022-04-18 16:24:00 +00:00
copiedProfile.BindingAircraftLiveries = new ObservableCollection<string>();
2022-01-27 13:40:04 +00:00
return AddProfile(copiedProfile, newProfileName);
}
public bool DeleteUserProfile(int profileId)
{
if (UserProfiles == null)
throw new Exception("User Profiles is null.");
if (profileId == -1)
return false;
var profileToRemove = UserProfiles.First(x => x.ProfileId == profileId);
UserProfiles.Remove(profileToRemove);
WriteUserProfiles();
Logger.LogStatus($"Profile '{profileToRemove.ProfileName}' has been deleted successfully.", StatusMessageType.Info);
return true;
}
public void AddProfileBinding(string planeTitle, int activeProfileId)
{
2022-04-18 22:07:30 +00:00
var boundProfile = UserProfiles.FirstOrDefault(p => p.BindingAircraftLiveries.ToList().Exists(p => p == planeTitle));
if (boundProfile != null)
2022-01-27 13:40:04 +00:00
{
2022-04-18 22:07:30 +00:00
Logger.LogStatus($"Unable to add binding to the profile because '{planeTitle}' was already bound to profile '{boundProfile.ProfileName}'.", StatusMessageType.Error);
2022-01-27 13:40:04 +00:00
return;
}
2022-04-18 16:24:00 +00:00
UserProfiles.First(p => p.ProfileId == activeProfileId).BindingAircraftLiveries.Add(planeTitle);
2022-01-27 13:40:04 +00:00
WriteUserProfiles();
2022-04-18 03:38:33 +00:00
2022-01-27 13:40:04 +00:00
Logger.LogStatus($"Binding for the profile has been added successfully.", StatusMessageType.Info);
}
2022-04-18 03:38:33 +00:00
public void DeleteProfileBinding(string planeTitle, int activeProfileId)
2022-01-27 13:40:04 +00:00
{
2022-04-18 16:24:00 +00:00
UserProfiles.First(p => p.ProfileId == activeProfileId).BindingAircraftLiveries.Remove(planeTitle);
2022-01-27 13:40:04 +00:00
WriteUserProfiles();
Logger.LogStatus($"Binding for the profile has been deleted successfully.", StatusMessageType.Info);
}
public void ReadUserProfiles()
{
try
{
using (StreamReader reader = new StreamReader(Path.Combine(FileIo.GetUserDataFilePath(), USER_PROFILE_DATA_FILENAME)))
{
UserProfiles = new ObservableCollection<UserProfile>(JsonConvert.DeserializeObject<List<UserProfile>>(reader.ReadToEnd()));
}
}
2022-04-18 16:24:00 +00:00
catch
2022-01-27 13:40:04 +00:00
{
UserProfiles = new ObservableCollection<UserProfile>(new List<UserProfile>());
}
}
public void WriteUserProfiles()
{
Debug.WriteLine("saving profile....");
if (UserProfiles == null)
throw new Exception("User Profiles is null.");
try
{
var userProfilePath = FileIo.GetUserDataFilePath();
if (!Directory.Exists(userProfilePath))
Directory.CreateDirectory(userProfilePath);
using (StreamWriter file = File.CreateText(Path.Combine(userProfilePath, USER_PROFILE_DATA_FILENAME)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, UserProfiles);
}
}
catch
{
Logger.LogStatus($"Unable to write user data file: {USER_PROFILE_DATA_FILENAME}", StatusMessageType.Error);
}
}
private int AddProfile(UserProfile userProfile, string newProfileName)
{
if (UserProfiles == null)
throw new Exception("User Profiles is null.");
var newPlaneProfile = userProfile;
var newProfileId = UserProfiles.Count > 0 ? UserProfiles.Max(x => x.ProfileId) + 1 : 1;
newPlaneProfile.ProfileName = newProfileName;
newPlaneProfile.ProfileId = newProfileId;
var tmpList = UserProfiles.ToList();
tmpList.Add(newPlaneProfile);
var index = tmpList.OrderBy(x => x.ProfileName).ToList().FindIndex(x => x.ProfileId == newProfileId);
UserProfiles.Insert(index, newPlaneProfile);
WriteUserProfiles();
Logger.LogStatus($"Profile '{newPlaneProfile.ProfileName}' has been added successfully.", StatusMessageType.Info);
return newProfileId;
}
}
}