Skip to content

Commit

Permalink
project: change from expanders to an edit dialog view model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganom committed Oct 15, 2024
1 parent 63f4ade commit dfa6005
Show file tree
Hide file tree
Showing 7 changed files with 477 additions and 407 deletions.
18 changes: 0 additions & 18 deletions SimpleTwitchEmoteSounds/Converters/BoolToDirectionConverter.cs

This file was deleted.

274 changes: 114 additions & 160 deletions SimpleTwitchEmoteSounds/ViewModels/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,98 +62,6 @@ public DashboardViewModel(TwitchService twitchService, IHotkeyService hotkeyServ
}
}

partial void OnUsernameChanged(string value)
{
ConfigService.State.Username = value;
}

private async void TwitchServiceMessageLogged(Privmsg msg)
{
if (!IsEnabled)
{
return;
}

foreach (var soundCommand in SoundCommands)
{
if (!soundCommand.Enabled)
{
Log.Information($"Sound command '{soundCommand.Name}' is disabled. Skipping.");
continue;
}

var isMatch = soundCommand.Names.Any(name =>
{
return soundCommand.SelectedMatchType switch
{
MatchType.Equals => msg.Content.Trim().Equals(name),
MatchType.StartsWith => msg.Content.Trim().StartsWith(name),
MatchType.ContainsWord => Regex.IsMatch(msg.Content, $@"\b{Regex.Escape(name)}\b"),
_ => throw new ArgumentOutOfRangeException()
};
});

if (!isMatch)
{
continue;
}

var shouldPlay = ShouldPlaySound(float.Parse(soundCommand.PlayChance));
Log.Information(
$"Command '{soundCommand.Name}' matched. Play chance: {soundCommand.PlayChance}%. Should play: {shouldPlay}");

if (!shouldPlay)
{
Log.Information(
$"Command '{soundCommand.Name}' matched but didn't pass the play chance check. Continuing to next command.");
continue;
}

Log.Information($"Playing sound for command: {soundCommand.Name}");
await AudioService.PlaySound(soundCommand);
break;
}
}

private static bool ShouldPlaySound(float playChance)
{
var randomValue = (float)Random.Shared.NextDouble();
var shouldPlay = randomValue <= playChance;
Log.Information(
$"Play chance check: Random value: {randomValue:F4}, Play chance: {playChance:F4}, Should play: {shouldPlay}");
return shouldPlay;
}

private void TwitchServiceConnectionStatus(TwitchStatus obj)
{
Log.Information($"Status: {obj.ToString()}");
switch (obj)
{
case TwitchStatus.Disconnected:
IsConnected = false;
ConnectButtonText = "Disconnected";
ConnectButtonColor = "#fc725a";
break;
case TwitchStatus.Connected:
IsConnected = true;
ConnectButtonText = "Connected";
ConnectButtonColor = "#5dc264";
break;
case TwitchStatus.Reconnecting:
IsConnected = false;
ConnectButtonText = "Reconnecting";
ConnectButtonColor = "#ffae43";
break;
default:
throw new ArgumentOutOfRangeException(nameof(obj), obj, null);
}
}

partial void OnSearchTextChanged(string value)
{
FilteredSoundCommands.Refresh();
}

[RelayCommand]
private async Task Connect()
{
Expand All @@ -173,13 +81,6 @@ private void ToggleEnabled()
IsEnabled = !IsEnabled;
}

public string EnabledButtonColor => IsEnabled ? "#5dc264" : "#fc725a";

partial void OnIsEnabledChanged(bool value)
{
OnPropertyChanged(nameof(EnabledButtonColor));
}

[RelayCommand]
private async Task OpenStandardDialog()
{
Expand Down Expand Up @@ -233,38 +134,30 @@ private async Task OpenStandardDialog()
}

[RelayCommand]
private async Task AddSoundFile(SoundCommand soundCommand)
private Task PreviewSound(SoundCommand soundCommand)
{
var topLevel = TopLevel.GetTopLevel(
((IClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!)
.MainWindow);
_ = AudioService.PlaySound(soundCommand);
return Task.CompletedTask;
}

var files = await topLevel?.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
[RelayCommand]
private async Task EditSound(SoundCommand soundCommand)
{
var dialog = new EditSoundCommandDialog(soundCommand)
{
Title = "Select Audio Files",
AllowMultiple = true,
FileTypeFilter =
[
new FilePickerFileType("Audio Files") { Patterns = ["*.mp3", "*.wav", "*.ogg"] }
]
})!;
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
var result = await dialog.ShowDialog<SoundCommand?>(GetMainWindow());

if (files is { Count: >= 1 })
if (result != null)
{
foreach (var f in files)
{
soundCommand.SoundFiles.Add(new SoundFile
{
FileName = f.Name,
FilePath = f.Path.LocalPath,
Percentage = "1"
});
}
FilteredSoundCommands.Refresh();
ConfigService.Settings.RefreshSubscriptions();
}
}

