public async Task RefreshAsync() { using (await AsyncLock.LockAsync()) { try { RefreshingGames = true; // Cache the game view models var displayVMCache = new Dictionary <Games, Page_Games_GameViewModel>(); Logger.Info("All displayed games are being refreshed..."); // Refresh all categories foreach (var category in GameCategories) { Logger.Info("The displayed games in {0} are being refreshed...", category.DisplayName.Value); try { // Clear collections category.InstalledGames.Clear(); category.NotInstalledGames.Clear(); category.AnyInstalledGames = false; category.AnyNotInstalledGames = false; // Enumerate each game foreach (Games game in category.Games) { // Get the game info var info = game.GetGameInfo(); // If cached, reuse the view model, otherwise create new and add to cache Page_Games_GameViewModel displayVM = displayVMCache.ContainsKey(game) ? displayVMCache[game] : displayVMCache[game] = info.GetDisplayViewModel(); // Check if it has been added if (info.IsAdded) { // Add the game to the collection category.InstalledGames.Add(displayVM); category.AnyInstalledGames = true; } else { category.NotInstalledGames.Add(displayVM); category.AnyNotInstalledGames = true; } } } catch (Exception ex) { Logger.Fatal(ex, "Refreshing games in {0}", category.DisplayName); throw; } Logger.Info("The displayed games in {0} have been refreshed with {1} installed and {2} not installed games", category.DisplayName, category.InstalledGames.Count, category.NotInstalledGames.Count); } // Allow game finder to run only if there are games which have not been found CanRunGameFinder = GameCategories.Any(x => x.AnyNotInstalledGames); } catch (Exception ex) { Logger.Fatal(ex, "Refreshing games"); throw; } finally { RefreshingGames = false; } } }
/// <summary> /// Refreshes the games /// </summary> /// <returns>The task</returns> public async Task RefreshAsync() { using (await AsyncLock.LockAsync()) { try { RefreshingGames = true; if (Application.Current.Dispatcher == null) { throw new Exception("Dispatcher can not be NULL"); } // Cache the game view models var displayVMCache = new Dictionary <Games, GameDisplayViewModel>(); RL.Logger?.LogInformationSource($"All displayed games are being refreshed..."); // Refresh all categories foreach (var category in GameCategories) { RL.Logger?.LogInformationSource($"The displayed games in {category.DisplayName.Value} are being refreshed..."); try { // Clear collections Application.Current.Dispatcher.Invoke(() => category.InstalledGames.Clear()); Application.Current.Dispatcher.Invoke(() => category.NotInstalledGames.Clear()); category.AnyInstalledGames = false; category.AnyNotInstalledGames = false; // Enumerate each game foreach (Games game in category.Games) { // Get the game info var info = game.GetGameInfo(); // If cached, reuse the view model, otherwise create new and add to cache GameDisplayViewModel displayVM = displayVMCache.ContainsKey(game) ? displayVMCache[game] : displayVMCache[game] = info.GetDisplayViewModel(); // Check if it has been added if (info.IsAdded) { // Add the game to the collection Application.Current.Dispatcher.Invoke(() => category.InstalledGames.Add(displayVM)); category.AnyInstalledGames = true; } else { Application.Current.Dispatcher.Invoke(() => category.NotInstalledGames.Add(displayVM)); category.AnyNotInstalledGames = true; } } } catch (Exception ex) { ex.HandleCritical($"Refreshing games in {category.DisplayName}"); throw; } RL.Logger?.LogInformationSource($"The displayed games in {category.DisplayName} have been refreshed with {category.InstalledGames.Count} installed and {category.NotInstalledGames.Count} not installed games"); } // Allow game finder to run only if there are games which have not been found // ReSharper disable once PossibleNullReferenceException await Application.Current.Dispatcher.InvokeAsync(() => { RunGameFinderCommand.CanExecuteCommand = GameCategories.Any(x => x.AnyNotInstalledGames); //// NOTE: This is a hacky solution to a weird WPF issue where an item can get duplicated in the view //foreach (var c in GameCategories) //{ // CollectionViewSource.GetDefaultView(c.InstalledGames).Refresh(); // CollectionViewSource.GetDefaultView(c.NotInstalledGames).Refresh(); //} }); } catch (Exception ex) { ex.HandleCritical("Refreshing games"); throw; } finally { RefreshingGames = false; } } }