refactor: Removed avalonia code.

This commit is contained in:
real-zony committed 2024-11-29 22:54:38 +08:00
1 parent 9ab9cd50e2
commit 7732ab52d3
31 files changed
-1324

No files matched your search

@@ -1,77 +0,0 @@
using System;
using System.Collections.ObjectModel;
using ReactiveUI;
using ZonyLrcTools.Common;
namespace ZonyLrcTools.Desktop.ViewModels;
public class HomeViewModel : ViewModelBase
{
private ObservableCollection<SongInfoViewModel> _songs = new();
public ObservableCollection<SongInfoViewModel> Songs
{
get => _songs;
set => this.RaiseAndSetIfChanged(ref _songs, value);
}
private int _downloadProgress;
public int DownloadProgress
{
get => _downloadProgress;
set => this.RaiseAndSetIfChanged(ref _downloadProgress, value);
}
private int _maxProgress;
public int MaxProgress
{
get => _maxProgress;
set => this.RaiseAndSetIfChanged(ref _maxProgress, value);
}
}
public class SongInfoViewModel : ReactiveObject
{
public SongInfoViewModel(MusicInfo info)
{
_info = info;
DownloadStatus = DownloadStatus.NotStarted;
this.WhenAnyValue(x => x.Info)
.Subscribe(_ =>
{
this.RaisePropertyChanged(nameof(SongName));
this.RaisePropertyChanged(nameof(ArtistName));
this.RaisePropertyChanged(nameof(FilePath));
});
}
private MusicInfo _info;
public MusicInfo Info
{
get => _info;
set => this.RaiseAndSetIfChanged(ref _info, value);
}
public string SongName => Info.Name;
public string ArtistName => Info.Artist;
public string FilePath => Info.FilePath;
private DownloadStatus _downloadStatus;
public DownloadStatus DownloadStatus
{
get => _downloadStatus;
set => this.RaiseAndSetIfChanged(ref _downloadStatus, value);
}
}
public enum DownloadStatus
{
NotStarted = 1,
Completed,
Failed
}
@@ -1,20 +0,0 @@
using ReactiveUI.Fody.Helpers;
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.ViewModels.Settings;
public class BlockWordViewModel : ViewModelBase
{
public BlockWordViewModel(GlobalOptions options)
{
IsEnable = options.Provider.Tag.BlockWord.IsEnable;
FilePath = options.Provider.Tag.BlockWord.FilePath;
SubscribeToProperty(this, x => x.IsEnable, x => options.Provider.Tag.BlockWord.IsEnable = x);
SubscribeToProperty(this, x => x.FilePath, x => options.Provider.Tag.BlockWord.FilePath = x!);
}
[Reactive] public bool IsEnable { get; set; }
[Reactive] public string? FilePath { get; set; }
}
@@ -1,75 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.ViewModels.Settings;
public class GlobalConfigurationViewModel : ViewModelBase
{
public GlobalConfigurationViewModel(GlobalOptions config)
{
var globalConfig = config.Provider.Lyric.Config;
InitializeLineBreakComboBoxItem(globalConfig);
IsOneLine = globalConfig.IsOneLine;
IsEnableTranslation = globalConfig.IsEnableTranslation;
IsSkipExistLyricFiles = globalConfig.IsSkipExistLyricFiles;
IsOnlyOutputTranslation = globalConfig.IsOnlyOutputTranslation;
SubscribeToProperty(this, x => x.IsOneLine, x => globalConfig.IsOneLine = x);
SubscribeToProperty(this, x => x.IsEnableTranslation, x => globalConfig.IsEnableTranslation = x);
SubscribeToProperty(this, x => x.IsSkipExistLyricFiles, x => globalConfig.IsSkipExistLyricFiles = x);
SubscribeToProperty(this, x => x.IsOnlyOutputTranslation, x => globalConfig.IsOnlyOutputTranslation = x);
}
[Reactive] public bool IsOneLine { get; set; }
[Reactive] public bool IsEnableTranslation { get; set; }
[Reactive] public bool IsSkipExistLyricFiles { get; set; }
[Reactive] public bool IsOnlyOutputTranslation { get; set; }
private void InitializeLineBreakComboBoxItem(GlobalLyricsConfigOptions config)
{
LineBreakOptions =
[
new TextComboboxItem { Name = "Windows", Value = "\r\n" },
new TextComboboxItem { Name = "Unix", Value = "\n" },
new TextComboboxItem { Name = "Mac", Value = "\r" }
];
SelectedLineBreak = LineBreakOptions.FirstOrDefault(x => x.Value == config.LineBreak)
?? LineBreakOptions.First();
FileEncodingOptions = Encoding.GetEncodings()
.Select(x => new TextComboboxItem { Name = x.DisplayName, Value = x.Name })
.ToList();
FileEncodingOptions.Insert(0, new TextComboboxItem { Name = "UTF-8-BOM", Value = "utf-8-bom" });
SelectedFileEncoding = FileEncodingOptions.FirstOrDefault(x => x.Value == config.FileEncoding)
?? FileEncodingOptions.First();
this.WhenAnyValue(x => x.SelectedLineBreak)
.Subscribe(x => config.LineBreak = x.Value);
this.WhenAnyValue(x => x.SelectedFileEncoding)
.Subscribe(x => config.FileEncoding = x.Value);
}
public List<TextComboboxItem> LineBreakOptions { get; private set; }
[Reactive] public TextComboboxItem SelectedLineBreak { get; set; }
public List<TextComboboxItem> FileEncodingOptions { get; private set; }
[Reactive] public TextComboboxItem SelectedFileEncoding { get; set; }
public class TextComboboxItem
{
public string Name { get; set; } = default!;
public string Value { get; set; } = default!;
}
}
@@ -1,49 +0,0 @@
using System;
using System.Globalization;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ReactiveUI.Fody.Helpers;
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.ViewModels.Settings;
public class LyricsProviderViewModel : ViewModelBase
{
public LyricsProviderViewModel(LyricsProviderOptions options)
{
var globalOptions = App.Current.Services.GetRequiredService<IOptions<GlobalOptions>>().Value;
Name = options.Name;
Priority = options.Priority;
Depth = options.Depth;
SubscribeToProperty(this, x => x.Priority, x => globalOptions.Provider.Lyric.GetLyricProviderOption(Name).Priority = x);
SubscribeToProperty(this, x => x.Depth, x => globalOptions.Provider.Lyric.GetLyricProviderOption(Name).Depth = x);
}
public string Name { get; set; }
[Reactive] public int Priority { get; set; }
[Reactive] public int Depth { get; set; }
}
internal class NullToDefaultValueConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value != null ? System.Convert.ToDecimal(value) : 0;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is decimal dValue)
{
return System.Convert.ToInt32(dValue);
}
return BindingOperations.DoNothing;
}
}
@@ -1,56 +0,0 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using ReactiveUI;
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.ViewModels.Settings;
public class SettingsViewModel : ViewModelBase
{
public SettingsViewModel(GlobalOptions globalOptions)
{
Config = new GlobalConfigurationViewModel(globalOptions);
LyricsProviders = new ObservableCollection<LyricsProviderViewModel>(globalOptions.Provider.Lyric.Plugin.Select(p => new LyricsProviderViewModel(p)));
Tag = new TagInfoViewModel(globalOptions);
BrowseBlockWordFileCommand = ReactiveCommand.CreateFromTask<Window>(BrowseBlockWordFile);
}
public static string Version => typeof(Program).Assembly.GetName().Version!.ToString();
public TagInfoViewModel Tag { get; }
public ReactiveCommand<Window, Unit> BrowseBlockWordFileCommand { get; }
public GlobalConfigurationViewModel Config { get; }
public ObservableCollection<LyricsProviderViewModel> LyricsProviders { get; }
private async Task BrowseBlockWordFile(Window parentWindow)
{
var storage = parentWindow.StorageProvider;
if (storage.CanOpen)
{
var options = new FilePickerOpenOptions
{
AllowMultiple = false,
FileTypeFilter = new[]
{
new FilePickerFileType("JSON")
{
Patterns = new[] { "*.json" }
}
}
};
var files = await storage.OpenFilePickerAsync(options);
if (files.Count > 0)
{
Tag.BlockWord.FilePath = files[0].Path.LocalPath;
}
}
}
}
@@ -1,42 +0,0 @@
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ReactiveUI.Fody.Helpers;
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.ViewModels.Settings;
public class TagInfoProviderViewModel : ViewModelBase
{
public TagInfoProviderViewModel(TagInfoProviderOptions options)
{
var globalOptions = App.Current.Services.GetRequiredService<IOptions<GlobalOptions>>().Value;
Name = options.Name;
Priority = options.Priority;
Extensions = options.Extensions == null ? [] : new ObservableCollection<ObservableKeyValuePair>(options.Extensions.Select(x => new ObservableKeyValuePair(x.Key, x.Value)));
SubscribeToProperty(this, x => x.Priority, x => globalOptions.Provider.Tag.GetTagProviderOption(Name).Priority = x);
SubscribeToProperty(this, x => x.Extensions, x => globalOptions.Provider.Tag.GetTagProviderOption(Name).Extensions = x.ToDictionary(pair => pair.Key, pair => pair.Value));
}
public string? Name { get; set; }
[Reactive] public int Priority { get; set; }
[Reactive] public ObservableCollection<ObservableKeyValuePair> Extensions { get; set; }
}
public class ObservableKeyValuePair : ViewModelBase
{
[Reactive] public string Key { get; set; }
[Reactive] public string Value { get; set; }
public ObservableKeyValuePair(string key, string value)
{
Key = key;
Value = value;
}
}
@@ -1,18 +0,0 @@
using System.Collections.ObjectModel;
using System.Linq;
using ZonyLrcTools.Common.Configuration;
namespace ZonyLrcTools.Desktop.ViewModels.Settings;
public class TagInfoViewModel : ViewModelBase
{
public TagInfoViewModel(GlobalOptions options)
{
BlockWord = new BlockWordViewModel(options);
TagInfoProviders = new ObservableCollection<TagInfoProviderViewModel>(options.Provider.Tag.Plugin.Select(p => new TagInfoProviderViewModel(p)));
}
public BlockWordViewModel BlockWord { get; }
public ObservableCollection<TagInfoProviderViewModel> TagInfoProviders { get; }
}
@@ -1,18 +0,0 @@
using System;
using System.Linq.Expressions;
using ReactiveUI;
namespace ZonyLrcTools.Desktop.ViewModels;
public abstract class ViewModelBase : ReactiveObject
{
protected void SubscribeToProperty<TSender, TRet>(TSender sender, Expression<Func<TSender, TRet>> property, Action<TRet> updateGlobalConfigAction)
{
sender.WhenAnyValue(property)
.Subscribe(x =>
{
updateGlobalConfigAction(x);
App.Current.UpdateConfiguration();
});
}
}