85 lines
2.2 KiB
C#
85 lines
2.2 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using AppClientBase.Services;
|
|
|
|
namespace AppClientBase.ViewModels;
|
|
|
|
/// <summary>
|
|
/// EN: Main page ViewModel.
|
|
/// VI: ViewModel của trang chính.
|
|
/// </summary>
|
|
public partial class MainViewModel : BaseViewModel
|
|
{
|
|
private readonly ISettingsService _settingsService;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
[ObservableProperty]
|
|
private string _welcomeMessage = "Welcome to AppClientBase!";
|
|
|
|
/// <summary>
|
|
/// EN: Click counter for demonstration.
|
|
/// VI: Bộ đếm click để minh họa.
|
|
/// </summary>
|
|
[ObservableProperty]
|
|
private int _clickCount;
|
|
|
|
/// <summary>
|
|
/// EN: Button text that updates with click count.
|
|
/// VI: Text nút cập nhật theo số lần click.
|
|
/// </summary>
|
|
[ObservableProperty]
|
|
private string _buttonText = "Click me";
|
|
|
|
/// <summary>
|
|
/// EN: Constructor with required services.
|
|
/// VI: Constructor với các services cần thiết.
|
|
/// </summary>
|
|
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
|
|
}
|
|
|
|
/// <summary>
|
|
/// EN: Increment click counter command.
|
|
/// VI: Command tăng bộ đếm click.
|
|
/// </summary>
|
|
[RelayCommand]
|
|
private void IncrementCounter()
|
|
{
|
|
ClickCount++;
|
|
try
|
|
{
|
|
_settingsService.Set("ClickCount", ClickCount);
|
|
}
|
|
catch
|
|
{
|
|
// Ignore settings errors
|
|
}
|
|
UpdateButtonText();
|
|
}
|
|
|
|
/// <summary>
|
|
/// EN: Update button text based on click count.
|
|
/// VI: Cập nhật text nút dựa trên số lần click.
|
|
/// </summary>
|
|
private void UpdateButtonText()
|
|
{
|
|
ButtonText = ClickCount switch
|
|
{
|
|
0 => "Click me",
|
|
1 => "Clicked 1 time",
|
|
_ => $"Clicked {ClickCount} times"
|
|
};
|
|
}
|
|
}
|