Subversion Repository Public Repository

Nextrek

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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
using ClassLibrary1;
using GuidaTv.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Notifications;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Grouped Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace GuidaTv
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class GroupedItemsPage : GuidaTv.Common.LayoutAwarePage
    {

        private int rowBlocks = 10;//numero di righe da visualizzare
        private bool allVisible = false;
        private bool showing = false;
        public int count = 1;

        private int lastvisible = 0;
        private static GroupedItemsPage instance;

        private bool centerAfterLoad = false;
   
        private string backgroundTaskName = "PockeTVguideBackgroundLiveTiles";
        private string backgroundTaskEntryPoint = "TileBackground.TileBackgroundUpdater";
        private DateTime currentDate;
       // private bool scrollingWithChannels = false;

        public GroupedItemsPage()
        {
            this.InitializeComponent();

            instance = this;
           
            Window.Current.SizeChanged += WindowSizeChanged;

            // tileUpdater.EnableNotificationQueue(true);
        }

        private void WindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            ApplicationViewState myViewState = ApplicationView.Value;
            if (myViewState == ApplicationViewState.Snapped && this.channelList.ItemsSource != null)
            {
                foreach (Channel ch in (ObservableCollection<Channel>)this.channelList.ItemsSource)
                {
                    ch.highlightNowProgram();
                   
                }
            }

        }


        private void Custom_HorizontalOffsetChanded(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            if (this.timebar != null && this.programListScrollviewer != null)
            {
                this.timebar.scrollToHorizontalOffset((double)e.NewValue);
                this.programListScrollviewer.ScrollToHorizontalOffset((double)e.NewValue);
            }
        }

        private void scrollTo(double hOffset)
        {
            this.tmp.HorizontalOffset = this.programListScrollviewer.HorizontalOffset;
            this.hScrollDoubleAnimation.To = hOffset;
            this.hScrollAnimation.Begin(); // THROWS EXCEPTION
        }

        private void centerNow()
        {
            this.timebar.highlightNow();
            scrollTo((DateTime.Now.TimeOfDay.TotalMinutes * 4)-300);
        }



        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        /// 
        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            //  var sampleDataGroups = SampleDataSource.GetGroups((String)navigationParameter);
            // this.DefaultViewModel["Groups"] = sampleDataGroups;


            if (DataLoader.data == null || DataLoader.data == "")
            {
                progressRing.Visibility = Visibility.Visible;
                progressRing.IsActive = true;
                await DataLoader.loadJson();

            }

            ObservableCollection<Channel> channels = DataLoader.getSelectedChannels();
            if (channels != null)
            {
                channelList.ItemsSource = channels;

                List<ObservableCollection<Programs>> allprograms = new List<ObservableCollection<Programs>>();
                foreach (DateTime d in channels[0].programs2Date.Keys)
                {
                    allprograms.Add(DataLoader.getProgramsByDate(d));
                }
                programList.ItemsSource = allprograms;
                programList.UpdateLayout();



                if (channels[0].programs2Date.ContainsKey(DateTime.Now.Date))
                {

                    showProgramsByDate(DateTime.Now.Date);

                }
                else //non ha l'epg di oggi. ha letto i dati da file(o il server ritorna un epg non aggiornato)
                {    //mostro l'epg più recente
                    var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
                    warningBar.showWarning(loader.GetString("dataNotUpdatedMessage"));
                    showProgramsByDate(channels[0].programs2Date.Keys.ElementAt(channels[0].programs2Date.Keys.Count - 1));


                }

                this.itemListView.ItemsSource = channels;
                RegisterBackgroundTasks();
            }



        }

      

        private void showProgramsByDate(DateTime date) {
            ObservableCollection<Channel> channels = DataLoader.getSelectedChannels();
            

            if (channels[0].programs2Date.ContainsKey(date))
            {
              
                /*foreach (Channel ch in channels) {//rimette a invisibile tutti i programmi
                    foreach (Programs ps in ch.programs2Date.Values) {
                        ps.isVisible = false;
                    }
                   
                    
                }*/
                this.channelListScrollviewer.ScrollToVerticalOffset(0);

                allVisible = false;
                lastvisible = 0;
                count = 1;
                this.currentDate = date;

                for (int i = 0; i < programList.Items.Count; i++)
                {


                    if (this.programList.ItemContainerGenerator.ContainerFromIndex(i) != null)
                    {
                        this.programList.ItemContainerGenerator.ContainerFromIndex(i).SetValue(VisibilityProperty, Visibility.Collapsed);
                    }
                }
                if (this.programList.ItemContainerGenerator.ContainerFromIndex(channels[0].programs2Date.Keys.ToList().IndexOf(this.currentDate)) != null)
                {
                    this.programList.ItemContainerGenerator.ContainerFromIndex(channels[0].programs2Date.Keys.ToList().IndexOf(this.currentDate)).SetValue(VisibilityProperty, Visibility.Visible);
                    this.programList.SelectedIndex = channels[0].programs2Date.Keys.ToList().IndexOf(this.currentDate);
                }
               //DataLoader.getProgramsByDate(date);
                this.prevButton.IsEnabled = false;
                this.nextButton.IsEnabled = false;
               
                setDateText();
                showPrograms();
                highlightNow();
                if (date == DateTime.Now.Date)
                {
                    centerAfterLoad = true;
                }
            }
        }


        private void setDateText()
        {
           
            string text = currentDate.Date.ToString("F", System.Globalization.CultureInfo.CurrentCulture);
            text = text.Substring(0, 1).ToUpper() + text.Substring(1, text.Length - 10);
            this.date.Text = text;
        }

        /*mostra righe di programmi in blocchi di 10*/
        private void showPrograms()
        {
            if (!allVisible && !showing)
            {
                this.warningBar.disableRefresh();
                showing = true;
                progressRing.Visibility = Visibility.Visible;
                progressRing.IsActive = true;
              
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
                timer.Tick += timer_Tick;
                timer.Start();
              
            }

        }
     

        void timer_Tick(object sender, object e)
        {   
         try{
            if (this.channelList.Items != null && this.channelList.Items.Count != 0)
            {

                if (this.channelList.Items.Count > lastvisible && count <= rowBlocks)
                {

                    var currentPrograms = (programList.Items[programList.SelectedIndex] as ObservableCollection<Programs>)[lastvisible];
                   
                     progressRingText.Text = currentPrograms.parent != null ? currentPrograms.parent.name : "";
                   
                    
                    currentPrograms.isVisible = true;
                        
                        //this.pro.UpdateLayout();
                        lastvisible++;
                        count++;
                    
                }
                else
                {
                    ((DispatcherTimer)sender).Stop();
                    progressRing.Visibility = Visibility.Collapsed;
                    
                   
                    progressRing.IsActive = false;
                    showing = false;
                    this.warningBar.enableRefresh();
                    count = 1;
                    this.prevButton.IsEnabled = true;
                    this.nextButton.IsEnabled = true;
                    if (lastvisible == this.channelList.Items.Count)
                    {
                        allVisible = true;
                        
                    }


                    if (centerAfterLoad)
                    {
                        centerAfterLoad = false;
                        centerNow();
                    }
                }
                 


                 }
             }
             catch { }

        }
        /*sincronizza lo scroller dei programmi con quello dei canali e quello della timebar*/
        private void Scrollviewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {


         
          //  this.channelListScrollviewer.ScrollToVerticalOffset(programListScrollviewer.VerticalOffset);
            this.timebar.scrollToHorizontalOffset(programListScrollviewer.HorizontalOffset);
           
           
           
          
        }



        private void programList_ProgramSelected(object sender, ClassLibrary1.Program selected)
        {

            this.selectedDetails.DataContext = selected;


        }





        /*a intervalli regolari, per ogni canale identifica il programma attualmente in onda(necessario per live tile e snappedview)*/
        private void highlightNow()
        {
            DispatcherTimer t = new DispatcherTimer();
            t.Interval = new TimeSpan(0, 1, 0);
            t.Tick += t_Tick;
            t.Start();
        }

        void t_Tick(object sender, object e)
        {

            ObservableCollection<Channel> channels = DataLoader.getSelectedChannels();

            foreach (Channel ch in channels)
            {
                ch.highlightNowProgram();
                this.timebar.highlightNow();

            }
        }

        private void warningBar_RefreshButtonPressed_1(object sender, EventArgs args)
        {
                
            
                showProgramsByDate(this.currentDate);
           
            
        }


 


        private void channelListScrollviewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
        {  //  this.programListScrollviewer.ScrollToVerticalOffset(this.channelListScrollviewer.VerticalOffset);
            if ((lastvisible - 7) * 75 < Math.Abs(this.channelListScrollviewer.VerticalOffset))
            {

                showPrograms();

            }
            
        }


        private void prevButton_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            ObservableCollection<Channel> channels = DataLoader.getSelectedChannels();
            DateTime yesterday = this.currentDate.AddDays(-1);
            if (channels[0].programs2Date.ContainsKey(yesterday)) {
                this.currentDate = yesterday;
                showProgramsByDate(this.currentDate);
                if (!channels[0].programs2Date.ContainsKey(this.currentDate.AddDays(-1))) {
                    this.prevButton.IsEnabled = false;
                }
            }

            
        }

        private void nextButton_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            ObservableCollection<Channel> channels = DataLoader.getSelectedChannels();

            if (channels[0].programs2Date.ContainsKey(this.currentDate.AddDays(1)))
            {
                this.currentDate = this.currentDate.AddDays(1);
                showProgramsByDate(this.currentDate);
                if (!channels[0].programs2Date.ContainsKey(this.currentDate.AddDays(1)))
                {
                    this.nextButton.IsEnabled = false;
                }
            }
        }

        private void hScrollDoubleAnimation_Completed_1(object sender, object e)
        {
            this.timebar.scrollToHorizontalOffset(this.programListScrollviewer.HorizontalOffset);
            this.fakeScrollviewer.ScrollToHorizontalOffset(this.programListScrollviewer.HorizontalOffset);
        }


        private async void RegisterBackgroundTasks()
        {  

            try
            {
                var result = await BackgroundExecutionManager.RequestAccessAsync();
                if (result == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                    result == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
                {
                    foreach (var task in BackgroundTaskRegistration.AllTasks)
                    {
                        if (task.Value.Name == backgroundTaskName)
                            task.Value.Unregister(true);
                    }

                    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                    builder.Name = backgroundTaskName;
                    builder.TaskEntryPoint = backgroundTaskEntryPoint;
                    builder.SetTrigger(new TimeTrigger(15, false));
                    IBackgroundCondition condition = new SystemCondition(SystemConditionType.InternetAvailable);
                    builder.AddCondition(condition);
                    IBackgroundTaskRegistration res = builder.Register();
                    // res.Completed += OnCompleted;
                }
            }
            catch (Exception e)
            {

            }

        }

        private void ScrollBar_ValueChanged_1(object sender, RangeBaseValueChangedEventArgs e)
        {
            this.programListScrollviewer.ScrollToHorizontalOffset(e.NewValue);
        }

        private void fakeScrollviewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
        {
            this.programListScrollviewer.ScrollToHorizontalOffset(this.fakeScrollviewer.HorizontalOffset);
        }


        /*selection panel*/
        private void SelectChannels_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            this.ChannelSelection.ItemsSource = new ObservableCollection<Channel>(DataLoader.getSelectedChannels());
            this.selectionPanel.Visibility = Visibility.Visible;
            this.BottomAppBar.IsOpen = false;
        }

        private void closeSelectionPopupButton_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
           
            this.selectionPanel.Visibility = Visibility.Collapsed;
           
        }

        private void saveSelectionPopupButton_Tapped_1(object sender, TappedRoutedEventArgs e)
        {   ObservableCollection<Channel> channels = this.ChannelSelection.ItemsSource as ObservableCollection<Channel>;
        Dictionary<String, String> dic = new Dictionary<string, string>();
             for (int i = 0; i < channels.Count; i++)
             {
                 channels[i].order = i;
                dic.Add(channels[i].name, i + "#" + channels[i].isSelected);

            }
             DataLoader.saveConfig(dic);//scrive su file
             DataLoader.resetChannels();//riordina e rimette isVisible a tutti i programs
             refresh();
             this.selectionPanel.Visibility = Visibility.Collapsed;
            
        }

        private void refresh() {
            allVisible = false;
            showing = false;
            count = 1;
            lastvisible = 0;


            ObservableCollection<Channel> channels = DataLoader.getSelectedChannels();
            channelList.ItemsSource = channels;
            List<ObservableCollection<Programs>> allprograms = new List<ObservableCollection<Programs>>();
            foreach (DateTime d in channels[0].programs2Date.Keys)
            {
                allprograms.Add(DataLoader.getProgramsByDate(d));
            }
            programList.ItemsSource = allprograms;
            programList.UpdateLayout();
            showProgramsByDate(this.currentDate);
            this.itemListView.ItemsSource = channels;

        }

       

      



    }
}

Commits for Nextrek/Windows8/GuidaTv/GuidaTv/GroupedItemsPage.xaml.cs

Diff revisions: vs.
Revision Author Commited Message
21 JMBauan picture JMBauan Wed 26 Jun, 2013 10:48:36 +0000