90 lines
2.6 KiB
C#
90 lines
2.6 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using UtopiaCanteenSystem.Services;
|
|
|
|
namespace UtopiaCanteenSystem.ViewModels;
|
|
|
|
/// <summary>
|
|
/// ViewModel for SettingsView: editable API endpoint (UIND sync URL), save, load from config.
|
|
/// </summary>
|
|
public partial class SettingsViewModel : ObservableObject
|
|
{
|
|
private readonly IConfigService _configService;
|
|
private readonly INavigationService _navigation;
|
|
private readonly IAdminAuditService _adminAudit;
|
|
|
|
[ObservableProperty]
|
|
private string _syncApiEndpoint = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _scanTimeoutSeconds = "30";
|
|
|
|
[ObservableProperty]
|
|
private string _adminCardId = "ADMIN";
|
|
|
|
[ObservableProperty]
|
|
private string _saveMessage = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool _isError;
|
|
|
|
[ObservableProperty]
|
|
private bool _isSaving;
|
|
|
|
public SettingsViewModel(IConfigService configService, INavigationService navigation, IAdminAuditService adminAudit)
|
|
{
|
|
_configService = configService;
|
|
_navigation = navigation;
|
|
_adminAudit = adminAudit;
|
|
LoadFromConfig();
|
|
}
|
|
|
|
/// <summary>Loads SyncApiEndpoint from config service.</summary>
|
|
public void LoadFromConfig()
|
|
{
|
|
SyncApiEndpoint = _configService.GetSyncApiEndpoint();
|
|
ScanTimeoutSeconds = _configService.GetScanTimeoutSeconds().ToString();
|
|
|
|
// Always prefer the latest admin login from the database.
|
|
var last = _adminAudit.GetLastLogin();
|
|
if (last != null && !string.IsNullOrWhiteSpace(last.EmployeeId))
|
|
AdminCardId = last.EmployeeId;
|
|
else
|
|
AdminCardId = "ADMIN";
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Save()
|
|
{
|
|
IsSaving = true;
|
|
if (!int.TryParse(ScanTimeoutSeconds, out var seconds) || seconds <= 0)
|
|
{
|
|
SaveMessage = "Scan timeout must be a positive number of seconds.";
|
|
IsError = true;
|
|
IsSaving = false;
|
|
return;
|
|
}
|
|
|
|
_configService.SetSyncApiEndpoint(SyncApiEndpoint?.Trim() ?? string.Empty);
|
|
_configService.SetScanTimeoutSeconds(seconds);
|
|
_configService.SetAdminCardId(AdminCardId?.Trim() ?? string.Empty);
|
|
SaveMessage = "Settings saved.";
|
|
IsError = false;
|
|
IsSaving = false;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Back()
|
|
{
|
|
_navigation.NavigateBackFromSettings(GetDashboardTimeout());
|
|
}
|
|
|
|
private TimeSpan GetDashboardTimeout()
|
|
{
|
|
var seconds = _configService.GetScanTimeoutSeconds();
|
|
if (seconds <= 0)
|
|
seconds = 60;
|
|
return TimeSpan.FromSeconds(seconds);
|
|
}
|
|
}
|