New attachment templates that support saving and opening attachment when composing message.

This commit is contained in:
Burak Kaan Köse
2024-11-27 19:49:10 +01:00
parent e586145f50
commit 96c98a6987
12 changed files with 196 additions and 62 deletions

View File

@@ -96,6 +96,7 @@ namespace Wino.Mail.ViewModels
private readonly IMailDialogService _dialogService;
private readonly IMailService _mailService;
private readonly IMimeFileService _mimeFileService;
private readonly IFileService _fileService;
private readonly IFolderService _folderService;
private readonly IAccountService _accountService;
private readonly IWinoRequestDelegator _worker;
@@ -107,6 +108,7 @@ namespace Wino.Mail.ViewModels
public ComposePageViewModel(IMailDialogService dialogService,
IMailService mailService,
IMimeFileService mimeFileService,
IFileService fileService,
INativeAppService nativeAppService,
IFolderService folderService,
IAccountService accountService,
@@ -125,11 +127,58 @@ namespace Wino.Mail.ViewModels
_dialogService = dialogService;
_mailService = mailService;
_mimeFileService = mimeFileService;
_fileService = fileService;
_accountService = accountService;
_worker = worker;
_winoServerConnectionManager = winoServerConnectionManager;
}
[RelayCommand]
private async Task OpenAttachmentAsync(MailAttachmentViewModel attachmentViewModel)
{
if (string.IsNullOrEmpty(attachmentViewModel.FilePath)) return;
try
{
await NativeAppService.LaunchFileAsync(attachmentViewModel.FilePath);
}
catch (Exception ex)
{
_dialogService.InfoBarMessage(Translator.Info_FailedToOpenFileTitle, Translator.Info_FailedToOpenFileMessage, InfoBarMessageType.Error);
}
}
[RelayCommand]
private async Task SaveAttachmentAsync(MailAttachmentViewModel attachmentViewModel)
{
if (attachmentViewModel.Content == null) return;
var pickedFilePath = await _dialogService.PickFilePathAsync(attachmentViewModel.FileName);
if (string.IsNullOrWhiteSpace(pickedFilePath)) return;
try
{
await _fileService.CopyFileAsync(attachmentViewModel.FilePath, pickedFilePath);
}
catch (Exception ex)
{
_dialogService.InfoBarMessage(Translator.Info_FailedToOpenFileTitle, Translator.Info_FailedToOpenFileMessage, InfoBarMessageType.Error);
}
}
[RelayCommand]
private async Task AttachFilesAsync()
{
var pickedFiles = await _dialogService.PickFilesAsync("*");
if (pickedFiles?.Count == 0) return;
foreach (var file in pickedFiles)
{
var attachmentViewModel = new MailAttachmentViewModel(file);
IncludedAttachments.Add(attachmentViewModel);
}
}
[RelayCommand]
private void RemoveAttachment(MailAttachmentViewModel attachmentViewModel)
=> IncludedAttachments.Remove(attachmentViewModel);

View File

@@ -2,6 +2,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using MimeKit;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Models.Common;
using Wino.Core.Extensions;
namespace Wino.Mail.ViewModels.Data
@@ -41,14 +42,14 @@ namespace Wino.Mail.ViewModels.Data
AttachmentType = GetAttachmentType(extension);
}
public MailAttachmentViewModel(string fullFilePath, byte[] content)
public MailAttachmentViewModel(SharedFile sharedFile)
{
Content = content;
Content = sharedFile.Data;
FileName = Path.GetFileName(fullFilePath);
FilePath = fullFilePath;
FileName = sharedFile.FileName;
FilePath = sharedFile.FullFilePath;
ReadableSize = ((long)content.Length).GetBytesReadable();
ReadableSize = ((long)sharedFile.Data.Length).GetBytesReadable();
var extension = Path.GetExtension(FileName);
AttachmentType = GetAttachmentType(extension);