[RelayCommand]
private async Task RemoveItem(SoundCommand soundCommand)
private async Task RemoveSound(SoundCommand soundCommand)
{
var result = await ShowConfirmationDialog(
"Remove Sound",
Expand All @@ -278,44 +171,6 @@ private async Task RemoveItem(SoundCommand soundCommand)
}
}


[RelayCommand]
private void ExpandAll()
{
foreach (var soundCommand in SoundCommands)
{
soundCommand.IsExpanded = true;
}
}

[RelayCommand]
private void CollapseAll()
{
foreach (var soundCommand in SoundCommands)
{
soundCommand.IsExpanded = false;
}
}

[RelayCommand]
private Task PreviewSound(SoundCommand soundCommand)
{
_ = AudioService.PlaySound(soundCommand);
return Task.CompletedTask;
}

[RelayCommand]
private void RemoveSoundFile(SoundFile soundFile)
{
foreach (var soundCommand in SoundCommands)
{
if (soundCommand.SoundFiles.Remove(soundFile))
{
break;
}
}
}

[RelayCommand]
private void ToggleListening()
{
Expand Down Expand Up @@ -346,6 +201,105 @@ private void ToggleSound(SoundCommand soundCommand)
soundCommand.Enabled = !soundCommand.Enabled;
}

partial void OnUsernameChanged(string value)
{
ConfigService.State.Username = value;
}

partial void OnSearchTextChanged(string value)
{
FilteredSoundCommands.Refresh();
}

partial void OnIsEnabledChanged(bool value)
{
OnPropertyChanged(nameof(EnabledButtonColor));
}

public string EnabledButtonColor => IsEnabled ? "#5dc264" : "#fc725a";

private async void TwitchServiceMessageLogged(Privmsg msg)
{
if (!IsEnabled)
{
return;
}

foreach (var soundCommand in SoundCommands)
{
if (!soundCommand.Enabled)
{
Log.Information($"Sound command '{soundCommand.Name}' is disabled. Skipping.");
continue;
}

var isMatch = soundCommand.Names.Any(name =>
{
return soundCommand.SelectedMatchType switch
{
MatchType.Equals => msg.Content.Trim().Equals(name),
MatchType.StartsWith => msg.Content.Trim().StartsWith(name),
MatchType.ContainsWord => Regex.IsMatch(msg.Content, $@"\b{Regex.Escape(name)}\b"),
_ => throw new ArgumentOutOfRangeException()
};
});

if (!isMatch)
{
continue;
}

var shouldPlay = ShouldPlaySound(float.Parse(soundCommand.PlayChance));
Log.Information(
$"Command '{soundCommand.Name}' matched. Play chance: {soundCommand.PlayChance}%. Should play: {shouldPlay}");

if (!shouldPlay)
{
Log.Information(
$"Command '{soundCommand.Name}' matched but didn't pass the play chance check. Continuing to next command.");
continue;
}

Log.Information($"Playing sound for command: {soundCommand.Name}");
await AudioService.PlaySound(soundCommand);
break;
}
}

private static bool ShouldPlaySound(float playChance)
{
var randomValue = (float)Random.Shared.NextDouble();
var shouldPlay = randomValue <= playChance;
Log.Information(
$"Play chance check: Random value: {randomValue:F4}, Play chance: {playChance:F4}, Should play: {shouldPlay}");
return shouldPlay;
}

private void TwitchServiceConnectionStatus(TwitchStatus obj)
{
Log.Information($"Status: {obj.ToString()}");
switch (obj)
{
case TwitchStatus.Disconnected:
IsConnected = false;
ConnectButtonText = "Disconnected";
ConnectButtonColor = "#fc725a";
break;
case TwitchStatus.Connected:
IsConnected = true;
ConnectButtonText = "Connected";
ConnectButtonColor = "#5dc264";
break;
case TwitchStatus.Reconnecting:
IsConnected = false;
ConnectButtonText = "Reconnecting";
ConnectButtonColor = "#ffae43";
break;
default:
throw new ArgumentOutOfRangeException(nameof(obj), obj, null);
}
}

private void ResetState()
{
IsListening = false;
Expand Down
Loading

0 comments on commit dfa6005

Please sign in to comment.