-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerPage.xaml.cs
191 lines (172 loc) · 7.12 KB
/
PlayerPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using RadioParadisePlayer.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace RadioParadisePlayer
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
internal sealed partial class PlayerPage : Page
{
Logic.Player Player { get; set; }
BitmapImage BitmapImageSlideshowOne { get; set; }
BitmapImage BitmapImageSlideshowTwo { get; set; }
BitmapImage BitmapImageCoverArt { get; set; }
SemaphoreSlim semaphoreCoverArtImage;
SemaphoreSlim semaphoreCoverSlideshowOne;
SemaphoreSlim semaphoreCoverSlideshowTwo;
AppWindow appWindow;
public PlayerPage()
{
this.InitializeComponent();
BitmapImageSlideshowOne = new BitmapImage();
BitmapImageSlideshowTwo = new BitmapImage();
BitmapImageCoverArt = new BitmapImage();
BitmapImageSlideshowOne.ImageOpened += BitmapImageSlideshowOne_ImageOpened;
BitmapImageSlideshowTwo.ImageOpened += BitmapImageSlideshowTwo_ImageOpened;
BitmapImageCoverArt.ImageOpened += BitmapImageCoverArt_ImageOpened;
semaphoreCoverArtImage = new SemaphoreSlim(1);
semaphoreCoverSlideshowOne = new SemaphoreSlim(1);
semaphoreCoverSlideshowTwo = new SemaphoreSlim(1);
appWindow = XamlHelpers.GetAppWindowForWindow(XamlHelpers.GetWindow());
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
this.Player = e.Parameter as Logic.Player;
Player.PropertyChanged += Player_PropertyChanged;
base.OnNavigatedTo(e);
if (!Player.IsPlaying) await Player.PlayAsync();
else
{
Player_PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("CurrentSlideshowPictureUrl"));
Player_PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("CurrentSong"));
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
this.Player.PropertyChanged -= Player_PropertyChanged;
base.OnNavigatedFrom(e);
}
private void BitmapImageSlideshowTwo_ImageOpened(object sender, RoutedEventArgs e)
{
semaphoreCoverSlideshowTwo.Release();
}
private void BitmapImageSlideshowOne_ImageOpened(object sender, RoutedEventArgs e)
{
semaphoreCoverSlideshowOne.Release();
}
private void BitmapImageCoverArt_ImageOpened(object sender, RoutedEventArgs e)
{
semaphoreCoverArtImage.Release();
}
private async void Player_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "CurrentSlideshowPictureUrl":
try
{
if ((appWindow?.Presenter as OverlappedPresenter)?.State == OverlappedPresenterState.Minimized)
{
//Don't download images if we're minimized.
break;
}
var stream = await Api.RpApiClient.DownloadImageAsync(Player.CurrentSlideshowPictureUrl);
if (imgSlideshowOne.Opacity == 0)
{
await semaphoreCoverSlideshowOne.WaitAsync();
try
{
await BitmapImageSlideshowOne.SetSourceAsync(stream.AsRandomAccessStream());
}
catch (OperationCanceledException)
{
//Ignore it. Something went wrong with loading the image
}
imgSlideshowOne.Opacity = 1;
imgSlideshowTwo.Opacity = 0;
}
else
{
await semaphoreCoverSlideshowTwo.WaitAsync();
try
{
await BitmapImageSlideshowTwo.SetSourceAsync(stream.AsRandomAccessStream());
}
catch (OperationCanceledException)
{
//Ignore it. Something went wrong with loading the image
}
imgSlideshowOne.Opacity = 0;
imgSlideshowTwo.Opacity = 1;
}
}
catch (HttpRequestException)
{
//Do nothing. Just ignore this.
}
break;
case "CurrentSong":
await semaphoreCoverArtImage.WaitAsync();
try
{
await LoadCoverArtAsync();
}
catch (OperationCanceledException)
{
//Ignore it. Something went wrong with loading the image
}
break;
}
}
private async Task LoadCoverArtAsync()
{
if (String.IsNullOrEmpty(Player.CurrentSongCoverArtPictureUrl)) return;
try
{
var stream = await Api.RpApiClient.DownloadImageAsync("https:" + Player.CurrentSongCoverArtPictureUrl);
await BitmapImageCoverArt.SetSourceAsync(stream.AsRandomAccessStream());
}
catch (HttpRequestException)
{
//Do nothing. Just ignore this.
}
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
if (spPlayerInfo.Children.Contains(gridInfo))
{
spPlayerInfo.Children.Remove(gridInfo);
(toggleInfo.Content as FontIcon).Glyph = "\xE70e";
}
else
{
spPlayerInfo.Children.Add(gridInfo);
(toggleInfo.Content as FontIcon).Glyph = "\xE70d";
}
}
private async void Flyout_Opening(object sender, object e)
{
await Player.LoadCurrentSongInfoAsync();
}
}
}