diff --git a/Services/TwitchService.cs b/Services/TwitchService.cs index 17a4144..1c058c4 100644 --- a/Services/TwitchService.cs +++ b/Services/TwitchService.cs @@ -32,6 +32,8 @@ public interface ITwitchService public void RegisterForEvents(TwitchChannel channel); public void UnregisterFromEvents(TwitchChannel channel); + public event EventHandler? UserLoginChanged; + public event EventHandler? TwitchChannelUpdated; public event PropertyChangingEventHandler? PropertyChanging; public event PropertyChangedEventHandler? PropertyChanged; } @@ -70,6 +72,7 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot SetField(ref _userChannel, value); _userChannel?.UpdateChannelData(this); + OnOnUserLoginChanged(); } } @@ -82,6 +85,21 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot set => SetField(ref _raidParticipants, value); } + public event EventHandler? UserLoginChanged; + public event EventHandler? TwitchChannelUpdated; + + public event EventHandler OnStreamDown + { + add => TwitchEvents.OnStreamDown += value; + remove => TwitchEvents.OnStreamDown -= value; + } + + public event EventHandler OnStreamUp + { + add => TwitchEvents.OnStreamUp += value; + remove => TwitchEvents.OnStreamUp -= value; + } + public TwitchService(ILogger logger, IWebToolsService webTools) { _logger = logger; @@ -221,6 +239,8 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot public void RegisterForEvents(TwitchChannel channel) { _logger.LogDebug("Registering for events for {channelName} with broadcaster id {channelBroadcasterId} ...", channel.Name, channel.BroadcasterId); + + channel.PropertyChanged += OnTwitchChannelUpdated; TwitchEvents.OnStreamUp += channel.OnStreamUp; TwitchEvents.OnStreamDown += channel.OnStreamDown; @@ -230,11 +250,13 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot TwitchEvents.SendTopics(AccessToken); } - + public void UnregisterFromEvents(TwitchChannel channel) { _logger.LogDebug("Unregistering from events for {channelName} with broadcaster id {channelBroadcasterId} ...", channel.Name, channel.BroadcasterId); + channel.PropertyChanged -= OnTwitchChannelUpdated; + TwitchEvents.OnStreamUp -= channel.OnStreamUp; TwitchEvents.OnStreamDown -= channel.OnStreamDown; TwitchEvents.OnViewCount -= channel.OnViewCount; @@ -388,6 +410,17 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot UserChannel.IsLive = true; } + private void OnTwitchChannelUpdated(object? sender, PropertyChangedEventArgs e) + { + if (sender is not TwitchChannel channel) + return; + + if (e.PropertyName != nameof(TwitchChannel.IsLive)) + return; + + TwitchChannelUpdated?.Invoke(this, channel); + } + public event PropertyChangingEventHandler? PropertyChanging; public event PropertyChangedEventHandler? PropertyChanged; @@ -413,4 +446,8 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot return true; } + private void OnOnUserLoginChanged() + { + UserLoginChanged?.Invoke(this, EventArgs.Empty); + } } \ No newline at end of file diff --git a/ViewModels/AddChannelWindowViewModel.cs b/ViewModels/AddChannelWindowViewModel.cs deleted file mode 100644 index 5c4bf01..0000000 --- a/ViewModels/AddChannelWindowViewModel.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using Microsoft.Extensions.Logging; - -namespace BetterRaid.ViewModels; - -public class AddChannelWindowViewModel : ViewModelBase -{ - public AddChannelWindowViewModel(ILogger logger) - { - logger.LogDebug("AddChannelWindowViewModel created"); - } -} \ No newline at end of file diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index a50e7b5..351727c 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -6,11 +6,11 @@ using System.Threading; using System.Threading.Tasks; using Avalonia.Controls; using BetterRaid.Extensions; -using BetterRaid.Misc; using BetterRaid.Models; using BetterRaid.Services; using BetterRaid.Views; using Microsoft.Extensions.Logging; +using TwitchLib.PubSub.Events; namespace BetterRaid.ViewModels; @@ -64,7 +64,8 @@ public class MainWindowViewModel : ViewModelBase _db = db; _synchronizationService = synchronizationService; - _twitch.PropertyChanged += OnTwitchPropertyChanged; + _twitch.UserLoginChanged += OnUserLoginChanged; + _twitch.TwitchChannelUpdated += OnTwitchChannelUpdated; LoadChannelsFromDb(); } @@ -106,8 +107,13 @@ public class MainWindowViewModel : ViewModelBase } Channels.Clear(); - - foreach (var channel in _db.Database.Channels) + + var channels = _db.Database.Channels + .ToList() + .OrderBy(c => c.IsLive) + .ToList(); + + foreach (var channel in channels) { Task.Run(() => { @@ -129,14 +135,6 @@ public class MainWindowViewModel : ViewModelBase return new ObservableCollection(filteredChannels); } - private void OnTwitchPropertyChanged(object? sender, PropertyChangedEventArgs e) - { - if (e.PropertyName != nameof(_twitch.UserChannel)) - return; - - OnPropertyChanged(nameof(IsLoggedIn)); - } - protected override void OnPropertyChanged(PropertyChangedEventArgs e) { base.OnPropertyChanged(e); @@ -146,4 +144,14 @@ public class MainWindowViewModel : ViewModelBase OnPropertyChanged(nameof(FilteredChannels)); } } + + private void OnTwitchChannelUpdated(object? sender, TwitchChannel channel) + { + LoadChannelsFromDb(); + } + + private void OnUserLoginChanged(object? sender, EventArgs e) + { + OnPropertyChanged(nameof(IsLoggedIn)); + } } diff --git a/Views/AddChannelWindow.axaml b/Views/AddChannelWindow.axaml deleted file mode 100644 index d86df06..0000000 --- a/Views/AddChannelWindow.axaml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - -