2025-04-02 03:44:23 +00:00
|
|
|
namespace ServiceLib.ViewModels;
|
|
|
|
|
|
|
|
|
|
public class SubSettingViewModel : MyReactiveObject
|
2023-01-01 11:42:01 +00:00
|
|
|
{
|
2025-08-31 07:41:25 +00:00
|
|
|
public IObservableCollection<SubItem> SubItems { get; } = new ObservableCollectionExtended<SubItem>();
|
2023-04-14 12:49:36 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
[Reactive]
|
|
|
|
|
public SubItem SelectedSource { get; set; }
|
2023-04-14 12:49:36 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public IList<SubItem> SelectedSources { get; set; }
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public ReactiveCommand<Unit, Unit> SubAddCmd { get; }
|
|
|
|
|
public ReactiveCommand<Unit, Unit> SubDeleteCmd { get; }
|
|
|
|
|
public ReactiveCommand<Unit, Unit> SubEditCmd { get; }
|
|
|
|
|
public ReactiveCommand<Unit, Unit> SubShareCmd { get; }
|
|
|
|
|
public bool IsModified { get; set; }
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public SubSettingViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
|
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
_config = AppManager.Instance.Config;
|
2025-04-02 03:44:23 +00:00
|
|
|
_updateView = updateView;
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
var canEditRemove = this.WhenAnyValue(
|
|
|
|
|
x => x.SelectedSource,
|
|
|
|
|
selectedSource => selectedSource != null && !selectedSource.Id.IsNullOrEmpty());
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
SubAddCmd = ReactiveCommand.CreateFromTask(async () =>
|
|
|
|
|
{
|
|
|
|
|
await EditSubAsync(true);
|
|
|
|
|
});
|
|
|
|
|
SubDeleteCmd = ReactiveCommand.CreateFromTask(async () =>
|
|
|
|
|
{
|
|
|
|
|
await DeleteSubAsync();
|
|
|
|
|
}, canEditRemove);
|
|
|
|
|
SubEditCmd = ReactiveCommand.CreateFromTask(async () =>
|
|
|
|
|
{
|
|
|
|
|
await EditSubAsync(false);
|
|
|
|
|
}, canEditRemove);
|
|
|
|
|
SubShareCmd = ReactiveCommand.CreateFromTask(async () =>
|
|
|
|
|
{
|
|
|
|
|
await _updateView?.Invoke(EViewAction.ShareSub, SelectedSource?.Url);
|
|
|
|
|
}, canEditRemove);
|
2024-10-21 05:46:13 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
_ = Init();
|
|
|
|
|
}
|
2024-10-21 05:46:13 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private async Task Init()
|
|
|
|
|
{
|
|
|
|
|
SelectedSource = new();
|
2024-10-21 05:46:13 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
await RefreshSubItems();
|
|
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public async Task RefreshSubItems()
|
|
|
|
|
{
|
2025-08-31 07:41:25 +00:00
|
|
|
SubItems.Clear();
|
|
|
|
|
SubItems.AddRange(await AppManager.Instance.SubItems());
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public async Task EditSubAsync(bool blNew)
|
|
|
|
|
{
|
|
|
|
|
SubItem item;
|
|
|
|
|
if (blNew)
|
2023-01-01 11:42:01 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
item = new();
|
2023-01-01 11:42:01 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
else
|
2023-01-01 11:42:01 +00:00
|
|
|
{
|
2025-08-17 09:31:55 +00:00
|
|
|
item = await AppManager.Instance.GetSubItem(SelectedSource?.Id);
|
2025-04-02 03:44:23 +00:00
|
|
|
if (item is null)
|
2023-01-01 11:42:01 +00:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
if (await _updateView?.Invoke(EViewAction.SubEditWindow, item) == true)
|
|
|
|
|
{
|
2024-10-21 01:45:33 +00:00
|
|
|
await RefreshSubItems();
|
2023-01-01 11:42:01 +00:00
|
|
|
IsModified = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
|
|
|
|
|
private async Task DeleteSubAsync()
|
|
|
|
|
{
|
|
|
|
|
if (await _updateView?.Invoke(EViewAction.ShowYesNo, null) == false)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var it in SelectedSources ?? [SelectedSource])
|
|
|
|
|
{
|
|
|
|
|
await ConfigHandler.DeleteSubItem(_config, it.Id);
|
|
|
|
|
}
|
|
|
|
|
await RefreshSubItems();
|
2025-08-17 09:31:55 +00:00
|
|
|
NoticeManager.Instance.Enqueue(ResUI.OperationSuccess);
|
2025-04-02 03:44:23 +00:00
|
|
|
IsModified = true;
|
|
|
|
|
}
|
2025-01-30 09:10:05 +00:00
|
|
|
}
|