2024-04-09 12:53:23 +02:00
|
|
|
using SharedGui.ViewModel;
|
2022-08-30 15:42:25 +02:00
|
|
|
using Xamarin.Forms;
|
|
|
|
using Xamarin.Forms.Xaml;
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
namespace SharedGui.View
|
2022-08-30 15:42:25 +02:00
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
[XamlCompilation(XamlCompilationOptions.Compile)]
|
|
|
|
public partial class BarLevelView : ContentView
|
|
|
|
{
|
|
|
|
private BarLevelViewModel _LocalViewModel = new BarLevelViewModel();
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public BarLevelView()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
// Do not bind entire control to local view model. Doing this would break binding of BatteryView control.
|
|
|
|
this.BarLevelLabel.BindingContext = _LocalViewModel;
|
|
|
|
this.BarLevelImage.BindingContext = _LocalViewModel;
|
|
|
|
}
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
2023-08-31 12:31:38 +02:00
|
|
|
/// Create bindable property to allow "Maximum"- property to act as a valid target for data binding.
|
2022-09-06 16:08:19 +02:00
|
|
|
/// </summary>
|
|
|
|
public static readonly BindableProperty MaximumProperty = BindableProperty.Create(
|
|
|
|
"Maximum",
|
|
|
|
typeof(string),
|
|
|
|
typeof(BarLevelView),
|
|
|
|
"", // Default value must match default value of view model object. Otherwise there might be an update issue.
|
|
|
|
propertyChanged: OnMaximumChanged);
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
2023-08-31 12:31:38 +02:00
|
|
|
/// Holds the count of bars which represent charing level full.
|
2022-09-06 16:08:19 +02:00
|
|
|
/// </summary>
|
|
|
|
public string Maximum
|
|
|
|
{
|
|
|
|
get => (string)GetValue(MaximumProperty);
|
|
|
|
set => SetValue(MaximumProperty, value);
|
|
|
|
}
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Notifies when value changes. Is required to make binding work.
|
|
|
|
/// </summary>
|
|
|
|
static void OnMaximumChanged(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
{
|
|
|
|
if (newValue != oldValue)
|
|
|
|
{
|
|
|
|
var batteryView = bindable as BarLevelView;
|
|
|
|
batteryView?.SetMaximum(int.TryParse(newValue.ToString(), out int maximum) ? (int?)maximum : null);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public void SetMaximum(int? maximum) => _LocalViewModel.Maximum = maximum;
|
2022-08-30 15:42:25 +02:00
|
|
|
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
2023-08-31 12:31:38 +02:00
|
|
|
/// Create bindable property to allow "Current"- property to act as a valid target for data binding.
|
2022-09-06 16:08:19 +02:00
|
|
|
/// </summary>
|
|
|
|
public static readonly BindableProperty CurrentProperty = BindableProperty.Create(
|
|
|
|
"Current",
|
|
|
|
typeof(string),
|
|
|
|
typeof(BarLevelView),
|
|
|
|
"", // Default value must match default value of view model object. Otherwise there might be an update issue.
|
|
|
|
propertyChanged: OnCurrentChanged);
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
2023-08-31 12:31:38 +02:00
|
|
|
/// Holds the count of bars which represent the current charing level.
|
2022-09-06 16:08:19 +02:00
|
|
|
/// </summary>
|
|
|
|
public string Current
|
|
|
|
{
|
|
|
|
get => (string)GetValue(CurrentProperty);
|
|
|
|
set => SetValue(CurrentProperty, value);
|
|
|
|
}
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Notifies when value changes. Is required to make binding work.
|
|
|
|
/// </summary>
|
|
|
|
static void OnCurrentChanged(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
{
|
|
|
|
if (newValue != oldValue)
|
|
|
|
{
|
|
|
|
var batteryView = bindable as BarLevelView;
|
|
|
|
int current;
|
|
|
|
batteryView?.SetCurrent(int.TryParse(newValue.ToString(), out current) ? (int?)current : null);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public void SetCurrent(int? current) => _LocalViewModel.Current = current;
|
|
|
|
}
|
2023-08-31 12:31:38 +02:00
|
|
|
}
|