Skip to content

Commit

Permalink
Updates sample app to .NET 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Mitermite committed Apr 18, 2021
1 parent 27f25b4 commit a1b220e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 70 deletions.
12 changes: 9 additions & 3 deletions GoogleCast.SampleApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GalaSoft.MvvmLight.Threading;
using System.Windows;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Mvvm.DependencyInjection;

namespace GoogleCast.SampleApp
{
Expand All @@ -10,7 +11,12 @@ public partial class App : Application
{
static App()
{
DispatcherHelper.Initialize();
var services = new ServiceCollection();
services.AddGoogleCast();
services.AddScoped<IDeviceLocator, DeviceLocator>();
services.AddScoped<ISender, Sender>();
services.AddScoped<MainViewModel>();
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
}
}
}
11 changes: 3 additions & 8 deletions GoogleCast.SampleApp/GoogleCast.SampleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{347B2A0B-04B1-40D7-A63E-D0077A651306}</ProjectGuid>
<OutputType>WinExe</OutputType>
<TargetFramework>net461</TargetFramework>
<TargetFramework>net5.0-windows</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Expand All @@ -25,12 +25,7 @@
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GoogleCast\GoogleCast.csproj" />
Expand Down
73 changes: 34 additions & 39 deletions GoogleCast.SampleApp/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using GoogleCast.Channels;
using GoogleCast.Models.Media;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;

namespace GoogleCast.SampleApp
{
/// <summary>
/// Main view model
/// </summary>
class MainViewModel : ViewModelBase
public class MainViewModel : ObservableObject
{
/// <summary>
/// Initializes a new instance of <see cref="MainViewModel"/> class
Expand All @@ -32,11 +31,6 @@ public MainViewModel(IDeviceLocator deviceLocator, ISender sender)
PauseCommand = new RelayCommand(async () => await TryAsync(PauseAsync), () => AreButtonsEnabled);
StopCommand = new RelayCommand(async () => await TryAsync(StopAsync), () => AreButtonsEnabled);
RefreshCommand = new RelayCommand(async () => await TryAsync(RefreshAsync), () => IsLoaded);

if (!IsInDesignMode)
{
Task.Run(RefreshAsync);
}
}

private IDeviceLocator DeviceLocator { get; }
Expand All @@ -49,7 +43,7 @@ public MainViewModel(IDeviceLocator deviceLocator, ISender sender)
public IEnumerable<IReceiver>? Receivers
{
get => _receivers;
private set => Set(nameof(Receivers), ref _receivers, value);
private set => SetProperty(ref _receivers, value);
}

private bool IsInitialized { get; set; }
Expand All @@ -68,8 +62,8 @@ public IReceiver? SelectedReceiver
{
_selectedReceiver = value;
IsInitialized = false;
RaisePropertyChanged(nameof(SelectedReceiver));
RaiseButtonsCommandsCanExecuteChanged();
OnPropertyChanged(nameof(SelectedReceiver));
NotifyButtonsCommandsCanExecuteChanged();
}
}
}
Expand All @@ -86,9 +80,9 @@ private set
if (_isLoaded != value)
{
_isLoaded = value;
RaisePropertyChanged(nameof(IsLoaded));
RefreshCommand.RaiseCanExecuteChanged();
RaiseButtonsCommandsCanExecuteChanged();
OnPropertyChanged(nameof(IsLoaded));
RefreshCommand.NotifyCanExecuteChanged();
NotifyButtonsCommandsCanExecuteChanged();
}
}
}
Expand All @@ -105,7 +99,7 @@ private set
public string? PlayerState
{
get => _playerState;
private set => Set(nameof(PlayerState), ref _playerState, value);
private set => SetProperty(ref _playerState, value);
}

private string _link = "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/DesigningForGoogleCast.mp4";
Expand All @@ -121,8 +115,8 @@ public string Link
{
_link = value;
IsInitialized = false;
RaisePropertyChanged(nameof(Link));
RaiseButtonsCommandsCanExecuteChanged();
OnPropertyChanged(nameof(Link));
NotifyButtonsCommandsCanExecuteChanged();
}
}
}
Expand All @@ -134,7 +128,7 @@ public string Link
public string Subtitle
{
get => _subtitle;
set => Set(nameof(Subtitle), ref _subtitle, value);
set => SetProperty(ref _subtitle, value);
}

private bool _isMuted;
Expand All @@ -149,13 +143,13 @@ public bool IsMuted
if (_isMuted != value)
{
_isMuted = value;
RaisePropertyChanged(nameof(IsMuted));
OnPropertyChanged(nameof(IsMuted));
Task.Run(SetIsMutedAsync);
}
}
}

public float _volume = 1;
private float _volume = 1;
/// <summary>
/// Gets or sets the volume
/// </summary>
Expand All @@ -171,7 +165,7 @@ public float Volume
IsMuted = false;
}
_volume = value;
RaisePropertyChanged(nameof(Volume));
OnPropertyChanged(nameof(Volume));
Task.Run(SetVolumeAsync);
}
}
Expand Down Expand Up @@ -203,12 +197,12 @@ private bool IsStopped
/// </summary>
public RelayCommand RefreshCommand { get; }

