129 lines
3.8 KiB
C#
129 lines
3.8 KiB
C#
using salesbook.Maui.Core.Interface;
|
|
using salesbook.Shared.Core.Dto;
|
|
using salesbook.Shared.Core.Interface;
|
|
|
|
namespace salesbook.Maui.Core.Services;
|
|
|
|
public class AttachedService(IFilePreviewService filePreviewService) : IAttachedService
|
|
{
|
|
public async Task<AttachedDTO?> SelectImageFromCamera()
|
|
{
|
|
var cameraPerm = await Permissions.RequestAsync<Permissions.Camera>();
|
|
var storagePerm = await Permissions.RequestAsync<Permissions.StorageWrite>();
|
|
|
|
if (cameraPerm != PermissionStatus.Granted || storagePerm != PermissionStatus.Granted)
|
|
return null;
|
|
|
|
FileResult? result = null;
|
|
|
|
try
|
|
{
|
|
result = await MediaPicker.Default.CapturePhotoAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Errore cattura foto: {ex.Message}");
|
|
SentrySdk.CaptureException(ex);
|
|
return null;
|
|
}
|
|
|
|
return result is null ? null : await ConvertToDto(result, AttachedDTO.TypeAttached.Image);
|
|
}
|
|
|
|
public async Task<AttachedDTO?> SelectImageFromGallery()
|
|
{
|
|
var storagePerm = await Permissions.RequestAsync<Permissions.StorageRead>();
|
|
if (storagePerm != PermissionStatus.Granted)
|
|
return null;
|
|
|
|
FileResult? result = null;
|
|
|
|
try
|
|
{
|
|
result = await MediaPicker.Default.PickPhotoAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Errore selezione galleria: {ex.Message}");
|
|
SentrySdk.CaptureException(ex);
|
|
return null;
|
|
}
|
|
|
|
return result is null ? null : await ConvertToDto(result, AttachedDTO.TypeAttached.Image);
|
|
}
|
|
|
|
public async Task<AttachedDTO?> SelectFile()
|
|
{
|
|
var perm = await Permissions.RequestAsync<Permissions.StorageRead>();
|
|
if (perm != PermissionStatus.Granted) return null;
|
|
|
|
var result = await FilePicker.PickAsync();
|
|
|
|
return result is null ? null : await ConvertToDto(result, AttachedDTO.TypeAttached.Document);
|
|
}
|
|
|
|
public async Task<AttachedDTO?> SelectPosition()
|
|
{
|
|
var perm = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
|
|
if (perm != PermissionStatus.Granted) return null;
|
|
|
|
var loc = await Geolocation.GetLastKnownLocationAsync();
|
|
if (loc is null) return null;
|
|
|
|
return new AttachedDTO
|
|
{
|
|
Name = "Posizione attuale",
|
|
Lat = loc.Latitude,
|
|
Lng = loc.Longitude,
|
|
Type = AttachedDTO.TypeAttached.Position
|
|
};
|
|
}
|
|
|
|
private static async Task<AttachedDTO> ConvertToDto(FileResult file, AttachedDTO.TypeAttached type)
|
|
{
|
|
var stream = await file.OpenReadAsync();
|
|
using var ms = new MemoryStream();
|
|
await stream.CopyToAsync(ms);
|
|
|
|
return new AttachedDTO
|
|
{
|
|
Name = file.FileName,
|
|
Path = file.FullPath,
|
|
MimeType = file.ContentType,
|
|
DimensionBytes = ms.Length,
|
|
FileBytes = ms.ToArray(),
|
|
Type = type
|
|
};
|
|
}
|
|
|
|
public async Task<string> SaveToTempStorage(Stream file, string fileName, CancellationToken ct = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(file);
|
|
|
|
if (file.CanSeek)
|
|
file.Position = 0;
|
|
|
|
fileName = Path.GetFileName(fileName);
|
|
|
|
var dir = FileSystem.CacheDirectory;
|
|
var filePath = Path.Combine(dir, fileName);
|
|
|
|
await using var fileStream = File.Create(filePath);
|
|
await file.CopyToAsync(fileStream, ct);
|
|
|
|
return filePath;
|
|
}
|
|
|
|
public Task OpenFile(string fileName, string filePath)
|
|
{
|
|
#if IOS
|
|
return filePreviewService.Preview(fileName, filePath);
|
|
#else
|
|
return Launcher.OpenAsync(new OpenFileRequest
|
|
{
|
|
Title = "Apri file",
|
|
File = new ReadOnlyFile(filePath)
|
|
});
|
|
#endif
|
|
}
|
|
} |