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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using System.IO;
using System.IO.IsolatedStorage;
using Newtonsoft.Json.Linq;
using DatacronVideoGuide.ViewModels;
using Microsoft.Phone.Shell;
namespace DatacronVideoGuide
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        private static MainPage Instance;

        public MainPage()
        {
            InitializeComponent();
            Instance = this;

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();

                updateLocalJson();

            }

            this.skills.ItemsSource = Skill.skillList;
           
            
            
        }
        //prende i dati da internet;da usare se si vuole aggiornare il json locale
        private void updateLocalJson() {
            var webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloadStrinCompletedUpdate);


            webClient.DownloadStringAsync(new Uri("http://dl.dropbox.com/u/66864163/datacrons.json", UriKind.Absolute));
        
        }

        private void downloadStrinCompletedUpdate(Object sender, DownloadStringCompletedEventArgs e) {

            //errore di connessione
            if (e.Error != null)
            {

                if (checkFile("datacrons.json"))
                {
                    loadJson();
                    
                }
                else {
                    MessageBox.Show("You need Internet connection.");
                    App.Quit();
                }

            }
            else {

                try
                {
                    IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                    IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("datacrons.json", FileMode.OpenOrCreate, FileAccess.Write);
                    using (StreamWriter writer = new StreamWriter(fileStream))
                    {

                        writer.Write(e.Result);
                        writer.Close();
                    }
                    loadJson();
                }
                catch { 
                //mostrare errore
                
                }
            
            }
        }



        private void loadJson() { 
            
         

          
           try
            {
                IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

                StreamReader reader = new StreamReader(new IsolatedStorageFileStream("datacrons.json", FileMode.Open, isoStore));
                String result = reader.ReadToEnd();
                
                parseJson(result);
            }
            catch {
                /*var webClient = new WebClient();
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloadStrinCompleted);


                webClient.DownloadStringAsync(new Uri("http://dl.dropbox.com/u/66864163/datacrons.json", UriKind.Absolute));*/
            
            }
            

            
            
          
        
        }



       private void parseJson(String jsonText){
            try
            {
                JsonTextReader jsReader = new JsonTextReader(new StringReader(jsonText));
                JObject jobj = JObject.Load(jsReader);
                var head = jobj.SelectToken("datacronMap");
                List<Planet> planets = new List<Planet>();
                Planet planet;

                foreach (var child in head.Children())
                {
                    planet = new Planet();

                    planet.parseJson(child);
                    planets.Add(planet);
                }
                this.Planets.ItemsSource = planets;

                List<Datacron> allDatacrons = new List<Datacron>();
                foreach (Planet p in planets)
                {
                    allDatacrons = allDatacrons.Concat(p.datacrons).ToList<Datacron>();
                }

                PhoneApplicationService.Current.State.Add("planets", planets);
                PhoneApplicationService.Current.State.Add("datacrons", allDatacrons);
                PhoneApplicationService.Current.State.Add("selectedPlanet", null);
                PhoneApplicationService.Current.State.Add("selectedDatacron", null);
                PhoneApplicationService.Current.State.Add("selectedSkill", null);

            }
            catch
            {}
        }

        private void Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (this.Planets.SelectedItem!=null){
                PhoneApplicationService.Current.State["selectedPlanet"] = this.Planets.SelectedItem;
                NavigationService.Navigate(new Uri("/PlanetView.xaml", UriKind.Relative));
            }
            
        }

        public static MainPage getInstance() {
            return Instance;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (this.skills.SelectedItem != null) {
                PhoneApplicationService.Current.State["selectedSkill"] = this.skills.SelectedItem;
                NavigationService.Navigate(new Uri("/SkillView.xaml", UriKind.Relative));
            }
        }

        private void Grid_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {   
            
                ((Planet)((Grid)sender).DataContext).opacity = Planet.maxOpacity;
            
        }

        private void Grid_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            
                ((Planet)((Grid)sender).DataContext).opacity = Planet.minOpacity;
            
        }

        //controlla se esiste un determinato file o se non è aggiornato
        private bool checkFile(string filePath) {
            bool created = true;

            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read);
                fileStream.Close();
               

            }
            catch {
                created = false;
            }

            return created;
        
        }
    }
}

Commits for Nextrek/WindowsPhone/SwtorDatacronVideoGuide/DatacronVideoGuide/DatacronVideoGuide/MainPage.xaml.cs

Diff revisions: vs.
Revision Author Commited Message
6 FMMortaroli picture FMMortaroli Sat 20 Apr, 2013 15:30:47 +0000