Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Using more StateTriggers #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions XamarinTV/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class MainViewModel : BaseViewModel

TwoPaneViewMode _twoPaneViewMode;
bool _settingsActive = false;
private bool spannedWithVideo;
private bool notSpannedWithVideo;
private bool spannedWithoutVideo;
private bool notSpannedWithoutVideo;
bool _spannedWithVideo;
bool _notSpannedWithVideo;
bool _spannedWithoutVideo;
bool _notSpannedWithoutVideo;

public Command<Video> PlayVideoCommand { get; }
public Command OpenSettingWindowCommand { get; }
Expand Down Expand Up @@ -90,10 +90,10 @@ public TwoPaneViewMode TwoPaneViewMode

public bool DeviceIsSpanned => DualScreenInfo.Current.SpanMode != TwoPaneViewMode.SinglePane;

public bool SpannedWithVideo { get => spannedWithVideo; set => SetProperty(ref spannedWithVideo, value); }
public bool NotSpannedWithVideo { get => notSpannedWithVideo; set => SetProperty(ref notSpannedWithVideo, value); }
public bool SpannedWithoutVideo { get => spannedWithoutVideo; set => SetProperty(ref spannedWithoutVideo, value); }
public bool NotSpannedWithoutVideo { get => notSpannedWithoutVideo; set => SetProperty(ref notSpannedWithoutVideo, value); }
public bool SpannedWithVideo { get => _spannedWithVideo; set => SetProperty(ref _spannedWithVideo, value); }
public bool NotSpannedWithVideo { get => _notSpannedWithVideo; set => SetProperty(ref _notSpannedWithVideo, value); }
public bool SpannedWithoutVideo { get => _spannedWithoutVideo; set => SetProperty(ref _spannedWithoutVideo, value); }
public bool NotSpannedWithoutVideo { get => _notSpannedWithoutVideo; set => SetProperty(ref _notSpannedWithoutVideo, value); }

public void UpdateLayouts()
{
Expand Down
13 changes: 12 additions & 1 deletion XamarinTV/ViewModels/VideoPlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ public class VideoPlayerViewModel : BaseViewModel
{
Video _video;
double _volume;
bool _isPlaying;

public VideoPlayerViewModel()
{
Volume = 0.2d;
IsPlaying = true; // AutoPlay
}

public Video Video
{
get { return _video; }
set
{
if(SetProperty(ref _video, value))
if (SetProperty(ref _video, value))
{
OnPropertyChanged(nameof(VideoSource));
}
Expand Down Expand Up @@ -47,6 +49,15 @@ public string VideoSource
}
}

public bool IsPlaying
{
get => _isPlaying;
set
{
SetProperty(ref _isPlaying, value);
}
}

public ICommand CloseCommand { get; set; }
}
}
2 changes: 0 additions & 2 deletions XamarinTV/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using XamarinTV.ViewModels.Base;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.ComponentModel;

namespace XamarinTV.Views
{
Expand All @@ -14,7 +13,6 @@ public MainPage()
InitializeComponent();
BindingContext = MainViewModel.Instance;
twoPaneView.LayoutChanged += OnTwoPaneViewLayoutChanged;

}

void OnTwoPaneViewLayoutChanged(object sender, System.EventArgs e)
Expand Down
33 changes: 22 additions & 11 deletions XamarinTV/Views/VideoPlayerView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
Property="Margin"
Value="0,10,10,0" />
</Style>

</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
Expand Down Expand Up @@ -123,35 +124,45 @@
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<BoxView BackgroundColor="Black" Opacity="0.6"/>
<ImageButton x:Name="PlayPauseToggle"
<BoxView
BackgroundColor="Black"
Opacity="0.6"/>
<ImageButton
x:Name="PlayPauseToggle"
Source="{StaticResource PauseIcon}"
Clicked="PlayPauseToggle_Clicked"
BackgroundColor="Transparent"
HorizontalOptions="Center"
VerticalOptions="Center">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="PlaybackStates">
<VisualState Name="paused">
<VisualState Name="Paused">
<VisualState.StateTriggers>
<CompareStateTrigger Property="{Binding IsPlaying}" Value="False"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="Source" Value="{StaticResource PlayIcon}"/>
</VisualState.Setters>
</VisualState>
<VisualState Name="playing">
<VisualState Name="Playing">
<VisualState.StateTriggers>
<CompareStateTrigger Property="{Binding IsPlaying}" Value="True"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="Source" Value="{StaticResource PauseIcon}"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ImageButton>
<Label x:Name="TimeAndDuration" Text="Loading..."
TextColor="White"
FontSize="14"
Margin="15"
VerticalOptions="End"
HorizontalOptions="End"/>

<Label
x:Name="TimeAndDuration"
Text="Loading..."
TextColor="White"
FontSize="14"
Margin="15"
VerticalOptions="End"
HorizontalOptions="End"/>
<Label
Text="{Binding Video.Title}"
Style="{StaticResource TitleStyle}" />
Expand Down
18 changes: 8 additions & 10 deletions XamarinTV/Views/VideoPlayerView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
using System.Timers;
using Xamarin.Forms;
using Xamarin.Forms.DualScreen;
using XamarinTV.ViewModels;

namespace XamarinTV.Views
{
public partial class VideoPlayerView : ContentView
{
Timer _inactivityTimer;
Timer _playbackTimer;
readonly Timer _inactivityTimer;
readonly Timer _playbackTimer;

public VideoPlayerView()
{
Expand Down Expand Up @@ -87,12 +88,12 @@ protected override void LayoutChildren(double x, double y, double width, double
UpdateAspectRatio();
}

private void OnPlaybackTimerElapsed(object sender, ElapsedEventArgs e)
void OnPlaybackTimerElapsed(object sender, ElapsedEventArgs e)
{
this.UpdateTimeDisplay();
UpdateTimeDisplay();
}

private async void OnInactivityTimerElapsed(object sender, ElapsedEventArgs e)
async void OnInactivityTimerElapsed(object sender, ElapsedEventArgs e)
{
await Task.WhenAny<bool>
(
Expand All @@ -105,12 +106,9 @@ await Task.WhenAny<bool>
_inactivityTimer.Start();
}

private void MediaElement_StateRequested(object sender, StateRequested e)
void MediaElement_StateRequested(object sender, StateRequested e)
{
VisualStateManager.GoToState(PlayPauseToggle,
(e.State == MediaElementState.Playing)
? "playing"
: "paused");
((VideoPlayerViewModel)BindingContext).IsPlaying = e.State == MediaElementState.Playing;

if (e.State == MediaElementState.Playing)
{
Expand Down