From 99b9c52b401c0f1a870531227363c1ad697fd8d9 Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Sat, 9 Jan 2021 22:11:49 +0100 Subject: [PATCH] Implemented search results window for AppID search --- GoldbergGUI.Core/Services/SteamService.cs | 18 ++++- GoldbergGUI.Core/ViewModels/MainViewModel.cs | 33 +++++++-- .../ViewModels/SearchResultViewModel.cs | 74 +++++++++++++++++++ GoldbergGUI.WPF/Views/SearchResultView.xaml | 33 +++++++++ .../Views/SearchResultView.xaml.cs | 14 ++++ 5 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 GoldbergGUI.Core/ViewModels/SearchResultViewModel.cs create mode 100644 GoldbergGUI.WPF/Views/SearchResultView.xaml create mode 100644 GoldbergGUI.WPF/Views/SearchResultView.xaml.cs diff --git a/GoldbergGUI.Core/Services/SteamService.cs b/GoldbergGUI.Core/Services/SteamService.cs index e11ecc7..dc111a3 100644 --- a/GoldbergGUI.Core/Services/SteamService.cs +++ b/GoldbergGUI.Core/Services/SteamService.cs @@ -75,13 +75,23 @@ namespace GoldbergGUI.Core.Services { var listOfAppsByName = _cache.Search(x => x.Name) .SetCulture(StringComparison.OrdinalIgnoreCase) - .ContainingAll(name.Split(' ')).ToHashSet(); + .ContainingAll(name.Split(' ')); + return listOfAppsByName; + /*var filteredList = new HashSet(); foreach (var steamApp in listOfAppsByName) { - var sa = Task.Run(async () => await AppDetails.GetAsync(steamApp.AppId).ConfigureAwait(false)).Result; - if (sa.Type != AppType.Game) listOfAppsByName.Remove(steamApp); + try + { + var task = AppDetails.GetAsync(steamApp.AppId); + var details = await task.ConfigureAwait(false); + if (details?.Type != null && details.Type == AppType.Game) filteredList.Add(steamApp); + } + catch (Exception e) + { + _log.Debug($"{e.GetType()}: {steamApp}"); + } } - return listOfAppsByName; + return filteredList;*/ } public SteamApp GetAppByName(string name) diff --git a/GoldbergGUI.Core/ViewModels/MainViewModel.cs b/GoldbergGUI.Core/ViewModels/MainViewModel.cs index 6849718..317ee1c 100644 --- a/GoldbergGUI.Core/ViewModels/MainViewModel.cs +++ b/GoldbergGUI.Core/ViewModels/MainViewModel.cs @@ -8,6 +8,7 @@ using Microsoft.Win32; using MvvmCross.Commands; using MvvmCross.Logging; + using MvvmCross.Navigation; using MvvmCross.ViewModels; namespace GoldbergGUI.Core.ViewModels @@ -15,6 +16,7 @@ // ReSharper disable once ClassNeverInstantiated.Global public class MainViewModel : MvxViewModel { + private readonly IMvxNavigationService _navigationService; private string _dllPath; private string _gameName; @@ -34,11 +36,13 @@ private bool _mainWindowEnabled; private bool _goldbergApplied; - public MainViewModel(ISteamService steam, IGoldbergService goldberg, IMvxLog log) + public MainViewModel(ISteamService steam, IGoldbergService goldberg, IMvxLogProvider logProvider, + IMvxNavigationService navigationService) { _steam = steam; _goldberg = goldberg; - _log = log; + _log = logProvider.GetLogFor(typeof(MainViewModel)); + _navigationService = navigationService; } public override void Prepare() @@ -212,15 +216,16 @@ await ReadConfig().ConfigureAwait(false); } - public IMvxCommand FindIdCommand => new MvxCommand(FindId); + public IMvxCommand FindIdCommand => new MvxAsyncCommand(FindId); - private void FindId() + private async Task FindId() { if (GameName.Contains("Game name...")) { _log.Error("No game name entered!"); return; } + MainWindowEnabled = false; var appByName = _steam.GetAppByName(_gameName); if (appByName != null) { @@ -229,8 +234,26 @@ } else { - _log.Warn("Steam app could not be found!"); + var list = _steam.GetListOfAppsByName(GameName); + var steamApps = list as SteamApp[] ?? list.ToArray(); + if (steamApps.Length == 1) + { + GameName = steamApps[0].Name; + AppId = steamApps[0].AppId; + } + else + { + var navigateTask = _navigationService + .Navigate, SteamApp>(steamApps); + var navigateResult = await navigateTask.ConfigureAwait(false); + if (navigateResult != null) + { + GameName = navigateResult.Name; + AppId = navigateResult.AppId; + } + } } + MainWindowEnabled = true; } //public IMvxCommand GetNameByIdCommand => new MvxAsyncCommand(GetNameById); diff --git a/GoldbergGUI.Core/ViewModels/SearchResultViewModel.cs b/GoldbergGUI.Core/ViewModels/SearchResultViewModel.cs new file mode 100644 index 0000000..dd46194 --- /dev/null +++ b/GoldbergGUI.Core/ViewModels/SearchResultViewModel.cs @@ -0,0 +1,74 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using GoldbergGUI.Core.Models; +using MvvmCross.Commands; +using MvvmCross.Logging; +using MvvmCross.Navigation; +using MvvmCross.ViewModels; + +namespace GoldbergGUI.Core.ViewModels +{ + public class SearchResultViewModel : MvxNavigationViewModel>, IMvxViewModel, SteamApp> + { + private readonly IMvxNavigationService _navigationService; + private readonly IMvxLog _log; + private IEnumerable _apps; + + public SearchResultViewModel(IMvxLogProvider logProvider, IMvxNavigationService navigationService) : + base(logProvider, navigationService) + { + _log = logProvider.GetLogFor(typeof(SearchResultViewModel)); + _navigationService = navigationService; + } + + public override void Prepare(IEnumerable parameter) + { + Apps = parameter; + } + + public IEnumerable Apps + { + get => _apps; + set + { + _apps = value; + RaisePropertyChanged(() => Apps); + } + } + + public SteamApp Selected + { + get; + set; + } + + public IMvxCommand SaveCommand => new MvxAsyncCommand(Save); + + public IMvxCommand CloseCommand => new MvxAsyncCommand(Close); + + public TaskCompletionSource CloseCompletionSource { get; set; } + + public override void ViewDestroy(bool viewFinishing = true) + { + if (viewFinishing && CloseCompletionSource != null && !CloseCompletionSource.Task.IsCompleted && + !CloseCompletionSource.Task.IsFaulted) + CloseCompletionSource?.TrySetCanceled(); + + base.ViewDestroy(viewFinishing); + } + + private async Task Save() + { + if (Selected != null) + { + _log.Info($"Successfully got app {Selected}"); + await _navigationService.Close(this, Selected).ConfigureAwait(false); + } + } + + private async Task Close() + { + await _navigationService.Close(this).ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/GoldbergGUI.WPF/Views/SearchResultView.xaml b/GoldbergGUI.WPF/Views/SearchResultView.xaml new file mode 100644 index 0000000..cb51b49 --- /dev/null +++ b/GoldbergGUI.WPF/Views/SearchResultView.xaml @@ -0,0 +1,33 @@ + + + + + + + +