-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
889 lines (808 loc) · 40.7 KB
/
App.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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
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.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
using System.Threading;
using mapapp.data;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Linq;
using System.ComponentModel;
using System.Text;
using System.IO.IsolatedStorage;
using System.Windows.Resources;
using Newtonsoft.Json;
namespace mapapp
{
public partial class App : Application, INotifyPropertyChanged
{
private static VoterViewModel _voterViewModel = null;
public MapAppSettings _settings = null;
/// <summary>
/// A static ViewModel used by the views to bind against.
/// </summary>
/// <returns>The VoterViewModel object.</returns>
public static VoterViewModel VotersViewModel
{
get
{
// Delay creation of the view model until necessary
if (_voterViewModel == null)
_voterViewModel = new VoterViewModel();
return _voterViewModel;
}
set
{
if (_voterViewModel != value)
{
_voterViewModel = value;
}
}
}
private PushpinModel _selectedVoter;
public PushpinModel SelectedHouse
{
get { return _selectedVoter; }
set
{
if (_selectedVoter != value)
{
_selectedVoter = value;
NotifyPropertyChanged("SelectedHouse");
}
}
}
public bool UpdateVoter()
{
bool success = false;
VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, _settings.DbFileName));
if (_voterDB.DatabaseExists())
{
try
{
IQueryable<VoterFileEntry> voterQuery = from voter in _voterDB.AllVoters where voter.VoterID == _selectedVoter.VoterID select voter;
VoterFileEntry voterToUpdate = voterQuery.FirstOrDefault();
if (voterToUpdate.VoterID == _selectedVoter.VoterID)
{
voterToUpdate.Party = _selectedVoter.VoterFile.Party;
voterToUpdate.ResultOfContact = _selectedVoter.ResultOfContact;
voterToUpdate.Email = _selectedVoter.Email;
voterToUpdate.CellPhone = _selectedVoter.CellPhone;
voterToUpdate.IsSupporter = _selectedVoter.IsSupporter;
voterToUpdate.IsVolunteer = _selectedVoter.IsVolunteer;
voterToUpdate.IsUpdated = _selectedVoter.VoterFile.IsUpdated;
voterToUpdate.Comments = _selectedVoter.Comments;
voterToUpdate.ModifiedTime = DateTime.Now;
_selectedVoter.VoterFile.ModifiedTime = voterToUpdate.ModifiedTime;
_voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
success = true;
NotifyPropertyChanged("HasUpdates");
}
else
Log(String.Format(" ERROR: Voter returned from query (id={0}) did not match selected voter (id={1}).", voterToUpdate.VoterID, _selectedVoter.VoterID));
}
catch (Exception ex)
{
Log(" Error updating voter record: " + _selectedVoter.FullName + " : " + ex.ToString());
}
}
_voterDB.Dispose();
return success;
}
public bool DbHasUpdates()
{
bool _bReturn = false;
VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, _settings.DbFileName));
if (_voterDB.DatabaseExists())
{
try
{
IEnumerable<VoterFileEntry> voters = from voter in _voterDB.AllVoters
where voter.ModifiedTime > _settings.GetSetting<DateTime>("lastsync")
select voter;
if (voters.Count() > 0)
_bReturn = true;
}
catch (Exception ex)
{
Log(" Error reporting changes: " + ex.ToString());
}
}
_voterDB.Dispose();
return _bReturn;
}
public void PostUpdate()
{
// TODO: Provide an option to only update if WiFi is available
// Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsWiFiEnabled;
// use DeviceNetworkInformation.NetworkAvailabilityChanged Event
if (App.thisApp.DbHasUpdates() && Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable)
{
DateTime updateTime = DateTime.Now;
string updateContent = App.thisApp.GetUpdateString(updateTime);
if (updateContent.Length > 40) // This is an arbitary stab at verifying that there is something to report
{
try
{
Uri voterListUri = new Uri(App.thisApp._settings.UploadUrl);
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/text";
webClient.Headers[HttpRequestHeader.ContentLength] = updateContent.Length.ToString();
webClient.Headers[HttpRequestHeader.Authorization] = "bearer " + App.thisApp._settings.GetSetting<string>("authkey");
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(UpdatePostCompleted);
App.Log("Posting update ... ");
webClient.UploadStringAsync(voterListUri, "POST", updateContent, updateTime);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
}
}
void UpdatePostCompleted(object sender, UploadStringCompletedEventArgs e)
{
string size = "0";
try
{
if (sender is WebClient)
{
WebClient wc = sender as WebClient;
UpdateResult result = JsonConvert.DeserializeObject<UpdateResult>(e.Result);
if (result != null && result.VersionTag.Length > 0)
{
DateTime updateTime = DateTime.Now;
if (e.UserState.GetType() == typeof(DateTime))
updateTime = (DateTime)e.UserState;
App.thisApp._settings.UpdateSetting("lastsync", updateTime);
}
}
App.Log(string.Format("Uploaded {0} bytes. Response is: {1}", size, e.Result));
NotifyPropertyChanged("HasUpdates");
}
catch (Exception ex)
{
App.Log("Problem logging results of upload: " + ex.ToString());
}
}
public string GetUpdateString(DateTime updateTime)
{
string stUpdates = "";
VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, _settings.DbFileName));
if (_voterDB.DatabaseExists())
{
try
{
IEnumerable<VoterFileEntry> voters = from voter in _voterDB.AllVoters
where (voter.ModifiedTime > _settings.GetSetting<DateTime>("lastsync") &&
voter.ModifiedTime <= updateTime && voter.IsUpdated)
select voter;
App.Log(" GetUpdateString Query completed.");
StringWriter _updateString = new StringWriter();
if (App.thisApp._settings.GetSetting<string>("dataformat") == "csv")
{
StringBuilder csv = new StringBuilder();
csv.Clear();
if (App.thisApp._settings.GetSetting<bool>("_trhack"))
csv.AppendLine("redid,party,resultofcontact,email,cellphone,issupporter,comments,modifiedtime");
else
csv.AppendLine("recid,party,resultofcontact,email,cellphone,issupporter,comments,modifiedtime");
_updateString.Write(csv.ToString());
csv.Clear();
foreach (VoterFileEntry voter in voters)
{
// TODO: Add WriteUpdateToCsv() method to VoterFileEntry object
csv.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7}{8}", voter.VoterID, voter.Party, voter.ResultOfContact, voter.Email, voter.CellPhone, voter.IsSupporter, voter.Comments, voter.ModifiedTime, System.Environment.NewLine);
_updateString.Write(csv.ToString());
csv.Clear();
}
_updateString.Flush();
stUpdates = _updateString.ToString();
_updateString.Close();
csv.Clear();
}
Log(" Completed: Updates = " + stUpdates);
}
catch (Exception ex)
{
Log(" Error reporting changes: " + ex.ToString());
stUpdates = "";
}
}
_voterDB.Dispose();
return stUpdates;
}
Thread _dbLoadThread;
private string _delayLoadDbFile = "";
/*
private bool _dbLoaded;
public bool IsDbLoaded
{
get { return _dbLoaded; }
set
{
_dbLoaded = value;
this.NotifyPropertyChanged("IsDbLoaded");
}
}
*/
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
/// <returns>The root frame of the Phone Application.</returns>
public PhoneApplicationFrame RootFrame { get; private set; }
public static App thisApp
{
get;
set;
}
public static Thread MainThread
{
get;
set;
}
/// <summary>
/// Constructor for the Application object.
/// </summary>
public App()
{
_settings = new MapAppSettings();
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;
// Enable non-production analysis visualization mode,
// which shows areas of a page that are handed off to GPU with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;
// Disable the application idle detection by setting the UserIdleDetectionMode property of the
// application's PhoneApplicationService object to Disabled.
// Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
// and consume battery power when the user is not using the phone.
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
thisApp = this;
MainThread = Thread.CurrentThread;
// Create the database if it does not exist.
PropertyChanged += App_PropertyChanged;
_delayLoadDbFile = "";
Thread.Sleep(1500);
}
void App_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "HasUpdates")
{
if (!DbHasUpdates() && _delayLoadDbFile != "")
{
if (_dbLoadThread == null || _dbLoadThread.ThreadState == ThreadState.Stopped)
{
_dbLoadThread = new Thread(LoadDatabase);
}
if (_dbLoadThread.ThreadState != ThreadState.Unstarted)
{
return;
}
_dbLoadThread.Start(_delayLoadDbFile);
_delayLoadDbFile = "";
}
}
}
public void LoadDatabaseFromFile(string fileName)
{
if (_settings.DbStatus == DbState.Loaded)
{
App.Log("ERROR: Trying to load data from file when database is already loaded.");
return;
}
else
{
if (App.VotersViewModel != null)
{
App.VotersViewModel.StreetList.Clear();
App.VotersViewModel.VoterList.Clear();
App.VotersViewModel.PrecinctList.Clear();
App.VotersViewModel.NewStreetList.Clear();
App.VotersViewModel = null;
}
if (_dbLoadThread == null || _dbLoadThread.ThreadState == ThreadState.Stopped)
{
_dbLoadThread = new Thread(LoadDatabase);
}
if (_dbLoadThread.ThreadState != ThreadState.Unstarted)
{
return;
}
_dbLoadThread.Start(fileName);
}
}
public IEnumerable<string> SplitCSV(string input)
{
System.Text.RegularExpressions.Regex csvSplit = new System.Text.RegularExpressions.Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", System.Text.RegularExpressions.RegexOptions.Compiled);
foreach (System.Text.RegularExpressions.Match match in csvSplit.Matches(input))
{
yield return match.Value.TrimStart(new[] { ',', '\"', '\\' }).TrimEnd(new[] { ',', '\"', '\\' });
}
}
public XElement GetVoterXmlFromCsv(string headers, string voterCsv)
{
string[] fieldNames = headers.Split(new[] { ',' });
IEnumerable<string> fields = SplitCSV(voterCsv);
System.Diagnostics.Debug.Assert(fieldNames.Count() == fields.Count<string>());
XElement voterXml = new XElement("voter");
int i = 0;
foreach (string fieldData in fields)
{
var xField = new XElement(fieldNames[i], fieldData);
voterXml.Add(xField);
i++;
}
return voterXml;
}
public void LoadDatabase(Object voterFileName)
{
// TODO: Update passed object to stream, rely on dataformat setting to get type of data in voter list
/* recid,address,address2,city,state,zip,phone,lastname,firstname,email,precinct,party,pri,gen,location,resultofcontact,cellphone,issupporter,comments,modifiedtime
WA00092212345,1234 Anystreet,,Sammamish,WA,98074,,BOND,JAMES,,SAM 45-1234,1,2,4,"47.6161616,-122.0551111",,,,,
*/
string fileName = "";
if (voterFileName is string)
{
fileName = voterFileName as string;
}
else
{
return;
}
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream voterFile = isf.OpenFile(fileName, FileMode.Open);
Stream voterDataStream = voterFile;
if (fileName.EndsWith(".csv"))
{
App.thisApp._settings.UpdateSetting("dataformat", "csv");
}
else if (fileName.EndsWith(".zip"))
{
string _voterFileName = "voters.csv";
if (voterFile.CanSeek)
{
byte[] fileFront = new byte[64];
int bytesRead = voterFile.Read(fileFront, 0, 64);
voterFile.Seek(0, SeekOrigin.Begin);
byte[] fileNameBytes = new byte[20];
int i = 0;
for (i = 0; i < 16; i++)
{
fileNameBytes[i] = fileFront[30 + i];
if (fileNameBytes[i] > 128)
{
fileNameBytes[i] = 0;
break;
}
}
_voterFileName = Encoding.UTF8.GetString(fileNameBytes, 0, i);
}
StreamResourceInfo zipInfo = new StreamResourceInfo(voterFile, "application/zip");
StreamResourceInfo streamInfo = Application.GetResourceStream(zipInfo, new Uri(_voterFileName, UriKind.Relative));
if (streamInfo != null)
voterDataStream = streamInfo.Stream;
}
int nVoters = 0;
VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, _settings.DbFileName));
if (_settings.DbStatus == DbState.Loaded && DbHasUpdates())
{
PostUpdate();
_delayLoadDbFile = fileName;
return;
}
else
{
try
{
if (!_voterDB.DatabaseExists())
{
App.Log("Creating new database...");
_voterDB.CreateDatabase();
_settings.DbStatus = DbState.Empty;
App.Log(" Database created");
}
else
{
App.Log("WARNING: Deleting existing database...");
_voterDB.DeleteDatabase();
App.Log("Creating new database...");
_voterDB.CreateDatabase();
_settings.DbStatus = DbState.Empty;
App.Log(" Database created");
}
if (App.thisApp._settings.GetSetting<string>("dataformat") == "csv")
{
// Load voters from rows in csv text
StreamReader csvReader = new StreamReader(voterDataStream);
String headerLine = csvReader.ReadLine().ToLower();
String voterLine = csvReader.ReadLine();
_settings.DbStatus = DbState.Loading;
App.Log(" Parsing voters from CSV");
while (voterLine != null)
{
// recid,firstname,lastname,address,address2,city,state,zip,precinct,lat,long,gender,birthdate,pri,gen,phone,email,party,cellphone,comments,issupporter
XElement voterElement = GetVoterXmlFromCsv(headerLine, voterLine);
// We need at least the basics of address, city, and voterid to do anything useful with this record
try
{
VoterFileEntry voter = new VoterFileEntry
{
Address = (string)voterElement.Element("address"),
City = (string)voterElement.Element("city"),
// VoterID = (string)voterElement.Element("recid"),
ModifiedTime = DateTime.Now
};
if (voterElement.Elements("recid").Count() > 0)
{
voter.VoterID = (string)voterElement.Element("recid");
App.thisApp._settings.UpdateSetting("_trhack", false);
}
// NOTE: This is a hack to fix my data error just before TechRoanoke conference
else if (voterElement.Elements("redid").Count() > 0)
{
voter.VoterID = (string)voterElement.Element("redid");
App.thisApp._settings.UpdateSetting("_trhack", true);
}
if (voterElement.Elements("firstname").Count() > 0)
voter.FirstName = (string)voterElement.Element("firstname");
if (voterElement.Elements("lastname").Count() > 0)
voter.LastName = (string)voterElement.Element("lastname");
if (voterElement.Elements("address2").Count() > 0)
voter.Address2 = (string)voterElement.Element("address2");
if (voterElement.Elements("state").Count() > 0)
voter.State = (string)voterElement.Element("state");
else
voter.State = "WA";
if (voterElement.Elements("zip").Count() > 0)
voter.Zip = (string)voterElement.Element("zip");
if (voterElement.Elements("party").Count() > 0)
voter.PartyString = (string)voterElement.Element("party");
if (voterElement.Elements("precinctname").Count() > 0) // Prefer "precinctname" over "precinct"
voter.Precinct = (string)voterElement.Element("precinctname");
else if (voterElement.Elements("precinct").Count() > 0)
voter.Precinct = (string)voterElement.Element("precinct");
if (voterElement.Elements("pri").Count() > 0)
voter.PrimaryVoteHistoryString = (string)voterElement.Element("pri");
if (voterElement.Elements("gen").Count() > 0)
voter.GeneralVoteHistoryString = (string)voterElement.Element("gen");
if (voterElement.Elements("email").Count() > 0)
voter.Email = (string)voterElement.Element("email");
if (voterElement.Elements("phone").Count() > 0)
voter.Phone = (string)voterElement.Element("phone");
if (voterElement.Elements("location").Count() > 0)
voter.Coordinates = (string)voterElement.Element("location");
else
{
if (voterElement.Elements("lat").Count() > 0)
voter.LatitudeString = (string)voterElement.Element("lat");
if (voterElement.Elements("long").Count() > 0)
voter.LongitudeString = (string)voterElement.Element("long");
}
if (voterElement.Elements("resultofcontact").Count() > 0)
voter.ResultOfContactString = (string)voterElement.Element("resultofcontact");
if (voterElement.Elements("cellphone").Count() > 0)
voter.CellPhone = (string)voterElement.Element("cellphone");
if (voterElement.Elements("issupporter").Count() > 0)
voter.IsSupporterString = (string)voterElement.Element("issupporter");
if (voterElement.Elements("isvolunteer").Count() > 0)
voter.IsVolunteerString = (string)voterElement.Element("isvolunteer");
if (voterElement.Elements("comments").Count() > 0)
voter.Comments = (string)voterElement.Element("comments");
if (voterElement.Elements("address2").Count() > 0)
voter.Address2 = (string)voterElement.Element("address2");
if (voterElement.Elements("gender").Count() > 0)
{
voter.Gender = (string)voterElement.Element("gender");
voter.FirstName = voter.FirstName + " " + voter.Gender;
}
if (voterElement.Elements("birthdate").Count() > 0)
{
voter.Birthdate = (string)voterElement.Element("birthdate");
if (voter.Age > 0)
voter.FirstName = voter.FirstName + " " + voter.Age.ToString();
}
// We don't have lat/long for this voter, but we can still show them in precinct and street lists
if (0.0 == voter.Latitude || 0.0 == voter.Longitude)
{
// continue;
}
nVoters++;
_voterDB.AllVoters.InsertOnSubmit(voter);
}
catch (Exception ex)
{
App.Log("There was a problem parsing voter record - " + voterLine + " : " + ex.ToString());
}
voterLine = csvReader.ReadLine();
}
App.Log(" Parsing voters completed");
}
else
{
App.Log("Unrecognized file type - " + fileName);
}
voterDataStream.Close();
voterDataStream.Dispose();
voterFile.Close();
voterFile.Dispose();
App.Log(" Voters submitted to database: " + nVoters.ToString());
_voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
isf.DeleteFile(fileName);
_settings.DbStatus = DbState.Loaded;
_settings.UpdateSetting("lastsync", DateTime.Now);
App.Log(" Loading Precincts table");
IEnumerable<string> precincts = (from v in _voterDB.AllVoters select v.Precinct).Distinct();
foreach (string precinct in precincts)
{
PrecinctTableEntry thisPrecinct = new PrecinctTableEntry();
thisPrecinct.Name = precinct;
List<VoterFileEntry> pList = (from VoterFileEntry vPrecinct in _voterDB.AllVoters where vPrecinct.Precinct == precinct && vPrecinct.Latitude > 47.0 && vPrecinct.Longitude < 122.0 select vPrecinct).ToList();
App.Log(String.Format(" There are {0} voters in {1} precinct.", pList.Count, precinct));
if (pList.Count > 0)
{
thisPrecinct.West = pList.Min<VoterFileEntry, double>(vMin => vMin.Longitude); // smallest longitude found in list (or largest absolute value)
thisPrecinct.East = pList.Max<VoterFileEntry, double>(vMax => vMax.Longitude); // largest longitude found in list (or smallest absolute value)
thisPrecinct.North = pList.Max<VoterFileEntry, double>(vMax => vMax.Latitude); // largest latitude found in list
thisPrecinct.South = pList.Min<VoterFileEntry, double>(vMin => vMin.Latitude); // smallest latitude found in list
App.Log(String.Format("Precinct {4} - North={0}, South={1}, East={2}, West={3}.", thisPrecinct.North, thisPrecinct.South, thisPrecinct.East, thisPrecinct.West, thisPrecinct.Name));
double diffLat = thisPrecinct.North - thisPrecinct.South;
double diffLong = thisPrecinct.East - thisPrecinct.West;
thisPrecinct.CenterLatitude = thisPrecinct.South + (diffLat / 2);
thisPrecinct.CenterLongitude = thisPrecinct.West + (diffLong / 2);
_voterDB.Precincts.InsertOnSubmit(thisPrecinct);
}
}
App.Log(" Submitting precincts to database: ");
_voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
App.Log(" Loading Streets table");
IEnumerable<string> _streets = (from v in _voterDB.AllVoters select v.Street).Distinct();
foreach (string street in _streets)
{
StreetTableEntry thisStreet = new StreetTableEntry();
thisStreet.Name = street;
List<VoterFileEntry> pList = (from VoterFileEntry vStreet in _voterDB.AllVoters where vStreet.Street == street && vStreet.Latitude > 47.0 && vStreet.Longitude < 122.0 select vStreet).ToList();
App.Log(String.Format(" There are {0} voters on {1} street.", pList.Count, street));
if (pList.Count > 0)
{
thisStreet.West = pList.Min<VoterFileEntry, double>(vMin => vMin.Longitude); // smallest longitude found in list (or largest absolute value)
thisStreet.East = pList.Max<VoterFileEntry, double>(vMax => vMax.Longitude); // largest longitude found in list (or smallest absolute value)
thisStreet.North = pList.Max<VoterFileEntry, double>(vMax => vMax.Latitude); // largest latitude found in list
thisStreet.South = pList.Min<VoterFileEntry, double>(vMin => vMin.Latitude); // smallest latitude found in list
App.Log(String.Format("Street {4} - North={0}, South={1}, East={2}, West={3}.", thisStreet.North, thisStreet.South, thisStreet.East, thisStreet.West, thisStreet.Name));
double diffLat = thisStreet.North - thisStreet.South;
double diffLong = thisStreet.East - thisStreet.West;
thisStreet.CenterLatitude = thisStreet.South + (diffLat / 2);
thisStreet.CenterLongitude = thisStreet.West + (diffLong / 2);
_voterDB.Streets.InsertOnSubmit(thisStreet);
}
}
App.Log(" Submitting streets to database: ");
_voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
App.Log(" Changes committed to database");
_settings.UpdateSetting("lastsync", DateTime.Now);
}
catch (Exception ex)
{
App.Log("Exception loading voters to database: " + ex.ToString());
}
}
_dbLoadThread = null;
}
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
// TODO: reload location of Car from isostore
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// TODO: reload location of Car from isostore
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
// TODO: write location of Car to isostore
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
// TODO: write location of Car to isostore
}
// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
#region Phone application initialization
// Avoid double-initialization
private bool phoneApplicationInitialized = false;
// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
// Do not add any additional code to this method
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify the page that a data context property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public static void Log(string messageString)
{
System.Diagnostics.Debug.WriteLine(string.Format("({2:X}) {0:H:mm:ss.fff}: {1}", DateTime.Now, messageString, Thread.CurrentThread.ManagedThreadId));
}
}
public class VoterViewModel : INotifyPropertyChanged
{
public VoterViewModel()
{
_inViewVoters = new ObservableCollection<PushpinModel>();
}
public void FillVoterList(List<PushpinModel> fillWith)
{
if (fillWith == null || fillWith.Count < 1)
{
VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, App.thisApp._settings.DbFileName));
if (!(App.thisApp._settings.DbStatus == DbState.Loaded) || !_voterDB.DatabaseExists())
{
App.Log("Database not ready to load voters yet.");
return;
}
App.VotersViewModel.StreetList.Clear();
App.VotersViewModel.VoterList.Clear();
IEnumerable<VoterFileEntry> data = from VoterFileEntry voter in _voterDB.AllVoters select voter;
foreach (VoterFileEntry _v in data)
{
PushpinModel p = new PushpinModel(_v);
if ((p.Street != null) && (!App.VotersViewModel.StreetList.Contains(p.Street)))
{
App.VotersViewModel.StreetList.Add(p.Street);
}
App.VotersViewModel.VoterList.Add(p);
}
_voterDB.Dispose();
}
else
{
App.VotersViewModel.StreetList.Clear();
App.VotersViewModel.VoterList.Clear();
foreach (PushpinModel p in fillWith)
{
if ((p.Street != null) && (!App.VotersViewModel.StreetList.Contains(p.Street)))
{
App.VotersViewModel.StreetList.Add(p.Street);
}
App.VotersViewModel.VoterList.Add(p);
}
}
if (App.VotersViewModel.VoterList.Count <= 0)
{
App.Log("There are no voters in database!");
return;
}
else
System.Diagnostics.Debug.WriteLine("Found {0} voters in database.", App.VotersViewModel.VoterList.Count);
}
private ObservableCollection<PushpinModel> _inViewVoters = new ObservableCollection<PushpinModel>();
public ObservableCollection<PushpinModel> VoterList
{
get
{
return _inViewVoters;
}
set
{
_inViewVoters = value;
NotifyPropertyChanged("VoterList");
}
}
private ObservableCollection<PrecinctPinModel> _precinctList = new ObservableCollection<PrecinctPinModel>();
public ObservableCollection<PrecinctPinModel> PrecinctList
{
get { return _precinctList; }
set
{
_precinctList = value;
NotifyPropertyChanged("PrecinctList");
}
}
private ObservableCollection<StreetPinModel> _streetList = new ObservableCollection<StreetPinModel>();
public ObservableCollection<StreetPinModel> NewStreetList
{
get { return _streetList; }
set
{
_streetList = value;
NotifyPropertyChanged("NewStreetList");
}
}
private List<string> _streets = new List<string>();
public List<string> StreetList
{
get
{
return _streets;
}
set
{
if (_streets != value)
{
_streets = value;
NotifyPropertyChanged("StreetList");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify the page that a data context property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}