2026-03-18 18:06:33 +08:00

114 lines
2.9 KiB
C#

using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace NFCLockDemoV2.ViewModels;
public class MainViewModel : ObservableObject
{
private ViewModels.OperationType? _operation;
public ViewModels.OperationType? Operation
{
get => _operation;
set => SetProperty(ref _operation, value);
}
private int _arcProgress;
public int ArcProgress
{
get => _arcProgress;
set => SetProperty(ref _arcProgress, value);
}
private string _deviceId = "--";
public string DeviceId
{
get => _deviceId;
set
{
if (SetProperty(ref _deviceId, value))
{
AddDevice(value);
}
}
}
private string _deviceStatus = "";
public string DeviceStatus
{
get => _deviceStatus;
set => SetProperty(ref _deviceStatus, value);
}
private string _deviceRssi = "--";
public string DeviceRssi
{
get => _deviceRssi;
set => SetProperty(ref _deviceRssi, value);
}
private string _password = "123456";
public string Password
{
get => _password;
set => SetProperty(ref _password, value);
}
public ICommand ControlCommand { get; set; }
public ICommand CopyCommand { get; set; }
public void UpdateMessage(string message)
{
DeviceStatus = message;
}
public ObservableCollection<string> Devices { get; set; }
private void AddDevice(string deviceId)
{
if (deviceId != "--" && !Devices.Contains(deviceId))
{
Devices.Add(deviceId);
}
}
private async Task CopyToDeviceClipboardAsync(string text)
{
await Clipboard.SetTextAsync(text);
await Application.Current.MainPage.DisplayAlert("提示", $"已复制:{text}", "确定");
}
public MainViewModel()
{
Devices = new ObservableCollection<string>();
ControlCommand = new AsyncRelayCommand<string>(val =>
{
switch (val)
{
case "Lock":
Operation = ViewModels.OperationType.LOCK;
break;
case "Unlock":
Operation = ViewModels.OperationType.UNLOCK;
break;
case "SetPassword":
Operation = ViewModels.OperationType.SET_PASSWORD;
break;
default:
Operation = null;
break;
}
return Task.CompletedTask;
});
CopyCommand = new AsyncRelayCommand<string>(async (text) =>
{
if (!string.IsNullOrEmpty(text))
{
await CopyToDeviceClipboardAsync(text);
}
});
}
}