This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
/
MainModel.cs
126 lines (99 loc) · 3.31 KB
/
MainModel.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cimbalino.Phone.Toolkit.Services;
using Newtonsoft.Json;
using PedroLamas.GDrive.Service;
namespace PedroLamas.GDrive.Model
{
public class MainModel : IMainModel
{
private const string FILENAME = @"data.txt";
private readonly object _lock = new object();
private readonly IGoogleAuthService _googleAuthService;
private readonly IStorageService _storageService;
private readonly ISystemTrayService _systemTrayService;
private readonly GoogleDriveBreadcrumbs _breadcrumbs = new GoogleDriveBreadcrumbs();
#region Properties
public IList<AccountModel> AvailableAccounts { get; private set; }
public AccountModel CurrentAccount { get; set; }
public bool ExecuteInitialLoad { get; set; }
public GoogleDriveFile CurrentFolder
{
get
{
GoogleDriveFile folder;
return _breadcrumbs.TryPeek(out folder) ? folder : null;
}
}
public GoogleDriveFile SelectedFile { get; set; }
public string CurrentFolderId
{
get
{
GoogleDriveFile folder;
return _breadcrumbs.TryPeek(out folder) ? folder.Id : CurrentAccount.Info.RootFolderId;
}
}
public string CurrentPath
{
get
{
return _breadcrumbs.CurrentPath;
}
}
#endregion
public MainModel(IGoogleAuthService googleAuthService, IStorageService storageService, ISystemTrayService systemTrayService)
{
_googleAuthService = googleAuthService;
_storageService = storageService;
_systemTrayService = systemTrayService;
Load();
}
public async Task CheckToken(CancellationToken cancellationToken)
{
var authToken = CurrentAccount.AuthToken;
if (DateTime.Now > authToken.ExpiresDateTime)
{
_systemTrayService.SetProgressIndicator("Refreshing the access token...");
CurrentAccount.AuthToken = await _googleAuthService.RefreshToken(authToken, cancellationToken);
Save();
}
}
public void Clear()
{
_breadcrumbs.Clear();
}
public void Push(GoogleDriveFile item)
{
_breadcrumbs.Push(item);
}
public bool TryPeek(out GoogleDriveFile item)
{
return _breadcrumbs.TryPeek(out item);
}
public bool TryPop(out GoogleDriveFile item)
{
return _breadcrumbs.TryPop(out item);
}
public void Save()
{
lock (_lock)
{
_storageService.WriteAllText(FILENAME, JsonConvert.SerializeObject(AvailableAccounts));
}
}
private void Load()
{
if (_storageService.FileExists(FILENAME))
{
AvailableAccounts = JsonConvert.DeserializeObject<List<AccountModel>>(_storageService.ReadAllText(FILENAME));
}
else
{
AvailableAccounts = new List<AccountModel>();
}
}
}
}