From f196b9caccd53f544ebd962096d05eaa060f480a Mon Sep 17 00:00:00 2001 From: Sander van Vliet Date: Tue, 21 Nov 2023 14:12:13 +0100 Subject: [PATCH] Remove OpenRoute command and resolve a bunch of warnings --- .../ViewModels/BuildRouteViewModel.cs | 112 ++---------------- .../DesignTimeBuildRouteViewModel.cs | 3 +- .../ViewModels/MainWindowViewModel.cs | 2 +- .../Views/BuildRoute.axaml | 1 - 4 files changed, 15 insertions(+), 103 deletions(-) diff --git a/src/RoadCaptain.App.RouteBuilder/ViewModels/BuildRouteViewModel.cs b/src/RoadCaptain.App.RouteBuilder/ViewModels/BuildRouteViewModel.cs index 2b918fe6..c11dae4c 100644 --- a/src/RoadCaptain.App.RouteBuilder/ViewModels/BuildRouteViewModel.cs +++ b/src/RoadCaptain.App.RouteBuilder/ViewModels/BuildRouteViewModel.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -12,7 +11,6 @@ using RoadCaptain.App.Shared.Commands; using RoadCaptain.App.Shared.Dialogs; using RoadCaptain.Ports; -using RoadCaptain.UseCases; namespace RoadCaptain.App.RouteBuilder.ViewModels { @@ -31,10 +29,9 @@ public class BuildRouteViewModel : ViewModelBase private bool _showElevationProfile; private readonly IWindowService _windowService; private Task? _simulationTask; - private readonly ConvertZwiftMapRouteUseCase _convertUseCase; public BuildRouteViewModel(RouteViewModel routeViewModel, IUserPreferences userPreferences, - IWindowService windowService, IWorldStore worldStore, ISegmentStore segmentStore, IStatusBarService statusBarService) + IWindowService windowService, ISegmentStore segmentStore, IStatusBarService statusBarService) { _userPreferences = userPreferences; _windowService = windowService; @@ -42,7 +39,6 @@ public BuildRouteViewModel(RouteViewModel routeViewModel, IUserPreferences userP _showClimbs = _userPreferences.ShowClimbs; _showSprints = _userPreferences.ShowSprints; _showElevationProfile = _userPreferences.ShowElevationProfile; - _convertUseCase = new ConvertZwiftMapRouteUseCase(worldStore, segmentStore); Route = routeViewModel; Route.PropertyChanged += (_, args) => HandleRoutePropertyChanged(segmentStore, args); @@ -54,32 +50,25 @@ public BuildRouteViewModel(RouteViewModel routeViewModel, IUserPreferences userP _ => Route.Sequence.Any()) .SubscribeTo(this, () => Route.Sequence) .OnSuccess(_ => statusBarService1.Info("Route saved successfully")) - .OnSuccessWithMessage(_ => statusBarService1.Info($"Route saved successfully: {_.Message}")) - .OnFailure(_ => statusBarService1.Error($"Failed to save route because: {_.Message}")) + .OnSuccessWithMessage(commandResult => statusBarService1.Info($"Route saved successfully: {commandResult.Message}")) + .OnFailure(commandResult => statusBarService1.Error($"Failed to save route because: {commandResult.Message}")) .OnNotExecuted(_ => statusBarService1.Info("Route hasn't changed dit not need to not saved")); - OpenRouteCommand = new AsyncRelayCommand( - _ => OpenRoute(), - _ => true) - .OnSuccess(_ => statusBarService1.Info("Route loaded successfully")) - .OnSuccessWithMessage(_ => statusBarService1.Info(_.Message)) - .OnFailure(_ => statusBarService1.Error($"Failed to load route because: {_.Message}")); - ClearRouteCommand = new AsyncRelayCommand( _ => ClearRoute(), _ => Route.ReadyToBuild && Route.Sequence.Any()) .SubscribeTo(this, () => Route.Sequence) .SubscribeTo(this, () => Route.ReadyToBuild) .OnSuccess(_ => statusBarService1.Info("Route cleared")) - .OnFailure(_ => statusBarService1.Error($"Failed to clear route because: {_.Message}")); + .OnFailure(commandResult => statusBarService1.Error($"Failed to clear route because: {commandResult.Message}")); SelectSegmentCommand = new AsyncRelayCommand( - _ => SelectSegment(_ as Segment ?? + parameter => SelectSegment(parameter as Segment ?? throw new ArgumentNullException(nameof(RelayCommand.CommandParameter))), _ => true) .OnSuccess(_ => statusBarService1.Info("Added segment")) - .OnSuccessWithMessage(_ => statusBarService1.Info($"Added segment {_.Message}")) - .OnFailure(_ => statusBarService1.Warning(_.Message)); + .OnSuccessWithMessage(commandResult => statusBarService1.Info($"Added segment {commandResult.Message}")) + .OnFailure(commandResult => statusBarService1.Warning(commandResult.Message)); SimulateCommand = new RelayCommand( _ => SimulateRoute(), @@ -99,24 +88,24 @@ public BuildRouteViewModel(RouteViewModel routeViewModel, IUserPreferences userP _ => Route.World != null) .SubscribeTo(this, () => Route.World); - ToggleShowClimbsCommand = new AsyncRelayCommand(async _ => + ToggleShowClimbsCommand = new AsyncRelayCommand(_ => { ShowClimbs = !ShowClimbs; - return CommandResult.Success(); + return Task.FromResult(CommandResult.Success()); }, _ => Route.World != null); - ToggleShowSprintsCommand = new AsyncRelayCommand(async _ => + ToggleShowSprintsCommand = new AsyncRelayCommand(_ => { ShowSprints = !ShowSprints; - return CommandResult.Success(); + return Task.FromResult(CommandResult.Success()); }, _ => Route.World != null); - ToggleShowElevationCommand = new AsyncRelayCommand(async _ => + ToggleShowElevationCommand = new AsyncRelayCommand(_ => { ShowElevationProfile = !ShowElevationProfile; - return CommandResult.Success(); + return Task.FromResult(CommandResult.Success()); }, _ => Route.World != null); } @@ -124,7 +113,6 @@ public BuildRouteViewModel(RouteViewModel routeViewModel, IUserPreferences userP public RouteViewModel Route { get; } public RouteSegmentListViewModel RouteSegmentListViewModel { get; } public ICommand SaveRouteCommand { get; } - public ICommand OpenRouteCommand { get; } public ICommand ClearRouteCommand { get; } public ICommand SelectSegmentCommand { get; } public ICommand SimulateCommand { get; } @@ -286,80 +274,6 @@ private async Task ClearRoute() return commandResult; } - private async Task OpenRoute() - { - if (Route.IsTainted) - { - MessageBoxResult questionResult = await _windowService.ShowShouldSaveRouteDialog(); - - if (questionResult == MessageBoxResult.Cancel) - { - return CommandResult.Aborted(); - } - - if (questionResult == MessageBoxResult.Yes) - { - var saveResult = await SaveRoute(); - - // If saving was not successful then return the - // result of SaveRoute instead of proceeding. - if (saveResult.Result != Result.Success) - { - return saveResult; - } - } - } - - var (plannedRoute, fileName) = await _windowService.ShowOpenRouteDialog(); - - if (plannedRoute != null) - { - Route.LoadFromPlannedRoute(plannedRoute); - - return CommandResult.Success(); - } - - if (fileName != null) - { - Route.OutputFilePath = fileName.EndsWith(".gpx") - ? Path.ChangeExtension(fileName, ".json") - : fileName; - - _userPreferences.LastUsedFolder = Path.GetDirectoryName(Route.OutputFilePath); - _userPreferences.Save(); - - SelectedSegment = null; - - try - { - string? successMessage = null; - - if (fileName.EndsWith(".gpx")) - { - var convertedRoute = _convertUseCase.Execute(ZwiftMapRoute.FromGpxFile(fileName)); - Route.LoadFromPlannedRoute(convertedRoute, true); - successMessage = $"Successfully imported ZwiftMap route: {Path.GetFileName(fileName)}"; - } - else - { - Route.Load(); - } - - this.RaisePropertyChanged(nameof(Route)); - - return successMessage == null - ? CommandResult.Success() - : CommandResult.SuccessWithMessage(successMessage); - } - catch (Exception e) - { - return CommandResult.Failure(e.Message); - } - } - - return CommandResult.Aborted(); - } - private CommandResult SimulateRoute() { if (_simulationTask != null && SimulationState == SimulationState.Running) diff --git a/src/RoadCaptain.App.RouteBuilder/ViewModels/DesignTimeBuildRouteViewModel.cs b/src/RoadCaptain.App.RouteBuilder/ViewModels/DesignTimeBuildRouteViewModel.cs index 0eb312c9..49e9dadf 100644 --- a/src/RoadCaptain.App.RouteBuilder/ViewModels/DesignTimeBuildRouteViewModel.cs +++ b/src/RoadCaptain.App.RouteBuilder/ViewModels/DesignTimeBuildRouteViewModel.cs @@ -24,8 +24,7 @@ static DesignTimeBuildRouteViewModel() public DesignTimeBuildRouteViewModel() : base(new RouteViewModel(Container.Resolve(), Container.Resolve()), new DummyUserPreferences(), - new WindowService(Container, Container.Resolve()), - Container.Resolve(), + new WindowService(Container, Container.Resolve()), Container.Resolve(), new StatusBarService() ) diff --git a/src/RoadCaptain.App.RouteBuilder/ViewModels/MainWindowViewModel.cs b/src/RoadCaptain.App.RouteBuilder/ViewModels/MainWindowViewModel.cs index d3ace58b..963c529b 100644 --- a/src/RoadCaptain.App.RouteBuilder/ViewModels/MainWindowViewModel.cs +++ b/src/RoadCaptain.App.RouteBuilder/ViewModels/MainWindowViewModel.cs @@ -74,7 +74,7 @@ public MainWindowViewModel(IRouteStore routeStore, ISegmentStore segmentStore, I } }; - BuildRouteViewModel = new BuildRouteViewModel(Route, userPreferences, windowService, worldStore, segmentStore, statusBarService); + BuildRouteViewModel = new BuildRouteViewModel(Route, userPreferences, windowService, segmentStore, statusBarService); OpenLinkCommand = new RelayCommand( link => OpenLink(link as string ?? throw new ArgumentNullException(nameof(RelayCommand.CommandParameter))), diff --git a/src/RoadCaptain.App.RouteBuilder/Views/BuildRoute.axaml b/src/RoadCaptain.App.RouteBuilder/Views/BuildRoute.axaml index 1ea5cb73..0cf5769f 100644 --- a/src/RoadCaptain.App.RouteBuilder/Views/BuildRoute.axaml +++ b/src/RoadCaptain.App.RouteBuilder/Views/BuildRoute.axaml @@ -87,7 +87,6 @@