2026-06-05 13:44:39 +09:00

267 lines
9.8 KiB
C#

/*
* Copyright (c) 2022 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using Aurora;
using Aurora.Api;
using Aurora.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace AuroraUIComponent.UserControls
{
public class UCSetAllButtonsViewModel : NotifyPropertyChangedBase
{
public ICommand CMD_SetAll { get; set; }
public ICommand DelegateSetAllCommand { get; set; }
//private GridLength _SetButtonHeight = new GridLength(40);
//public GridLength SetButtonHeight { get => _SetButtonHeight; set => SetProperty(ref _SetButtonHeight, value); }
public class EditItem : NotifyPropertyChangedBase
{
public string Key { get => _Key; set => SetProperty(ref _Key, value); }
private string _Key;
public string Label { get => _Label; set => SetProperty(ref _Label, value); }
private string _Label;
public string LabelToken { get => _LabelToken; set => SetProperty(ref _LabelToken, value); }
private string _LabelToken;
public object DefaultValue {
get {
if (ValueType == typeof(double)) {
return DoubleValue;
} else if (ValueType == typeof(int)) {
return IntValue;
} else if (ValueType == typeof(string)) {
return StringValue;
} else if (ValueType == typeof(bool)) {
return BoolValue;
}
return null;
}
set {
if (ValueType == typeof(double)) {
DoubleValue = (double)value;
} else if (ValueType == typeof(int)) {
IntValue = (int)value;
} else if (ValueType == typeof(string)) {
StringValue = (string)value;
} else if (ValueType == typeof(bool)) {
BoolValue = (bool)value;
}
}
}
public Type ValueType { get => _ValueType; set => SetProperty(ref _ValueType, value); }
private Type _ValueType;
public bool IsHidden { get => _IsHidden; set => SetProperty(ref _IsHidden, value); }
private bool _IsHidden = false;
public string StringValue { get => _StringValue; set { if (SetProperty(ref _StringValue, value)) { NotifyPropertyChanged("DefaultValue"); } } }
private string _StringValue = "";
public bool BoolValue { get => _BoolValue; set { if (SetProperty(ref _BoolValue, value)) { NotifyPropertyChanged("DefaultValue"); } } }
private bool _BoolValue = false;
public int IntValue { get => _IntValue; set { if (SetProperty(ref _IntValue, value)) { NotifyPropertyChanged("NumberValue"); NotifyPropertyChanged("DefaultValue"); } } }
private int _IntValue = 0;
public double DoubleValue { get => _DoubleValue; set { if (SetProperty(ref _DoubleValue, value)) { NotifyPropertyChanged("NumberValue"); NotifyPropertyChanged("DefaultValue"); } } }
private double _DoubleValue = 0.0;
public double NumberValue {
get {
return ValueType == typeof(double) ? DoubleValue : IntValue;
}
set {
if (ValueType == typeof(double)) {
DoubleValue = (double)value;
} else if (ValueType == typeof(int)) {
IntValue = (int)value;
}
}
}
public EditItem(string label, string key, bool isHidden, Type valueType)
{
Label = label;
Key = key;
IsHidden = isHidden;
ValueType = valueType;
NotifyPropertyChanged("DefaultValue");
UpdateToken();
}
public EditItem(string key, bool isHidden, Type valueType) : this(key, key, isHidden, valueType) { }
public void UpdateToken()
{
var labelToken = Application.Current.FindStrToken(Label);
LabelToken = labelToken == null ? Label : labelToken;
}
}
public UCSetAllButtonsViewModel()
{
CMD_SetAll = new DelegateCommand(SetAllItem);
Application.Current.AuroraEnv().EventObservable.AsObservable(AuroraSysEvent.SYS_LANGUAGE_CHANGED).Subscribe(_ => UpdateTokens());
}
private void UpdateTokens()
{
foreach (var item in ItemList) {
item.UpdateToken();
}
}
public bool IsSupportedType(Type valueType)
{
if (valueType == typeof(double)) {
return true;
} else if (valueType == typeof(int)) {
return true;
} else if (valueType == typeof(string)) {
return true;
} else if (valueType == typeof(bool)) {
return true;
}
return false;
}
public bool AddItem(string key, Type valueType)
{
return AddItem(key, key, false, valueType);
}
public bool AddItem(string label, string key, bool isHidden, Type valueType)
{
if (ItemList.Count(x => x.Key == key) > 0) return false;
if (IsSupportedType(valueType)) {
ItemList.Add(new EditItem(label, key, isHidden, valueType));
}
return true;
}
private void SetAllItem(object obj)
{
//if (obj is string strKey) {
// var findItem = ItemList.FirstOrDefault(x => x.Key == strKey);
// if (findItem != null) {
// if (DelegateSetAllCommand != null) {
// DelegateSetAllCommand.Execute(findItem);
// }
// }
//}
if (DelegateSetAllCommand != null) {
DelegateSetAllCommand.Execute(obj as EditItem);
}
}
public IList<EditItem> ItemList { get => _ItemList; set => SetProperty(ref _ItemList, value); }
private IList<EditItem> _ItemList = new ObservableCollection<EditItem>();
}
public class UCSetAllButtonsTemplate<T> : NotifyPropertyChangedBase
{
private UCSetAllButtonsViewModel _SetAllDataVM;
public UCSetAllButtonsViewModel SetAllDataVM { get => _SetAllDataVM; set => SetProperty(ref _SetAllDataVM, value); }
private List<(string label, string key, bool isHidden)> _ShowPropertyList;
public List<(string label, string key, bool isHidden)> ShowPropertyList { get => _ShowPropertyList; set => SetProperty(ref _ShowPropertyList, value); }
private IList<T> _DataList;
public IList<T> DataList { get => _DataList; set => SetProperty(ref _DataList, value); }
public UCSetAllButtonsTemplate(IList<T> dataList, List<(string label, string key, bool isHidden)> propList)
{
DataList = dataList;
bool addAll = !(propList?.Count > 0);
if (addAll) {
AddAllProperties();
} else {
ShowPropertyList = propList;
}
SetAllDataVM = new UCSetAllButtonsViewModel();
var properties = typeof(T).GetProperties();
foreach (var item in ShowPropertyList) {
var prop = properties.FirstOrDefault(x => x.Name == item.key);
if (prop != null) {
SetAllDataVM.AddItem(item.label, item.key, item.isHidden, prop.PropertyType);
}
}
SetAllDataVM.DelegateSetAllCommand = new DelegateCommand(ProcessSetAllData);
}
public void UpdateRecipe(IList<T> dataList)
{
DataList = dataList;
}
private void AddAllProperties()
{
ShowPropertyList = new List<(string label, string key, bool isHidden)>();
foreach (PropertyInfo property in typeof(T).GetProperties()) {
ShowPropertyList.Add((property.Name, property.Name, false));
}
}
public void CopyDefaultSetting(T recipe)
{
foreach (var item in SetAllDataVM.ItemList) {
foreach (var propertyInfo in typeof(T).GetProperties()) {
if (item.Key == propertyInfo.Name) {
item.DefaultValue = propertyInfo.GetValue(recipe);
}
}
}
}
private void CopyProperty(string key, object value)
{
foreach (var item in DataList) {
PropertyInfo propertyInfo = typeof(T).GetProperty(key);
if (propertyInfo != null) {
propertyInfo.SetValue(item, value);
}
}
}
private void ProcessSetAllData(object obj)
{
if (obj is UCSetAllButtonsViewModel.EditItem editItem) {
if (editItem.ValueType == typeof(double)) {
CopyProperty(editItem.Key, editItem.DoubleValue);
} else if (editItem.ValueType == typeof(int)) {
CopyProperty(editItem.Key, editItem.IntValue);
} else if (editItem.ValueType == typeof(string)) {
CopyProperty(editItem.Key, editItem.StringValue);
} else if (editItem.ValueType == typeof(bool)) {
CopyProperty(editItem.Key, editItem.BoolValue);
}
}
}
}
}