/
MainPage.xaml.cs
120 lines (102 loc) · 3.75 KB
/
MainPage.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Device.Location;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using ParApply.Business;
using ParApply.Services;
namespace ParApply
{
public partial class MainPage : PhoneApplicationPage
{
private GeoCoordinateWatcher _coordinateWatcher;
private YrService _yrService;
private ParaplyService _paraplyService;
private GeoCoordinate _myLocation;
private static Noreg _norge;
private Sted _sted;
private BackgroundWorker _backgroundWorker;
private StedParser _norgeParser;
private bool _isFirstLookup = false;
private bool _parsingComplete;
// Constructor
public MainPage()
{
InitializeComponent();
_coordinateWatcher = new GeoCoordinateWatcher();
_coordinateWatcher.PositionChanged += SaveCurrentPosition;
_coordinateWatcher.Start(false);
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.DoWork += ParseNorgeFile;
_backgroundWorker.RunWorkerCompleted += SetParsingCompleted;
// Stop dreaming about IoC-containers!
_norgeParser = new StedParser();
_norge = new Noreg();
_paraplyService = new ParaplyService();
_yrService = new YrService();
_backgroundWorker.RunWorkerAsync();
}
private void ParseNorgeFile(object sender, DoWorkEventArgs e)
{
using (var stream = ResourceHelper.Noreg())
{
var steder = _norgeParser.Parse(stream);
foreach (var sted in steder)
{
_norge.AddSted(sted);
}
}
}
private void SetParsingCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_parsingComplete = true;
TryUpdateData();
}
private void TryUpdateData()
{
if(_parsingComplete && _myLocation != null)
{
_sted = _norge.FindClosestSted(_myLocation);
_yrService.GetYrData(_sted, UpdateUI);
}
}
private void SaveCurrentPosition(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
_isFirstLookup = _myLocation == null;
_myLocation = e.Position.Location;
if (_isFirstLookup)
{
_coordinateWatcher.Stop();
_coordinateWatcher.Dispose();
}
TryUpdateData();
}
private void UpdateUI(Result<IEnumerable<YrData>> yrResult)
{
var useParaply = _paraplyService.ShouldUseParaply(yrResult);
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => SetImage(useParaply));
}
private void SetImage(UseParaplyResult useParaply)
{
if(!useParaply.HasError())
{
StedInfoTextBlock.Text = string.Format("{0}, {1}, {2} ", _sted.Navn, useParaply.YrData.SymbolName, useParaply.YrData.GetPeriode());
}
switch (useParaply.Result)
{
case UseParaply.Unknown:
ParaplyImage.Source = ResourceHelper.QuestionMark();
break;
case UseParaply.Yes:
ParaplyImage.Source = ResourceHelper.UseUmbrella();
break;
case UseParaply.No:
ParaplyImage.Source = ResourceHelper.DontUseUmbrella();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}