|
4 | 4 | using CodeHub.Core.Services; |
5 | 5 | using System.Linq; |
6 | 6 | using CodeHub.Core.Factories; |
7 | | -using System.Threading.Tasks; |
8 | 7 |
|
9 | 8 | namespace CodeHub.Core.ViewModels.App |
10 | 9 | { |
11 | | - public class StartupViewModel : BaseStartupViewModel |
| 10 | + public class StartupViewModel : BaseStartupViewModel |
12 | 11 | { |
13 | 12 | private readonly ILoginFactory _loginFactory; |
14 | | - private readonly IApplicationService _applicationService; |
| 13 | + private readonly IApplicationService _applicationService; |
15 | 14 |
|
16 | 15 | public StartupViewModel(ILoginFactory loginFactory, IApplicationService applicationService) |
17 | | - { |
18 | | - _loginFactory = loginFactory; |
19 | | - _applicationService = applicationService; |
20 | | - } |
| 16 | + { |
| 17 | + _loginFactory = loginFactory; |
| 18 | + _applicationService = applicationService; |
| 19 | + } |
21 | 20 |
|
22 | | - protected async override void Startup() |
23 | | - { |
24 | | - if (!_applicationService.Accounts.Any()) |
25 | | - { |
26 | | - ShowViewModel<Accounts.AccountsViewModel>(); |
27 | | - ShowViewModel<Accounts.NewAccountViewModel>(); |
28 | | - return; |
29 | | - } |
| 21 | + protected async override void Startup() |
| 22 | + { |
| 23 | + if (!_applicationService.Accounts.Any()) |
| 24 | + { |
| 25 | + ShowViewModel<Accounts.AccountsViewModel>(); |
| 26 | + ShowViewModel<Accounts.NewAccountViewModel>(); |
| 27 | + return; |
| 28 | + } |
30 | 29 |
|
31 | | - var account = GetDefaultAccount() as GitHubAccount; |
32 | | - if (account == null) |
33 | | - { |
34 | | - ShowViewModel<Accounts.AccountsViewModel>(); |
35 | | - return; |
36 | | - } |
| 30 | + var account = GetDefaultAccount() as GitHubAccount; |
| 31 | + if (account == null) |
| 32 | + { |
| 33 | + ShowViewModel<Accounts.AccountsViewModel>(); |
| 34 | + return; |
| 35 | + } |
37 | 36 |
|
38 | | - var isEnterprise = account.IsEnterprise || !string.IsNullOrEmpty(account.Password); |
39 | | - if (account.DontRemember) |
40 | | - { |
41 | | - ShowViewModel<Accounts.AccountsViewModel>(); |
| 37 | + var isEnterprise = account.IsEnterprise || !string.IsNullOrEmpty(account.Password); |
| 38 | + if (account.DontRemember) |
| 39 | + { |
| 40 | + ShowViewModel<Accounts.AccountsViewModel>(); |
42 | 41 |
|
43 | | - //Hack for now |
44 | | - if (isEnterprise) |
45 | | - { |
46 | | - ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id }); |
47 | | - } |
48 | | - else |
49 | | - { |
50 | | - ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account)); |
51 | | - } |
| 42 | + //Hack for now |
| 43 | + if (isEnterprise) |
| 44 | + { |
| 45 | + ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id }); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account)); |
| 50 | + } |
52 | 51 |
|
53 | | - return; |
54 | | - } |
| 52 | + return; |
| 53 | + } |
55 | 54 |
|
56 | | - //Lets login! |
57 | | - try |
58 | | - { |
59 | | - IsLoggingIn = true; |
60 | | - var client = await _loginFactory.LoginAccount(account); |
61 | | - _applicationService.ActivateUser(account, client); |
62 | | - } |
63 | | - catch (GitHubSharp.UnauthorizedException e) |
64 | | - { |
| 55 | + //Lets login! |
| 56 | + try |
| 57 | + { |
| 58 | + IsLoggingIn = true; |
| 59 | + |
| 60 | + Uri accountAvatarUri = null; |
| 61 | + Uri.TryCreate(account.AvatarUrl, UriKind.Absolute, out accountAvatarUri); |
| 62 | + ImageUrl = accountAvatarUri; |
| 63 | + Status = "Logging in as " + account.Username; |
| 64 | + |
| 65 | + var client = await _loginFactory.LoginAccount(account); |
| 66 | + _applicationService.ActivateUser(account, client); |
| 67 | + } |
| 68 | + catch (GitHubSharp.UnauthorizedException e) |
| 69 | + { |
65 | 70 | DisplayAlert("The credentials for the selected account are incorrect. " + e.Message); |
66 | 71 |
|
67 | | - ShowViewModel<Accounts.AccountsViewModel>(); |
68 | | - if (isEnterprise) |
69 | | - ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id }); |
70 | | - else |
71 | | - ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account)); |
72 | | - } |
73 | | - catch (Exception e) |
74 | | - { |
| 72 | + ShowViewModel<Accounts.AccountsViewModel>(); |
| 73 | + if (isEnterprise) |
| 74 | + ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id }); |
| 75 | + else |
| 76 | + ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account)); |
| 77 | + } |
| 78 | + catch (Exception e) |
| 79 | + { |
75 | 80 | DisplayAlert(e.Message); |
76 | | - ShowViewModel<Accounts.AccountsViewModel>(); |
77 | | - } |
78 | | - finally |
79 | | - { |
80 | | - IsLoggingIn = false; |
81 | | - } |
| 81 | + ShowViewModel<Accounts.AccountsViewModel>(); |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + IsLoggingIn = false; |
| 86 | + } |
82 | 87 |
|
83 | | - } |
| 88 | + } |
84 | 89 | } |
85 | 90 | } |
86 | 91 |
|
0 commit comments