This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
/
ViewFileViewModel.cs
202 lines (168 loc) · 5.67 KB
/
ViewFileViewModel.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
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PedroLamas.GDrive.Helpers;
using PedroLamas.GDrive.Model;
using PedroLamas.GDrive.Service;
namespace PedroLamas.GDrive.ViewModel
{
public class ViewFileViewModel : ViewModelBase
{
private readonly IMainModel _mainModel;
private readonly IGoogleDriveService _googleDriveService;
private readonly IMessageBoxService _messageBoxService;
private readonly ISystemTrayService _systemTrayService;
private readonly IMediaLibraryService _mediaLibraryService;
private CancellationTokenSource _cancellationTokenSource;
#region Properties
public string AccountName
{
get
{
return _mainModel.CurrentAccount.Name.ToUpperInvariant();
}
}
public string CurrentPath
{
get
{
return _mainModel.CurrentPath;
}
}
public string Filename
{
get
{
return _mainModel.SelectedFile.Title;
}
}
public string FileSize
{
get
{
if (_mainModel.SelectedFile.FileSize.HasValue)
{
return _mainModel.SelectedFile.FileSize.Value.FormatAsSize();
}
return "(unknown)";
}
}
public string FileModifiedDate
{
get
{
if (_mainModel.SelectedFile.ModifiedDate.HasValue)
{
return _mainModel.SelectedFile.ModifiedDate.Value.ToString(CultureInfo.CurrentUICulture);
}
return "(unknown)";
}
}
public Uri ThumbnailUri
{
get
{
try
{
if (!string.IsNullOrEmpty(_mainModel.SelectedFile.ThumbnailLink))
{
return new Uri(_mainModel.SelectedFile.ThumbnailLink, UriKind.Absolute);
}
}
catch
{
}
return null;
}
}
public bool HasThumbnail
{
get
{
return ThumbnailUri != null;
}
}
public bool CanDownload
{
get
{
switch (_mainModel.SelectedFile.MimeType)
{
case "image/jpeg":
case "image/png":
return true;
default:
return false;
}
}
}
public RelayCommand DownloadFileCommand { get; private set; }
public RelayCommand<CancelEventArgs> BackKeyPressCommand { get; private set; }
public bool IsBusy
{
get
{
return _systemTrayService.IsBusy;
}
}
#endregion
public ViewFileViewModel(IMainModel mainModel, IGoogleDriveService googleDriveService, IMessageBoxService messageBoxService, ISystemTrayService systemTrayService, IMediaLibraryService mediaLibraryService)
{
_mainModel = mainModel;
_googleDriveService = googleDriveService;
_messageBoxService = messageBoxService;
_systemTrayService = systemTrayService;
_mediaLibraryService = mediaLibraryService;
DownloadFileCommand = new RelayCommand(DownloadFile, () => !IsBusy && CanDownload);
BackKeyPressCommand = new RelayCommand<CancelEventArgs>(e =>
{
AbortCurrentCall(false);
});
}
private async void DownloadFile()
{
try
{
AbortCurrentCall();
await _mainModel.CheckToken(CancellationToken.None);
_systemTrayService.SetProgressIndicator("Downloading file...");
var fileData = await _googleDriveService.FileDownload(_mainModel.CurrentAccount.AuthToken, _mainModel.SelectedFile.DownloadUrl, CancellationToken.None);
var destinationFilename = string.Format("{0}_{1:ddMMyyyyHHmmss}{2}",
Path.GetFileNameWithoutExtension(_mainModel.SelectedFile.Title),
DateTime.Now,
Path.GetExtension(_mainModel.SelectedFile.Title));
_mediaLibraryService.SavePicture(destinationFilename, fileData);
_systemTrayService.HideProgressIndicator();
_messageBoxService.Show("Image file downloaded and saved to phone successfully!", "File Downloaded");
}
catch (TaskCanceledException)
{
_systemTrayService.HideProgressIndicator();
}
catch (Exception)
{
_systemTrayService.HideProgressIndicator();
_messageBoxService.Show("Unable to download file!", "Error");
}
}
private void AbortCurrentCall(bool createNew = true)
{
_systemTrayService.HideProgressIndicator();
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
if (createNew)
{
_cancellationTokenSource = new CancellationTokenSource();
}
}
}
}