Skip to content

Commit

Permalink
error dialog when no token
Browse files Browse the repository at this point in the history
  • Loading branch information
exp111 committed Mar 20, 2024
1 parent a44a3ce commit aef1069
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Turbulence.Core/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public partial class MainWindowViewModel : ViewModelBase,

private readonly IPlatformClient _client = Ioc.Default.GetService<IPlatformClient>()!;

public event EventHandler<string>? ErrorEvent;

[ObservableProperty]
private bool _searchOpen = false;

Expand All @@ -51,7 +53,14 @@ public void Connect()
return;

// Get token
var token = new ConfigurationManager().AddUserSecrets<MainWindowViewModel>().Build()["token"]! ?? throw new Exception("No token set"); // TODO: use other storage
var token = new ConfigurationManager().AddUserSecrets<MainWindowViewModel>().Build()["token"]; //TODO: use other storage
if (token == null)
{
ErrorEvent?.Invoke(this, "No Token set.");
Messenger.Send(new SetStatusMsg("Error"));
return;
}

Task.Run(() => _client.Start(token));
Messenger.Send(new SetStatusMsg("Connecting..."));
}
Expand Down
23 changes: 23 additions & 0 deletions Turbulence.Desktop/Dialogs/OkWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dialogs="clr-namespace:Turbulence.Desktop.Dialogs"
mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="150"
MaxWidth="250" MaxHeight="150"
WindowStartupLocation="CenterOwner"
x:Class="Turbulence.Desktop.Dialogs.OkWindow"
x:DataType="dialogs:OkWindow"
Title="Info">
<Grid Background="#313338">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="Auto" />
<RowDefinition Height="30" />
<RowDefinition Height="Auto" />
<RowDefinition Height="15" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" HorizontalAlignment="Center" Text="{Binding Prompt}" TextWrapping="Wrap">Prompt</TextBlock>
<Button Grid.Row="3" Name="abort" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Click="OnClick">OK</Button>
</Grid>
</Window>
45 changes: 45 additions & 0 deletions Turbulence.Desktop/Dialogs/OkWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Turbulence.Desktop.Dialogs;

public partial class OkWindow : Window, INotifyPropertyChanged
{
// Needed to notify the view that a property has changed
public event PropertyChangedEventHandler? PropertyChanged;

Check warning on line 14 in Turbulence.Desktop/Dialogs/OkWindow.axaml.cs

View workflow job for this annotation

GitHub Actions / build

'OkWindow.PropertyChanged' hides inherited member 'AvaloniaObject.PropertyChanged'. Use the new keyword if hiding was intended.

Check warning on line 14 in Turbulence.Desktop/Dialogs/OkWindow.axaml.cs

View workflow job for this annotation

GitHub Actions / build

'OkWindow.PropertyChanged' hides inherited member 'AvaloniaObject.PropertyChanged'. Use the new keyword if hiding was intended.

// The prompt shown at the top
private string? prompt;

public string Prompt
{
get => prompt ?? "";

set
{
if (value != prompt)
{
prompt = value;
NotifyPropertyChanged();
}
}
}

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public OkWindow()
{
InitializeComponent();
DataContext = this;
}

private void OnClick(object sender, RoutedEventArgs e) => Close();
}
12 changes: 12 additions & 0 deletions Turbulence.Desktop/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Avalonia.Input;
using Avalonia.Interactivity;
using Turbulence.Core.ViewModels;
using Turbulence.Desktop.Dialogs;

namespace Turbulence.Desktop;

Expand All @@ -16,6 +17,17 @@ public MainWindow()
{
InitializeComponent();
_vm = (MainWindowViewModel)DataContext!;
_vm.ErrorEvent += OnErrorEvent;
}

private async void OnErrorEvent(object? sender, string e)
{
OkWindow errorDialog = new()
{
Title = "Error",
Prompt = e
};
await errorDialog.ShowDialog(this);
}

// Menu Item Handlers //TODO: move them into the menu bar view + use it??
Expand Down

0 comments on commit aef1069

Please sign in to comment.