From 34ae9069c752c857b95540c15235cfce66e8ebdf Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 5 Mar 2021 14:44:53 +1300 Subject: [PATCH] Refactored tracking errors Moved tracking profile errors into ProfileRepository and moved shortcut errors into ShortcutRepository. --- DisplayMagician/ShortcutItem.cs | 2 +- DisplayMagician/ShortcutRepository.cs | 13 +++++++++ DisplayMagician/UIForms/DisplayProfileForm.cs | 15 +++++------ .../UIForms/ImageListViewRenderers.cs | 5 ++-- DisplayMagician/UIForms/ShortcutForm.cs | 6 +++++ .../UIForms/ShortcutLibraryForm.cs | 21 +++++++++++---- DisplayMagicianShared/ProfileRepository.cs | 27 +++++++++++++++++-- 7 files changed, 71 insertions(+), 18 deletions(-) diff --git a/DisplayMagician/ShortcutItem.cs b/DisplayMagician/ShortcutItem.cs index b7fec17..fa4c3b7 100644 --- a/DisplayMagician/ShortcutItem.cs +++ b/DisplayMagician/ShortcutItem.cs @@ -1937,7 +1937,7 @@ namespace DisplayMagician // Is the profile still valid right now? i.e. are all the screens available? if (ProfileToUse == null) { - return (false,string.Format("The profile is not valid right now and cannot be used.")); + return (false,string.Format("The profile does not exist (probably deleted) and cannot be used.")); } if (!ProfileToUse.IsPossible) { diff --git a/DisplayMagician/ShortcutRepository.cs b/DisplayMagician/ShortcutRepository.cs index 80ae22b..780c70b 100644 --- a/DisplayMagician/ShortcutRepository.cs +++ b/DisplayMagician/ShortcutRepository.cs @@ -26,6 +26,7 @@ namespace DisplayMagician #region Class Variables // Common items to the class private static List _allShortcuts = new List(); + public static Dictionary _shortcutValidityLookup = new Dictionary(); private static bool _shortcutsLoaded = false; // Other constants that are useful private static string AppShortcutStoragePath = Path.Combine(Program.AppDataPath, $"Shortcuts"); @@ -90,6 +91,18 @@ namespace DisplayMagician } } + public static Dictionary ShortcutValidityLookup + { + get + { + if (!_shortcutsLoaded) + // Load the Shortcuts from storage if they need to be + LoadShortcuts(); + + return _shortcutValidityLookup; + } + } + public static int ShortcutCount { diff --git a/DisplayMagician/UIForms/DisplayProfileForm.cs b/DisplayMagician/UIForms/DisplayProfileForm.cs index 778e892..3e11bb7 100644 --- a/DisplayMagician/UIForms/DisplayProfileForm.cs +++ b/DisplayMagician/UIForms/DisplayProfileForm.cs @@ -18,7 +18,7 @@ namespace DisplayMagician.UIForms //private static bool _inDialog = false; private static ProfileItem _profileToLoad = null; private ProfileAdaptor _profileAdaptor = new ProfileAdaptor(); - public static Dictionary profileValidity = new Dictionary(); + //public static Dictionary profileValidity = new Dictionary(); public DisplayProfileForm() { @@ -129,8 +129,6 @@ namespace DisplayMagician.UIForms // Empty the imageListView ilv_saved_profiles.Items.Clear(); - profileValidity.Clear(); - //IOrderedEnumerable orderedProfiles = ProfileRepository.AllProfiles.OrderBy(p => p.Name); // Check if the last selected profile is still in the list of profiles @@ -149,7 +147,7 @@ namespace DisplayMagician.UIForms newItem.Selected = true; - profileValidity[profile.Name] = profile.IsPossible; + //ProfileRepository.ProfileValidityLookup[profile.Name] = profile.IsPossible; // Add it to the list! ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); @@ -316,6 +314,9 @@ namespace DisplayMagician.UIForms // Lets save the old names for usage next string oldProfileName = _selectedProfile.Name; + // Lets rename the selectedProfile to the new name + ProfileRepository.RenameProfile(_selectedProfile, txt_profile_save_name.Text); + // Lets rename the entry in the imagelistview to the new name foreach (ImageListViewItem myItem in ilv_saved_profiles.Items) { @@ -324,9 +325,7 @@ namespace DisplayMagician.UIForms myItem.Text = txt_profile_save_name.Text; } } - // Lets rename the selectedProfile to the new name - ProfileRepository.RenameProfile(_selectedProfile, txt_profile_save_name.Text); - + // Lets update the rest of the profile screen too lbl_profile_shown.Text = txt_profile_save_name.Text; @@ -339,7 +338,7 @@ namespace DisplayMagician.UIForms ChangeSelectedProfile(_selectedProfile); // now update the profiles image listview - //ilv_saved_profiles.Refresh(); + RefreshDisplayProfileUI(); } diff --git a/DisplayMagician/UIForms/ImageListViewRenderers.cs b/DisplayMagician/UIForms/ImageListViewRenderers.cs index 15e124d..f193fc1 100644 --- a/DisplayMagician/UIForms/ImageListViewRenderers.cs +++ b/DisplayMagician/UIForms/ImageListViewRenderers.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using DisplayMagicianShared; using Manina.Windows.Forms; namespace DisplayMagician.UIForms @@ -95,7 +96,7 @@ namespace DisplayMagician.UIForms { Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize)); - if (ShortcutLibraryForm.shortcutValidity[item.Text]) + if (ShortcutRepository.ShortcutValidityLookup[item.Text]) { // Draw the full color image as the shortcuts is not invalid g.DrawImage(img, pos); @@ -253,7 +254,7 @@ namespace DisplayMagician.UIForms { Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize)); - if (DisplayProfileForm.profileValidity[item.Text]) + if (ProfileRepository.ProfileValidityLookup[item.Text]) { // Draw the full color image as the shortcuts is not invalid g.DrawImage(img, pos); diff --git a/DisplayMagician/UIForms/ShortcutForm.cs b/DisplayMagician/UIForms/ShortcutForm.cs index 3770d08..f6a2ea6 100644 --- a/DisplayMagician/UIForms/ShortcutForm.cs +++ b/DisplayMagician/UIForms/ShortcutForm.cs @@ -878,6 +878,12 @@ namespace DisplayMagician.UIForms } } + + MessageBox.Show( + @"The Display Profile used by this Shortcut no longer exists and cannot be used. You need to choose a new Display Profile for this Shortcut. We have selected the current Display Profile, but you can choose another profile if you wish.", + @"Display Profile no longer exists", + MessageBoxButtons.OK, + MessageBoxIcon.Exclamation); } // Now start populating the other fields if they need it diff --git a/DisplayMagician/UIForms/ShortcutLibraryForm.cs b/DisplayMagician/UIForms/ShortcutLibraryForm.cs index e43e568..420e8be 100644 --- a/DisplayMagician/UIForms/ShortcutLibraryForm.cs +++ b/DisplayMagician/UIForms/ShortcutLibraryForm.cs @@ -19,7 +19,7 @@ namespace DisplayMagician.UIForms private ShortcutAdaptor _shortcutAdaptor = new ShortcutAdaptor(); private ShortcutItem _selectedShortcut = null; - public static Dictionary shortcutValidity = new Dictionary(); + //public static Dictionary shortcutValidity = new Dictionary(); public ShortcutLibraryForm() { @@ -44,6 +44,8 @@ namespace DisplayMagician.UIForms RemoveWarningIfShortcuts(); } + + private void RefreshShortcutLibraryUI() { @@ -58,7 +60,7 @@ namespace DisplayMagician.UIForms ImageListViewItem newItem = null; ilv_saved_shortcuts.Items.Clear(); - shortcutValidity.Clear(); + ShortcutRepository.ShortcutValidityLookup.Clear(); foreach (ShortcutItem loadedShortcut in ShortcutRepository.AllShortcuts.OrderBy(s => s.Name)) { @@ -69,7 +71,7 @@ namespace DisplayMagician.UIForms newItem.Selected = true; (bool result, string thing) = loadedShortcut.IsValid(); - shortcutValidity[loadedShortcut.Name] = result; + ShortcutRepository.ShortcutValidityLookup[loadedShortcut.Name] = result; //ilv_saved_profiles.Items.Add(newItem); ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor); @@ -186,7 +188,7 @@ namespace DisplayMagician.UIForms if (_selectedShortcut == null) return; - if (!shortcutValidity[_selectedShortcut.Name]) + if (!ShortcutRepository.ShortcutValidityLookup[_selectedShortcut.Name]) return; // Run the selected shortcut @@ -195,7 +197,7 @@ namespace DisplayMagician.UIForms private void SetRunOption() { - if (shortcutValidity[_selectedShortcut.Name]) + if (ShortcutRepository.ShortcutValidityLookup[_selectedShortcut.Name]) { btn_run.Visible = true; cms_shortcuts.Items[1].Enabled = true; @@ -211,6 +213,7 @@ namespace DisplayMagician.UIForms private void btn_new_Click(object sender, EventArgs e) { + this.Cursor = Cursors.WaitCursor; var shortcutForm = new ShortcutForm(new ShortcutItem()); shortcutForm.ShowDialog(this); if (shortcutForm.DialogResult == DialogResult.OK) @@ -219,7 +222,9 @@ namespace DisplayMagician.UIForms _selectedShortcut = shortcutForm.Shortcut; RefreshShortcutLibraryUI(); } + this.Cursor = Cursors.Default; RemoveWarningIfShortcuts(); + } private void btn_edit_Click(object sender, EventArgs e) @@ -231,6 +236,8 @@ namespace DisplayMagician.UIForms if (_selectedShortcut == null) return; + this.Cursor = Cursors.WaitCursor; + var shortcutForm = new ShortcutForm(_selectedShortcut); shortcutForm.ShowDialog(this); if (shortcutForm.DialogResult == DialogResult.OK) @@ -240,6 +247,7 @@ namespace DisplayMagician.UIForms ShortcutRepository.SaveShortcuts(); } + this.Cursor = Cursors.Default; } private void btn_delete_Click(object sender, EventArgs e) @@ -313,6 +321,9 @@ namespace DisplayMagician.UIForms private void ShortcutLibraryForm_Activated(object sender, EventArgs e) { + // Refresh the Shortcut Library UI + RefreshShortcutLibraryUI(); + RemoveWarningIfShortcuts(); } diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index 5cb371f..5121c03 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -21,6 +21,7 @@ namespace DisplayMagicianShared #region Class Variables // Common items to the class private static List _allProfiles = new List(); + public static Dictionary _profileValidityLookup = new Dictionary(); private static bool _profilesLoaded = false; public static Version _version = new Version(1, 0, 0); private static ProfileItem _currentProfile; @@ -89,6 +90,18 @@ namespace DisplayMagicianShared } } + public static Dictionary ProfileValidityLookup + { + get + { + if (!_profilesLoaded) + // Load the Profiles from storage if they need to be + LoadProfiles(); + + return _profileValidityLookup; + } + } + public static ProfileItem CurrentProfile { get @@ -148,10 +161,12 @@ namespace DisplayMagicianShared SaveProfiles(); } + // Refresh the profiles to see whats valid + IsPossibleRefresh(); + //Doublecheck it's been added if (ContainsProfile(profile)) { - IsPossibleRefresh(); return true; } else @@ -439,6 +454,8 @@ namespace DisplayMagicianShared profile.Name = GetValidFilename(renamedName); + IsPossibleRefresh(); + // If it's been added to the list of AllProfiles // then we also need to reproduce the Icons if (ContainsProfile(profile)) @@ -454,7 +471,6 @@ namespace DisplayMagicianShared return false; } - IsPossibleRefresh(); } @@ -488,6 +504,8 @@ namespace DisplayMagicianShared SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository."); _currentProfile = activeProfile; + //IsPossibleRefresh(); + } @@ -686,6 +704,9 @@ namespace DisplayMagicianShared if (_profilesLoaded && _allProfiles.Count > 0) { + + _profileValidityLookup.Clear(); + foreach (ProfileItem loadedProfile in AllProfiles) { @@ -701,12 +722,14 @@ namespace DisplayMagicianShared { SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {loadedProfile.Name} is possible!"); loadedProfile.IsPossible = true; + _profileValidityLookup[loadedProfile.Name] = true; } else { SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {loadedProfile.Name} is NOT possible!"); loadedProfile.IsPossible = false; + _profileValidityLookup[loadedProfile.Name] = false; } }