Con il permesso negato "Aggiungi una foto" non faceva nulla: il servizio restituiva null come per un annullamento. Dopo un rifiuto definitivo Android non ripropone la richiesta, quindi il rilevatore restava bloccato senza capire perche'. - AttachedService: la richiesta dei permessi diventa RequestCameraPermissions, usata anche da SelectImageFromCamera; OpenAppSettings apre le impostazioni dell'app. - ModalFormScheda e ModalAddAttached: chiedono i permessi prima di scattare e, se negati, mostrano un avviso con l'azione "Impostazioni". Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
78 lines
2.5 KiB
C#
78 lines
2.5 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<bool> RequestCameraPermissions()
|
|
{
|
|
var cameraPerm = await Permissions.RequestAsync<Permissions.Camera>();
|
|
var storagePerm = await Permissions.RequestAsync<Permissions.StorageWrite>();
|
|
|
|
return cameraPerm == PermissionStatus.Granted && storagePerm == PermissionStatus.Granted;
|
|
}
|
|
|
|
public void OpenAppSettings() => AppInfo.Current.ShowSettingsUI();
|
|
|
|
public async Task<AttachedDto?> SelectImageFromCamera()
|
|
{
|
|
if (!await RequestCameraPermissions())
|
|
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;
|
|
}
|
|
} |