Skip to content

Commit

Permalink
Avoid show save file dialog when adding a GIF to gallery. Show actual…
Browse files Browse the repository at this point in the history
… saved file name in dialog.
  • Loading branch information
lemalcs committed Mar 27, 2022
1 parent 05246df commit c00b908
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
VerticalAlignment="Center">

<!-- File name-->
<TextBlock Text="{Binding FileChatMessage.FileName}"
<TextBlock Text="{Binding NewFileName}"
FontFamily="{StaticResource RobotoBoldFont}"
Style="{StaticResource TextblockItemStyle}"
Margin="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,22 @@ private async void SaveAnimation()
string newFile = FilePath;
try
{
string mp4Extension = "avi";
string aviExtension = "avi";

//Get a new file name with MP4 extension
newFile = FileHelper.GetFileNameWithExtension(FilePath, mp4Extension);
// Get a new file name with MP4 extension
newFile = FileHelper.GetFileNameWithExtension(FilePath, aviExtension);

if (File.Exists(newFile))
newFile = FileHelper.GetNewFileName(newFile);

SaveFile(newFile);
// This is a smaller video file than the original GIF
// due to performance reasons
if (!SaveFileWithShowProgress(newFile))
{
throw new Exception("Error while saving temporal video file.");
}

//The height of row in GIF gallery
// The height of row in GIF gallery
int rowHeight = 100;

int newWidth = (int)Width;
Expand All @@ -152,19 +157,19 @@ private async void SaveAnimation()
newWidth = (int)Width * rowHeight / (int)Height;
}

//Get new height according to gallery row height
// Get new height according to gallery row height
int newHeight = Height < rowHeight ? (int)Height : rowHeight;

//Convert AVI video to MP4 format with a smaller size
// Convert AVI video to MP4 format with a smaller size
MediaHelper.ConvertTo(FilePath, newFile, newWidth, newHeight);

byte[] animation = null;

//Read the generated MP4 video file
// Read the generated MP4 video file to reduce size of file
using (FileStream fs = new FileStream(newFile, FileMode.Open, FileAccess.Read, FileShare.None))
using (MemoryStream mem = new MemoryStream())
{
byte[] buffer = new byte[1024];
byte[] buffer = new byte[8000];

int readBytes = fs.Read(buffer, 0, buffer.Length);

Expand All @@ -176,14 +181,23 @@ private async void SaveAnimation()
animation = mem.ToArray();
}

//Get id file stored in database
// Get id file stored in database
string idFile = IoCContainer.Get<Messenger>().GetFileId(Id);

//Save MP4 video file to database
// Save MP4 video file to database
int idAnimation = IoCContainer.Get<Messenger>().SaveAnimation(Id, idFile, newWidth, newHeight, animation);

if (idAnimation > 0)
IoCContainer.Get<ChatMessageListViewModel>().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)
{
Expand All @@ -195,7 +209,7 @@ await IoCContainer.UI.ShowMessageBox(new MessageBoxViewModel
ShowAcceptOption = true,
AcceptOptionLabel = "OK",
ShowCancelOption = false,
}); ;
});
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -263,7 +261,7 @@ await applicationDialog.ShowMessageBox(new MessageBoxViewModel
/// Save the file of this message to local system with a given destination path.
/// </summary>
/// <param name="fileName">The path where to save file to.</param>
public async void SaveFile(string fileName)
public async void SaveFile(string fileName, bool showProgress = true)
{
try
{
Expand All @@ -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)
{
Expand All @@ -292,21 +296,28 @@ await applicationDialog.ShowMessageBox(new MessageBoxViewModel
}

/// <summary>
/// Save this file message to local system.
/// Saves a file without showing a progress dialog.
/// </summary>
/// <param name="fileName">The path where to save file to.</param>
public Task SaveFileAsync(string fileName, IFileObserver fileObserver)
/// <param name="fileName">The path of destination file.</param>
/// <returns>True if file was saved successfully otherwise false.</returns>
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;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public class SaveFileDialogViewModel : BaseModel
/// </summary>
private bool isRunning;

/// <summary>
/// Blocks the caller until save operation is complete.
/// Results can be:
/// - True: file saved successfully.
/// - False: error while saving file, check <see cref="ErrorDescription"/> for details.
/// </summary>
TaskCompletionSource<bool> saveResult;

/// <summary>
/// The new name of file that will be saved.
/// </summary>
private string newFileName;

public FileMessageItemViewModel FileChatMessage
{
get => fileChatMessage;
Expand Down Expand Up @@ -136,6 +149,18 @@ public bool IsRunning
}
}
}
public string NewFileName
{
get => newFileName;
set
{
if (newFileName != value)
{
newFileName = value;
OnPropertyChanged(nameof(NewFileName));
}
}
}

#endregion

Expand All @@ -155,6 +180,7 @@ public SaveFileDialogViewModel()
OpenFolderCommand = new RelayCommand(OpenFolder);
CancelSavingCommand = new RelayCommand(CancelSaving);
Result = SaveFileResult.NoStarted;
saveResult = new TaskCompletionSource<bool>();
}

private void OpenFolder()
Expand All @@ -167,15 +193,17 @@ private void OpenFolder()
/// </summary>
/// <param name="destinationPath">The path where to save file to.</param>
/// <returns>A <see cref="Task"/> instance.</returns>
public Task Execute(string destinationPath)
public Task<bool> 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<Messenger>().DownloadLocalFile(fileChatMessage.Id))
{
int chunk = 1048576;
Expand Down Expand Up @@ -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;

}

/// <summary>
Expand Down

0 comments on commit c00b908

Please sign in to comment.