using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using AppClientBase.Services; namespace AppClientBase.ViewModels; /// /// EN: Main page ViewModel. /// VI: ViewModel của trang chính. /// public partial class MainViewModel : BaseViewModel { private readonly ISettingsService _settingsService; /// /// EN: Welcome message displayed on the main page. /// VI: Thông điệp chào mừng hiển thị trên trang chính. /// [ObservableProperty] private string _welcomeMessage = "Welcome to AppClientBase!"; /// /// EN: Click counter for demonstration. /// VI: Bộ đếm click để minh họa. /// [ObservableProperty] private int _clickCount; /// /// EN: Button text that updates with click count. /// VI: Text nút cập nhật theo số lần click. /// [ObservableProperty] private string _buttonText = "Click me"; /// /// EN: Constructor with required services. /// VI: Constructor với các services cần thiết. /// public MainViewModel( INavigationService navigationService, ISettingsService settingsService) : base(navigationService) { _settingsService = settingsService; Title = "Home"; // Defer loading settings to avoid startup crash // ClickCount will be loaded on first interaction } /// /// EN: Increment click counter command. /// VI: Command tăng bộ đếm click. /// [RelayCommand] private void IncrementCounter() { ClickCount++; try { _settingsService.Set("ClickCount", ClickCount); } catch { // Ignore settings errors } UpdateButtonText(); } /// /// EN: Update button text based on click count. /// VI: Cập nhật text nút dựa trên số lần click. /// private void UpdateButtonText() { ButtonText = ClickCount switch { 0 => "Click me", 1 => "Clicked 1 time", _ => $"Clicked {ClickCount} times" }; } }