This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
/
RenameFileViewModel.cs
123 lines (101 loc) · 3.56 KB
/
RenameFileViewModel.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
using System;
using System.Threading;
using System.Threading.Tasks;
using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PedroLamas.GDrive.Model;
using PedroLamas.GDrive.Service;
namespace PedroLamas.GDrive.ViewModel
{
public class RenameFileViewModel : ViewModelBase
{
private const string GoogleDriveFileFields = "description,etag,fileSize,id,labels,mimeType,modifiedDate,title";
private readonly IMainModel _mainModel;
private readonly INavigationService _navigationService;
private readonly IGoogleDriveService _googleDriveService;
private readonly ISystemTrayService _systemTrayService;
private readonly IMessageBoxService _messageBoxService;
private string _fileName;
#region Properties
public string AccountName
{
get
{
return _mainModel.CurrentAccount.Name.ToUpperInvariant();
}
}
public string CurrentPath
{
get
{
return _mainModel.CurrentPath;
}
}
public string FileName
{
get
{
return _fileName;
}
set
{
if (_fileName == value)
return;
_fileName = value;
RaisePropertyChanged(() => FileName);
}
}
public RelayCommand RenameFileCommand { get; private set; }
public RelayCommand PageLoadedCommand { get; private set; }
public bool IsBusy
{
get
{
return _systemTrayService.IsBusy;
}
}
#endregion
public RenameFileViewModel(IMainModel mainModel, INavigationService navigationService, IGoogleDriveService googleDriveService, ISystemTrayService systemTrayService, IMessageBoxService messageBoxService)
{
_mainModel = mainModel;
_navigationService = navigationService;
_googleDriveService = googleDriveService;
_systemTrayService = systemTrayService;
_messageBoxService = messageBoxService;
RenameFileCommand = new RelayCommand(RenameFile, () => !IsBusy);
PageLoadedCommand = new RelayCommand(() =>
{
FileName = _mainModel.SelectedFile.Title;
});
}
private async void RenameFile()
{
try
{
await _mainModel.CheckToken(CancellationToken.None);
_systemTrayService.SetProgressIndicator("Renaming the file...");
var file = await _googleDriveService.FilesUpdate(_mainModel.CurrentAccount.AuthToken, _mainModel.SelectedFile.Id, new GoogleDriveFilesUpdateRequest()
{
File = new GoogleDriveFile()
{
Title = FileName
},
Fields = GoogleDriveFileFields
}, CancellationToken.None);
_mainModel.SelectedFile = null;
MessengerInstance.Send(new RefreshFilesMessage());
_navigationService.GoBack();
}
catch (TaskCanceledException)
{
_systemTrayService.HideProgressIndicator();
}
catch (Exception)
{
_systemTrayService.HideProgressIndicator();
_messageBoxService.Show("Unable to rename the file!", "Error");
}
}
}
}