From 23186bc68d4a299dc35d6efb390ff1d374c270bf Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sun, 12 Jul 2015 19:02:42 -0700 Subject: [PATCH] Toggle LibraryProvider availability on authentication status - Add Visible property to LibraryProviders - Filter LibraryProviders based on .Visible - Add authentication status to ChangeCloudSyncStatus calls --- ApplicationView/MainApplicationWidget.cs | 9 +++- Library/Provider/LibraryProvider.cs | 2 + Library/Provider/LibraryProviderFileSystem.cs | 5 ++ Library/Provider/LibraryProviderSelector.cs | 54 ++++++++++++++++--- Library/Provider/LibraryProviderSqlite.cs | 5 ++ VersionManagement/WebRequestHandler.cs | 2 +- 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/ApplicationView/MainApplicationWidget.cs b/ApplicationView/MainApplicationWidget.cs index 7ae9b453f..a3b2ca2ff 100644 --- a/ApplicationView/MainApplicationWidget.cs +++ b/ApplicationView/MainApplicationWidget.cs @@ -333,9 +333,14 @@ namespace MatterHackers.MatterControl ReloadAdvancedControlsPanelTrigger.CallEvents(this, null); } - public void ChangeCloudSyncStatus() + public void ChangeCloudSyncStatus(bool userAuthenticated) { - CloudSyncStatusChanged.CallEvents(this, null); + CloudSyncStatusChanged.CallEvents(this, new CloudSyncEventArgs() { IsAuthenticated = userAuthenticated }); + } + + public class CloudSyncEventArgs : EventArgs + { + public bool IsAuthenticated { get; set; } } } } \ No newline at end of file diff --git a/Library/Provider/LibraryProvider.cs b/Library/Provider/LibraryProvider.cs index 801dcfb20..3d8da7a69 100644 --- a/Library/Provider/LibraryProvider.cs +++ b/Library/Provider/LibraryProvider.cs @@ -52,6 +52,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider #region Member Methods + public abstract bool Visible { get; } + public bool HasParent { get diff --git a/Library/Provider/LibraryProviderFileSystem.cs b/Library/Provider/LibraryProviderFileSystem.cs index bf1f05391..2d1f635c6 100644 --- a/Library/Provider/LibraryProviderFileSystem.cs +++ b/Library/Provider/LibraryProviderFileSystem.cs @@ -78,6 +78,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider GetFilesAndCollectionsInCurrentDirectory(); } + public override bool Visible + { + get { return true; } + } + public override void Dispose() { directoryWatcher.EnableRaisingEvents = false; diff --git a/Library/Provider/LibraryProviderSelector.cs b/Library/Provider/LibraryProviderSelector.cs index a6f1bcbf9..f8e404699 100644 --- a/Library/Provider/LibraryProviderSelector.cs +++ b/Library/Provider/LibraryProviderSelector.cs @@ -36,6 +36,8 @@ using System.Collections.Generic; using System.ComponentModel; using MatterHackers.Localizations; using System.IO; +using System.Linq; +using MatterHackers.Agg.UI; namespace MatterHackers.MatterControl.PrintLibrary.Provider { @@ -44,9 +46,16 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider private static LibraryProviderSelector instance = null; private List libraryProviders = new List(); + private List visibleProviders; + + private event EventHandler unregisterEvents; + private LibraryProviderSelector() : base(null) { + + ApplicationController.Instance.CloudSyncStatusChanged.RegisterEvent(CloudSyncStatusChanged, ref unregisterEvents); + // put in the sqlite provider libraryProviders.Add(new LibraryProviderSQLite(null, this)); @@ -67,6 +76,30 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { libraryProviders.Add(new LibraryProviderFileSystem(downloadsDirectory, "Downloads", this)); } + + this.FilterProviders(); + } + + + private void FilterProviders() + { + this.visibleProviders = libraryProviders.Where(p => p.Visible).ToList(); + LibraryProvider.OnDataReloaded(null); + } + + public void CloudSyncStatusChanged(object sender, EventArgs eventArgs) + { + var e = eventArgs as ApplicationController.CloudSyncEventArgs; + + // If signing out, we need to force selection to this provider + if(e != null && !e.IsAuthenticated) + { + // Switch to the purchased library + LibraryDataView.CurrentLibraryProvider = this; + } + + // Refresh state + UiThread.RunOnIdle(FilterProviders, 1); } public static LibraryProviderSelector Instance @@ -96,10 +129,15 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { get { - return libraryProviders.Count; + return this.visibleProviders.Count; } } + public override bool Visible + { + get { return true; } + } + public override void Dispose() { } @@ -162,22 +200,22 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override PrintItemCollection GetCollectionItem(int collectionIndex) { - LibraryProvider provider = libraryProviders[collectionIndex]; + LibraryProvider provider = visibleProviders[collectionIndex]; return new PrintItemCollection(provider.Name, provider.ProviderKey); } public override PrintItemWrapper GetPrintItemWrapper(int itemIndex) { - if (libraryProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey) + if (visibleProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey) { throw new Exception("It is expected these are the same."); } - return libraryProviders[0].GetPrintItemWrapper(itemIndex); + return visibleProviders[0].GetPrintItemWrapper(itemIndex); } public override LibraryProvider GetProviderForItem(PrintItemCollection collection) { - foreach (LibraryProvider libraryProvider in libraryProviders) + foreach (LibraryProvider libraryProvider in visibleProviders) { if (collection.Key == libraryProvider.ProviderKey) { @@ -198,7 +236,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider List subProviderSavePath; int libraryProviderToUseIndex = GetProviderIndex(printItemWrapper, out subProviderSavePath); - libraryProviders[libraryProviderToUseIndex].RemoveItem(printItemWrapper); + visibleProviders[libraryProviderToUseIndex].RemoveItem(printItemWrapper); } public override void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath = null) @@ -220,9 +258,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider if (providerSavePath != null && providerSavePath.Count > 1) // key 0 is this provider so we want to look at the next provider { - for (int i = 0; i < libraryProviders.Count; i++) + for (int i = 0; i < visibleProviders.Count; i++) { - if (libraryProviders[i].ProviderKey == providerSavePath[1].Key) + if (visibleProviders[i].ProviderKey == providerSavePath[1].Key) { subProviderSavePath = new List(providerSavePath); subProviderSavePath.RemoveAt(0); diff --git a/Library/Provider/LibraryProviderSqlite.cs b/Library/Provider/LibraryProviderSqlite.cs index e1b81fe22..9553db3ad 100644 --- a/Library/Provider/LibraryProviderSqlite.cs +++ b/Library/Provider/LibraryProviderSqlite.cs @@ -89,6 +89,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } + public override bool Visible + { + get { return true; } + } + public override void Dispose() { } diff --git a/VersionManagement/WebRequestHandler.cs b/VersionManagement/WebRequestHandler.cs index 4e5903d09..645f857d0 100644 --- a/VersionManagement/WebRequestHandler.cs +++ b/VersionManagement/WebRequestHandler.cs @@ -225,7 +225,7 @@ namespace MatterHackers.MatterControl.VersionManagement UserSettings.Instance.set("CredentialsInvalidReason", "Session Expired".Localize()); // Notify connection status changed and now invalid - ApplicationController.Instance.ChangeCloudSyncStatus(); + ApplicationController.Instance.ChangeCloudSyncStatus(false); } } catch