using AuroraDesk.Presentation.ViewModels.Base;
using ReactiveUI;
using System.Collections.Generic;
using System.Linq;
namespace AuroraDesk.Presentation.ViewModels.Pages;
///
/// 基于 Skottie 的呼吸动画页面 ViewModel
///
public class BreathePageViewModel : RoutableViewModel
{
private readonly IList _availableAnimations;
private int _repeatCount = Avalonia.Skia.Lottie.Lottie.Infinity;
private string _animationPath = "avares://AuroraDesk.Presentation/Resources/Animations/breathe.json";
private string _pageTitle = "呼吸动画";
private string _pageDescription = "演示如何在 Avalonia 中使用 Skottie 播放 Lottie 动画。";
private AnimationOption? _selectedAnimation;
private bool _isUpdatingSelection;
///
/// 构造函数
///
/// 宿主 Screen
public BreathePageViewModel(IScreen hostScreen)
: base(hostScreen, "BreatheAnimation")
{
_availableAnimations =
[
new AnimationOption("呼吸引导", "avares://AuroraDesk.Presentation/Resources/Animations/breathe.json"),
new AnimationOption("呼吸引导 V2", "avares://AuroraDesk.Presentation/Resources/Animations/breatheV2.json"),
new AnimationOption("科技律动 V3", "avares://AuroraDesk.Presentation/Resources/Animations/breathe_V3.json"),
];
_selectedAnimation = _availableAnimations.FirstOrDefault(x => x.Path == _animationPath)
?? _availableAnimations.FirstOrDefault();
if (_selectedAnimation is not null && _selectedAnimation.Path != _animationPath)
{
_animationPath = _selectedAnimation.Path;
}
}
///
/// 动画文件的资源路径(avares URI)
///
public string AnimationPath
{
get => _animationPath;
set
{
if (value == _animationPath)
{
return;
}
this.RaiseAndSetIfChanged(ref _animationPath, value);
if (_isUpdatingSelection)
{
return;
}
try
{
_isUpdatingSelection = true;
var match = _availableAnimations.FirstOrDefault(x => x.Path == value);
SelectedAnimation = match;
}
finally
{
_isUpdatingSelection = false;
}
}
}
///
/// 动画重复次数,默认无限循环
///
public int RepeatCount
{
get => _repeatCount;
set => this.RaiseAndSetIfChanged(ref _repeatCount, value);
}
///
/// 页面标题
///
public string PageTitle
{
get => _pageTitle;
set => this.RaiseAndSetIfChanged(ref _pageTitle, value);
}
///
/// 页面描述
///
public string PageDescription
{
get => _pageDescription;
set => this.RaiseAndSetIfChanged(ref _pageDescription, value);
}
///
/// 可供选择的动画资源列表
///
public IReadOnlyList AvailableAnimations => (IReadOnlyList)_availableAnimations;
///
/// 当前选中的动画资源
///
public AnimationOption? SelectedAnimation
{
get => _selectedAnimation;
set
{
if (ReferenceEquals(_selectedAnimation, value))
{
return;
}
this.RaiseAndSetIfChanged(ref _selectedAnimation, value);
if (_isUpdatingSelection)
{
return;
}
if (value is null)
{
return;
}
try
{
_isUpdatingSelection = true;
AnimationPath = value.Path;
}
finally
{
_isUpdatingSelection = false;
}
}
}
///
/// 表示可选动画的显示名称与路径
///
public record AnimationOption(string DisplayName, string Path);
}