You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
7.1 KiB
206 lines
7.1 KiB
using AIK.Models;
|
|
using AIK.Models.ApiModels.TpmsModels;
|
|
using AIK.Service.IService;
|
|
using AIK.ViewModels.Messages;
|
|
using AIK.ViewModels.RemoteGeneration;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace AIK.ViewModels.TirePressure
|
|
{
|
|
public partial class TirePressureViewModel : ObservableObject, ILoadAware
|
|
{
|
|
private readonly IAikDataService _aikDataService;
|
|
private readonly IConfigurationService _configurationService;
|
|
private readonly IDataCacheService _dataCacheService;
|
|
private readonly ILogger<TirePressureViewModel> _logger;
|
|
private List<AlphabetGroup> groups = new();
|
|
[ObservableProperty]
|
|
private ObservableCollection<AlphabetGroup> _filteredGroups = new();
|
|
private readonly IMessenger _messenger;
|
|
[ObservableProperty]
|
|
private string searchText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private TpmsType? _selectedItem;
|
|
|
|
[ObservableProperty]
|
|
private bool _isLoading = false;
|
|
|
|
private bool isSingletonInstance = false;
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<TpmsType> detailGroups = new ObservableCollection<TpmsType>();
|
|
public TirePressureViewModel(IAikDataService aikDataService, IDataCacheService dataCacheService, ILogger<TirePressureViewModel> logger)
|
|
{
|
|
_aikDataService = aikDataService;
|
|
_dataCacheService = dataCacheService;
|
|
_logger = logger;
|
|
groups = CreateEmptyGroups();
|
|
FilteredGroups = new ObservableCollection<AlphabetGroup>(groups);
|
|
}
|
|
private List<AlphabetGroup> CreateEmptyGroups()
|
|
{
|
|
var list = new List<AlphabetGroup>();
|
|
for (char c = 'A'; c <= 'Z'; c++)
|
|
{
|
|
list.Add(new AlphabetGroup { Letter = c.ToString() });
|
|
}
|
|
return list;
|
|
}
|
|
public async Task OnLoadedAsync()
|
|
{
|
|
if (!isSingletonInstance)
|
|
{
|
|
await LoadDataInBackground();
|
|
isSingletonInstance = true;
|
|
}
|
|
|
|
}
|
|
private async Task LoadDataInBackground()
|
|
{
|
|
try
|
|
{
|
|
var tpmsdata = await _aikDataService.GetTpmsType(string.Empty,0);
|
|
tpmsdata = tpmsdata.OrderBy(a => a.py.Substring(0, 1)).ToList();
|
|
if (tpmsdata != null && tpmsdata.Count > 0)
|
|
{
|
|
foreach (var item in tpmsdata)
|
|
{
|
|
var firstName = item.py.Substring(0, 1).ToUpper();
|
|
var model = groups.Find(a => a.Letter == firstName);
|
|
if (model != null)
|
|
{
|
|
item.IsSelected = false;
|
|
model.Items.Add(item);
|
|
}
|
|
}
|
|
var tpmsModel = tpmsdata.FirstOrDefault();
|
|
if (tpmsModel != null)
|
|
{
|
|
tpmsModel.IsSelected = true;
|
|
//var usertoken = await _dataCacheService.GetLatestUserTokenAsync();
|
|
var detailGroupList = await _aikDataService.GetTpmsType(string.Empty,tpmsModel?.id);
|
|
if (detailGroupList!=null)
|
|
{
|
|
DetailGroups = new ObservableCollection<TpmsType>(detailGroupList);
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FilteredGroups = new ObservableCollection<AlphabetGroup>();
|
|
_messenger.Send(new CarSelectMessage(null));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "加载汽车品牌数据时出错");
|
|
throw;
|
|
}
|
|
//finally { IsLoading = false; }
|
|
|
|
}
|
|
#region 页面方法
|
|
[RelayCommand]
|
|
private async void WordClicked(TpmsType tpmsmodel)
|
|
{
|
|
if (!string.IsNullOrEmpty(tpmsmodel.py))
|
|
{
|
|
SelectedItem = tpmsmodel;
|
|
foreach (var group in FilteredGroups)
|
|
{
|
|
foreach (var item in group.Items)
|
|
{
|
|
item.IsSelected = false;
|
|
}
|
|
}
|
|
tpmsmodel.IsSelected = true;
|
|
var detailGroupList = await _aikDataService.GetTpmsType(string.Empty, tpmsmodel?.id);
|
|
if (detailGroupList != null)
|
|
{
|
|
DetailGroups = new ObservableCollection<TpmsType>(detailGroupList);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 具体型号点击事件
|
|
/// </summary>
|
|
/// <param name="tpmsmodel"></param>
|
|
[RelayCommand]
|
|
private async void SubItemClick(TpmsType tpmsmodel)
|
|
{
|
|
if (!string.IsNullOrEmpty(tpmsmodel.py))
|
|
{
|
|
SelectedItem = tpmsmodel;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Search()
|
|
{
|
|
UpdateFilteredGroups();
|
|
}
|
|
// 当搜索文本改变时自动过滤
|
|
partial void OnSearchTextChanged(string value)
|
|
{
|
|
UpdateFilteredGroups();
|
|
}
|
|
#endregion
|
|
private void UpdateFilteredGroups()
|
|
{
|
|
FilteredGroups.Clear();
|
|
var TempList = new List<AlphabetGroup>();
|
|
if (string.IsNullOrWhiteSpace(SearchText))
|
|
{
|
|
// 显示所有非空分组
|
|
foreach (var group in groups.Where(g => g.Items.Any()))
|
|
{
|
|
TempList.Add(group);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var searchLower = SearchText.ToLower();
|
|
|
|
foreach (var group in groups)
|
|
{
|
|
var filteredItems = group.Items
|
|
.Where(item =>
|
|
item.py?.ToLower().Contains(searchLower) == true
|
|
|| item.name?.ToLower().Contains(searchLower) == true
|
|
)
|
|
.ToList();
|
|
|
|
if (filteredItems.Any())
|
|
{
|
|
var filteredGroup = new AlphabetGroup();
|
|
foreach (var item in filteredItems)
|
|
{
|
|
filteredGroup.Items.Add(item);
|
|
}
|
|
TempList.Add(filteredGroup);
|
|
}
|
|
}
|
|
}
|
|
FilteredGroups = new ObservableCollection<AlphabetGroup>(TempList);
|
|
}
|
|
}
|
|
|
|
public class AlphabetGroup
|
|
{
|
|
public string Letter { get; set; }
|
|
public ObservableCollection<TpmsType> Items { get; set; } = new ObservableCollection<TpmsType>();
|
|
}
|
|
}
|
|
|