sharee.bike-App/SharedBusinessLogic/Model/Bikes/BikeInfoNS/DriveNS/BatteryNS/BatteryMutable.cs
2024-04-09 12:53:23 +02:00

84 lines
2.2 KiB
C#

using System.ComponentModel;
namespace ShareeBike.Model.Bikes.BikeInfoNS.DriveNS.BatteryNS
{
/// <summary>
/// Manages the state of a chargeable battery.
/// </summary>
public class BatteryMutable : IBatteryMutable, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
IBattery _battery;
public BatteryMutable(IBattery battery)
{
_battery = battery;
}
/// <summary>
/// Gets the current charging level of the battery in percent, double.NaN if unknown.
/// </summary>
public double CurrentChargePercent => _battery.CurrentChargePercent;
/// <summary>
/// Gets or sets the current charging level of the battery in bars, null if unknown.
/// </summary>
public int? CurrentChargeBars
{
get => _battery.CurrentChargeBars;
set
{
double GetCurrentChargePercent()
{
if (value == null)
{
// Filling level is unknown.
return double.NaN;
}
if (_battery.MaxChargeBars == null || _battery.MaxChargeBars == 0)
{
// Percentage filling level can not be calculated.
return _battery.CurrentChargePercent;
}
return (int)(100 * value / _battery.MaxChargeBars);
}
if (_battery.CurrentChargeBars == value)
{
// Nothing to do.
return;
}
_battery = new Battery.Builder
{
MaxChargeBars = _battery.MaxChargeBars,
IsBackendAccessible = _battery.IsBackendAccessible,
IsHidden = _battery.IsHidden,
CurrentChargeBars = value,
CurrentChargePercent = GetCurrentChargePercent(),
}.Build();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentChargeBars)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentChargePercent)));
}
}
/// <summary>
/// Gets the maximum charging level of the battery in bars, null if unknown.
/// </summary>
public int? MaxChargeBars => _battery.MaxChargeBars;
/// <summary>
/// Gets whether backend is aware of battery charging level.
/// </summary>
public bool? IsBackendAccessible => _battery.IsBackendAccessible;
/// <summary>
/// Gets whether to display battery level or not.
/// </summary>
public bool? IsHidden => _battery.IsHidden;
}
}