first edit
This commit is contained in:
113
XLIMS.PSV/ViewModels/BrowseSerialViewModel.cs
Normal file
113
XLIMS.PSV/ViewModels/BrowseSerialViewModel.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using XLIMS.CONTRACT;
|
||||
using XLIMS.DATA.Models;
|
||||
using XLIMS.MVVM.Base;
|
||||
|
||||
namespace XLIMS.PSV.ViewModels
|
||||
{
|
||||
|
||||
public class BrowseSerialViewModel : ViewModelBase, IDisposable
|
||||
{
|
||||
private readonly ILimsService _limsService;
|
||||
private int _divisionId;
|
||||
|
||||
private DeviceSet _currentSerial = null;
|
||||
private string _search = string.Empty;
|
||||
private CancellationTokenSource? _searchCts = null;
|
||||
private bool _disposed = false;
|
||||
|
||||
public ObservableCollection<DeviceSet> AllSerial { get; } = new();
|
||||
public ICollectionView SerialView { get; }
|
||||
|
||||
public DeviceSet? CurrentSerial
|
||||
{
|
||||
get => _currentSerial;
|
||||
set
|
||||
{
|
||||
_currentSerial = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string Search
|
||||
{
|
||||
get => _search;
|
||||
set
|
||||
{
|
||||
_search = value;
|
||||
DebounceSearch();
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public BrowseSerialViewModel(ILimsService limsService, int divisionId)
|
||||
{
|
||||
_limsService = limsService;
|
||||
_divisionId = divisionId;
|
||||
|
||||
SerialView = CollectionViewSource.GetDefaultView(AllSerial);
|
||||
SerialView.Filter = SerialFilter;
|
||||
|
||||
LoadSerialsAsync();
|
||||
}
|
||||
|
||||
private async Task LoadSerialsAsync()
|
||||
{
|
||||
AllSerial.Clear();
|
||||
var devList = await _limsService.Devices.GetAllAsync();
|
||||
foreach (var dev in devList.Where(e=>e.DivisionId==_divisionId))
|
||||
{
|
||||
AllSerial.Add(dev);
|
||||
}
|
||||
SerialView.Refresh();
|
||||
}
|
||||
private bool SerialFilter(object item)
|
||||
{
|
||||
if (item is not DeviceSet dev) return false;
|
||||
if (string.IsNullOrWhiteSpace(Search)) return true;
|
||||
|
||||
var search = Search.Trim();
|
||||
return (dev.Serial?.Contains(search, StringComparison.OrdinalIgnoreCase) == true)
|
||||
|| (dev.Dpzn?.Contains(search, StringComparison.OrdinalIgnoreCase) == true)
|
||||
|| (dev.Tip?.Contains(search, StringComparison.OrdinalIgnoreCase) == true)
|
||||
|| (dev.Name?.Contains(search, StringComparison.OrdinalIgnoreCase) == true);
|
||||
}
|
||||
|
||||
private ICommand? _saveCommand = null;
|
||||
public ICommand SaveCommand => _saveCommand ??= new RelayCommand(_ => Save(), _ => CurrentSerial != null);
|
||||
|
||||
private void Save()
|
||||
{
|
||||
}
|
||||
|
||||
private async void DebounceSearch()
|
||||
{
|
||||
_searchCts?.Cancel();
|
||||
_searchCts = new CancellationTokenSource();
|
||||
try
|
||||
{
|
||||
await Task.Delay(300, _searchCts.Token);
|
||||
SerialView.Refresh();
|
||||
}
|
||||
catch (TaskCanceledException) { }
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user