diff --git a/AutoPrintr/AutoPrintr/ViewModels/JobsViewModel.cs b/AutoPrintr/AutoPrintr/ViewModels/JobsViewModel.cs index eb85a6b..ba470be 100644 --- a/AutoPrintr/AutoPrintr/ViewModels/JobsViewModel.cs +++ b/AutoPrintr/AutoPrintr/ViewModels/JobsViewModel.cs @@ -14,6 +14,7 @@ public class JobsViewModel : BaseViewModel { #region Properties private readonly IWindowsServiceClient _windowsServiceClient; + private readonly ILoggerService _loggingService; public ObservableCollection Jobs { get; private set; } @@ -44,10 +45,12 @@ public DocumentType? SelectedDocumentType #region Constructors public JobsViewModel(INavigationService navigationService, - IWindowsServiceClient windowsServiceClient) + IWindowsServiceClient windowsServiceClient, + ILoggerService loggingService) : base(navigationService) { _windowsServiceClient = windowsServiceClient; + _loggingService = loggingService; JobStates = Enum.GetValues(typeof(JobState)) .OfType() @@ -89,26 +92,36 @@ public void NavigatedFrom() private async void LoadJobs() { - ShowBusyControl(); + try + { + ShowBusyControl(); + + Jobs.Clear(); - Jobs.Clear(); + var jobs = (await _windowsServiceClient.GetJobsAsync())? + .Where(x => x != null) + .Where(x => SelectedJobState.HasValue ? x.State == SelectedJobState.Value : true) + .Where(x => SelectedDocumentType.HasValue ? x.Document.Type == SelectedDocumentType.Value : true) + .OrderByDescending(x => x.UpdatedOn); - var jobs = (await _windowsServiceClient.GetJobsAsync())? - .Where(x => SelectedJobState.HasValue ? x.State == SelectedJobState.Value : true) - .Where(x => SelectedDocumentType.HasValue ? x.Document.Type == SelectedDocumentType.Value : true) - .OrderByDescending(x => x.UpdatedOn); + if (jobs == null) + { + ShowMessageControl("Jobs cannot be loaded, the AutoPrintr service is not available. Please run the service and try again"); + HideBusyControl(); + return; + } - if (jobs == null) + foreach (var job in jobs) + Jobs.Add(job); + } + catch (Exception e) + { + _loggingService?.WriteError(e); + } + finally { - ShowMessageControl("Jobs cannot be loaded, the AutoPrintr service is not available. Please run the service and try again"); HideBusyControl(); - return; } - - foreach (var job in jobs) - Jobs.Add(job); - - HideBusyControl(); } private void JobChanged(Job job)