using System.ComponentModel;
namespace TINK.Model.Bikes.BikeInfoNS.DriveNS.BatteryNS
{
///
/// Manages the state of a chargeable battery.
///
public class BatteryMutable : IBatteryMutable, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
IBattery _battery;
public BatteryMutable(IBattery battery)
{
_battery = battery;
}
///
/// Gets the current charging level of the battery in percent, double.NaN if unknown.
///
public double CurrentChargePercent => _battery.CurrentChargePercent;
///
/// Gets or sets the current charging level of the battery in bars, null if unknown.
///
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)));
}
}
///
/// Gets the maximum charging level of the battery in bars, null if unknown.
///
public int? MaxChargeBars => _battery.MaxChargeBars;
///
/// Gets whether backend is aware of battery charging level.
///
public bool? IsBackendAccessible => _battery.IsBackendAccessible;
///
/// Gets whether to display battery level or not.
///
public bool? IsHidden => _battery.IsHidden;
}
}