using System; using System.Collections.ObjectModel; using System.Linq; using InABox.Core; using Xamarin.CommunityToolkit.ObjectModel; using Xamarin.Forms; namespace PRS.Mobile { public class StockLocationSelectionViewModel : BindableObject { public bool MultiSelect { get; set; } public bool DisplayStockTakeInfo { get; set; } public bool CurrentStockTakesOnly { get; set; } = false; public bool IncludeInactive { get; set; } public bool PullToRefresh { get => _pullToRefresh; set { if (value == _pullToRefresh) return; _pullToRefresh = value; OnPropertyChanged(); } } public DateTime LastUpdated { get; set; } private StockWarehouseShell _warehouse; public StockWarehouseShell Warehouse { get => _warehouse; set { if (Equals(value, _warehouse)) return; _warehouse = value; Area = new StockAreaShell(); } } private StockAreaShell _area; public StockAreaShell Area { get => _area; set { if (Equals(value, _area)) return; _area = value; ReloadLocations(); } } public int SelectedPage { get; set; } public ObservableRangeCollection Locations { get; private set; } public ObservableRangeCollection Favourites { get; private set; } public ObservableCollection Selected { get; private set; } public StockLocationSelectionViewModel() { _excludedLocations = new Guid[] { }; _warehouse = new StockWarehouseShell(); _area = new StockAreaShell(); Selected = new ObservableCollection(); Selected.CollectionChanged += (sender, args) => ReloadLocations(); //App.Data.StockLocations.Refresh(false); Favourites = new ObservableRangeCollection(App.Data.StockLocations.Where(x => x.Favourite)); Locations = new ObservableRangeCollection(App.Data.StockLocations.ToArray()); } private Guid[] _excludedLocations; public Guid[] ExcludedLocations { get => _excludedLocations; set { _excludedLocations = value; Favourites.ReplaceRange(App.Data.StockLocations.Where(x => x.Favourite && !ExcludedLocations.Contains(x.ID))); Locations.ReplaceRange(App.Data.StockLocations.Where(x => !ExcludedLocations.Contains(x.ID))); } } public bool AddVisible { get; set; } private string _searchText; private bool _pullToRefresh; public string SearchText { get => _searchText; set { if (value == _searchText) return; _searchText = value; ReloadLocations(); OnPropertyChanged(); } } public void RefreshData(bool force) { App.Data.StockLocations.Refresh(force); if (PullToRefresh) LastUpdated = App.Data.StockLocations.LastUpdated; ReloadLocations(); } public void ReloadLocations() { var favourites = App.Data.StockLocations.Where(x => Selected.All(s => s.ID != x.ID) && (IncludeInactive || x.Active) && x.Favourite && !ExcludedLocations.Contains(x.ID) ); if (DisplayStockTakeInfo && CurrentStockTakesOnly) favourites = favourites.Where(x => !x.CurrentStocktake.IsEmpty()); Favourites.ReplaceRange(favourites); var locations = App.Data.StockLocations.Where(x => Selected.All(s => s.ID != x.ID) && (IncludeInactive || x.Active) && (_area.ID == Guid.Empty || x.AreaID == _area.ID) && (_warehouse.ID == Guid.Empty || x.WarehouseID == _warehouse.ID) && !ExcludedLocations.Contains(x.ID) && (String.IsNullOrWhiteSpace(_searchText) || x.Code.ToUpper().Contains(_searchText.ToUpper()) || x.Description.ToUpper().Contains(_searchText.ToUpper()) ) ); if (DisplayStockTakeInfo && CurrentStockTakesOnly) locations = locations.Where(x => !x.CurrentStocktake.IsEmpty()); Locations.ReplaceRange(locations); } } }