using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using UtopiaCanteenSystem.Services; namespace UtopiaCanteenSystem.ViewModels; /// /// ViewModel for SettingsView: editable API endpoint (UIND sync URL), save, load from config. /// public partial class SettingsViewModel : ObservableObject { private readonly IConfigService _configService; private readonly INavigationService _navigation; [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) { _configService = configService; _navigation = navigation; LoadFromConfig(); } /// Loads SyncApiEndpoint from config service. public void LoadFromConfig() { SyncApiEndpoint = _configService.GetSyncApiEndpoint(); ScanTimeoutSeconds = _configService.GetScanTimeoutSeconds().ToString(); AdminCardId = _configService.GetAdminCardId(); } [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); } }