sharee.bike-App/SharedBusinessLogic/ViewModel/Map/CargoCitybikeToggleViewModel.cs

85 lines
3.3 KiB
C#
Raw Normal View History

2024-04-09 12:53:23 +02:00
using System.Collections.Generic;
using System.Linq;
using ShareeBike.Model;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Connector.Filter;
using Xamarin.Forms;
namespace ShareeBike.ViewModel.Map
{
/// <remarks> Old name: MapPageFilter. </remarks>
public class CargoCitybikeToggleViewModel : ICargoCitybikeToggleViewModel
{
/// <summary> Constructs a map page filter view model object from values.</summary>
/// <param name="currentFilter">Filters and options dictionary.</param>
public CargoCitybikeToggleViewModel(IGroupFilterMapPage currentFilter)
{
FilterDictionary = currentFilter ?? new GroupFilterMapPage();
}
/// <summary> Gets the filter values.</summary>
public IGroupFilterMapPage FilterDictionary { get; }
/// <summary> Gets the activated filter.</summary>
public string CurrentFilter
{
get
{
return FilterDictionary.FirstOrDefault(x => x.Value == FilterState.On).Key ?? string.Empty;
}
}
/// <summary> Gets value whether ShareeBike is enabled or not. </summary>
public bool IsCargoEnabled => !string.IsNullOrEmpty(CurrentFilter) && CurrentFilter.GetBikeCategory() != FilterHelper.CARGOBIKE;
/// <summary> Gets color of the ShareeBike button. </summary>
public Color CargoColor => CurrentFilter.GetBikeCategory() == FilterHelper.CARGOBIKE ? Color.White : Color.FromRgba(0, 0, 0, 0);
public Color NoCargoColor => CurrentFilter.GetBikeCategory() == FilterHelper.CARGOBIKE ? Color.FromRgb(210, 17, 20) : Color.White;
/// <summary> Gets value whether Citybike is enabled or not. </summary>
public bool IsCitybikeEnabled => !string.IsNullOrEmpty(CurrentFilter) && CurrentFilter.GetBikeCategory() != FilterHelper.CITYBIKE;
/// <summary> Gets color of the Citybike button. </summary>
public Color CitybikeColor => CurrentFilter.GetBikeCategory() == FilterHelper.CITYBIKE ? Color.White : Color.FromRgba(0, 0, 0, 0);
public Color NoCitybikeColor => CurrentFilter.GetBikeCategory() == FilterHelper.CITYBIKE ? Color.FromRgb(210, 17, 20) : Color.White;
/// <summary> Gets whether toggle functionality is visible or not. </summary>
public bool IsToggleVisible =>
FilterDictionary.Select(x => x.Key).ContainsGroupId(FilterHelper.CITYBIKE)
&& FilterDictionary.Select(x => x.Key).ContainsGroupId(FilterHelper.CARGOBIKE)
&& (IsCargoEnabled || IsCitybikeEnabled);
/// <summary>
/// Toggles from bike group Cargo to Citybike or vice versa.
/// Toggling means one of
/// - ShareeBike => Citybike or
/// - ShareeBike => Citybike.
/// </summary>
/// <param name="p_strCurrentFilterSet">Filter set to toggle.</param>
/// <returns></returns>
public CargoCitybikeToggleViewModel DoToggle()
{
var currentFilterSet = FilterDictionary.ToArray();
if (currentFilterSet.Length < 2)
{
// There is nothing to toggle because filter set contains only one element.
return new CargoCitybikeToggleViewModel(FilterDictionary);
}
var currentState = currentFilterSet[currentFilterSet.Length - 1].Value == FilterState.On
? FilterState.On
: FilterState.Off;
var toggledFilterSet = new Dictionary<string, FilterState>();
foreach (var filterElement in currentFilterSet)
{
toggledFilterSet.Add(filterElement.Key, currentState);
currentState = filterElement.Value;
}
return new CargoCitybikeToggleViewModel(new GroupFilterMapPage(toggledFilterSet));
}
}
}