Se lo scatto falliva, AttachedService restituiva null come per un annullamento: il rilevatore toccava "Aggiungi una foto" e non succedeva nulla. - AttachedService: espone IsCameraAvailable (IsCaptureSupported) e, dopo aver registrato l'errore e averlo inviato a Sentry, lo rilancia invece di restituire null. - ModalFormScheda: dalla scheda il pulsante apre solo la fotocamera, quindi senza fotocamera non c'era alternativa. Ora avvisa e apre la galleria; se lo scatto fallisce mostra un errore. - ModalAddAttached: stessa gestione dell'errore di scatto. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using SteUp.Maui.Core.Utility;
|
|
using SteUp.Shared.Core.Dto;
|
|
using SteUp.Shared.Core.Helpers;
|
|
using SteUp.Shared.Core.Interface.System;
|
|
|
|
namespace SteUp.Maui.Core.Services;
|
|
|
|
public class AttachedService(ILogger<FileManager> logger) : IAttachedService
|
|
{
|
|
public bool IsCameraAvailable => MediaPicker.Default.IsCaptureSupported;
|
|
|
|
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;
|
|
|
|
try
|
|
{
|
|
result = await MediaPicker.Default.CapturePhotoAsync();
|
|
result?.FileName = $"img_{DateTime.Now:ddMMyyy_hhmmss}{result.FileName[result.FileName.IndexOf('.')..]}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, ex.Message);
|
|
Console.WriteLine($"Errore cattura foto: {ex.Message}");
|
|
SentrySdk.CaptureException(ex);
|
|
|
|
// Rilancia: restituire null qui era indistinguibile da un annullamento,
|
|
// e il rilevatore toccava "Aggiungi una foto" senza vedere nulla.
|
|
throw;
|
|
}
|
|
|
|
return result is null ? null : await UtilityFile.ConvertToDto(result, AttachedDto.TypeAttached.Image);
|
|
}
|
|
|
|
public async Task<List<AttachedDto>?> SelectImageFromGallery()
|
|
{
|
|
List<FileResult>? resultList;
|
|
var storagePerm = await Permissions.RequestAsync<Permissions.StorageRead>();
|
|
if (storagePerm != PermissionStatus.Granted)
|
|
return null;
|
|
|
|
try
|
|
{
|
|
resultList = await MediaPicker.Default.PickPhotosAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, ex.Message);
|
|
Console.WriteLine($"Errore selezione galleria: {ex.Message}");
|
|
SentrySdk.CaptureException(ex);
|
|
return null;
|
|
}
|
|
|
|
if (resultList.IsNullOrEmpty()) return null;
|
|
|
|
List<AttachedDto> returnList = [];
|
|
foreach (var fileResult in resultList)
|
|
{
|
|
returnList.Add(await UtilityFile.ConvertToDto(fileResult, AttachedDto.TypeAttached.Image));
|
|
}
|
|
|
|
return returnList;
|
|
}
|
|
} |