2026-03-30 17:09:29 +09:00

147 lines
5.1 KiB
C#

/*
* Copyright (c) 2022 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Aurora;
using Aurora.Api;
using Aurora.Api.Control;
using Aurora.Api.Manager;
using Aurora.Framework.Core;
using Aurora.View;
using Hermes;
using Hermes.Functional;
namespace AuroraUIComponent.UserControls
{
class UCRecipeSelectViewModel : NotifyPropertyChangedBase
{
private static readonly Logger LOG = new Logger("RecipeSelect");
public ICommand CMD_ActivateRecipe { get; }
//public class RecipeInfo : NotifyPropertyChangedBase
//{
// private int _Id;
// public int Id { get => _Id; set => SetProperty(ref _Id, value); }
// private string _Name;
// public string Name { get => _Name; set => SetProperty(ref _Name, value); }
// public RecipeInfo(int id, string name)
// {
// Id = id;
// Name = name;
// }
//}
private List<RecipeInfo> _Recipes;
public List<RecipeInfo> Recipes { get => _Recipes; set => SetProperty(ref _Recipes, value); }
private RecipeInfo _SelectedRecipe;
public RecipeInfo SelectedRecipe { get => _SelectedRecipe; set => SetProperty(ref _SelectedRecipe, value); }
private RecipeInfo _ActivatedRecipe;
public RecipeInfo ActivatedRecipe { get => _ActivatedRecipe; set => SetProperty(ref _ActivatedRecipe, value); }
private readonly IEventObservable _EventObservable;
private readonly IRecipeManager _RecipeManager;
//private readonly IModelManager _ModelManager;
public UCRecipeSelectViewModel(IRecipeManager manager, IEventObservable eventObservable)
{
_EventObservable = eventObservable;
_RecipeManager = manager;
//ModelManager = modelManager;
//RecipeMainViewModel = new UCRecipeMainViewModel(aurora);
//Recipes = new ObservableCollection<RecipeInfo>(_RecipeManager.Recipes().Map(r => new RecipeInfo(r.Id, r.Name)));
Recipes = _RecipeManager.Recipes().ToList();
_EventObservable.AsObservable(AuroraSysEvent.SYS_RECIPE_ENTRY_CHANGED).Subscribe(x => LoadRecipeList());
_EventObservable.AsObservable(AuroraSysEvent.SYS_RECIPE_ACTIVATED).Select(x => (RecipeInfo)x.Param).Subscribe(x => SelectRecipe(x));
CMD_ActivateRecipe = new DelegateCommand(HandleActivateRecipe);
LoadRecipeList();
}
private void LoadRecipeList()
{
//Recipes = new ObservableCollection<RecipeInfo>(_RecipeManager.Recipes().Map(r => new RecipeInfo(r.Id, r.Name)));
Recipes = _RecipeManager.Recipes().ToList();
SelectActivateRecipe();
}
private void SelectActivateRecipe()
{
SelectRecipe(_RecipeManager.ActivatedRecipe());
}
private void SelectRecipe(RecipeInfo recipe) // (int id, string name) recipe
{
if (Recipes.Count == 0) {
return;
}
//ActivatedRecipe = Recipes.FirstOrDefault(x => x.Id == recipe.Id && x.Name == recipe.Name);
ActivatedRecipe = recipe;
SelectedRecipe = ActivatedRecipe;
}
void HandleActivateRecipe(object obj)
{
//if (obj is RecipeInfo recipe) {
//if (_RecipeManager.Recipes().Any(x => x.Id == SelectedRecipe.Id && x.Name == SelectedRecipe.Name)) {
if (SelectedRecipe != null) {
var result = GetConfirmFromUser(SelectedRecipe, "ActivateRecipe", false);
if (result) {
int err = _RecipeManager.ActivateRecipe(SelectedRecipe.Id, "USER_via_CustomUI");
if (err != E1.OK) {
LOG.FuncE("Activate Fail");
}
ActivatedRecipe = SelectedRecipe;
//if (_ModelSubject != null)
//{
// _ModelName = _RecipeManager.ActivatedModelName();
// ModelManager.ActivateModel(_ModelName, "USER_via_UI");
// NotifyPropertyChanged("ModelName");
//}
}
}
}
bool GetConfirmFromUser(RecipeInfo templateVM, string titleTokenKey, bool editEnabled = true)
{
var result = MessageDialog.Show(
Application.Current.MainWindow,
Application.Current.FindStrToken(titleTokenKey),
new EditRecipeView {
DataContext = templateVM,
IsEnabled = editEnabled
},
MessageDialog.Type.AffirmativeAndNegative
);
if (result != MessageDialog.Result.Affirmative)
return false;
return true;
}
}
}