From 885b3cc6b6557d43834382bea713e8b353ea82d7 Mon Sep 17 00:00:00 2001 From: MarcoE Date: Wed, 21 Oct 2026 08:49:34 +0200 Subject: [PATCH] Avvisa quando manca il permesso della fotocamera 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) --- SteUp.Maui/Core/Services/AttachedService.cs | 11 ++++++++-- .../Modal/ModalAddAttached.razor | 15 +++++++++++++ .../Modal/ModalFormScheda.razor | 22 +++++++++++++++++++ .../Core/Interface/System/IAttachedService.cs | 5 +++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/SteUp.Maui/Core/Services/AttachedService.cs b/SteUp.Maui/Core/Services/AttachedService.cs index fde9132..9b39f25 100644 --- a/SteUp.Maui/Core/Services/AttachedService.cs +++ b/SteUp.Maui/Core/Services/AttachedService.cs @@ -10,12 +10,19 @@ public class AttachedService(ILogger logger) : IAttachedService { public bool IsCameraAvailable => MediaPicker.Default.IsCaptureSupported; - public async Task SelectImageFromCamera() + public async Task RequestCameraPermissions() { var cameraPerm = await Permissions.RequestAsync(); var storagePerm = await Permissions.RequestAsync(); - if (cameraPerm != PermissionStatus.Granted || storagePerm != PermissionStatus.Granted) + return cameraPerm == PermissionStatus.Granted && storagePerm == PermissionStatus.Granted; + } + + public void OpenAppSettings() => AppInfo.Current.ShowSettingsUI(); + + public async Task SelectImageFromCamera() + { + if (!await RequestCameraPermissions()) return null; FileResult? result; diff --git a/SteUp.Shared/Components/SingleElements/Modal/ModalAddAttached.razor b/SteUp.Shared/Components/SingleElements/Modal/ModalAddAttached.razor index 224703d..00dfa18 100644 --- a/SteUp.Shared/Components/SingleElements/Modal/ModalAddAttached.razor +++ b/SteUp.Shared/Components/SingleElements/Modal/ModalAddAttached.razor @@ -40,6 +40,21 @@ private async Task OnCamera() { + if (!await AttachedService.RequestCameraPermissions()) + { + Snackbar.Clear(); + Snackbar.Add("Per scattare le foto serve il permesso della fotocamera", Severity.Warning, config => + { + config.Action = "Impostazioni"; + config.OnClick = _ => + { + AttachedService.OpenAppSettings(); + return Task.CompletedTask; + }; + }); + return; + } + AttachedDto? selectImageFromCamera; try { diff --git a/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor b/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor index 7a60f47..5e3de39 100644 --- a/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor +++ b/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor @@ -754,12 +754,34 @@ #region File + // Dopo un rifiuto definitivo Android non ripropone la richiesta: il + // permesso si riattiva solo dalle impostazioni, quindi le apriamo noi. + private void ShowCameraPermissionDenied() + { + Snackbar.Clear(); + Snackbar.Add("Per scattare le foto serve il permesso della fotocamera", Severity.Warning, config => + { + config.Action = "Impostazioni"; + config.OnClick = _ => + { + AttachedService.OpenAppSettings(); + return Task.CompletedTask; + }; + }); + } + private async Task OpenAddAttached() { List? attachedList; if (AttachedService.IsCameraAvailable) { + if (!await AttachedService.RequestCameraPermissions()) + { + ShowCameraPermissionDenied(); + return; + } + AttachedDto? selectImageFromCamera; try { diff --git a/SteUp.Shared/Core/Interface/System/IAttachedService.cs b/SteUp.Shared/Core/Interface/System/IAttachedService.cs index f39fcce..588d61a 100644 --- a/SteUp.Shared/Core/Interface/System/IAttachedService.cs +++ b/SteUp.Shared/Core/Interface/System/IAttachedService.cs @@ -6,6 +6,11 @@ public interface IAttachedService { bool IsCameraAvailable { get; } + /// Chiede i permessi per scattare; false se negati. + Task RequestCameraPermissions(); + + void OpenAppSettings(); + /// Null se l'utente annulla; lancia se lo scatto fallisce. Task SelectImageFromCamera(); Task?> SelectImageFromGallery();