forked from microsoft/BuildCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeedDetails.xaml.cs
309 lines (250 loc) · 11 KB
/
FeedDetails.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using BuildCast.DataModel;
using BuildCast.Helpers;
using BuildCast.Services.Navigation;
using BuildCast.ViewModels;
using Windows.System.Profile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
namespace BuildCast.Views
{
public sealed partial class FeedDetails : Page, IPageWithViewModel<FeedDetailsViewModel>
{
private UIElement cachedSecondaryCommandPanel = null;
/// <summary>
/// Initializes a new instance of the <see cref="FeedDetails"/> class.
/// </summary>
public FeedDetails()
{
this.InitializeComponent();
// Customize sizing for Xbox
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Xbox")
{
podimage.Width = 208;
podimage.Height = 208;
TopBorder.Height = 306;
}
Loaded += FeedDetails_Loaded;
}
public FeedDetailsViewModel ViewModel { get; set; }
public void UpdateBindings()
{
if (ViewModel.PersistedEpisode != null)
{
feeditems.Loaded += (s, ev) => feeditems.ScrollIntoView(ViewModel.PersistedEpisode, ScrollIntoViewAlignment.Leading);
}
Bindings.Update();
}
private void FeedDetails_Loaded(object sender, RoutedEventArgs e)
{
btnrefresh.Focus(FocusState.Programmatic);
Loaded -= FeedDetails_Loaded;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Canvas.SetZIndex(this, 0);
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
Canvas.SetZIndex(this, 1);
}
private void Feeditems_ChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
// Do we already have an ItemContainer? If so, we're done here.
if (args.ItemContainer != null)
{
return;
}
// Otherwise, we need to make a new container. Wire up the events on the container, and set it as the ItemContainer.
ListViewItem containerItem = new ListViewItem();
// Wire up stagger animations on items
containerItem.Loaded += ContainerItem_Loaded;
// Show hover buttons
containerItem.PointerEntered += ContainerItem_PointerEntered;
containerItem.PointerExited += ContainerItem_PointerExited;
// Listen for key events.
containerItem.KeyDown += ContainerItem_KeyDown;
args.ItemContainer = containerItem;
}
#region stagger animation on list items
private void ContainerItem_Loaded(object sender, RoutedEventArgs e)
{
var itemsPanel = (ItemsStackPanel)feeditems.ItemsPanelRoot;
var itemContainer = (ListViewItem)sender;
var itemIndex = feeditems.IndexFromContainer(itemContainer);
var relativeIndex = itemIndex - itemsPanel.FirstVisibleIndex;
Grid uc;
if (itemContainer.ContentTemplateRoot as SwipeControl != null)
{
uc = (itemContainer.ContentTemplateRoot as SwipeControl).Content as Grid;
}
else
{
uc = itemContainer.ContentTemplateRoot as Grid;
}
if (itemContainer.Content != ViewModel.PersistedEpisode && itemIndex >= 0 && itemIndex >= itemsPanel.FirstVisibleIndex && itemIndex <= itemsPanel.LastVisibleIndex)
{
var itemVisual = ElementCompositionPreview.GetElementVisual(uc);
ElementCompositionPreview.SetIsTranslationEnabled(uc, true);
var staggerDelay = TimeSpan.FromMilliseconds(relativeIndex * 100);
var offsetAnimation = VisualHelpers.CreateHorizontalOffsetAnimation(0.7, 150, staggerDelay.TotalSeconds);
itemVisual.StartAnimation("Translation.X", offsetAnimation);
var opacityAnimation = VisualHelpers.CreateOpacityAnimation(0.5);
opacityAnimation.DelayBehavior = Windows.UI.Composition.AnimationDelayBehavior.SetInitialValueBeforeDelay;
opacityAnimation.DelayTime = staggerDelay;
itemVisual.StartAnimation("Opacity", opacityAnimation);
}
itemContainer.Loaded -= this.ContainerItem_Loaded;
}
#endregion
private void ContainerItem_PointerEntered(object sender, PointerRoutedEventArgs e)
{
// Only show the hover buttons when the mouse or pen enters the item.
if (e.Pointer.PointerDeviceType != Windows.Devices.Input.PointerDeviceType.Touch)
{
try
{
var item = sender as ListViewItem;
var secondaryCommandPanel = item.GetVisualChildByName<StackPanel>("SecondaryCommandPanel");
secondaryCommandPanel.Visibility = Visibility.Visible;
cachedSecondaryCommandPanel = secondaryCommandPanel;
}
catch (Exception ex)
{
Debug.WriteLine("Catastrophic error: " + ex.Message);
}
}
}
private void ContainerItem_PointerExited(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType != Windows.Devices.Input.PointerDeviceType.Touch && cachedSecondaryCommandPanel != null)
{
cachedSecondaryCommandPanel.Visibility = Visibility.Collapsed;
cachedSecondaryCommandPanel = null;
}
}
private void ContainerItem_KeyDown(object sender, KeyRoutedEventArgs e)
{
Episode episode = feeditems.SelectedItem as Episode;
// Do not try to perform an action if an item isn't selected.
if (episode == null)
{
return;
}
if (e.Key == Windows.System.VirtualKey.P)
{
ViewModel.PlayEpisode(episode);
}
if (e.Key == Windows.System.VirtualKey.F)
{
FavoriteEpisode(episode);
}
if (e.Key == Windows.System.VirtualKey.D)
{
DownloadEpisode(episode);
}
}
private void FavoriteEpisode(Episode episode) => ViewModel.FavoriteEpisode(episode);
private void PlayEpisode(Episode episode) => ViewModel.PlayEpisode(episode);
private void DownloadEpisode(Episode episode) => ViewModel.DownloadEpisode(episode);
private void MenuFlyout_Opening(object sender, object e)
{
MenuFlyout senderAsMenuFlyout = sender as MenuFlyout;
foreach (object menuFlyoutItem in senderAsMenuFlyout.Items)
{
if (menuFlyoutItem.GetType() == typeof(MenuFlyoutItem))
{
// Associate the particular FeedItem with the menu flyout (so the MenuFlyoutItem knows which FeedItem to act upon)
ListViewItem itemContainer = senderAsMenuFlyout.Target as ListViewItem;
Episode feedItem = feeditems.ItemFromContainer(itemContainer) as Episode;
(menuFlyoutItem as MenuFlyoutItem).CommandParameter = feedItem;
}
}
}
private void Feeditems_ItemClick(object sender, ItemClickEventArgs e)
{
Episode detailsItem = e.ClickedItem as Episode;
ViewModel.GoToEpisodeDetails(detailsItem);
}
private void PlayMenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
PlayEpisode((sender as MenuFlyoutItem).CommandParameter as Episode);
}
private void PlayIconButton_Click(object sender, RoutedEventArgs e)
{
PlayEpisode((sender as Button).CommandParameter as Episode);
}
private void DownloadFlyoutItem_Click(object sender, RoutedEventArgs e)
{
DownloadEpisode((sender as MenuFlyoutItem).CommandParameter as Episode);
}
private void DownloadButton_Click(object sender, RoutedEventArgs e)
{
DownloadEpisode((sender as Button).CommandParameter as Episode);
}
private void FavoriteFlyoutItem_Click(object sender, RoutedEventArgs e)
{
FavoriteEpisode((sender as MenuFlyoutItem).CommandParameter as Episode);
}
private void FavoriteButton_Click(object sender, RoutedEventArgs e)
{
FavoriteEpisode((sender as Button).CommandParameter as Episode);
}
// Don't do anything but close the swipe container if a non-favorite swipe action occurs.
private void SwipeItem_Invoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
if (args.SwipeControl.DataContext is Episode target)
{
DownloadEpisode(target);
}
}
// Favorite the particular item, and then close the container.
private void FavoriteSwipeItem_Invoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
if (args.SwipeControl.DataContext is Episode episode)
{
FavoriteEpisode(episode);
}
}
private async void RefreshButton_Click(object sender, RoutedEventArgs e)
{
await LoadMoreData();
}
private async void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
await this.ViewModel.RemoveTopThree();
await UIHelpers.ShowContentAsync("Removing top 3 items");
}
private async Task LoadMoreData()
{
this.feeditems.ItemContainerTransitions = new TransitionCollection
{
new AddDeleteThemeTransition(),
};
var newEpisodeCount = await ViewModel.RefreshData();
RefreshStatusTextBlock.Text = (newEpisodeCount == 0) ? "Already up-to-date." : $"Found {newEpisodeCount} new episodes.";
}
private void Podimage_ImageOpened(object sender, RoutedEventArgs e)
{
}
}
}