Skip to content

Commit

Permalink
Disable new session fields and prevent switching tab when OpenCommand…
Browse files Browse the repository at this point in the history
…Async.IsRunning; Fix #6;
  • Loading branch information
BobLd committed Nov 21, 2021
1 parent 58e8610 commit 9727f46
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Panoptes.Model/Sessions/File/FileSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void Unsubscribe()

public void Dispose()
{
_watcher.Dispose();
_watcher?.Dispose();
}

public SessionState State
Expand Down
6 changes: 6 additions & 0 deletions Panoptes.ViewModels/NewSession/NewFileSessionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Panoptes.Model.Sessions.File;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -51,6 +52,11 @@ private async Task OpenAsync(CancellationToken cancellationToken)
Error = null;
await _sessionService.OpenAsync(_fileSessionParameters, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException ocEx)
{
Debug.WriteLine($"NewFileSessionViewModel.OpenAsync: Operation was canceled.\n{ocEx}");
//Error = ocEx.ToString();
}
catch (Exception ex)
{
Error = ex.ToString();
Expand Down
4 changes: 3 additions & 1 deletion Panoptes.ViewModels/NewSession/NewMongoSessionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Panoptes.Model.Sessions;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Security;
using System.Threading;
Expand Down Expand Up @@ -66,7 +67,8 @@ private async Task OpenAsync(CancellationToken cancellationToken)
}
catch (OperationCanceledException ocEx)
{
Error = ocEx.ToString();
Debug.WriteLine($"NewMongoSessionViewModel.OpenAsync: Operation was canceled.\n{ocEx}");
//Error = ocEx.ToString();
}
catch (TimeoutException toEx)
{
Expand Down
29 changes: 28 additions & 1 deletion Panoptes.ViewModels/NewSession/NewSessionWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Panoptes.ViewModels.NewSession
{
Expand All @@ -25,7 +26,11 @@ public ObservableCollection<INewSessionViewModel> NewSessionViewModels

public INewSessionViewModel SelectedViewModel
{
get { return _selectedViewModel; }
get
{
return _selectedViewModel;
}

set
{
_selectedViewModel = value;
Expand All @@ -35,7 +40,29 @@ public INewSessionViewModel SelectedViewModel

public NewSessionWindowViewModel(IEnumerable<INewSessionViewModel> newSessionViewModels)
{
foreach (var session in newSessionViewModels)
{
session.OpenCommandAsync.PropertyChanged += OpenCommandAsync_PropertyChanged;
}
NewSessionViewModels = new ObservableCollection<INewSessionViewModel>(newSessionViewModels);
}

private void OpenCommandAsync_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
IsAnyRunning = NewSessionViewModels.Any(ns => ns.OpenCommandAsync.IsRunning);
}

private bool _isAnyRunning;
public bool IsAnyRunning
{
get { return _isAnyRunning; }

set
{
if (_isAnyRunning == value) return;
_isAnyRunning = value;
OnPropertyChanged();
}
}
}
}
41 changes: 35 additions & 6 deletions Panoptes.ViewModels/NewSession/NewStreamSessionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Microsoft.Toolkit.Mvvm.Input;
using Panoptes.Model.Sessions;
using Panoptes.Model.Sessions.Stream;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -28,17 +30,33 @@ public NewStreamSessionViewModel(ISessionService sessionService)
});
}

private Task OpenAsync(CancellationToken cancellationToken)
private async Task OpenAsync(CancellationToken cancellationToken)
{
return Task.Run(() =>
try
{
_sessionService.OpenAsync(new StreamSessionParameters
await _sessionService.OpenAsync(new StreamSessionParameters
{
CloseAfterCompleted = true,
Host = Host,
Port = int.Parse(Port)
}, cancellationToken);
}, cancellationToken);
}, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException ocEx)
{
Debug.WriteLine($"NewStreamSessionViewModel.OpenAsync: Operation was canceled.\n{ocEx}");
//Error = ocEx.ToString();
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
Error = ex.InnerException.ToString();
}
else
{
Error = ex.ToString();
}
}
}

