sharee.bike-App/TINKLib/ViewModel/Map/TinkKonradToggleViewModel.cs

83 lines
3.4 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Linq;
using TINK.Model;
using TINK.Model.Connector;
2021-11-07 21:28:13 +01:00
using TINK.Model.Connector.Filter;
2021-05-13 20:03:07 +02:00
using Xamarin.Forms;
namespace TINK.ViewModel.Map
{
/// <remarks> Old name: MapPageFilter. </remarks>
public class TinkKonradToggleViewModel : ITinkKonradToggleViewModel
{
/// <summary> Constructs a map page filter view model object from values.</summary>
/// <param name="currentFilter">Filters and options dictionary.</param>
public TinkKonradToggleViewModel(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 TINK is enabled or not. </summary>
2022-01-04 18:54:03 +01:00
public bool IsTinkEnabled => !string.IsNullOrEmpty(CurrentFilter) && CurrentFilter.GetBikeCategory() != FilterHelper.CARGOBIKE;
2021-05-13 20:03:07 +02:00
/// <summary> Gets color of the TINK button. </summary>
2022-01-04 18:54:03 +01:00
public Color TinkColor => CurrentFilter.GetBikeCategory() == FilterHelper.CARGOBIKE ? Color.Blue : Color.Gray;
2021-05-13 20:03:07 +02:00
/// <summary> Gets value whether Konrad is enabled or not. </summary>
2022-01-04 18:54:03 +01:00
public bool IsKonradEnabled => !string.IsNullOrEmpty(CurrentFilter) && CurrentFilter.GetBikeCategory() != FilterHelper.CITYBIKE;
2021-05-13 20:03:07 +02:00
/// <summary> Gets color of the Konrad button. </summary>
2022-01-04 18:54:03 +01:00
public Color KonradColor => CurrentFilter.GetBikeCategory() == FilterHelper.CITYBIKE ? Color.Red : Color.Gray;
2021-05-13 20:03:07 +02:00
/// <summary> Gets whether toggle functionality is visible or not. </summary>
public bool IsToggleVisible =>
2022-01-04 18:54:03 +01:00
FilterDictionary.Select(x => x.Key).ContainsGroupId(FilterHelper.CITYBIKE)
&& FilterDictionary.Select(x => x.Key).ContainsGroupId(FilterHelper.CARGOBIKE)
2021-05-13 20:03:07 +02:00
&& (IsTinkEnabled || IsKonradEnabled);
/// <summary>
/// Toggles from bike group TINK to Konrad or vice versa.
/// Toggling means one of
/// - TINK => Konrad or
/// - TINK => Konrad.
/// </summary>
/// <param name="p_strCurrentFilterSet">Filter set to toggle.</param>
/// <returns></returns>
public TinkKonradToggleViewModel DoToggle()
{
var l_oCurrentFilterSet = FilterDictionary.ToArray();
if (l_oCurrentFilterSet.Length < 2)
{
// There is nothing to toggle because filter set contains only one element.
return new TinkKonradToggleViewModel(FilterDictionary);
}
var l_oCurrentState = l_oCurrentFilterSet[l_oCurrentFilterSet.Length - 1].Value == FilterState.On
? FilterState.On
: FilterState.Off;
var l_oToggledFilterSet = new Dictionary<string, FilterState>();
foreach (var l_oFilterElement in l_oCurrentFilterSet)
{
l_oToggledFilterSet.Add(l_oFilterElement.Key, l_oCurrentState);
l_oCurrentState = l_oFilterElement.Value;
}
return new TinkKonradToggleViewModel(new GroupFilterMapPage(l_oToggledFilterSet));
}
}
}