using Microsoft.Win32; using PDF_Master.Helper; using PDF_Master.Model.CloudDrive; using PDF_Master.Properties; using PDF_Master.ViewModels.HomePanel.CloudDrive.CloudDriveType; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Master.ViewModels.HomePanel.CloudDrive { //各云盘事件 public class CloudFilesContentViewModel : BindableBase { #region 云盘变量 public CloudDriveManager CloudManager => CloudDriveManager.CloudManager; private ObservableCollection _folderItems = new ObservableCollection(); public ObservableCollection FolderItems { get { return _folderItems; } set { SetProperty (ref _folderItems, value); } } private ObservableCollection _filesList = new ObservableCollection(); public ObservableCollection FilesList { get { return _filesList; } set { SetProperty(ref _filesList, value); } } private CloudType _currentCloudType = CloudType.GoogleDrive; public CloudType CurrentCloudType { get { return _currentCloudType; } set { SetProperty(ref _currentCloudType, value); } } #endregion #region 事件 /// /// 根据目录,更新文件列表 /// public DelegateCommand OpenFolderCommand { get; set; } /// /// 选中文件夹,更新指定文件夹里的文件列表 /// public DelegateCommand SelectedFolderCommand { get; set; } /// /// 下载并打开文件 /// public DelegateCommand OpenFileCommand { get; set; } /// /// 上传文件 /// public DelegateCommand UpLoadFileCommand { get; set; } public DelegateCommand LoginCommand { get; set; } public DelegateCommand RemoveUserCommand { get; set; } #endregion public CloudFilesContentViewModel() { InitVariable(); InitCommand(); } private void InitVariable() { } private void InitCommand() { OpenFolderCommand = new DelegateCommand(OpenFolder); SelectedFolderCommand = new DelegateCommand(SelectedFolder); OpenFileCommand = new DelegateCommand(OpenFile); UpLoadFileCommand = new DelegateCommand(UpLoadFile); LoginCommand = new DelegateCommand(LoginUser); RemoveUserCommand = new DelegateCommand(RemoveUser); } #region 云盘公用接口 #endregion #region 用户帐号 /// /// 登录 /// private async void LoginUser(CloudBoxItem cloudDriveItem) { if (cloudDriveItem != null) await CloudManager.LoginUser(cloudDriveItem.CloudDiskType); } /// /// 移除用户 /// private async void RemoveUser(UserBaseItem user) { if(user != null) { var result = await CloudManager.RemoveUser(user); if(result) { FilesList.Clear(); FolderItems.Clear(); } } } #endregion #region 文件 private async void OpenFolder(FileOperation fileOperation) { FilesList = await CloudManager.OpenFolder(fileOperation); AddFolder(fileOperation); } /// /// 在文件夹列表,选择文件夹 /// private void SelectedFolder(FolderBaseItem obj) { if (obj != null) { var objIndex = FolderItems.IndexOf(obj); var count = FolderItems.Count; if (objIndex == count - 1) return; if (obj.Level == -1) { for (int i = 1; i < count; i++) { FolderItems.RemoveAt(i); } } else { if (objIndex != -1) { for (int i = count - 1; i > objIndex; i--) { FolderItems.Remove(FolderItems[i]); } } } SelectedFolder(obj.Operation); } } /// /// 在文件列表点击文件夹,而更新文件列表 /// /// private async void SelectedFolder(FileOperation fileOperation) { FilesList = await CloudManager.OpenFolder(fileOperation); } private void AddFolder(FileOperation folder) { if (folder != null) { if (folder.DoFile != null) { FolderBaseItem folderItem = new FolderBaseItem(); folderItem.FolderName = folder.DoFile.Name; folderItem.Level = folder.DoFile.Level; folderItem.Operation = folder; FolderItems.Add(folderItem); } else { FolderItems.Clear(); FolderBaseItem folderItem = new FolderBaseItem(); folderItem.FolderName = "云文档"; folderItem.Level = -1; folderItem.Operation = folder; FolderItems.Add(folderItem); } } } /// /// 下载并打开文件 /// private async void OpenFile(FileOperation fileOperation) { await CloudManager.OpenFile(fileOperation); } /// /// 上传文件 /// private async void UpLoadFile(FileOperation fileOperation) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = Properties.Resources.OpenDialogFilter; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { if (string.IsNullOrEmpty(openFileDialog.FileName) == false) { fileOperation.SourceFile = openFileDialog.FileName; fileOperation.SourceFileName = openFileDialog.SafeFileName; var result =await CloudManager.UpLoadFile(fileOperation); if (result) { FilesList = await CloudManager.OpenFolder(fileOperation); MessageBox.Show("文件上传完成"); } else { MessageBox.Show("文件上传失败"); } } } } #endregion } }