private bool CanOpen()
Expand Down Expand Up @@ -102,6 +120,17 @@ public string Port
}
}

public string Error { get; }
private string _error;
public string Error
{
get { return _error; }

set
{
if (_error == value) return;
_error = value;
OnPropertyChanged();
}
}
}
}
1 change: 1 addition & 0 deletions Panoptes.ViewModels/Panels/CashBookPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ private void Clear()
{
try
{
Debug.WriteLine("CashBookPanelViewModel: Clear");
_cashesDic.Clear();
_resultBgWorker.ReportProgress((int)ActionsThreadUI.Clear);
}
Expand Down
1 change: 1 addition & 0 deletions Panoptes.ViewModels/Panels/HoldingsPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ private void Clear()
{
try
{
Debug.WriteLine("HoldingsPanelViewModel: Clear");
_holdingsDic.Clear();
// _resultsQueue ??
_resultBgWorker.ReportProgress((int)ActionsThreadUI.Clear);
Expand Down
1 change: 1 addition & 0 deletions Panoptes.ViewModels/Panels/LogPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private void Clear()
{
try
{
Debug.WriteLine("LogPanelViewModel: Clear");
_resultBgWorker.ReportProgress((int)ActionsThreadUI.Clear);
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions Panoptes.ViewModels/Panels/ProfitLossPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private void Clear()
{
try
{
Debug.WriteLine("ProfitLossPanelViewModel: Clear");
_pnlBgWorker.ReportProgress((int)ActionsThreadUI.Clear);
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void Clear()
{
try
{
Debug.WriteLine("RuntimeStatisticsPanelViewModel: Clear");
// _resultsQueue ??
_statisticsBgWorker.ReportProgress((int)ActionsThreadUI.Clear);
}
Expand Down
2 changes: 2 additions & 0 deletions Panoptes.ViewModels/Panels/StatisticsPanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private void Clear()
{
try
{
Debug.WriteLine("StatisticsPanelViewModel: Clear");
// _resultsQueue ??
Statistics.Clear(); // Need to do that from UI thread
}
Expand All @@ -50,6 +51,7 @@ private void Clear()

private void ParseResult(Result result)
{
if (result.Statistics == null || result.Statistics.Count == 0) return;
Statistics = new ObservableCollection<StatisticViewModel>(result.Statistics.Select(s => new StatisticViewModel
{
Name = s.Key,
Expand Down
1 change: 1 addition & 0 deletions Panoptes/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<converters:StatusBarColorConverter x:Key="StatusBarColorConverter"/>
<converters:LogPanelLogoTypeConverter x:Key="LogPanelLogoTypeConverter"/>
<converters:PositiveNegativeNumberConverter x:Key="PositiveNegativeNumberConverter"/>
<converters:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
</ResourceDictionary>
</Application.Resources>
</Application>
24 changes: 24 additions & 0 deletions Panoptes/Views/Converters/InverseBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Avalonia.Data.Converters;
using System;

namespace Panoptes.Views.Converters
{
// https://stackoverflow.com/questions/1039636/how-to-bind-inverse-boolean-properties-in-wpf
internal sealed class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");

return !(bool)value;
}

public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
9 changes: 6 additions & 3 deletions Panoptes/Views/NewSession/NewFileSessionControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
</Grid.RowDefinitions>

<Label Grid.Row="0">File</Label>
<TextBox Name="_textBoxFileName" Margin="0,0,0,4" Grid.Row="0" Grid.Column="1" Text="{Binding FileName}"/>
<Button Grid.Row="0" Grid.Column="3" Content="Find" Margin="2,0,0,4" Name="_buttonOpenFile"/>
<TextBox Name="_textBoxFileName" Margin="0,0,0,4" Grid.Row="0" Grid.Column="1" Text="{Binding FileName}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>
<Button Grid.Row="0" Grid.Column="3" Content="Find" Margin="2,0,0,4" Name="_buttonOpenFile"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<CheckBox Grid.Row="1" Grid.Column="1" IsChecked="{Binding FileWatch}" Content="Monitor for changes"/>
<CheckBox Grid.Row="1" Grid.Column="1" IsChecked="{Binding FileWatch}" Content="Monitor for changes"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>
<ProgressBar Grid.Row="3" Grid.ColumnSpan="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
IsIndeterminate="{Binding OpenCommandAsync.IsRunning}" IsVisible="{Binding OpenCommandAsync.IsRunning}"/>

Expand Down
12 changes: 8 additions & 4 deletions Panoptes/Views/NewSession/NewMongoSessionControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@
</Grid.RowDefinitions>

<Label Grid.Row="0">Host</Label>
<TextBox Margin="0,0,0,4" Grid.Row="0" Grid.Column="1" Text="{Binding Host}"/>
<TextBox Margin="0,0,0,4" Grid.Row="0" Grid.Column="1" Text="{Binding Host}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<Label Grid.Row="1" Grid.Column="0">Port</Label>
<TextBox Margin="0,0,0,4" Grid.Row="1" Grid.Column="1" Text="{Binding Port}"/>
<TextBox Margin="0,0,0,4" Grid.Row="1" Grid.Column="1" Text="{Binding Port}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<Label Grid.Row="2" Grid.Column="0">User Name</Label>
<TextBox Margin="0,0,0,4" Grid.Row="2" Grid.Column="1" Text="{Binding UserName}"/>
<TextBox Margin="0,0,0,4" Grid.Row="2" Grid.Column="1" Text="{Binding UserName}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<Label Grid.Row="3" Grid.Column="0">Password</Label>
<TextBox Margin="0,0,0,4" Grid.Row="3" Grid.Column="1" PasswordChar="*" Text="{Binding Password}"/>
<TextBox Margin="0,0,0,4" Grid.Row="3" Grid.Column="1" PasswordChar="*" Text="{Binding Password}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<ProgressBar Grid.Row="5" Grid.ColumnSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
IsIndeterminate="{Binding OpenCommandAsync.IsRunning}" IsVisible="{Binding OpenCommandAsync.IsRunning}"/>
Expand Down
3 changes: 2 additions & 1 deletion Panoptes/Views/NewSession/NewSessionWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
<RowDefinition Height="36"></RowDefinition>
</Grid.RowDefinitions>

<TabControl Margin="5" Items="{Binding NewSessionViewModels}" SelectedItem="{Binding SelectedViewModel}">
<TabControl Margin="5" Items="{Binding NewSessionViewModels}" SelectedItem="{Binding SelectedViewModel}"
IsEnabled="{Binding IsAnyRunning, Converter={StaticResource InverseBooleanConverter}}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
Expand Down
9 changes: 6 additions & 3 deletions Panoptes/Views/NewSession/NewStreamSessionControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
</Grid.RowDefinitions>

<Label Grid.Row="0">Host</Label>
<TextBox Margin="0,0,0,4" Grid.Row="0" Grid.Column="1" Text="{Binding Host}"/>
<TextBox Margin="0,0,0,4" Grid.Row="0" Grid.Column="1" Text="{Binding Host}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<Label Grid.Row="1" Grid.Column="0">Port</Label>
<TextBox Margin="0,0,0,4" Grid.Row="1" Grid.Column="1" Text="{Binding Port}"/>
<TextBox Margin="0,0,0,4" Grid.Row="1" Grid.Column="1" Text="{Binding Port}"
IsEnabled="{Binding OpenCommandAsync.IsRunning, Converter={StaticResource InverseBooleanConverter}}"/>

<Label Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock TextWrapping="Wrap">NOT RECOMMENDED - Configure LEAN to use the 'StreamingMessageHandler' implementation. You can start this program to connect automatically to localhost by passing the port as a commandline parameter.</TextBlock>
<TextBlock TextWrapping="Wrap">NOT RECOMMENDED - Will block the Lean engine and this app will become the bottleneck.
Configure LEAN to use the 'StreamingMessageHandler' implementation. You can start this program to connect automatically to localhost by passing the port as a commandline parameter.</TextBlock>
</Label>
</Grid>
</UserControl>

0 comments on commit 9727f46

Please sign in to comment.