diff --git a/Khernet.UI/Khernet.UI.Presentation/Controls/Dialogs/SaveFileDialogControl.xaml b/Khernet.UI/Khernet.UI.Presentation/Controls/Dialogs/SaveFileDialogControl.xaml index ee43dd3..fb60d62 100644 --- a/Khernet.UI/Khernet.UI.Presentation/Controls/Dialogs/SaveFileDialogControl.xaml +++ b/Khernet.UI/Khernet.UI.Presentation/Controls/Dialogs/SaveFileDialogControl.xaml @@ -62,7 +62,7 @@ VerticalAlignment="Center"> - ().GetFileId(Id); - //Save MP4 video file to database + // Save MP4 video file to database int idAnimation = IoCContainer.Get().SaveAnimation(Id, idFile, newWidth, newHeight, animation); if (idAnimation > 0) IoCContainer.Get().AddAnimationToGallery(idAnimation); + + await IoCContainer.UI.ShowMessageBox(new MessageBoxViewModel + { + Message = "GIF added successfully to gallery", + Title = "Khernet", + ShowAcceptOption = true, + AcceptOptionLabel = "OK", + ShowCancelOption = false, + }); } catch (Exception error) { @@ -195,7 +209,7 @@ await IoCContainer.UI.ShowMessageBox(new MessageBoxViewModel ShowAcceptOption = true, AcceptOptionLabel = "OK", ShowCancelOption = false, - }); ; + }); } finally { diff --git a/Khernet.UI/Khernet.UI.Presentation/ViewModels/Chat/FileMessageItemViewModel.cs b/Khernet.UI/Khernet.UI.Presentation/ViewModels/Chat/FileMessageItemViewModel.cs index 6c96df9..6ce16c8 100644 --- a/Khernet.UI/Khernet.UI.Presentation/ViewModels/Chat/FileMessageItemViewModel.cs +++ b/Khernet.UI/Khernet.UI.Presentation/ViewModels/Chat/FileMessageItemViewModel.cs @@ -2,10 +2,8 @@ using Khernet.Services.Messages; using Khernet.UI.Files; using Khernet.UI.IoC; -using Khernet.UI.Managers; using System; using System.IO; -using System.Threading.Tasks; using System.Windows.Input; namespace Khernet.UI @@ -263,7 +261,7 @@ await applicationDialog.ShowMessageBox(new MessageBoxViewModel /// Save the file of this message to local system with a given destination path. /// /// The path where to save file to. - public async void SaveFile(string fileName) + public async void SaveFile(string fileName, bool showProgress = true) { try { @@ -274,8 +272,14 @@ public async void SaveFile(string fileName) { FileChatMessage = this, }; - _ = saveProgressDialog.Execute(fileName); - await applicationDialog.ShowDialog(saveProgressDialog); + + if (showProgress) + { + _ = saveProgressDialog.Execute(fileName); + await applicationDialog.ShowDialog(saveProgressDialog); + } + else + await saveProgressDialog.Execute(fileName); } catch (Exception error) { @@ -292,21 +296,28 @@ await applicationDialog.ShowMessageBox(new MessageBoxViewModel } /// - /// Save this file message to local system. + /// Saves a file without showing a progress dialog. /// - /// The path where to save file to. - public Task SaveFileAsync(string fileName, IFileObserver fileObserver) + /// The path of destination file. + /// True if file was saved successfully otherwise false. + public bool SaveFileWithShowProgress(string fileName) { - return Task.Run(() => + try { - applicationDialog.ShowDialog(new SaveFileDialogViewModel + if (fileName == null) + return false; + + SaveFileDialogViewModel saveProgressDialog = new SaveFileDialogViewModel { FileChatMessage = this, - - }); - - - }); + }; + return saveProgressDialog.Execute(fileName).Result; + } + catch (Exception error) + { + LogDumper.WriteLog(error); + return false; + } } /// diff --git a/Khernet.UI/Khernet.UI.Presentation/ViewModels/Dialogs/SaveFileDialogViewModel.cs b/Khernet.UI/Khernet.UI.Presentation/ViewModels/Dialogs/SaveFileDialogViewModel.cs index 403d97e..c6c45ff 100644 --- a/Khernet.UI/Khernet.UI.Presentation/ViewModels/Dialogs/SaveFileDialogViewModel.cs +++ b/Khernet.UI/Khernet.UI.Presentation/ViewModels/Dialogs/SaveFileDialogViewModel.cs @@ -73,6 +73,19 @@ public class SaveFileDialogViewModel : BaseModel /// private bool isRunning; + /// + /// Blocks the caller until save operation is complete. + /// Results can be: + /// - True: file saved successfully. + /// - False: error while saving file, check for details. + /// + TaskCompletionSource saveResult; + + /// + /// The new name of file that will be saved. + /// + private string newFileName; + public FileMessageItemViewModel FileChatMessage { get => fileChatMessage; @@ -136,6 +149,18 @@ public bool IsRunning } } } + public string NewFileName + { + get => newFileName; + set + { + if (newFileName != value) + { + newFileName = value; + OnPropertyChanged(nameof(NewFileName)); + } + } + } #endregion @@ -155,6 +180,7 @@ public SaveFileDialogViewModel() OpenFolderCommand = new RelayCommand(OpenFolder); CancelSavingCommand = new RelayCommand(CancelSaving); Result = SaveFileResult.NoStarted; + saveResult = new TaskCompletionSource(); } private void OpenFolder() @@ -167,15 +193,17 @@ private void OpenFolder() /// /// The path where to save file to. /// A instance. - public Task Execute(string destinationPath) + public Task Execute(string destinationPath) { - return Task.Run(() => + + var t = Task.Run(() => { IsRunning = true; if (destinationPath != null) { try { + NewFileName = Path.GetFileName(destinationPath); using (Stream dtStream = IoCContainer.Get().DownloadLocalFile(fileChatMessage.Id)) { int chunk = 1048576; @@ -212,21 +240,25 @@ public Task Execute(string destinationPath) } } + IsRunning = false; if (cancelSaving) { File.Delete(destinationPath); ErrorDescription = "Save file was canceled"; Result = SaveFileResult.Canceled; + saveResult.TrySetResult(false); } else if (string.IsNullOrEmpty(ErrorDescription)) { savedFilePath = destinationPath; ErrorDescription = "File saved successfully"; Result = SaveFileResult.Saved; + saveResult.TrySetResult(true); } - IsRunning = false; }); + return saveResult.Task; + } ///