Skip to content

Commit

Permalink
dialog: use mvvm pattern for sound command dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganom committed Oct 15, 2024
1 parent 9cf7515 commit 63f4ade
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Avalonia.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace SimpleTwitchEmoteSounds.ViewModels;

public partial class NewSoundCommandDialogViewModel : ObservableObject
{
[ObservableProperty] private string _name = string.Empty;
[ObservableProperty] private string _category = string.Empty;

[RelayCommand]
private void Ok()
{
CloseRequested?.Invoke(this, new NewSoundCommandResult(Name, Category));
}

[RelayCommand]
private void Cancel()
{
CloseRequested?.Invoke(this, null);
}

[RelayCommand]
private void KeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Ok();
}
}

public event EventHandler<NewSoundCommandResult?>? CloseRequested;
}

public record NewSoundCommandResult(string Name, string Category);
24 changes: 18 additions & 6 deletions SimpleTwitchEmoteSounds/Views/NewSoundCommandDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
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:vm="using:SimpleTwitchEmoteSounds.ViewModels"
xmlns:i="using:Avalonia.Xaml.Interactivity"
xmlns:ia="using:Avalonia.Xaml.Interactions.Core"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="200"
x:Class="SimpleTwitchEmoteSounds.Views.NewSoundCommandDialog"
x:DataType="vm:NewSoundCommandDialogViewModel"
Title="New Sound Command"
Width="300" Height="200">
<i:Interaction.Behaviors>
<ia:EventTriggerBehavior EventName="KeyDown">
<ia:InvokeCommandAction Command="{Binding KeyDownCommand}" PassEventArgsToCommand="True" />
</ia:EventTriggerBehavior>
</i:Interaction.Behaviors>
<Design.DataContext>
<vm:NewSoundCommandDialogViewModel />
</Design.DataContext>
<StackPanel Margin="20">
<TextBlock Text="Name:" Margin="0,0,0,5"/>
<TextBox x:Name="NameTextBox" Margin="0,0,0,10"/>
<TextBlock Text="Category:" Margin="0,0,0,5"/>
<TextBox x:Name="CategoryTextBox" Margin="0,0,0,10"/>
<TextBlock Text="Name:" Margin="0,0,0,5" />
<TextBox Text="{Binding Name}" Margin="0,0,0,10" />
<TextBlock Text="Category:" Margin="0,0,0,5" />
<TextBox Text="{Binding Category}" Margin="0,0,0,10" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button Content="Cancel" Click="CancelButton_Click" Margin="0,0,10,0"/>
<Button Content="OK" Click="OkButton_Click"/>
<Button Content="Cancel" Command="{Binding CancelCommand}" Margin="0,0,10,0" />
<Button Content="OK" Command="{Binding OkCommand}" />
</StackPanel>
</StackPanel>
</Window>
30 changes: 6 additions & 24 deletions SimpleTwitchEmoteSounds/Views/NewSoundCommandDialog.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using SimpleTwitchEmoteSounds.ViewModels;

namespace SimpleTwitchEmoteSounds.Views;

Expand All @@ -11,34 +10,17 @@ public partial class NewSoundCommandDialog : Window
public NewSoundCommandDialog()
{
InitializeComponent();
KeyDown += NewSoundCommandDialog_KeyDown;
DataContext = new NewSoundCommandDialogViewModel();
((NewSoundCommandDialogViewModel)DataContext).CloseRequested += ViewModel_CloseRequested;
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

private void NewSoundCommandDialog_KeyDown(object? sender, KeyEventArgs e)
private void ViewModel_CloseRequested(object? sender, NewSoundCommandResult? result)
{
if (e.Key == Key.Enter)
{
OkButton_Click(null, null);
}
Close(result);
}

private void OkButton_Click(object? sender, RoutedEventArgs? e)
{
var name = this.FindControl<TextBox>("NameTextBox")?.Text ?? string.Empty;
var category = this.FindControl<TextBox>("CategoryTextBox")?.Text ?? string.Empty;

Close(new NewSoundCommandResult(name, category));
}

private void CancelButton_Click(object? sender, RoutedEventArgs e)
{
Close(null);
}
}

public record NewSoundCommandResult(string Name, string Category);
}

0 comments on commit 63f4ade

Please sign in to comment.