1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-29 09:10:09 +00:00
msfs-popout-panel-manager/Orchestration/ProfileData.cs

222 lines
7.5 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.Shared;
using MSFSPopoutPanelManager.UserDataAgent;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
namespace MSFSPopoutPanelManager.Orchestration
{
public class ProfileData : ObservableObject
{
public event PropertyChangedEventHandler ActiveProfileChanged;
public ProfileData()
{
Profiles = new ObservableCollection<Profile>();
}
public ObservableCollection<Profile> Profiles { get; private set; }
public FlightSimData FlightSimData { private get; set; }
public AppSettingData AppSettingData { private get; set; }
public int AddProfile(string profileName)
{
var newProfileId = ProfileManager.AddProfile(profileName, Profiles);
UpdateActiveProfile(newProfileId);
AppSettingData.AppSetting.LastUsedProfileId = newProfileId;
return newProfileId;
}
public int AddProfile(string profileName, int copyFromProfileId)
{
var newProfileId = ProfileManager.AddProfile(profileName, copyFromProfileId, Profiles);
UpdateActiveProfile(newProfileId);
AppSettingData.AppSetting.LastUsedProfileId = newProfileId;
return newProfileId;
}
public bool DeleteProfile(int profileId)
{
if (ActiveProfile == null)
return false;
2022-07-23 20:04:06 +00:00
var success = ProfileManager.DeleteProfile(profileId, Profiles);
UpdateActiveProfile(-1);
return true;
2022-07-23 19:23:32 +00:00
}
2022-08-01 23:21:42 +00:00
public void AddProfileBinding(string aircraft, int activeProfileId)
2022-07-23 19:23:32 +00:00
{
if (ActiveProfile == null)
return;
2022-08-01 23:21:42 +00:00
ProfileManager.AddProfileBinding(aircraft, activeProfileId, Profiles);
2022-07-23 19:23:32 +00:00
RefreshProfile();
}
2022-08-01 23:21:42 +00:00
public void DeleteProfileBinding(string aircraft, int activeProfileId)
2022-07-23 19:23:32 +00:00
{
if (ActiveProfile == null)
return;
2022-08-01 23:21:42 +00:00
ProfileManager.DeleteProfileBinding(aircraft, activeProfileId, Profiles);
2022-07-23 19:23:32 +00:00
RefreshProfile();
}
public void ReadProfiles()
{
Profiles = new ObservableCollection<Profile>(ProfileManager.ReadProfiles());
}
public void WriteProfiles()
{
ProfileManager.WriteProfiles(Profiles);
}
public void UpdateActiveProfile(int profileId)
{
if (profileId == -1 && Profiles.Count > 0)
ActiveProfile = Profiles.FirstOrDefault(p => p.ProfileId == Profiles[0].ProfileId);
else if (profileId == -1 || Profiles.Count == 0)
ActiveProfile = null;
else
ActiveProfile = Profiles.FirstOrDefault(p => p.ProfileId == profileId);
// Set active profile flag, this is used only for MVVM binding
Profiles.ToList().ForEach(p => p.IsActive = false);
if (ActiveProfile != null)
{
ActiveProfile.IsActive = true;
AppSettingData.AppSetting.LastUsedProfileId = ActiveProfile.ProfileId;
}
else
{
AppSettingData.AppSetting.LastUsedProfileId = -1;
}
ActiveProfileChanged?.Invoke(this, null);
}
public Profile ActiveProfile { get; private set; }
public bool HasActiveProfile { get { return ActiveProfile != null; } }
public bool IsAircraftBoundToProfile
{
get
{
if (ActiveProfile == null)
return false;
2022-08-01 23:21:42 +00:00
return ActiveProfile.BindingAircrafts.Any(p => p == FlightSimData.CurrentMsfsAircraft);
2022-07-23 19:23:32 +00:00
}
}
public bool IsAllowedDeleteAircraftBinding
{
get
{
2022-08-01 23:21:42 +00:00
if (ActiveProfile == null || !FlightSimData.HasCurrentMsfsAircraft)
2022-07-23 19:23:32 +00:00
return false;
2022-08-01 23:21:42 +00:00
var uProfile = Profiles.FirstOrDefault(u => u.BindingAircrafts.Any(p => p == FlightSimData.CurrentMsfsAircraft));
2022-07-23 19:23:32 +00:00
if (uProfile != null && uProfile.ProfileId != ActiveProfile.ProfileId)
return false;
2022-08-01 23:21:42 +00:00
return ActiveProfile.BindingAircrafts.Any(p => p == FlightSimData.CurrentMsfsAircraft);
2022-07-23 19:23:32 +00:00
}
}
public bool IsAllowedAddAircraftBinding
{
get
{
2022-08-01 23:21:42 +00:00
if (ActiveProfile == null || !FlightSimData.HasCurrentMsfsAircraft)
2022-07-23 19:23:32 +00:00
return false;
2022-08-01 23:21:42 +00:00
var uProfile = Profiles.FirstOrDefault(u => u.BindingAircrafts.Any(p => p == FlightSimData.CurrentMsfsAircraft));
2022-07-23 19:23:32 +00:00
if (uProfile != null && uProfile.ProfileId != ActiveProfile.ProfileId)
return false;
2022-08-01 23:21:42 +00:00
if (FlightSimData == null || ActiveProfile.BindingAircrafts == null)
2022-07-25 12:40:21 +00:00
return false;
2022-08-01 23:21:42 +00:00
return ActiveProfile == null ? false : !ActiveProfile.BindingAircrafts.Any(p => p == FlightSimData.CurrentMsfsAircraft);
2022-07-23 19:23:32 +00:00
}
}
public void RefreshProfile()
{
int currentProfileId;
if (ActiveProfile == null)
currentProfileId = -1;
else
currentProfileId = ActiveProfile.ProfileId;
ActiveProfile = null;
UpdateActiveProfile(currentProfileId);
}
2022-08-13 06:14:49 +00:00
public void AutoSwitchProfile()
2022-07-23 19:23:32 +00:00
{
2022-08-01 23:21:42 +00:00
// Automatic switching of active profile when SimConnect active aircraft changes
2022-08-13 06:14:49 +00:00
if (Profiles != null && !string.IsNullOrEmpty(FlightSimData.CurrentMsfsAircraft))
2022-07-23 19:23:32 +00:00
{
2022-08-13 06:14:49 +00:00
var matchedProfile = Profiles.FirstOrDefault(p => p.BindingAircrafts.Any(t => t == FlightSimData.CurrentMsfsAircraft));
2022-07-23 19:23:32 +00:00
if (matchedProfile != null)
UpdateActiveProfile(matchedProfile.ProfileId);
}
}
2022-08-05 03:43:51 +00:00
// This is to migrate profile binding from aircraft livery to aircraft name
// Started in v3.4.2
public void MigrateLiveryToAircraftBinding(string liveryName, string aircraftName)
{
2022-08-19 16:03:12 +00:00
bool hasChanges = false;
2022-08-13 06:14:49 +00:00
if (Profiles != null && !string.IsNullOrEmpty(liveryName) && !string.IsNullOrEmpty(aircraftName))
2022-08-05 03:43:51 +00:00
{
var matchedProfile = Profiles.FirstOrDefault(p => p.BindingAircraftLiveries.Any(t => t == liveryName));
2022-08-19 16:03:12 +00:00
2022-08-05 03:43:51 +00:00
if (matchedProfile != null && !matchedProfile.BindingAircrafts.Any(a => a == aircraftName))
{
matchedProfile.BindingAircrafts.Add(aircraftName);
2022-08-19 16:03:12 +00:00
hasChanges = true;
}
if (matchedProfile != null)
{
2022-08-18 14:20:12 +00:00
matchedProfile.BindingAircraftLiveries.Remove(liveryName);
2022-08-19 16:03:12 +00:00
hasChanges = true;
}
if (hasChanges)
{
2022-08-05 03:43:51 +00:00
WriteProfiles();
2022-08-13 06:14:49 +00:00
RefreshProfile();
2022-08-05 03:43:51 +00:00
}
}
}
2022-08-13 06:14:49 +00:00
public void MigrateLiveryToAircraftBinding()
{
MigrateLiveryToAircraftBinding(FlightSimData.CurrentMsfsLiveryTitle, FlightSimData.CurrentMsfsAircraft);
}
public void SaveMsfsGameWindowConfig()
{
if (ActiveProfile == null)
return;
var msfsGameWindowConfig = WindowsAgent.WindowActionManager.GetMsfsGameWindowLocation();
if (msfsGameWindowConfig.IsValid)
{
ActiveProfile.MsfsGameWindowConfig = msfsGameWindowConfig;
WriteProfiles();
}
}
2022-07-23 19:23:32 +00:00
}
}