GoldbergGUI/GoldbergGUI.Core/ViewModels/SearchResultViewModel.cs

74 lines
2.3 KiB
C#
Raw Normal View History

using GoldbergGUI.Core.Models;
2022-05-12 15:40:28 -04:00
using Microsoft.Extensions.Logging;
using MvvmCross.Commands;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;
2021-12-22 07:15:28 -05:00
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GoldbergGUI.Core.ViewModels
{
public class SearchResultViewModel : MvxNavigationViewModel<IEnumerable<SteamApp>>, IMvxViewModel<IEnumerable<SteamApp>, SteamApp>
{
private readonly IMvxNavigationService _navigationService;
2022-05-12 15:40:28 -04:00
private readonly ILogger<SearchResultViewModel> _log;
private IEnumerable<SteamApp> _apps;
2022-05-12 15:40:28 -04:00
public SearchResultViewModel(ILoggerFactory logProvider, IMvxNavigationService navigationService) :
base(logProvider, navigationService)
{
2022-05-12 15:40:28 -04:00
_log = logProvider.CreateLogger<SearchResultViewModel>();//.GetLogFor(typeof(SearchResultViewModel));
_navigationService = navigationService;
}
public override void Prepare(IEnumerable<SteamApp> parameter)
{
Apps = parameter;
}
2021-12-22 07:15:28 -05:00
public IEnumerable<SteamApp> Apps
{
get => _apps;
set
{
_apps = value;
RaisePropertyChanged(() => Apps);
}
}
2021-12-22 07:15:28 -05:00
public SteamApp Selected
{
get;
set;
}
public IMvxCommand SaveCommand => new MvxAsyncCommand(Save);
public IMvxCommand CloseCommand => new MvxAsyncCommand(Close);
public TaskCompletionSource<object> 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)
{
2022-05-12 15:40:28 -04:00
_log.LogInformation($"Successfully got app {Selected}");
await _navigationService.Close(this, Selected).ConfigureAwait(false);
}
}
private async Task Close()
{
await _navigationService.Close(this).ConfigureAwait(false);
}
}
}