Too much to name... but damn, it got where we are needed

This commit is contained in:
TheXamlGuy
2024-01-14 15:06:30 +00:00
parent 66f4bb8757
commit 1283e8ff58
59 changed files with 511 additions and 250 deletions
@@ -1,3 +1,3 @@
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.MediaController;
public record BackwardRequest : INotification;
@@ -1,3 +1,3 @@
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.MediaController;
public record FowardRequest : INotification;
@@ -14,29 +14,18 @@
<None Remove="MediaInformationView.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230913002" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
<PackageReference Include="CommunityToolkit.WinUI" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.2" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.231202003-experimental1" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.25936-preview" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hyperbar.Windows.Controls\Hyperbar.Windows.Controls.csproj" />
<ProjectReference Include="..\Hyperbar.Windows.UI\Hyperbar.Windows.UI.csproj" />
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
</ItemGroup>
<ItemGroup>
<Page Update="MediaInformationView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="MediaControllerView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="MediaControllerWidgetView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
</Project>
@@ -0,0 +1,5 @@
namespace Hyperbar.Windows.MediaController;
public record Media;
@@ -1,19 +1,34 @@
using Windows.Media.Control;
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.MediaController;
public class MediaController :
INotificationHandler<PlayRequest>
INotificationHandler<Play>,
INotificationHandler<Pause>
{
private readonly IMediator mediator;
private readonly GlobalSystemMediaTransportControlsSession session;
public MediaController(GlobalSystemMediaTransportControlsSession session,
IMediator mediator)
public MediaController(IMediator mediator,
GlobalSystemMediaTransportControlsSession session)
{
this.mediator = mediator;
this.session = session;
mediator.Subscribe(this);
session.MediaPropertiesChanged += OnMediaPropertiesChanged;
}
public async ValueTask Handle(PlayRequest notification, CancellationToken cancellationToken) =>
private void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender,
MediaPropertiesChangedEventArgs args)
{
mediator.PublishAsync(new Changed<Media>());
}
public async ValueTask Handle(Play notification, CancellationToken cancellationToken) =>
await session.TryPlayAsync();
public async ValueTask Handle(Pause notification, CancellationToken cancellationToken) =>
await session.TryPauseAsync();
}
@@ -0,0 +1,34 @@
using Windows.Media.Control;
namespace Hyperbar.Windows.MediaController;
public class MediaControllerFactory(IMediator mediator,
IServiceScopeFactory<MediaController> serviceScopeFactory) :
IFactory<GlobalSystemMediaTransportControlsSession, MediaController?>
{
public MediaController? Create(GlobalSystemMediaTransportControlsSession value)
{
if (serviceScopeFactory.Create(value) is MediaController mediaController)
{
return mediaController;
//if (serviceScope.ServiceProvider.GetService<IServiceFactory>() is IServiceFactory serviceFactory)
//{
// if (serviceFactory.Create<MediaController>(value) is MediaController mediaController)
// {
// //if (serviceScope.ServiceProvider.GetService<IFactory<MediaControllerViewModel?>>()
// // is IFactory<MediaControllerViewModel?> factory)
// //{
// // if (factory.Create() is MediaControllerViewModel mediaControllerViewModel)
// // {
// // _ = await mediator.PublishAsync(new Created<MediaControllerViewModel>(mediaControllerViewModel));
// // }
// //}
// }
//}
}
return default;
}
}
@@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
namespace Hyperbar.Windows.MediaController;
public class MediaControllerHandler(IMediator mediator,
IServiceScopeProvider<MediaController> scopeProvider) :
INotificationHandler<Created<MediaController>>
{
public async ValueTask Handle(Created<MediaController> notification,
CancellationToken cancellationToken)
{
if (scopeProvider.TryGet(notification.Value, out IServiceScope? serviceScope))
{
if (serviceScope is not null)
{
if (serviceScope.ServiceProvider.GetService<IFactory<MediaControllerViewModel?>>()
is IFactory<MediaControllerViewModel?> factory)
{
if (factory.Create() is MediaControllerViewModel mediaControllerViewModel)
{
await mediator.PublishAsync(new Created<MediaControllerViewModel>(mediaControllerViewModel),
cancellationToken);
}
}
}
}
}
}
@@ -1,40 +1,15 @@
using System.Collections.Concurrent;
using System.Threading.Channels;
using Windows.Media.Control;
namespace Hyperbar.Windows.Primary;
public class MediaControllerManager :
namespace Hyperbar.Windows.MediaController;
public class MediaControllerManager(IMediator mediator,
IFactory<GlobalSystemMediaTransportControlsSession, MediaController> factory) :
IInitializer
{
private readonly ConcurrentDictionary<GlobalSystemMediaTransportControlsSession, MediaController> cachedSessions = [];
private readonly IMediator mediator;
private readonly Queue<MediaController> mediaControllers;
private readonly IServiceFactory serviceFactory;
public MediaControllerManager(IServiceFactory serviceFactory,
IMediator mediator,
Queue<MediaController> mediaControllers)
{
this.serviceFactory = serviceFactory;
this.mediator = mediator;
this.mediaControllers = mediaControllers;
}
private Channel<MediaController> d;
public async Task InitializeAsync()
{
d = Channel.CreateUnbounded<MediaController>();
_ = Task.Run(async () => {
await foreach (var coordinates in d.Reader.ReadAllAsync())
{
Console.WriteLine(coordinates);
}
});
GlobalSystemMediaTransportControlsSessionManager mediaTransportControlsSessionManager =
await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
mediaTransportControlsSessionManager.SessionsChanged += OnSessionsChanged;
@@ -50,14 +25,10 @@ public class MediaControllerManager :
private async Task InitializeSessionAsync(GlobalSystemMediaTransportControlsSession session)
{
if (serviceFactory.Create<MediaController>(session) is MediaController mediaController)
if (factory.Create(session) is MediaController mediaController)
{
await d.Writer.WriteAsync(mediaController);
mediaControllers.Enqueue(mediaController);
cachedSessions.TryAdd(session, mediaController);
await mediator.PublishAsync(new Created<MediaController>(mediaController));
cachedSessions.TryAdd(session, mediaController);
}
}
@@ -81,13 +52,4 @@ public class MediaControllerManager :
await InitializeSessionAsync(session);
}
}
private void RemoveSession(GlobalSystemMediaTransportControlsSession session)
{
if (serviceFactory.Create<MediaController>(session) is MediaController mediaController)
{
cachedSessions.TryAdd(session, mediaController);
}
}
}
@@ -4,11 +4,16 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="using:Hyperbar.Windows.UI">
<ItemsControl ItemTemplateSelector="{Binding Mode=TwoWay, Converter={ui:DataTemplateConverter}}" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<Grid Width="400">
<ItemsControl
HorizontalAlignment="Center"
ItemTemplateSelector="{Binding Converter={ui:DataTemplateConverter}}"
ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Spacing="8" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
@@ -1,17 +1,6 @@
using CommunityToolkit.Mvvm.Input;
namespace Hyperbar.Windows.Primary;
//public class MediaControllerViewModelFactory(IServiceFactory service,
// IMediator mediator,
// Queue<MediaController> mediaControllers) :
// IFactory<MediaControllerViewModel>
//{
// public MediaControllerViewModel Create()
// {
// throw new NotImplementedException();
// }
//}
namespace Hyperbar.Windows.MediaController;
public class MediaControllerViewModel :
ObservableCollectionViewModel<WidgetComponentViewModel>,
@@ -24,11 +13,11 @@ public class MediaControllerViewModel :
{
TemplateFactory = templateFactory;
this.Add<MediaInformationViewModel>();
this.Add<WidgetButtonViewModel>("\uEB9E");
this.Add<WidgetButtonViewModel>("\uE768");
this.Add<WidgetButtonViewModel>("\uE769");
this.Add<WidgetButtonViewModel>("\uEB9D");
Add<MediaInformationViewModel>();
Add<WidgetButtonViewModel>("Backward", "\uEB9E");
Add<WidgetButtonViewModel>("Play", "\uE768", new RelayCommand(async () => await mediator.SendAsync(new Play())));
Add<WidgetButtonViewModel>("Pause", "\uE769", new RelayCommand(async () => await mediator.PublishAsync(new Pause())));
Add<WidgetButtonViewModel>("Forward", "\uEB9D");
}
public ITemplateFactory TemplateFactory { get; set; }
@@ -0,0 +1,10 @@
namespace Hyperbar.Windows.MediaController;
public class MediaControllerViewModelFactory(IServiceFactory service) :
IFactory<MediaControllerViewModel?>
{
public MediaControllerViewModel? Create()
{
return service.Create<MediaControllerViewModel>();
}
}
@@ -1,16 +1,22 @@
using Hyperbar.Windows.MediaController;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Collections.Concurrent;
using Windows.Media.Control;
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.MediaController;
public class MediaControllerWidgetProvider :
IWidgetProvider
{
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
services.AddWidgetTemplate<MediaControllerWidgetViewModel, MediaControllerWidgetView>()
.AddSingleton<Queue<MediaController>>()
.AddSingleton<IInitializer, MediaControllerManager>()
.AddContentTemplate<MediaControllerViewModel, MediaControllerView>();
.AddTransient<IServiceScopeFactory<MediaController>, ServiceScopeFactory<MediaController>>()
.AddTransient<IServiceScopeProvider<MediaController>, ServiceScopeProvider<MediaController>>()
.AddSingleton<ConcurrentDictionary<MediaController, IServiceScope>>()
.AddTransient<IFactory<GlobalSystemMediaTransportControlsSession, MediaController?>, MediaControllerFactory>()
.AddHandler<MediaControllerHandler>()
.AddTransient<IFactory<MediaControllerViewModel?>, MediaControllerViewModelFactory>()
.AddContentTemplate<MediaControllerViewModel, MediaControllerView>()
.AddContentTemplate<MediaInformationViewModel, MediaInformationView>();
}
@@ -6,21 +6,8 @@
xmlns:ui="using:Hyperbar.Windows.UI">
<FlipView
x:Name="FlipView"
Width="320"
Width="400"
Background="Transparent"
ItemTemplateSelector="{Binding Converter={ui:DataTemplateConverter}}"
ItemsSource="{Binding}">
<FlipView.ItemContainerStyle>
<Style TargetType="FlipViewItem">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="HorizontalContentAlignment" Value="Right" />
</Style>
</FlipView.ItemContainerStyle>
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel AreScrollSnapPointsRegular="False" Orientation="Horizontal" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
</FlipView>
ItemsSource="{Binding}" />
</UserControl>
@@ -1,4 +1,4 @@
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.MediaController;
public class MediaControllerWidgetViewModel(ITemplateFactory templateFactory,
IServiceFactory serviceFactory,
@@ -3,5 +3,23 @@
x:Class="Hyperbar.Windows.MediaController.MediaInformationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button>sdfsdf</Button>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="8" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border
Grid.Column="0"
Width="40"
Height="40"
Background="Red" />
<StackPanel Grid.Column="2">
<TextBlock Style="{ThemeResource BaseTextBlockStyle}" Text="{Binding Title}" />
<TextBlock
Opacity="0.7"
Style="{ThemeResource CaptionTextBlockStyle}"
Text="{Binding Description}" />
</StackPanel>
</Grid>
</UserControl>
@@ -1,6 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.MediaController;
public partial class MediaInformationViewModel :
WidgetComponentViewModel
@@ -0,0 +1,3 @@
namespace Hyperbar.Windows.MediaController;
public record Pause : INotification;
@@ -1,3 +0,0 @@
namespace Hyperbar.Windows.Primary;
public record PauseRequest : INotification;
+3
View File
@@ -0,0 +1,3 @@
namespace Hyperbar.Windows.MediaController;
public record Play : INotification;
@@ -1,3 +0,0 @@
namespace Hyperbar.Windows.Primary;
public record PlayRequest : INotification;