Files
Denso/srcs/RobotNet10/RobotApp/RobotNet10.RobotApp.Client/Services/DockStationConfigState.cs
2026-07-03 16:31:37 +07:00

128 lines
3.2 KiB
C#

using RobotNet10.RobotApp.Shared.DockStation;
namespace RobotNet10.RobotApp.Client.Services;
public class DockStationConfigState(DockStationApiService apiService)
{
public List<DockStationConfigSummaryDto> Configs { get; private set; } = [];
public DockStationConfigDto? SelectedConfig { get; private set; }
public bool IsLoading { get; private set; }
public bool IsSaving { get; private set; }
public string? ErrorMessage { get; private set; }
public event Action? OnStateChanged;
public async Task LoadConfigsAsync()
{
IsLoading = true;
ErrorMessage = null;
NotifyStateChanged();
try
{
Configs = await apiService.GetAllAsync();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
Configs = [];
}
IsLoading = false;
NotifyStateChanged();
}
public async Task SelectConfigAsync(Guid id)
{
IsLoading = true;
ErrorMessage = null;
NotifyStateChanged();
try
{
SelectedConfig = await apiService.GetByIdAsync(id);
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
SelectedConfig = null;
}
IsLoading = false;
NotifyStateChanged();
}
public void ClearSelection()
{
SelectedConfig = null;
NotifyStateChanged();
}
public async Task<DockStationConfigDto> CreateConfigAsync(CreateDockStationConfigRequest request)
{
IsSaving = true;
ErrorMessage = null;
NotifyStateChanged();
try
{
var result = await apiService.CreateAsync(request);
await LoadConfigsAsync();
SelectedConfig = result;
return result;
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
throw;
}
finally
{
IsSaving = false;
NotifyStateChanged();
}
}
public async Task UpdateConfigAsync(Guid id, UpdateDockStationConfigRequest request)
{
IsSaving = true;
ErrorMessage = null;
NotifyStateChanged();
try
{
SelectedConfig = await apiService.UpdateAsync(id, request);
await LoadConfigsAsync();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
throw;
}
finally
{
IsSaving = false;
NotifyStateChanged();
}
}
public async Task DeleteConfigAsync(Guid id)
{
IsSaving = true;
ErrorMessage = null;
NotifyStateChanged();
try
{
await apiService.DeleteAsync(id);
if (SelectedConfig?.Id == id) ClearSelection();
await LoadConfigsAsync();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
throw;
}
finally
{
IsSaving = false;
NotifyStateChanged();
}
}
private void NotifyStateChanged() => OnStateChanged?.Invoke();
}