From 684393f85db7d7f059e2e8e98458d7d2e8a63f72 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sun, 7 Apr 2019 15:50:00 +0200 Subject: [PATCH] Add PlantPage logic --- .../CaptorValueToDoubleConverter.cs | 32 ++++++ .../PlantBox.Client/Forms/HomePage.xaml.cs | 7 +- .../Forms/Plant/PlantPage.xaml.cs | 4 +- .../PlantBox.Client/Models/Historic.cs | 28 +++++ .../PlantBox.Client/Models/HistoricEntry.cs | 28 +++++ .../ViewModels/PlantViewModel.cs | 103 ++++++++++++++++++ 6 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 PlantBox.Client/PlantBox.Client/Converters/CaptorValueToDoubleConverter.cs create mode 100644 PlantBox.Client/PlantBox.Client/Models/Historic.cs create mode 100644 PlantBox.Client/PlantBox.Client/Models/HistoricEntry.cs create mode 100644 PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs diff --git a/PlantBox.Client/PlantBox.Client/Converters/CaptorValueToDoubleConverter.cs b/PlantBox.Client/PlantBox.Client/Converters/CaptorValueToDoubleConverter.cs new file mode 100644 index 0000000..51d739c --- /dev/null +++ b/PlantBox.Client/PlantBox.Client/Converters/CaptorValueToDoubleConverter.cs @@ -0,0 +1,32 @@ +using PlantBox.Shared.Communication.Commands; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; +using Xamarin.Forms; + +namespace PlantBox.Client.Converters +{ + class CaptorValueToDoubleConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is CaptorValue captor) + { + return (captor.Value - captor.Min) / (captor.Max - captor.Min); + } + + throw new ArgumentException($"Invalid source: excepted 'CaptorValue' and got '${value.GetType().Name}'", nameof(value)); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is double step && parameter is CaptorValue captor) + { + return new CaptorValue(captor.Min, captor.Max, (captor.Max - captor.Min) * step - captor.Max); + } + + throw new ArgumentException(); + } + } +} diff --git a/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs b/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs index 4a14150..771db09 100644 --- a/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs +++ b/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs @@ -1,5 +1,6 @@ using PlantBox.Client.Forms.Plant; using PlantBox.Client.Models; +using PlantBox.Client.ViewModels; using PlantBox.Shared.Communication.Commands; using System; using System.Collections.Generic; @@ -25,8 +26,12 @@ namespace PlantBox.Client.Forms { var plant = (PlantInfo)e.Item; - await Navigation.PushAsync(new PlantPage(plant) + // Prepare view model + var viewModel = new PlantViewModel(plant); + + await Navigation.PushAsync(new PlantPage() { + BindingContext = viewModel, Title = plant.Name }); } diff --git a/PlantBox.Client/PlantBox.Client/Forms/Plant/PlantPage.xaml.cs b/PlantBox.Client/PlantBox.Client/Forms/Plant/PlantPage.xaml.cs index ff0bbb1..9ea5db9 100644 --- a/PlantBox.Client/PlantBox.Client/Forms/Plant/PlantPage.xaml.cs +++ b/PlantBox.Client/PlantBox.Client/Forms/Plant/PlantPage.xaml.cs @@ -14,16 +14,18 @@ namespace PlantBox.Client.Forms.Plant [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PlantPage : TabbedPage { - public PlantPage(PlantInfo plant) + public PlantPage() { InitializeComponent(); Children.Add(new CaptorsPage() { + BindingContext = BindingContext, Title = Locale.Informations }); Children.Add(new HistoricPage() { + BindingContext = BindingContext, Title = Locale.Historic }); } diff --git a/PlantBox.Client/PlantBox.Client/Models/Historic.cs b/PlantBox.Client/PlantBox.Client/Models/Historic.cs new file mode 100644 index 0000000..069a14b --- /dev/null +++ b/PlantBox.Client/PlantBox.Client/Models/Historic.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PlantBox.Client.Models +{ + class Historic + { + public HistoricEntry MinutelyHistoric { get; } + public HistoricEntry HourlyHistoric { get; } + public HistoricEntry DailyHistoric { get; } + public HistoricEntry WeeklyHistoric { get; } + public HistoricEntry MonthlyHistoric { get; } + + public Historic + ( + HistoricEntry minutelyHistoric, HistoricEntry hourlyHistoric, HistoricEntry dailyHistoric, + HistoricEntry weeklyHistoric, HistoricEntry monthlyHistoric + ) + { + MinutelyHistoric = minutelyHistoric; + HourlyHistoric = hourlyHistoric; + DailyHistoric = dailyHistoric; + WeeklyHistoric = weeklyHistoric; + MonthlyHistoric = monthlyHistoric; + } + } +} diff --git a/PlantBox.Client/PlantBox.Client/Models/HistoricEntry.cs b/PlantBox.Client/PlantBox.Client/Models/HistoricEntry.cs new file mode 100644 index 0000000..9c91255 --- /dev/null +++ b/PlantBox.Client/PlantBox.Client/Models/HistoricEntry.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PlantBox.Client.Models +{ + class HistoricEntry + { + public DateTime LastTime { get; } + public TimeSpan TimeInterval { get; } + + public double[] Humidities { get; } + public double[] Luminosities { get; } + public double[] Temperatures { get; } + + public DateTime EndTime => LastTime - new TimeSpan(TimeInterval.Ticks * Count); + public int Count => Humidities.Length; + + public HistoricEntry(DateTime lastTime, TimeSpan timeInterval, double[] humidities, double[] luminosities, double[] temperatures) + { + LastTime = lastTime; + TimeInterval = timeInterval; + Humidities = humidities; + Luminosities = luminosities; + Temperatures = temperatures; + } + } +} diff --git a/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs b/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs new file mode 100644 index 0000000..7acb9b8 --- /dev/null +++ b/PlantBox.Client/PlantBox.Client/ViewModels/PlantViewModel.cs @@ -0,0 +1,103 @@ +using PlantBox.Client.Models; +using PlantBox.Shared.Communication.Commands; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Text; + +namespace PlantBox.Client.ViewModels +{ + class PlantViewModel : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + + public PlantViewModel(PlantInfo plant) + { + PlantInfo = plant; + + LoadValues(); + } + + private Historic _historic; + public Historic Historic + { + get => _historic; + set + { + if (value != _historic) + { + _historic = value; + OnPropertyChanged(); + } + } + } + + private PlantInfo _plantInfo; + public PlantInfo PlantInfo + { + get => _plantInfo; + set + { + if (value != _plantInfo) + { + _plantInfo = value; + OnPropertyChanged(); + } + } + } + + private CaptorValue _humidityCaptor; + public CaptorValue HumidityCaptor + { + get => _humidityCaptor; + set + { + if (value != _humidityCaptor) + { + _humidityCaptor = value; + OnPropertyChanged(); + } + } + } + + private CaptorValue _luminosityCaptor; + public CaptorValue LuminosityCaptor + { + get => _luminosityCaptor; + set + { + if (value != _luminosityCaptor) + { + _luminosityCaptor = value; + OnPropertyChanged(); + } + } + } + + private CaptorValue _temperatureCaptor; + public CaptorValue TemperatureCaptor + { + get => _temperatureCaptor; + set + { + if (value != _temperatureCaptor) + { + _temperatureCaptor = value; + OnPropertyChanged(); + } + } + } + + private void LoadValues() + { + // TODO + //throw new NotImplementedException(); + } + + protected void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +}