Working on event stuff

This commit is contained in:
Enrico Ludwig 2024-09-06 22:51:22 +02:00
parent 6d1959fb4c
commit 791cb531e2
5 changed files with 58 additions and 74 deletions

View File

@ -32,6 +32,8 @@ public interface ITwitchService
public void RegisterForEvents(TwitchChannel channel);
public void UnregisterFromEvents(TwitchChannel channel);
public event EventHandler<EventArgs>? UserLoginChanged;
public event EventHandler<TwitchChannel>? 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<EventArgs>? UserLoginChanged;
public event EventHandler<TwitchChannel>? TwitchChannelUpdated;
public event EventHandler<OnStreamDownArgs> OnStreamDown
{
add => TwitchEvents.OnStreamDown += value;
remove => TwitchEvents.OnStreamDown -= value;
}
public event EventHandler<OnStreamUpArgs> OnStreamUp
{
add => TwitchEvents.OnStreamUp += value;
remove => TwitchEvents.OnStreamUp -= value;
}
public TwitchService(ILogger<TwitchService> 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);
}
}

View File

@ -1,12 +0,0 @@
using System;
using Microsoft.Extensions.Logging;
namespace BetterRaid.ViewModels;
public class AddChannelWindowViewModel : ViewModelBase
{
public AddChannelWindowViewModel(ILogger<AddChannelWindowViewModel> logger)
{
logger.LogDebug("AddChannelWindowViewModel created");
}
}

View File

@ -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<TwitchChannel>(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));
}
}

View File

@ -1,38 +0,0 @@
<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:vm="using:BetterRaid.ViewModels"
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="50"
x:Class="BetterRaid.Views.AddChannelWindow"
x:DataType="vm:AddChannelWindowViewModel"
Icon="/Assets/logo.png"
Width="200"
Height="80"
MaxWidth="200"
MaxHeight="80"
Title="Add Channel">
<Design.DataContext>
<vm:AddChannelWindowViewModel/>
</Design.DataContext>
<StackPanel Orientation="Vertical"
Margin="5"
Spacing="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<TextBox HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
x:Name="channelNameTxt"
Watermark="Enter Channelname" />
<Button HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="OK"
x:Name="okBtn"
IsEnabled="True" />
</StackPanel>
</Window>

View File

@ -1,11 +0,0 @@
using Avalonia.Controls;
namespace BetterRaid.Views;
public partial class AddChannelWindow : Window
{
public AddChannelWindow()
{
InitializeComponent();
}
}