/
SplashViewModel.cs
150 lines (130 loc) · 4.47 KB
/
SplashViewModel.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
using System;
using System.Windows;
using Caliburn.Micro;
using JetBrains.Annotations;
using Microsoft.Phone.Controls;
using trello.Services;
using trello.Views;
using trellow.api;
using trellow.api.Data.Services;
namespace trello.ViewModels
{
[UsedImplicitly]
public class SplashViewModel : Screen
{
private readonly INavigationService _navigationService;
private readonly ITrelloApiSettings _settings;
private readonly ICache _cache;
private readonly ITrello _api;
private string _status;
public string Status
{
get { return _status; }
set
{
if (value == _status) return;
_status = value;
NotifyOfPropertyChange(() => Status);
}
}
public SplashViewModel(INavigationService navigationService, ITrelloApiSettings settings, ITrello api, ICache cache)
{
_navigationService = navigationService;
_settings = settings;
_cache = cache;
_api = api;
}
protected override async void OnViewLoaded(object view)
{
Status = "Loading...";
Status = "Populating Cache...";
var success = await _cache.Initialize();
if (!success)
Status = "Invalidating the cache...";
#if DISCONNECTED
_settings.AccessToken = new OAuthToken("publicKey", "privateKey");
#endif
var validated = await _api.AccessTokenIsFresh(_settings.AccessToken);
if (validated)
{
Status = "Signed in";
AccessGranted(_settings.AccessToken);
}
else
{
Status = "Signing in...";
AccessDenied();
}
}
[UsedImplicitly]
public void BrowserNavigating(NavigatingEventArgs args)
{
if (!args.Uri.Host.Equals("localhost"))
return;
args.Cancel = true;
// We've been redirected back w/ the token
UsingView(async view =>
{
view.Browser.Visibility = Visibility.Collapsed;
var parms = args.Uri.Query.ParseQueryString();
var verifier = parms["oauth_verifier"];
var token = await _api.Verify(verifier);
if (token != null)
{
AccessGranted(token);
}
else
{
AccessDenied();
}
});
}
private async void AccessGranted(OAuthToken token)
{
_settings.AccessToken = token;
var profile = await _api.Members.Me();
if (profile != null)
{
_settings.MemberId = profile.Id;
_settings.Username = profile.Username;
_settings.Fullname = profile.FullName;
_settings.AvatarHash = profile.AvatarHash;
}
UsingView(view => view.Browser.Visibility = Visibility.Collapsed);
_navigationService.UriFor<ShellViewModel>().Navigate();
}
private async void AccessDenied()
{
var uri = await _api.GetAuthorizationUri("Trellow", Scope.ReadWriteAccount, Expiration.Never);
if (uri != null)
{
LoadLogin(uri);
}
else
{
Status = "Could not sign you in.\n\nPlease ensure you have an active internet connection.";
}
}
private void LoadLogin(Uri uri)
{
UsingView(async view =>
{
// clear the browser cookies so that we can re-login
await view.Browser.ClearCookiesAsync();
view.Browser.Navigate(uri);
view.Browser.Visibility = Visibility.Visible;
});
}
private void UsingView(Action<SplashView> action)
{
var view = base.GetView() as SplashView;
if (view == null)
{
MessageBox.Show("There was a problem with logging into Trello. " +
"Please verify that you can browse the internet.");
return;
}
action(view);
}
}
}