CloudFilesContentViewModel.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using Microsoft.Win32;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model.CloudDrive;
  4. using PDF_Master.Properties;
  5. using PDF_Master.ViewModels.HomePanel.CloudDrive.CloudDriveType;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace PDF_Master.ViewModels.HomePanel.CloudDrive
  16. {
  17. //各云盘事件
  18. public class CloudFilesContentViewModel : BindableBase
  19. {
  20. #region 云盘变量
  21. public CloudDriveManager CloudManager => CloudDriveManager.CloudManager;
  22. private ObservableCollection<FolderBaseItem> _folderItems = new ObservableCollection<FolderBaseItem>();
  23. public ObservableCollection<FolderBaseItem> FolderItems
  24. {
  25. get { return _folderItems; }
  26. set { SetProperty (ref _folderItems, value); }
  27. }
  28. private ObservableCollection<FilesBaseItem> _filesList = new ObservableCollection<FilesBaseItem>();
  29. public ObservableCollection<FilesBaseItem> FilesList
  30. {
  31. get { return _filesList; }
  32. set { SetProperty(ref _filesList, value); }
  33. }
  34. private CloudType _currentCloudType = CloudType.GoogleDrive;
  35. public CloudType CurrentCloudType
  36. {
  37. get { return _currentCloudType; }
  38. set { SetProperty(ref _currentCloudType, value); }
  39. }
  40. #endregion
  41. #region 事件
  42. /// <summary>
  43. /// 根据目录,更新文件列表
  44. /// </summary>
  45. public DelegateCommand<FileOperation> OpenFolderCommand { get; set; }
  46. /// <summary>
  47. /// 选中文件夹,更新指定文件夹里的文件列表
  48. /// </summary>
  49. public DelegateCommand<FolderBaseItem> SelectedFolderCommand { get; set; }
  50. /// <summary>
  51. /// 下载并打开文件
  52. /// </summary>
  53. public DelegateCommand<FileOperation> OpenFileCommand { get; set; }
  54. /// <summary>
  55. /// 上传文件
  56. /// </summary>
  57. public DelegateCommand<FileOperation> UpLoadFileCommand { get; set; }
  58. public DelegateCommand<CloudBoxItem> LoginCommand { get; set; }
  59. public DelegateCommand<UserBaseItem> RemoveUserCommand { get; set; }
  60. #endregion
  61. public CloudFilesContentViewModel()
  62. {
  63. InitVariable();
  64. InitCommand();
  65. }
  66. private void InitVariable()
  67. {
  68. }
  69. private void InitCommand()
  70. {
  71. OpenFolderCommand = new DelegateCommand<FileOperation>(OpenFolder);
  72. SelectedFolderCommand = new DelegateCommand<FolderBaseItem>(SelectedFolder);
  73. OpenFileCommand = new DelegateCommand<FileOperation>(OpenFile);
  74. UpLoadFileCommand = new DelegateCommand<FileOperation>(UpLoadFile);
  75. LoginCommand = new DelegateCommand<CloudBoxItem>(LoginUser);
  76. RemoveUserCommand = new DelegateCommand<UserBaseItem>(RemoveUser);
  77. }
  78. #region 云盘公用接口
  79. #endregion
  80. #region 用户帐号
  81. /// <summary>
  82. /// 登录
  83. /// </summary>
  84. private async void LoginUser(CloudBoxItem cloudDriveItem)
  85. {
  86. if (cloudDriveItem != null)
  87. await CloudManager.LoginUser(cloudDriveItem.CloudDiskType);
  88. }
  89. /// <summary>
  90. /// 移除用户
  91. /// </summary>
  92. private async void RemoveUser(UserBaseItem user)
  93. {
  94. if(user != null)
  95. {
  96. var result = await CloudManager.RemoveUser(user);
  97. if(result)
  98. {
  99. FilesList.Clear();
  100. FolderItems.Clear();
  101. }
  102. }
  103. }
  104. #endregion
  105. #region 文件
  106. private async void OpenFolder(FileOperation fileOperation)
  107. {
  108. FilesList = await CloudManager.OpenFolder(fileOperation);
  109. AddFolder(fileOperation);
  110. }
  111. /// <summary>
  112. /// 在文件夹列表,选择文件夹
  113. /// </summary>
  114. private void SelectedFolder(FolderBaseItem obj)
  115. {
  116. if (obj != null)
  117. {
  118. var objIndex = FolderItems.IndexOf(obj);
  119. var count = FolderItems.Count;
  120. if (objIndex == count - 1)
  121. return;
  122. if (obj.Level == -1)
  123. {
  124. for (int i = 1; i < count; i++)
  125. {
  126. FolderItems.RemoveAt(i);
  127. }
  128. }
  129. else
  130. {
  131. if (objIndex != -1)
  132. {
  133. for (int i = count - 1; i > objIndex; i--)
  134. {
  135. FolderItems.Remove(FolderItems[i]);
  136. }
  137. }
  138. }
  139. SelectedFolder(obj.Operation);
  140. }
  141. }
  142. /// <summary>
  143. /// 在文件列表点击文件夹,而更新文件列表
  144. /// </summary>
  145. /// <param name="fileOperation"></param>
  146. private async void SelectedFolder(FileOperation fileOperation)
  147. {
  148. FilesList = await CloudManager.OpenFolder(fileOperation);
  149. }
  150. private void AddFolder(FileOperation folder)
  151. {
  152. if (folder != null)
  153. {
  154. if (folder.DoFile != null)
  155. {
  156. FolderBaseItem folderItem = new FolderBaseItem();
  157. folderItem.FolderName = folder.DoFile.Name;
  158. folderItem.Level = folder.DoFile.Level;
  159. folderItem.Operation = folder;
  160. FolderItems.Add(folderItem);
  161. }
  162. else
  163. {
  164. FolderItems.Clear();
  165. FolderBaseItem folderItem = new FolderBaseItem();
  166. folderItem.FolderName = "云文档";
  167. folderItem.Level = -1;
  168. folderItem.Operation = folder;
  169. FolderItems.Add(folderItem);
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 下载并打开文件
  175. /// </summary>
  176. private async void OpenFile(FileOperation fileOperation)
  177. {
  178. await CloudManager.OpenFile(fileOperation);
  179. }
  180. /// <summary>
  181. /// 上传文件
  182. /// </summary>
  183. private async void UpLoadFile(FileOperation fileOperation)
  184. {
  185. OpenFileDialog openFileDialog = new OpenFileDialog();
  186. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  187. openFileDialog.Multiselect = true;
  188. if ((bool)openFileDialog.ShowDialog())
  189. {
  190. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  191. {
  192. fileOperation.SourceFile = openFileDialog.FileName;
  193. fileOperation.SourceFileName = openFileDialog.SafeFileName;
  194. var result = await CloudManager.UpLoadFile(fileOperation);
  195. if (result)
  196. {
  197. FilesList = await CloudManager.OpenFolder(fileOperation);
  198. MessageBox.Show("文件上传完成");
  199. }
  200. else
  201. {
  202. MessageBox.Show("文件上传失败");
  203. }
  204. }
  205. }
  206. }
  207. #endregion
  208. }
  209. }