private void LoadItems() { ShowLoadingAnimation = true; var items = new List <AssetEntitySearchViewModelBase>(); var worker = new BackgroundWorker(); worker.DoWork += (o, ea) => { if (!string.IsNullOrEmpty(ParentItem.InnerItemID) && ParentItem.InnerItemID != RootItemId) { items.Add(new RootSearchViewModel(ParentItem.Parent)); } switch (ParentItem.Type) { case AssetType.Folder: case AssetType.Container: items.AddRange( _assetRepository.GetChildrenFolders(ParentItem.InnerItemID) .Select(x => new FolderSearchViewModel(x, ParentItem))); if (ParentItem.InnerItemID != null) { items.AddRange( _assetRepository.GetChildrenFolderItems(ParentItem.InnerItemID) .Select(x => new FileSearchViewModel(x))); } break; case AssetType.Parent: items.AddRange( _assetRepository.GetChildrenFolders(ParentItem.InnerItemID) .Select(x => new FolderSearchViewModel(x, ParentItem))); break; } OnUIThread(() => { SelectedFolderItems.SetItems(items); UpdateAddressBar(); }); }; worker.RunWorkerCompleted += (o, ea) => { ShowLoadingAnimation = false; }; worker.RunWorkerAsync(); }
private void RaiseUploadRequest() { if (ParentItem.Parent == null) { CommonNotifyRequest.Raise(new Notification { Content = "Can not upload files to the root. Please select a folder first.".Localize(), Title = "Error".Localize(null, LocalizationScope.DefaultCategory) }); return; } IEnumerable <FileType> fileTypes = new[] { new FileType("all files".Localize(), ".*"), new FileType("jpg image".Localize(), ".jpg"), new FileType("bmp image".Localize(), ".bmp"), new FileType("png image".Localize(), ".png"), new FileType("Report".Localize(), ".rld"), new FileType("Report".Localize(), ".rldc") }; if (fileDialogService == null) { fileDialogService = new System.Waf.VirtoCommerce.ManagementClient.Services.FileDialogService(); } var result = fileDialogService.ShowOpenFileDialog(this, fileTypes); if (result.IsValid) { var delimiter = !string.IsNullOrEmpty(ParentItem.InnerItemID) && !ParentItem.InnerItemID.EndsWith(NamePathDelimiter) ? NamePathDelimiter : string.Empty; // construct new FolderItemId var fileInfo = new FileInfo(result.FileName); var fileName = string.Format("{0}{1}{2}", ParentItem.InnerItemID, delimiter, fileInfo.Name); var canUpload = true; var fileExists = SelectedFolderItems.OfType <IFileSearchViewModel>().Any(x => x.InnerItem.FolderItemId.EndsWith(NamePathDelimiter + fileInfo.Name, StringComparison.OrdinalIgnoreCase)); if (fileExists) { CommonConfirmRequest.Raise(new ConditionalConfirmation { Title = "Upload file".Localize(), Content = string.Format("There is already a file with the same name in this location.\nDo you want to overwrite and replace the existing file '{0}'?".Localize(), fileInfo.Name) }, (x) => { canUpload = x.Confirmed; }); } if (canUpload) { ShowLoadingAnimation = true; var worker = new BackgroundWorker(); worker.DoWork += (o, ea) => { var id = o.GetHashCode().ToString(); var item = new StatusMessage { ShortText = "File upload in progress".Localize(), StatusMessageId = id }; EventSystem.Publish(item); using (var info = new UploadStreamInfo()) using (var fileStream = new FileStream(result.FileName, FileMode.Open, FileAccess.Read)) { info.FileName = fileName; info.FileByteStream = fileStream; info.Length = fileStream.Length; _assetRepository.Upload(info); } }; worker.RunWorkerCompleted += (o, ea) => { ShowLoadingAnimation = false; var item = new StatusMessage { StatusMessageId = o.GetHashCode().ToString() }; if (ea.Cancelled) { item.ShortText = "File upload was canceled!".Localize(); item.State = StatusMessageState.Warning; } else if (ea.Error != null) { item.ShortText = string.Format("Failed to upload file: {0}".Localize(), ea.Error.Message); item.Details = ea.Error.ToString(); item.State = StatusMessageState.Error; } else { item.ShortText = "File uploaded".Localize(); item.State = StatusMessageState.Success; RefreshCommand.Execute(); } EventSystem.Publish(item); }; worker.RunWorkerAsync(); } } }