Skip to content

Commit c00b908

Browse files
author
Luis
committed
Avoid show save file dialog when adding a GIF to gallery. Show actual saved file name in dialog.
1 parent 05246df commit c00b908

4 files changed

Lines changed: 88 additions & 31 deletions

File tree

Khernet.UI/Khernet.UI.Presentation/Controls/Dialogs/SaveFileDialogControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
VerticalAlignment="Center">
6363

6464
<!-- File name-->
65-
<TextBlock Text="{Binding FileChatMessage.FileName}"
65+
<TextBlock Text="{Binding NewFileName}"
6666
FontFamily="{StaticResource RobotoBoldFont}"
6767
Style="{StaticResource TextblockItemStyle}"
6868
Margin="0"

Khernet.UI/Khernet.UI.Presentation/ViewModels/Chat/AnimationChatMessageViewModel.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,22 @@ private async void SaveAnimation()
131131
string newFile = FilePath;
132132
try
133133
{
134-
string mp4Extension = "avi";
134+
string aviExtension = "avi";
135135

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

139139
if (File.Exists(newFile))
140140
newFile = FileHelper.GetNewFileName(newFile);
141141

142-
SaveFile(newFile);
142+
// This is a smaller video file than the original GIF
143+
// due to performance reasons
144+
if (!SaveFileWithShowProgress(newFile))
145+
{
146+
throw new Exception("Error while saving temporal video file.");
147+
}
143148

144-
//The height of row in GIF gallery
149+
// The height of row in GIF gallery
145150
int rowHeight = 100;
146151

147152
int newWidth = (int)Width;
@@ -152,19 +157,19 @@ private async void SaveAnimation()
152157
newWidth = (int)Width * rowHeight / (int)Height;
153158
}
154159

155-
//Get new height according to gallery row height
160+
// Get new height according to gallery row height
156161
int newHeight = Height < rowHeight ? (int)Height : rowHeight;
157162

158-
//Convert AVI video to MP4 format with a smaller size
163+
// Convert AVI video to MP4 format with a smaller size
159164
MediaHelper.ConvertTo(FilePath, newFile, newWidth, newHeight);
160165

161166
byte[] animation = null;
162167

