Gestita fotocamera per allegare immagini

This commit is contained in:
2025-09-18 10:56:21 +02:00
parent 8a45bffebc
commit 06bda7c881
5 changed files with 139 additions and 95 deletions

View File

@@ -5,45 +5,49 @@ namespace salesbook.Maui.Core.Services;
public class AttachedService : IAttachedService 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 cameraPerm = await Permissions.RequestAsync<Permissions.Camera>();
var storagePerm = await Permissions.RequestAsync<Permissions.StorageRead>(); if (cameraPerm != PermissionStatus.Granted)
if (cameraPerm != PermissionStatus.Granted && storagePerm != PermissionStatus.Granted)
return null; 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; FileResult? result = null;
try try
{
if (action == "Scatta foto")
{ {
result = await MediaPicker.Default.CapturePhotoAsync(); result = await MediaPicker.Default.CapturePhotoAsync();
} }
else if (action == "Scegli dalla galleria")
{
result = await MediaPicker.Default.PickPhotoAsync();
}
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Errore selezione immagine: {ex.Message}"); Console.WriteLine($"Errore cattura foto: {ex.Message}");
return null; return null;
} }
return result is null ? null : await ConvertToDto(result, AttachedDTO.TypeAttached.Image); 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() public async Task<AttachedDTO?> SelectFile()
{ {
var perm = await Permissions.RequestAsync<Permissions.StorageRead>(); var perm = await Permissions.RequestAsync<Permissions.StorageRead>();

View File

@@ -218,30 +218,6 @@
</MudButton> </MudButton>
</YesButton> </YesButton>
</MudMessageBox> </MudMessageBox>
<MudMessageBox @ref="AddNamePosition" Class="c-messageBox" Title="Nome della posizione" CancelText="Annulla">
<MessageContent>
<MudTextField @bind-Value="NamePosition" Variant="Variant.Outlined"/>
</MessageContent>
<YesButton>
<MudButton Size="Size.Small" Variant="Variant.Filled" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Check">
Salva
</MudButton>
</YesButton>
</MudMessageBox>
<MudMessageBox @ref="ConfirmMemo" Class="c-messageBox" Title="Attenzione!" CancelText="Annulla">
<MessageContent>
Vuoi creare un promemoria?
</MessageContent>
<YesButton>
<MudButton Size="Size.Small" Variant="Variant.Filled" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Check">
Crea
</MudButton>
</YesButton>
</MudMessageBox>
</DialogContent> </DialogContent>
</MudDialog> </MudDialog>
@@ -282,12 +258,9 @@
//MessageBox //MessageBox
private MudMessageBox ConfirmDelete { get; set; } private MudMessageBox ConfirmDelete { get; set; }
private MudMessageBox ConfirmMemo { get; set; }
private MudMessageBox AddNamePosition { get; set; }
//Attached //Attached
private List<AttachedDTO>? AttachedList { get; set; } private List<AttachedDTO>? AttachedList { get; set; }
private string? NamePosition { get; set; }
private bool CanAddPosition { get; set; } = true; private bool CanAddPosition { get; set; } = true;
// cache per commesse // cache per commesse
@@ -634,16 +607,8 @@
var attached = (AttachedDTO)result.Data; var attached = (AttachedDTO)result.Data;
if (attached.Type == AttachedDTO.TypeAttached.Position) if (attached.Type == AttachedDTO.TypeAttached.Position)
{
CanAddPosition = false; CanAddPosition = false;
var resultNamePosition = await AddNamePosition.ShowAsync();
if (resultNamePosition is true)
attached.Description = NamePosition;
attached.Name = NamePosition!;
}
AttachedList ??= []; AttachedList ??= [];
AttachedList.Add(attached); AttachedList.Add(attached);

View File

@@ -8,6 +8,14 @@
<DialogContent> <DialogContent>
<HeaderLayout ShowProfile="false" SmallHeader="true" Cancel="true" OnCancel="() => MudDialog.Cancel()" Title="Aggiungi allegati"/> <HeaderLayout ShowProfile="false" SmallHeader="true" Cancel="true" OnCancel="() => MudDialog.Cancel()" Title="Aggiungi allegati"/>
@if (RequireNewName)
{
<MudTextField @bind-Value="NewName" Class="px-3" Variant="Variant.Outlined"/>
}
else
{
@if (!SelectTypePicture)
{
<div style="margin-bottom: 1rem;" class="content attached"> <div style="margin-bottom: 1rem;" class="content attached">
<MudFab Size="Size.Small" Color="Color.Primary" <MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Image" StartIcon="@Icons.Material.Rounded.Image"
@@ -24,7 +32,30 @@
Label="Posizione" OnClick="OnPosition"/> Label="Posizione" OnClick="OnPosition"/>
} }
</div> </div>
}
else
{
<div style="margin-bottom: 1rem;" class="content attached">
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.CameraAlt"
Label="Camera" OnClick="OnCamera"/>
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Image"
Label="Galleria" OnClick="OnGallery"/>
</div>
}
}
</DialogContent> </DialogContent>
<DialogActions>
@if (RequireNewName)
{
<MudButton Disabled="NewName.IsNullOrEmpty()" Class="my-3" Size="Size.Small" Variant="Variant.Filled" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Check" OnClick="OnNewName">
Salva
</MudButton>
}
</DialogActions>
</MudDialog> </MudDialog>
<SaveOverlay VisibleOverlay="VisibleOverlay" SuccessAnimation="SuccessAnimation"/> <SaveOverlay VisibleOverlay="VisibleOverlay" SuccessAnimation="SuccessAnimation"/>
@@ -40,36 +71,47 @@
private AttachedDTO? Attached { get; set; } private AttachedDTO? Attached { get; set; }
private bool RequireNewName { get; set; }
private bool SelectTypePicture { get; set; } private bool SelectTypePicture { get; set; }
private string? _newName;
private string? NewName
{
get => _newName;
set
{
_newName = value;
StateHasChanged();
}
}
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
SelectTypePicture = false;
RequireNewName = false;
Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter; Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
_ = LoadData();
}
private async Task LoadData()
{
}
private async Task Save()
{
VisibleOverlay = true;
StateHasChanged();
SuccessAnimation = true;
StateHasChanged();
await Task.Delay(1250);
MudDialog.Close();
} }
private async Task OnImage() private async Task OnImage()
{ {
Attached = await AttachedService.SelectImage(); SelectTypePicture = true;
MudDialog.Close(Attached); StateHasChanged();
}
private async Task OnCamera()
{
Attached = await AttachedService.SelectImageFromCamera();
RequireNewName = true;
StateHasChanged();
}
private async Task OnGallery()
{
Attached = await AttachedService.SelectImageFromGallery();
RequireNewName = true;
StateHasChanged();
} }
private async Task OnFile() private async Task OnFile()
@@ -81,6 +123,37 @@
private async Task OnPosition() private async Task OnPosition()
{ {
Attached = await AttachedService.SelectPosition(); Attached = await AttachedService.SelectPosition();
RequireNewName = true;
StateHasChanged();
}
private void OnNewName()
{
switch (Attached.Type)
{
case AttachedDTO.TypeAttached.Position:
{
CanAddPosition = false;
Attached.Description = NewName!;
Attached.Name = NewName!;
break;
}
case AttachedDTO.TypeAttached.Image:
{
var extension = Path.GetExtension(Attached.Name);
Attached.Name = NewName! + extension;
break;
}
case AttachedDTO.TypeAttached.Document:
break;
default:
throw new ArgumentOutOfRangeException();
}
MudDialog.Close(Attached); MudDialog.Close(Attached);
} }

View File

@@ -80,7 +80,8 @@ public class ModalHelpers
{ {
FullScreen = false, FullScreen = false,
CloseButton = false, CloseButton = false,
NoHeader = true NoHeader = true,
BackdropClick = false
} }
); );

View File

@@ -4,7 +4,8 @@ namespace salesbook.Shared.Core.Interface;
public interface IAttachedService public interface IAttachedService
{ {
Task<AttachedDTO?> SelectImage(); Task<AttachedDTO?> SelectImageFromCamera();
Task<AttachedDTO?> SelectImageFromGallery();
Task<AttachedDTO?> SelectFile(); Task<AttachedDTO?> SelectFile();
Task<AttachedDTO?> SelectPosition(); Task<AttachedDTO?> SelectPosition();
Task OpenFile(Stream file, string fileName); Task OpenFile(Stream file, string fileName);