private void RaiseButtonsCommandsCanExecuteChanged()
private void NotifyButtonsCommandsCanExecuteChanged()
{
RaisePropertyChanged(nameof(AreButtonsEnabled));
PlayCommand.RaiseCanExecuteChanged();
PauseCommand.RaiseCanExecuteChanged();
StopCommand.RaiseCanExecuteChanged();
OnPropertyChanged(nameof(AreButtonsEnabled));
PlayCommand.NotifyCanExecuteChanged();
PauseCommand.NotifyCanExecuteChanged();
StopCommand.NotifyCanExecuteChanged();
}

private async Task TryAsync(Func<Task> action)
Expand Down Expand Up @@ -305,15 +299,16 @@ private async Task StopAsync()
}
}

private async Task RefreshAsync()
/// <summary>
/// Finds the receivers
/// </summary>
/// <returns>a <see cref="Task"/> that represents the asynchronous operation</returns>
public async Task RefreshAsync()
{
await DispatcherHelper.RunAsync(async () =>
{
IsLoaded = false;
Receivers = await DeviceLocator.FindReceiversAsync();
SelectedReceiver = Receivers.FirstOrDefault();
IsLoaded = true;
});
IsLoaded = false;
Receivers = await DeviceLocator.FindReceiversAsync();
SelectedReceiver = Receivers.FirstOrDefault();
IsLoaded = true;
}

private async Task SetVolumeAsync()
Expand All @@ -326,9 +321,9 @@ private async Task SetIsMutedAsync()
await SendChannelCommandAsync<IReceiverChannel>(IsStopped, null, async c => await c.SetIsMutedAsync(IsMuted));
}

private void MediaChannelStatusChanged(object sender, EventArgs e)
private void MediaChannelStatusChanged(object? sender, EventArgs e)
{
var status = ((IMediaChannel)sender).Status?.FirstOrDefault();
var status = (sender as IMediaChannel)?.Status?.FirstOrDefault();
var playerState = status?.PlayerState;
if (playerState == "IDLE" && !string.IsNullOrEmpty(status!.IdleReason))
{
Expand All @@ -337,11 +332,11 @@ private void MediaChannelStatusChanged(object sender, EventArgs e)
PlayerState = playerState;
}

private void ReceiverChannelStatusChanged(object sender, EventArgs e)
private void ReceiverChannelStatusChanged(object? sender, EventArgs e)
{
if (!IsInitialized)
{
var status = ((IReceiverChannel)sender).Status;
var status = (sender as IReceiverChannel)?.Status;
if (status != null)
{
if (status.Volume.Level != null)
Expand Down
3 changes: 2 additions & 1 deletion GoogleCast.SampleApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mc:Ignorable="d"
Title="GoogleCast" Height="200" Width="784"
ResizeMode="CanResizeWithGrip"
Loaded="WindowLoadedAsync"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}">
<Grid>
<Grid.RowDefinitions>
Expand Down Expand Up @@ -57,4 +58,4 @@
</Grid>
<TextBlock Grid.Row="4" TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding PlayerState}" />
</Grid>
</Window>
</Window>
15 changes: 13 additions & 2 deletions GoogleCast.SampleApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.ComponentModel;
using System.Windows;

namespace GoogleCast.SampleApp
{
Expand All @@ -14,5 +15,15 @@ public MainWindow()
{
InitializeComponent();
}

private new MainViewModel DataContext => (MainViewModel)base.DataContext;

private async void WindowLoadedAsync(object sender, RoutedEventArgs e)
{
if (!DesignerProperties.GetIsInDesignMode(this))
{
await DataContext.RefreshAsync();
}
}
}
}
}
21 changes: 5 additions & 16 deletions GoogleCast.SampleApp/ViewModelLocator.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
using CommonServiceLocator;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Toolkit.Mvvm.DependencyInjection;

namespace GoogleCast.SampleApp
{
class ViewModelLocator
{
static ViewModelLocator()
{
var simpleIoc = SimpleIoc.Default;
simpleIoc.Reset();
ServiceLocator.SetLocatorProvider(() => simpleIoc);
simpleIoc.Register<IDeviceLocator, DeviceLocator>();
simpleIoc.Register<ISender>(() => new Sender());
simpleIoc.Register<MainViewModel>();
}

public MainViewModel Main
{
get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
}
/// <summary>
/// Gets the main view model
/// </summary>
public static MainViewModel Main => Ioc.Default.GetRequiredService<MainViewModel>();
}
}
3 changes: 2 additions & 1 deletion GoogleCast.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ VisualStudioVersion = 16.0.31205.134
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GoogleCast", "GoogleCast\GoogleCast.csproj", "{0B336E66-C5C4-47B7-BD45-85847CCEF991}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleCast.SampleApp", "GoogleCast.SampleApp\GoogleCast.SampleApp.csproj", "{347B2A0B-04B1-40D7-A63E-D0077A651306}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GoogleCast.SampleApp", "GoogleCast.SampleApp\GoogleCast.SampleApp.csproj", "{347B2A0B-04B1-40D7-A63E-D0077A651306}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DA711E96-61F5-4F90-8E8A-54AB95FC155D}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
EndProjectSection
EndProject
Global
Expand Down

0 comments on commit a1b220e

Please sign in to comment.