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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.ApplicationModel.Resources.Core;

namespace ClassLibrary1
{
    public static class DataLoader
    {

        
        private static String epgFileName = "epg.json";
        private static String confFileName = "conf.txt";
        private static String Data;
        private static ObservableCollection<Channel> allChannels;
        private static ObservableCollection<Channel> selectedChannels;
        private static Dictionary<string,string> channelConf = new Dictionary<string,string>();//ordine dei canali + selezionato oppure no
        public static String data
        {
            get
            {
                return Data;
            }
        }

        //get datacron.json from internet;download successfully-> write in a file and parse it
        //                                 download failed->read the local datacrons.json
        //At FIRST start of the app, internet connection must be available.

        public static async Task loadJson()
        {
            bool noConnectionProblem = true;
            HttpResponseMessage response = new HttpResponseMessage();
            HttpClient http;



            try
            {
                http = new System.Net.Http.HttpClient();

               var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
               var urlString = loader.GetString("epgUrl");
                response = await http.GetAsync(urlString);

            }
            catch
            {
                noConnectionProblem = false;
            }


            if (noConnectionProblem && response.Content != null)
            {
                String text = await response.Content.ReadAsStringAsync();
                if (text.Contains("Rai"))//puo' capitare il caso in cui si è connessi ma cè prima l'autenticazione per la connessione
                {
                    await writeToFile(text,epgFileName);
                    Data = text;
                }
                else
                {
                    Data = await readData(epgFileName);
                    
                }
            }
            else
            {

                Data = await readData(epgFileName);
            }
            await loadConf();
          


        }

        private static async Task writeToFile(String data, String fileName)
        {
            
           try
           {
               StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

               StorageFile sampleFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
               await FileIO.WriteTextAsync(sampleFile, data);


           
            }
            catch (Exception e)
            {
                /* Windows.UI.Popups.MessageDialog dialog =

                         new Windows.UI.Popups.MessageDialog("Error on writing data.Please restart the App.");
                         //new Windows.UI.Popups.MessageDialog(e.Message+"\n\n"+e.StackTrace.ToString());


                 dialog.ShowAsync();*/
            }

        }

        private static async Task<String> readData(string fileName)
        {

            String ret = "";

           
            try
            {
                StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile file = await localFolder.GetFileAsync(fileName);
                ret = await FileIO.ReadTextAsync(file);
               
            }
            catch (Exception e)
            {

                /*Windows.UI.Popups.MessageDialog dialog =

                new Windows.UI.Popups.MessageDialog("Error on reading local data.If this is the first time the App is launched, make sure you are connected to Internet.");


                dialog.ShowAsync();*/


            }
            return ret;

        }

          public static ObservableCollection<Channel> getSelectedChannels(){

              if (allChannels == null || allChannels.Count == 0)
              {
                  allChannels = new ObservableCollection<Channel>();
                try
                {
                    if (Data != null && Data != "")
                    {
                        JsonTextReader jsReader = new JsonTextReader(new StringReader(Data));
                        JObject jobj = JObject.Load(jsReader);
                       
                        Channel channel;

                        foreach (var child in jobj.Children())
                        {
                            if (!((Newtonsoft.Json.Linq.JProperty)child).Name.Contains("France") && !((Newtonsoft.Json.Linq.JProperty)child).Name.Contains("Rai Yoyo") && !((Newtonsoft.Json.Linq.JProperty)child).Name.Contains("Rai News"))
                            {
                                channel = new Channel();

                                channel.parseJson(child);
                                //ordinamento di default
                                //poi sovrascritto in applyConf();
                                channel.order = getChannelOrderByName(channel.name);
                                allChannels.Add(channel);
                            }
                        }
                        applyConf();
                        allChannels = new ObservableCollection<Channel>(allChannels.OrderBy(ch => ch.order));
                        selectedChannels = new ObservableCollection<Channel>(allChannels.Where(ch => ch.isSelected));

                       // channels = new ObservableCollection<Channel>(planets.OrderBy<Planet, string>(pl => pl.Name));

                    }

                }
                catch (Exception e)
                {
                 

                }
            }

            return selectedChannels;
        }

          public static ObservableCollection<Channel> getAllChannels() {
              if (allChannels == null) {
                  getSelectedChannels();
              }
              return allChannels;
          }


          public static ObservableCollection<Programs> getProgramsByDate(System.DateTime date)
          {


              ObservableCollection<Programs> result = new ObservableCollection<Programs>();
              Programs fakePrograms;

              foreach (Channel ch in selectedChannels)
              {
                  if (date != DateTime.MaxValue)
                  {
                      if (ch.programs2Date.ContainsKey(date))
                      {
                          result.Add(ch.programs2Date[date]);
                      }
                      else {
                          fakePrograms = new Programs();//capita che alcuni canali non sono presenti per certe date mentre per altre si
                          fakePrograms.Add(new Program() { width = 60 * 24 * 4 });
                          fakePrograms.parent = ch;
                          result.Add(fakePrograms);

                      }
                  }
                  else {
                      result.Add(ch.programs2Date.Values.ElementAt(ch.programs2Date.Values.Count-1));
                  }

              }
               return result;
          }

          private static int getChannelOrderByName(String name) {
              
              switch (name) {
                  case "Rai 1": return 1;
                  case "Rai 2": return 2;
                  case "Rai 3": return 3;
                  case "Rai 4": return 4;
                  case "Rete 4": return 5;
                  case "Canale 5": return 6;
                  case "Italia 1": return 7;
                  case "La7": return 8;
                  case "MTV music": return 9;
                  default: return 100;

              }
          }

          public static void  saveConfig(Dictionary<String,String> conf){
              string confString = "";

              foreach (KeyValuePair<String, String> pair in conf) {
                  confString += pair.Key + "#" + pair.Value + "\n";
              }

              writeToFile(confString, confFileName);

        
          }

        public static void resetChannels(){
            allChannels = new ObservableCollection<Channel>(allChannels.OrderBy(ch => ch.order));
            foreach (Channel ch in allChannels) { 
                foreach(Programs p in ch.programs2Date.Values){
                    p.isVisible = false;
                }
            }
            selectedChannels = new ObservableCollection<Channel>(allChannels.Where(ch => ch.isSelected));

        }

        private static async Task  loadConf(){
            string str = await readData(confFileName);
            string[] splitted = str.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            string[] splitted2;
            foreach (string s in splitted)
            {
                splitted2 = s.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
                channelConf.Add(splitted2[0], splitted2[1] + "#" + splitted2[2]);
            }

        }

        private static  void applyConf() {
           
            
            if (allChannels != null)
            {
                string[] splitted2;
                
                foreach (Channel ch in allChannels) {
                    if (channelConf.ContainsKey(ch.name))
                    {
                        splitted2 = channelConf[ch.name].Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
                        ch.order = int.Parse(splitted2[0]);
                        ch.isSelected = bool.Parse(splitted2[1]);
                    }
                }
            }

          
        
        }

      


    }
}

Commits for Nextrek/Windows8/GuidaTv/ClassLibrary1/DataLoader.cs

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