Skip to content

Commit

Permalink
Remove OpenRoute command and resolve a bunch of warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sandermvanvliet committed Nov 21, 2023
1 parent 4b0905e commit f196b9c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 103 deletions.
112 changes: 13 additions & 99 deletions src/RoadCaptain.App.RouteBuilder/ViewModels/BuildRouteViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand All @@ -31,18 +29,16 @@ 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;
var statusBarService1 = statusBarService;
_showClimbs = _userPreferences.ShowClimbs;
_showSprints = _userPreferences.ShowSprints;
_showElevationProfile = _userPreferences.ShowElevationProfile;
_convertUseCase = new ConvertZwiftMapRouteUseCase(worldStore, segmentStore);

Route = routeViewModel;
Route.PropertyChanged += (_, args) => HandleRoutePropertyChanged(segmentStore, args);
Expand All @@ -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(),
Expand All @@ -99,32 +88,31 @@ 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);
}

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; }
Expand Down Expand Up @@ -286,80 +274,6 @@ private async Task<CommandResult> ClearRoute()
return commandResult;
}

private async Task<CommandResult> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ static DesignTimeBuildRouteViewModel()
public DesignTimeBuildRouteViewModel()
: base(new RouteViewModel(Container.Resolve<IRouteStore>(), Container.Resolve<ISegmentStore>()),
new DummyUserPreferences(),
new WindowService(Container, Container.Resolve<MonitoringEvents>()),
Container.Resolve<IWorldStore>(),
new WindowService(Container, Container.Resolve<MonitoringEvents>()),
Container.Resolve<ISegmentStore>(),
new StatusBarService()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down
1 change: 0 additions & 1 deletion src/RoadCaptain.App.RouteBuilder/Views/BuildRoute.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
<Button Content="🌐" Command="{Binding Path=ResetWorldCommand}" ToolTip.Tip="Select world and sport"
Classes="toolbar" />
<Button Content="💾" Command="{Binding Path=SaveRouteCommand}" ToolTip.Tip="Save route" Classes="toolbar" />
<Button Content="📂" Command="{Binding Path=OpenRouteCommand}" ToolTip.Tip="Open route" Classes="toolbar" />
<Button Content="" Command="{Binding Path=RemoveLastSegmentCommand}" ToolTip.Tip="Remove last segment"
Classes="toolbar" />
<Button Content="" Command="{Binding Path=ToggleShowClimbsCommand}" ToolTip.Tip="Show the KOM start (red) and finish (green) markers on the map" Classes="toolbar" />
Expand Down

0 comments on commit f196b9c

Please sign in to comment.