From fa75c586333be874926afbb5d6f0c761bf3a1b1f Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Mon, 20 Jan 2020 15:42:59 -0800 Subject: [PATCH] Refactor GQL code. Auto load and save active shops. Auto start monitors. --- BodyshopUploader/App.config | 6 ++ .../Properties/Resources.Designer.cs | 18 +++++ BodyshopUploader/Properties/Resources.resx | 6 ++ .../Properties/Settings.Designer.cs | 24 ++++++ BodyshopUploader/Properties/Settings.settings | 6 ++ BodyshopUploader/Utils/GraphQL.cs | 24 ++++++ BodyshopUploader/Utils/JobProcessingQueue.cs | 26 ++++--- .../ViewModels/MainViewModel.commands.cs | 23 +++++- BodyshopUploader/ViewModels/MainViewModel.cs | 78 ++++++++----------- .../ViewModels/MainViewModel.props.cs | 8 +- BodyshopUploader/Views/Main.xaml | 15 ++-- 11 files changed, 166 insertions(+), 68 deletions(-) diff --git a/BodyshopUploader/App.config b/BodyshopUploader/App.config index 1a9db1e..c02d0a6 100644 --- a/BodyshopUploader/App.config +++ b/BodyshopUploader/App.config @@ -40,6 +40,12 @@ False + + + + + True + \ No newline at end of file diff --git a/BodyshopUploader/Properties/Resources.Designer.cs b/BodyshopUploader/Properties/Resources.Designer.cs index f08b708..217f11b 100644 --- a/BodyshopUploader/Properties/Resources.Designer.cs +++ b/BodyshopUploader/Properties/Resources.Designer.cs @@ -114,6 +114,15 @@ namespace BodyshopUploader.Properties { } } + /// + /// Looks up a localized string similar to Auto-start Monitors. + /// + public static string Label_AutoStartMonitor { + get { + return ResourceManager.GetString("Label_AutoStartMonitor", resourceCulture); + } + } + /// /// Looks up a localized string similar to File Path. /// @@ -168,6 +177,15 @@ namespace BodyshopUploader.Properties { } } + /// + /// Looks up a localized string similar to Error uploading job.. + /// + public static string Msg_NewJobUploadError { + get { + return ResourceManager.GetString("Msg_NewJobUploadError", resourceCulture); + } + } + /// /// Looks up a localized string similar to Password. /// diff --git a/BodyshopUploader/Properties/Resources.resx b/BodyshopUploader/Properties/Resources.resx index 5a29646..a2e1b8f 100644 --- a/BodyshopUploader/Properties/Resources.resx +++ b/BodyshopUploader/Properties/Resources.resx @@ -135,6 +135,9 @@ Add Path + + Auto-start Monitors + File Path @@ -153,6 +156,9 @@ New Job Uploaded + + Error uploading job. + Password diff --git a/BodyshopUploader/Properties/Settings.Designer.cs b/BodyshopUploader/Properties/Settings.Designer.cs index 6e20028..3b0ae13 100644 --- a/BodyshopUploader/Properties/Settings.Designer.cs +++ b/BodyshopUploader/Properties/Settings.Designer.cs @@ -68,5 +68,29 @@ namespace BodyshopUploader.Properties { this["AutoStartMonitors"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string LastSelectedShop { + get { + return ((string)(this["LastSelectedShop"])); + } + set { + this["LastSelectedShop"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool AutoStartMonitor { + get { + return ((bool)(this["AutoStartMonitor"])); + } + set { + this["AutoStartMonitor"] = value; + } + } } } diff --git a/BodyshopUploader/Properties/Settings.settings b/BodyshopUploader/Properties/Settings.settings index d6b608b..29c6021 100644 --- a/BodyshopUploader/Properties/Settings.settings +++ b/BodyshopUploader/Properties/Settings.settings @@ -14,5 +14,11 @@ False + + + + + True + \ No newline at end of file diff --git a/BodyshopUploader/Utils/GraphQL.cs b/BodyshopUploader/Utils/GraphQL.cs index a677a8c..bf7fd5b 100644 --- a/BodyshopUploader/Utils/GraphQL.cs +++ b/BodyshopUploader/Utils/GraphQL.cs @@ -1,4 +1,6 @@ using GraphQL.Client; +using GraphQL.Common.Request; +using GraphQL.Common.Response; using System; using System.Collections.Generic; using System.Linq; @@ -9,11 +11,33 @@ namespace BodyshopUploader.Utils { public static class GraphQL { + private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + public static GraphQLClient CreateGQLClient() { var graphQLClient = new GraphQLClient("https://bodyshop-dev-db.herokuapp.com/v1/graphql"); graphQLClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + Utils.Auth.authlink.FirebaseToken); return graphQLClient; } + + public static async Task ExecuteQuery(GraphQLRequest r) + { + using (var g = Utils.GraphQL.CreateGQLClient()) + { + logger.Trace("Firing GQL Query: {0} {1}", r.Query.ToString(), r.Variables); + var graphQLResponse = await g.PostAsync(r); + if (graphQLResponse.Errors == null) + { + logger.Trace("GQL Response: {0}", graphQLResponse.Data); + return graphQLResponse.Data; + } + else + { + logger.Error("Error executing query."); + Array.ForEach(graphQLResponse.Errors, x => logger.Error(x.ToString())); + return null; + } + } + } } } diff --git a/BodyshopUploader/Utils/JobProcessingQueue.cs b/BodyshopUploader/Utils/JobProcessingQueue.cs index 9b414b1..9e1bfc3 100644 --- a/BodyshopUploader/Utils/JobProcessingQueue.cs +++ b/BodyshopUploader/Utils/JobProcessingQueue.cs @@ -83,15 +83,12 @@ namespace BodyshopUploader.Utils } private static async Task UpsertQueueItem(DTO_QueueItem item) - { - //Save the job to the DB. - - + { item.Job.shopid = "52b7357c-0edd-4c95-85c3-dfdbcdfad9ac"; item.Job.est_number = "123"; item.Job.vehicle.data.shopid = "52b7357c-0edd-4c95-85c3-dfdbcdfad9ac"; - logger.Info("Should upsert the job graphqlly here. {0}", item.Job); + logger.Info("Manually setting the shop id! {0}", item.Job); var r = new GraphQLRequest { @@ -104,20 +101,25 @@ namespace BodyshopUploader.Utils } }", Variables = new - { - + { jobInput = item.Job } }; - using (var g = Utils.GraphQL.CreateGQLClient()) + var d = await Utils.GraphQL.ExecuteQuery(r); + if(d!= null) { - var graphQLResponse = await g.PostAsync(r); - if(graphQLResponse.Errors == null) + App.Current.Dispatcher.Invoke(() => { - logger.Trace("Job posted succesfully."); - } + Growler.AddNotification(new Notification() + { + Title = Properties.Resources.Msg_NewJobUploadError, + Subtitle = item.Job?.owner?.first_name?.Value + " " + item.Job?.owner?.last_name?.Value, + Message = item.Job?.vehicle?.v_model_yr?.Value + " " + item.Job?.vehicle?.v_make_desc?.Value + " " + item.Job?.vehicle?.v_model_desc?.Value + }); + }); } + _jobs.Dequeue(); App.Current.Dispatcher.Invoke(() => diff --git a/BodyshopUploader/ViewModels/MainViewModel.commands.cs b/BodyshopUploader/ViewModels/MainViewModel.commands.cs index bbe6495..954829b 100644 --- a/BodyshopUploader/ViewModels/MainViewModel.commands.cs +++ b/BodyshopUploader/ViewModels/MainViewModel.commands.cs @@ -79,9 +79,9 @@ namespace BodyshopUploader.ViewModels if (_startFolderMonitorsCommand == null) { _startFolderMonitorsCommand = new RelayCommand( - p => MonitoringPaths.Count > 0, + p => MonitoringPaths.Count > 0 && ActiveShop != null, p => StartAllFolderMonitors() - ); + ) ; } return _startFolderMonitorsCommand; } @@ -102,5 +102,24 @@ namespace BodyshopUploader.ViewModels return _stopFolderMonitorsCommand; } } + + private ICommand _quitCommand; + public ICommand QuitCommand + { + get + { + if (_quitCommand == null) + { + _quitCommand = new RelayCommand( + p => true, + p => + { + Properties.Settings.Default.Save(); App.Current.Shutdown(0); + }); + } + return _quitCommand; + } + } + } } diff --git a/BodyshopUploader/ViewModels/MainViewModel.cs b/BodyshopUploader/ViewModels/MainViewModel.cs index 744fd82..748f2da 100644 --- a/BodyshopUploader/ViewModels/MainViewModel.cs +++ b/BodyshopUploader/ViewModels/MainViewModel.cs @@ -31,20 +31,18 @@ namespace BodyshopUploader.ViewModels Growler = new GrowlNotification(this); Utils.JobProcessingQueue.SetGrowler(Growler); - - } private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { ((BackgroundWorker)sender).Dispose(); - //Progress = 0; - logger.Trace("BW Completed."); + Progress = 0; } private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { Progress = e.ProgressPercentage; + StartFolderMonitorsCommand.Execute(null); } private void bw_InitVm(object sender, DoWorkEventArgs e) @@ -65,7 +63,6 @@ namespace BodyshopUploader.ViewModels //Cannot use await. LoadBodyshopData().Wait(); ; - _callingThread.ReportProgress(80); logger.Debug("VM Init Complete"); @@ -75,7 +72,10 @@ namespace BodyshopUploader.ViewModels private void MonitoringPathsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { - logger.Warn("TODO: Change monitoring lifecycles for folder watchers."); + IndeterminateLoading = true; + StopAllFolderMonitors(); + StartAllFolderMonitors(); + IndeterminateLoading = false; } public void AddFolderMonitoringPath() @@ -101,6 +101,7 @@ namespace BodyshopUploader.ViewModels public void StartAllFolderMonitors() { + IndeterminateLoading = true; if (MonitoringPaths.Count > 0) foreach (var m in MonitoringPaths) { @@ -109,19 +110,20 @@ namespace BodyshopUploader.ViewModels foreach (var p in MonitoringPaths) { - //Ensure the directory exists, then start monitoring for CIECA files. p.StartMonitor(); - } + IndeterminateLoading = false; } public void StopAllFolderMonitors() { + IndeterminateLoading = true; if (MonitoringPaths.Count > 0) foreach (var m in MonitoringPaths) { m.StopMonitor(); } + IndeterminateLoading = false; } public async Task LoadBodyshopData() @@ -140,33 +142,31 @@ namespace BodyshopUploader.ViewModels }" }; - using (var g = Utils.GraphQL.CreateGQLClient()) + var Data = await Utils.GraphQL.ExecuteQuery(r); + if (Data != null) { - logger.Trace("Firing GQL Query: {0}", r.ToString()); - var graphQLResponse = await g.PostAsync(r); - if (graphQLResponse.Errors == null) - { - logger.Trace("GQL Response: {0}", graphQLResponse.Data.bodyshops); - var p = graphQLResponse.Data.bodyshops; - ShopData = graphQLResponse.Data.bodyshops.ToObject>(); - - } - else - { - logger.Error("Error querying bodyshop data."); - } - + ShopData = Data.bodyshops.ToObject>(); + } + string SettingsSelectedShopUuid = Properties.Settings.Default.LastSelectedShop; + if (string.IsNullOrEmpty(SettingsSelectedShopUuid)) + { + ActiveShop = ShopData[0] ?? null; + Properties.Settings.Default.LastSelectedShop = ShopData[0].Id ?? null; + Properties.Settings.Default.Save(); + } + else + { + ActiveShop = ShopData.Where(_ => _.Id == SettingsSelectedShopUuid).FirstOrDefault(); } } - public async Task SetActiveBodyshop(Bodyshop b) + public async Task SetActiveBodyshop() { + IndeterminateLoading = true; foreach (var s in ShopData) { if (s.Id == ActiveShop.Id) s.AssociationActive = true; else s.AssociationActive = false; - - var r = new GraphQLRequest { Query = @" @@ -185,21 +185,11 @@ namespace BodyshopUploader.ViewModels assocActive = s.AssociationActive } }; - - using (var g = Utils.GraphQL.CreateGQLClient()) - { - logger.Trace("Firing GQL Query: {0}", r.ToString()); - var graphQLResponse = await g.PostAsync(r); - if (graphQLResponse.Errors == null) - { - logger.Trace("GQL Response: {0}", graphQLResponse.Data); - } - else - { - logger.Error("Error mutating data."); - } - } + await Utils.GraphQL.ExecuteQuery(r); } + Properties.Settings.Default.LastSelectedShop = ActiveShop.Id; + Properties.Settings.Default.Save(); + IndeterminateLoading = false; } public async Task TestGql() @@ -213,7 +203,6 @@ namespace BodyshopUploader.ViewModels }; Growler.AddNotification(_n); - var r = new GraphQLRequest { Query = @" @@ -224,12 +213,7 @@ namespace BodyshopUploader.ViewModels } }" }; - - using (var g = Utils.GraphQL.CreateGQLClient()) - { - var graphQLResponse = await g.PostAsync(r); - logger.Info("GQL Response: {0}", graphQLResponse.Data.bodyshops); - } + await Utils.GraphQL.ExecuteQuery(r); } } } diff --git a/BodyshopUploader/ViewModels/MainViewModel.props.cs b/BodyshopUploader/ViewModels/MainViewModel.props.cs index d50d276..3891b60 100644 --- a/BodyshopUploader/ViewModels/MainViewModel.props.cs +++ b/BodyshopUploader/ViewModels/MainViewModel.props.cs @@ -34,12 +34,18 @@ namespace BodyshopUploader.ViewModels set { SetProperty(ref _progress, value); } } + private bool _indeterminateLoading; + public bool IndeterminateLoading + { + get { return _indeterminateLoading; } + set { SetProperty(ref _indeterminateLoading, value); } + } private Bodyshop _activeShop; public Bodyshop ActiveShop { get { return _activeShop; } - set { SetProperty(ref _activeShop, value); SetActiveBodyshop(value); } + set { SetProperty(ref _activeShop, value); Task.Run(async () => await SetActiveBodyshop()); } } } } diff --git a/BodyshopUploader/Views/Main.xaml b/BodyshopUploader/Views/Main.xaml index 342c6d2..db487d7 100644 --- a/BodyshopUploader/Views/Main.xaml +++ b/BodyshopUploader/Views/Main.xaml @@ -29,6 +29,11 @@ + + - - + @@ -76,17 +80,16 @@ - +