163-
//Read the generated MP4 video file
168+
// Read the generated MP4 video file to reduce size of file
164169
using (FileStream fs = new FileStream(newFile, FileMode.Open, FileAccess.Read, FileShare.None))
165170
using (MemoryStream mem = new MemoryStream())
166171
{
167-
byte[] buffer = new byte[1024];
172+
byte[] buffer = new byte[8000];
168173

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

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

179-
//Get id file stored in database
184+
// Get id file stored in database
180185
string idFile = IoCContainer.Get<Messenger>().GetFileId(Id);
181186

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

185190
if (idAnimation > 0)
186191
IoCContainer.Get<ChatMessageListViewModel>().AddAnimationToGallery(idAnimation);
192+
193+
await IoCContainer.UI.ShowMessageBox(new MessageBoxViewModel
194+
{
195+
Message = "GIF added successfully to gallery",
196+
Title = "Khernet",
197+
ShowAcceptOption = true,
198+
AcceptOptionLabel = "OK",
199+
ShowCancelOption = false,
200+
});
187201
}
188202
catch (Exception error)
189203
{
@@ -195,7 +209,7 @@ await IoCContainer.UI.ShowMessageBox(new MessageBoxViewModel
195209
ShowAcceptOption = true,
196210
AcceptOptionLabel = "OK",
197211
ShowCancelOption = false,
198-
}); ;
212+
});
199213
}
200214
finally
201215
{

Khernet.UI/Khernet.UI.Presentation/ViewModels/Chat/FileMessageItemViewModel.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
using Khernet.Services.Messages;
33
using Khernet.UI.Files;
44
using Khernet.UI.IoC;
5-
using Khernet.UI.Managers;
65
using System;
76
using System.IO;
8-
using System.Threading.Tasks;
97
using System.Windows.Input;
108

119
namespace Khernet.UI
@@ -263,7 +261,7 @@ await applicationDialog.ShowMessageBox(new MessageBoxViewModel
263261
/// Save the file of this message to local system with a given destination path.
264262
/// </summary>
265263
/// <param name="fileName">The path where to save file to.</param>
266-
public async void SaveFile(string fileName)
264+
public async void SaveFile(string fileName, bool showProgress = true)
267265
{
268266
try
269267
{
@@ -274,8 +272,14 @@ public async void SaveFile(string fileName)
274272
{
275273
FileChatMessage = this,
276274
};
277-
_ = saveProgressDialog.Execute(fileName);
278-
await applicationDialog.ShowDialog(saveProgressDialog);
275+
276+
if (showProgress)
277+
{
278+
_ = saveProgressDialog.Execute(fileName);
279+
await applicationDialog.ShowDialog(saveProgressDialog);
280+
}
281+
else
282+
await saveProgressDialog.Execute(fileName);
279283
}
280284
catch (Exception error)
281285
{
@@ -292,21 +296,28 @@ await applicationDialog.ShowMessageBox(new MessageBoxViewModel
292296
}
293297

294298
/// <summary>
295-
/// Save this file message to local system.
299+
/// Saves a file without showing a progress dialog.
296300
/// </summary>
297-
/// <param name="fileName">The path where to save file to.</param>
298-
public Task SaveFileAsync(string fileName, IFileObserver fileObserver)
301+
/// <param name="fileName">The path of destination file.</param>
302+
/// <returns>True if file was saved successfully otherwise false.</returns>
303+
public bool SaveFileWithShowProgress(string fileName)
299304
{
300-
return Task.Run(() =>
305+
try
301306
{
302-
applicationDialog.ShowDialog(new SaveFileDialogViewModel
307+
if (fileName == null)
308+
return false;
309+
310+
SaveFileDialogViewModel saveProgressDialog = new SaveFileDialogViewModel
303311
{
304312
FileChatMessage = this,
305-
306-
});
307-
308-
309-
});
313+
};
314+
return saveProgressDialog.Execute(fileName).Result;
315+
}
316+
catch (Exception error)
317+
{
318+
LogDumper.WriteLog(error);
319+
return false;
320+
}
310321
}
311322

312323
/// <summary>

Khernet.UI/Khernet.UI.Presentation/ViewModels/Dialogs/SaveFileDialogViewModel.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ public class SaveFileDialogViewModel : BaseModel
7373
/// </summary>
7474
private bool isRunning;
7575

76+
/// <summary>
77+
/// Blocks the caller until save operation is complete.
78+
/// Results can be:
79+
/// - True: file saved successfully.
80+
/// - False: error while saving file, check <see cref="ErrorDescription"/> for details.
81+
/// </summary>
82+
TaskCompletionSource<bool> saveResult;
83+
84+
/// <summary>
85+
/// The new name of file that will be saved.
86+
/// </summary>
87+
private string newFileName;
88+
7689
public FileMessageItemViewModel FileChatMessage
7790
{
7891
get => fileChatMessage;
@@ -136,6 +149,18 @@ public bool IsRunning
136149
}
137150
}
138151
}
152+
public string NewFileName
153+
{
154+
get => newFileName;
155+
set
156+
{
157+
if (newFileName != value)
158+
{
159+
newFileName = value;
160+
OnPropertyChanged(nameof(NewFileName));
161+
}
162+
}
163+
}
139164

140165
#endregion
141166

@@ -155,6 +180,7 @@ public SaveFileDialogViewModel()
155180
OpenFolderCommand = new RelayCommand(OpenFolder);
156181
CancelSavingCommand = new RelayCommand(CancelSaving);
157182
Result = SaveFileResult.NoStarted;
183+
saveResult = new TaskCompletionSource<bool>();
158184
}
159185

160186
private void OpenFolder()
@@ -167,15 +193,17 @@ private void OpenFolder()
167193
/// </summary>
168194
/// <param name="destinationPath">The path where to save file to.</param>
169195
/// <returns>A <see cref="Task"/> instance.</returns>
170-
public Task Execute(string destinationPath)
196+
public Task<bool> Execute(string destinationPath)
171197
{
172-
return Task.Run(() =>
198+
199+
var t = Task.Run(() =>
173200
{
174201
IsRunning = true;
175202
if (destinationPath != null)
176203
{
177204
try
178205
{
206+
NewFileName = Path.GetFileName(destinationPath);
179207
using (Stream dtStream = IoCContainer.Get<Messenger>().DownloadLocalFile(fileChatMessage.Id))
180208
{
181209
int chunk = 1048576;
@@ -212,21 +240,25 @@ public Task Execute(string destinationPath)
212240
}
213241
}
214242

243+
IsRunning = false;
215244
if (cancelSaving)
216245
{
217246
File.Delete(destinationPath);
218247
ErrorDescription = "Save file was canceled";
219248
Result = SaveFileResult.Canceled;
249+
saveResult.TrySetResult(false);
220250
}
221251
else if (string.IsNullOrEmpty(ErrorDescription))
222252
{
223253
savedFilePath = destinationPath;
224254
ErrorDescription = "File saved successfully";
225255
Result = SaveFileResult.Saved;
256+
saveResult.TrySetResult(true);
226257
}
227-
IsRunning = false;
228258
});
229259

260+
return saveResult.Task;
261+
230262
}
231263

232264
/// <summary>

0 commit comments

Comments
 (0)