Skip to content

Commit

Permalink
Add possibility to open multiple chat windows
Browse files Browse the repository at this point in the history
  • Loading branch information
joacand committed Jul 26, 2020
1 parent 89b20af commit 87c8235
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 15 deletions.
7 changes: 7 additions & 0 deletions VattenMedia.Core/Interfaces/ITwitchChatClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VattenMedia.Core.Interfaces
{
public interface ITwitchChatClientFactory
{
ITwitchChatClient Create();
}
}
3 changes: 2 additions & 1 deletion VattenMedia.Infrastructure/Extensions/ContainerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Unity;
using Unity.Lifetime;
using VattenMedia.Core.Interfaces;
using VattenMedia.Infrastructure.Factories;
using VattenMedia.Infrastructure.Services;

namespace VattenMedia.Infrastructure.Extensions
Expand All @@ -14,7 +15,7 @@ public static void AddInfrastructureRegistrations(this IUnityContainer container
container.RegisterType<IStatusManager, StatusManager>(new ContainerControlledLifetimeManager());
container.RegisterType<ITwitchService, TwitchService>();
container.RegisterType<IYoutubeService, YoutubeService>();
container.RegisterType<ITwitchChatClient, TwitchChatClient>();
container.RegisterType<ITwitchChatClientFactory, TwitchChatClientFactory>();
}
}
}
13 changes: 13 additions & 0 deletions VattenMedia.Infrastructure/Factories/TwitchChatClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using VattenMedia.Core.Interfaces;
using VattenMedia.Infrastructure.Services;

namespace VattenMedia.Infrastructure.Factories
{
internal class TwitchChatClientFactory : ITwitchChatClientFactory
{
public ITwitchChatClient Create()
{
return new TwitchChatClient();
}
}
}
1 change: 0 additions & 1 deletion VattenMedia/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ protected override void OnStartup(StartupEventArgs e)
mainWindowViewModel.StreamGridControl = new StreamGridView { DataContext = mainWindowViewModel };
mainWindowViewModel.VideoListControl = new VideoListView { DataContext = mainWindowViewModel };
mainWindowViewModel.StreamContentControl = mainWindowViewModel.StreamListControl;
mainWindowViewModel.ChatViewModel = container.Resolve<ChatViewModel>();
mainWindowViewModel.Initialize();

var window = new MainWindow { DataContext = mainWindowViewModel };
Expand Down
2 changes: 2 additions & 0 deletions VattenMedia/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Unity;
using Unity.Lifetime;
using VattenMedia.Core.Entities;
using VattenMedia.Factories;
using VattenMedia.Infrastructure.Extensions;

namespace VattenMedia
Expand All @@ -10,6 +11,7 @@ public static class Bootstrapper
{
public static void AddRegistrations(this IUnityContainer container)
{
container.RegisterType<IViewModelFactory, ViewModelFactory>();
container.AddInfrastructureRegistrations();
container.RegisterInstance(CreateAppConfiguration(), new ContainerControlledLifetimeManager());
}
Expand Down
9 changes: 9 additions & 0 deletions VattenMedia/Factories/IViewModelFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using VattenMedia.ViewModels;

namespace VattenMedia.Factories
{
internal interface IViewModelFactory
{
ChatViewModel CreateChatViewModel();
}
}
20 changes: 20 additions & 0 deletions VattenMedia/Factories/ViewModelFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using VattenMedia.Core.Interfaces;
using VattenMedia.ViewModels;

namespace VattenMedia.Factories
{
internal class ViewModelFactory : IViewModelFactory
{
private readonly ITwitchChatClientFactory twitchChatClientFactory;

public ViewModelFactory(ITwitchChatClientFactory twitchChatClientFactory)
{
this.twitchChatClientFactory = twitchChatClientFactory;
}

public ChatViewModel CreateChatViewModel()
{
return new ChatViewModel(twitchChatClientFactory.Create());
}
}
}
24 changes: 11 additions & 13 deletions VattenMedia/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Input;
using VattenMedia.Core.Entities;
using VattenMedia.Core.Interfaces;
using VattenMedia.Factories;
using VattenMedia.Models;
using VattenMedia.Views;
using LiveChannel = VattenMedia.Models.LiveChannel;
Expand All @@ -16,6 +17,7 @@ namespace VattenMedia.ViewModels
{
internal class MainWindowViewModel : BaseViewModel
{
private readonly IViewModelFactory viewModelFactory;
private readonly IConfigHandler configHandler;
private readonly ITwitchService twitchService;
private readonly IYoutubeService youtubeService;
Expand Down Expand Up @@ -48,24 +50,24 @@ internal class MainWindowViewModel : BaseViewModel
public UserControl StreamGridControl { get; set; }
public UserControl StreamListControl { get; set; }
public UserControl VideoListControl { get; set; }
public ChatViewModel ChatViewModel { get; set; }

public MainWindowViewModel(
IViewModelFactory viewModelFactory,
IConfigHandler configHandler,
ITwitchService twitchService,
IYoutubeService youtubeService,
IStatusManager statusManager,
IStreamStarterService streamStarterService,
AppConfiguration appConfiguration)
{
this.viewModelFactory = viewModelFactory;
this.configHandler = configHandler;
this.twitchService = twitchService;
this.youtubeService = youtubeService;
this.statusManager = statusManager;
statusManager.SetCallback(ChangeStatusText);
this.streamStarterService = streamStarterService;
this.appConfiguration = appConfiguration;

streamStarterService.RunningProcessesChanged +=
(s, e) => { RunningProcessesChangedHandler(e); };
ListChannels();
Expand Down Expand Up @@ -272,8 +274,7 @@ private void OpenChat(string channelName)
ChangeStatusText("Username is missing - cannot open chat");
return;
}

var chatWindow = new ChatView { DataContext = ChatViewModel };
var chatWindow = new ChatView { DataContext = viewModelFactory.CreateChatViewModel() };
chatWindow.Initialize();
chatWindow.StartChat(configHandler.Config.TwitchUsername, channelName, configHandler.Config.TwitchAccessToken);
chatWindow.Show();
Expand Down Expand Up @@ -309,16 +310,13 @@ private void StartStream(Uri url)

private List<string> GetQualityFromRadioButtons()
{
switch (SelectedQuality)
return SelectedQuality switch
{
case Quality.High:
return QualityOptions.HighQuality;
case Quality.Medium:
return QualityOptions.MediumQuality;
case Quality.Low:
return QualityOptions.LowQuality;
}
throw new ArgumentOutOfRangeException(nameof(SelectedQuality));
Quality.High => QualityOptions.HighQuality,
Quality.Medium => QualityOptions.MediumQuality,
Quality.Low => QualityOptions.LowQuality,
_ => throw new ArgumentOutOfRangeException(nameof(SelectedQuality)),
};
}
}
}

0 comments on commit 87c8235

Please sign in to comment.