Gestita fotocamera per allegare immagini
This commit is contained in:
@@ -5,45 +5,49 @@ namespace salesbook.Maui.Core.Services;
|
||||
|
||||
public class AttachedService : IAttachedService
|
||||
{
|
||||
public async Task<AttachedDTO?> SelectImage()
|
||||
public async Task<AttachedDTO?> SelectImageFromCamera()
|
||||
{
|
||||
// Richiesta permessi base
|
||||
var cameraPerm = await Permissions.RequestAsync<Permissions.Camera>();
|
||||
var storagePerm = await Permissions.RequestAsync<Permissions.StorageRead>();
|
||||
|
||||
if (cameraPerm != PermissionStatus.Granted && storagePerm != PermissionStatus.Granted)
|
||||
if (cameraPerm != PermissionStatus.Granted)
|
||||
return null;
|
||||
|
||||
// Mostro all'utente un popup di scelta
|
||||
var action = await Application.Current.MainPage.DisplayActionSheet(
|
||||
"Aggiungi immagine",
|
||||
"Annulla",
|
||||
null,
|
||||
"Scatta foto",
|
||||
"Scegli dalla galleria");
|
||||
|
||||
FileResult? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (action == "Scatta foto")
|
||||
{
|
||||
result = await MediaPicker.Default.CapturePhotoAsync();
|
||||
}
|
||||
else if (action == "Scegli dalla galleria")
|
||||
{
|
||||
result = await MediaPicker.Default.PickPhotoAsync();
|
||||
}
|
||||
result = await MediaPicker.Default.CapturePhotoAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore selezione immagine: {ex.Message}");
|
||||
Console.WriteLine($"Errore cattura foto: {ex.Message}");
|
||||
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}");
|
||||
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>();
|
||||
|
||||
Reference in New Issue
Block a user