Add PlantPage logic

This commit is contained in:
2019-04-07 15:50:00 +02:00
parent 5dc9007ad2
commit 684393f85d
6 changed files with 200 additions and 2 deletions

View File

@@ -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();
}
}
}

View File

@@ -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
});
}

View File

@@ -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
});
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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));
}
}
}