Gestiti allegati nel form
This commit is contained in:
@@ -5,117 +5,47 @@
|
||||
|
||||
<MudDialog Class="disable-safe-area">
|
||||
<DialogContent>
|
||||
<HeaderLayout SmallHeader="true" Cancel="true" OnCancel="@(() => MudDialog.Cancel())" Title="@TitleModal"/>
|
||||
<HeaderLayout SmallHeader="true" Cancel="true" OnCancel="@(() => MudDialog.Cancel())" Title="Aggiungi allegati"/>
|
||||
|
||||
@if (RequireNewName)
|
||||
{
|
||||
<MudTextField @bind-Value="NewName" Class="px-3" Variant="Variant.Outlined"/>
|
||||
}
|
||||
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"/>
|
||||
<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>
|
||||
}
|
||||
<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>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!;
|
||||
[Parameter] public bool CanAddPosition { get; set; }
|
||||
|
||||
private AttachedDto? Attached { get; set; }
|
||||
|
||||
private bool _requireNewName;
|
||||
|
||||
private bool RequireNewName
|
||||
{
|
||||
get => _requireNewName;
|
||||
set
|
||||
{
|
||||
_requireNewName = value;
|
||||
TitleModal = _requireNewName ? "Nome allegato" : "Aggiungi allegati";
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string TitleModal { get; set; } = "Aggiungi allegati";
|
||||
|
||||
private string? _newName;
|
||||
|
||||
private string? NewName
|
||||
{
|
||||
get => _newName;
|
||||
set
|
||||
{
|
||||
_newName = value;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
private List<AttachedDto>? Attached { get; set; }
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
RequireNewName = false;
|
||||
Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
|
||||
}
|
||||
|
||||
private async Task OnCamera()
|
||||
{
|
||||
Attached = await AttachedService.SelectImageFromCamera();
|
||||
|
||||
if (Attached != null)
|
||||
var selectImageFromCamera = await AttachedService.SelectImageFromCamera();
|
||||
|
||||
if (selectImageFromCamera != null)
|
||||
{
|
||||
RequireNewName = true;
|
||||
StateHasChanged();
|
||||
Attached ??= [];
|
||||
Attached.Add(selectImageFromCamera);
|
||||
MudDialog.Close(Attached);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnGallery()
|
||||
{
|
||||
Attached = await AttachedService.SelectImageFromGallery();
|
||||
|
||||
if (Attached != null)
|
||||
{
|
||||
RequireNewName = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
if (Attached != null) MudDialog.Close(Attached);
|
||||
}
|
||||
|
||||
private void OnNewName()
|
||||
{
|
||||
if (Attached != null)
|
||||
{
|
||||
switch (Attached.Type)
|
||||
{
|
||||
case AttachedDto.TypeAttached.Image:
|
||||
{
|
||||
var extension = Path.GetExtension(Attached.Name);
|
||||
Attached.Name = NewName! + extension;
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
MudDialog.Close(Attached);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user