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 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>();

View File

@@ -101,7 +101,7 @@
ShowProgressIndicator="true"
DebounceInterval="300"
MaxItems="50"
Class="customIcon-select"
Class="customIcon-select"
AdornmentIcon="@Icons.Material.Filled.Code"/>
</div>
@@ -218,30 +218,6 @@
</MudButton>
</YesButton>
</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>
</MudDialog>
@@ -282,12 +258,9 @@
//MessageBox
private MudMessageBox ConfirmDelete { get; set; }
private MudMessageBox ConfirmMemo { get; set; }
private MudMessageBox AddNamePosition { get; set; }
//Attached
private List<AttachedDTO>? AttachedList { get; set; }
private string? NamePosition { get; set; }
private bool CanAddPosition { get; set; } = true;
// cache per commesse
@@ -634,16 +607,8 @@
var attached = (AttachedDTO)result.Data;
if (attached.Type == AttachedDTO.TypeAttached.Position)
{
CanAddPosition = false;
var resultNamePosition = await AddNamePosition.ShowAsync();
if (resultNamePosition is true)
attached.Description = NamePosition;
attached.Name = NamePosition!;
}
AttachedList ??= [];
AttachedList.Add(attached);

View File

@@ -8,23 +8,54 @@
<DialogContent>
<HeaderLayout ShowProfile="false" SmallHeader="true" Cancel="true" OnCancel="() => MudDialog.Cancel()" Title="Aggiungi allegati"/>
<div style="margin-bottom: 1rem;" class="content attached">
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Image"
Label="Immagini" OnClick="OnImage"/>
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.InsertDriveFile"
Label="File" OnClick="OnFile"/>
@if (CanAddPosition)
@if (RequireNewName)
{
<MudTextField @bind-Value="NewName" Class="px-3" Variant="Variant.Outlined"/>
}
else
{
@if (!SelectTypePicture)
{
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.AddLocationAlt"
Label="Posizione" OnClick="OnPosition"/>
<div style="margin-bottom: 1rem;" class="content attached">
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.Image"
Label="Immagini" OnClick="OnImage"/>
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.InsertDriveFile"
Label="File" OnClick="OnFile"/>
@if (CanAddPosition)
{
<MudFab Size="Size.Small" Color="Color.Primary"
StartIcon="@Icons.Material.Rounded.AddLocationAlt"
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>
<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>
<SaveOverlay VisibleOverlay="VisibleOverlay" SuccessAnimation="SuccessAnimation"/>
@@ -40,36 +71,47 @@
private AttachedDTO? Attached { get; set; }
private bool RequireNewName { get; set; }
private bool SelectTypePicture { get; set; }
private string? _newName;
private string? NewName
{
get => _newName;
set
{
_newName = value;
StateHasChanged();
}
}
protected override async Task OnInitializedAsync()
{
SelectTypePicture = false;
RequireNewName = false;
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()
{
Attached = await AttachedService.SelectImage();
MudDialog.Close(Attached);
SelectTypePicture = true;
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()
@@ -81,6 +123,37 @@
private async Task OnPosition()
{
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);
}

View File

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

View File

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