Add client add button
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
using PlantBox.Client.Extensions;
|
using PlantBox.Client.Extensions;
|
||||||
using PlantBox.Client.Forms.Plant;
|
using PlantBox.Client.Forms.Plant;
|
||||||
using PlantBox.Client.Models;
|
using PlantBox.Client.Models;
|
||||||
|
using PlantBox.Client.Resources;
|
||||||
|
using PlantBox.Client.Util;
|
||||||
using PlantBox.Client.ViewModels;
|
using PlantBox.Client.ViewModels;
|
||||||
|
using PlantBox.Shared.Communication;
|
||||||
using PlantBox.Shared.Communication.Commands;
|
using PlantBox.Shared.Communication.Commands;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@@ -44,18 +48,58 @@ namespace PlantBox.Client.Forms
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Button_Clicked(object sender, EventArgs e)
|
private async void Button_Clicked(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
string input = await FormsDialog.InputBox(Navigation, "Add PlantBox", "Enter a valid ID:");
|
||||||
|
|
||||||
|
if (ulong.TryParse(input, out ulong id) && await IsValidId(id))
|
||||||
|
{
|
||||||
|
App.Settings.IDs.Add(id);
|
||||||
|
|
||||||
|
await Refresh();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await DisplayAlert(Locale.Error, Locale.InvalidID, Locale.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<bool> IsValidId(ulong id)
|
||||||
|
{
|
||||||
|
string plantName = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT))
|
||||||
|
using (var commandStream = new CommandStream(client.GetStream()))
|
||||||
|
{
|
||||||
|
await commandStream.SendAsync(new InfoRequest().ToCommandPacket(id));
|
||||||
|
|
||||||
|
(_, var info) = await commandStream.ReceiveAsync<InfoResponse>();
|
||||||
|
|
||||||
|
plantName = info.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return plantName != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void ListView_Refreshing(object sender, EventArgs e)
|
private async void ListView_Refreshing(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
var homeViewModel = (HomeViewModel)BindingContext;
|
await Refresh();
|
||||||
|
|
||||||
await Task.Run(() => homeViewModel.Plants = homeViewModel.LoadPlants(App.Settings.IDs));
|
|
||||||
|
|
||||||
List.EndRefresh();
|
List.EndRefresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task Refresh()
|
||||||
|
{
|
||||||
|
var homeViewModel = (HomeViewModel)BindingContext;
|
||||||
|
|
||||||
|
await Task.Run(() => homeViewModel.Plants = homeViewModel.LoadPlants(App.Settings.IDs));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,6 +78,15 @@ namespace PlantBox.Client.Resources {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recherche une chaîne localisée semblable à Cancel.
|
||||||
|
/// </summary>
|
||||||
|
internal static string Cancel {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("Cancel", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Recherche une chaîne localisée semblable à Captors.
|
/// Recherche une chaîne localisée semblable à Captors.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -96,6 +105,15 @@ namespace PlantBox.Client.Resources {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recherche une chaîne localisée semblable à Error.
|
||||||
|
/// </summary>
|
||||||
|
internal static string Error {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("Error", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Recherche une chaîne localisée semblable à General.
|
/// Recherche une chaîne localisée semblable à General.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -150,6 +168,15 @@ namespace PlantBox.Client.Resources {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recherche une chaîne localisée semblable à Invalid id.
|
||||||
|
/// </summary>
|
||||||
|
internal static string InvalidID {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("InvalidID", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Recherche une chaîne localisée semblable à Changes will take effect at next start.
|
/// Recherche une chaîne localisée semblable à Changes will take effect at next start.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -240,6 +267,15 @@ namespace PlantBox.Client.Resources {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recherche une chaîne localisée semblable à OK.
|
||||||
|
/// </summary>
|
||||||
|
internal static string OK {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("OK", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Recherche une chaîne localisée semblable à Server.
|
/// Recherche une chaîne localisée semblable à Server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -123,12 +123,18 @@
|
|||||||
<data name="Author" xml:space="preserve">
|
<data name="Author" xml:space="preserve">
|
||||||
<value>Auteur</value>
|
<value>Auteur</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Cancel" xml:space="preserve">
|
||||||
|
<value>Annuler</value>
|
||||||
|
</data>
|
||||||
<data name="Captors" xml:space="preserve">
|
<data name="Captors" xml:space="preserve">
|
||||||
<value>Capteurs</value>
|
<value>Capteurs</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="Day" xml:space="preserve">
|
<data name="Day" xml:space="preserve">
|
||||||
<value>Jour</value>
|
<value>Jour</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Error" xml:space="preserve">
|
||||||
|
<value>Erreur</value>
|
||||||
|
</data>
|
||||||
<data name="General" xml:space="preserve">
|
<data name="General" xml:space="preserve">
|
||||||
<value>Général</value>
|
<value>Général</value>
|
||||||
</data>
|
</data>
|
||||||
@@ -147,6 +153,9 @@
|
|||||||
<data name="Interval" xml:space="preserve">
|
<data name="Interval" xml:space="preserve">
|
||||||
<value>Intervalle:</value>
|
<value>Intervalle:</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidID" xml:space="preserve">
|
||||||
|
<value>Id invalide</value>
|
||||||
|
</data>
|
||||||
<data name="LanguageChangedMessage" xml:space="preserve">
|
<data name="LanguageChangedMessage" xml:space="preserve">
|
||||||
<value>Les changements prendront effet au prochain lancement</value>
|
<value>Les changements prendront effet au prochain lancement</value>
|
||||||
</data>
|
</data>
|
||||||
@@ -177,6 +186,9 @@
|
|||||||
<data name="NotificationsMuted" xml:space="preserve">
|
<data name="NotificationsMuted" xml:space="preserve">
|
||||||
<value>Rendre muet les notifications</value>
|
<value>Rendre muet les notifications</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="OK" xml:space="preserve">
|
||||||
|
<value>OK</value>
|
||||||
|
</data>
|
||||||
<data name="Server" xml:space="preserve">
|
<data name="Server" xml:space="preserve">
|
||||||
<value>Serveur</value>
|
<value>Serveur</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
@@ -123,12 +123,18 @@
|
|||||||
<data name="Author" xml:space="preserve">
|
<data name="Author" xml:space="preserve">
|
||||||
<value>Author</value>
|
<value>Author</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Cancel" xml:space="preserve">
|
||||||
|
<value>Cancel</value>
|
||||||
|
</data>
|
||||||
<data name="Captors" xml:space="preserve">
|
<data name="Captors" xml:space="preserve">
|
||||||
<value>Captors</value>
|
<value>Captors</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="Day" xml:space="preserve">
|
<data name="Day" xml:space="preserve">
|
||||||
<value>Day</value>
|
<value>Day</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Error" xml:space="preserve">
|
||||||
|
<value>Error</value>
|
||||||
|
</data>
|
||||||
<data name="General" xml:space="preserve">
|
<data name="General" xml:space="preserve">
|
||||||
<value>General</value>
|
<value>General</value>
|
||||||
</data>
|
</data>
|
||||||
@@ -147,6 +153,9 @@
|
|||||||
<data name="Interval" xml:space="preserve">
|
<data name="Interval" xml:space="preserve">
|
||||||
<value>Interval:</value>
|
<value>Interval:</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidID" xml:space="preserve">
|
||||||
|
<value>Invalid id</value>
|
||||||
|
</data>
|
||||||
<data name="LanguageChangedMessage" xml:space="preserve">
|
<data name="LanguageChangedMessage" xml:space="preserve">
|
||||||
<value>Changes will take effect at next start</value>
|
<value>Changes will take effect at next start</value>
|
||||||
</data>
|
</data>
|
||||||
@@ -177,6 +186,9 @@
|
|||||||
<data name="NotificationsMuted" xml:space="preserve">
|
<data name="NotificationsMuted" xml:space="preserve">
|
||||||
<value>Mute notifications</value>
|
<value>Mute notifications</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="OK" xml:space="preserve">
|
||||||
|
<value>OK</value>
|
||||||
|
</data>
|
||||||
<data name="Server" xml:space="preserve">
|
<data name="Server" xml:space="preserve">
|
||||||
<value>Server</value>
|
<value>Server</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
77
PlantBox.Client/PlantBox.Client/Util/FormsDialog.cs
Normal file
77
PlantBox.Client/PlantBox.Client/Util/FormsDialog.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
using PlantBox.Client.Resources;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xamarin.Forms;
|
||||||
|
|
||||||
|
namespace PlantBox.Client.Util
|
||||||
|
{
|
||||||
|
class FormsDialog
|
||||||
|
{
|
||||||
|
public static Task<string> InputBox(INavigation navigation, string title, string description)
|
||||||
|
{
|
||||||
|
// wait in this proc, until user did his input
|
||||||
|
var tcs = new TaskCompletionSource<string>();
|
||||||
|
|
||||||
|
var lblTitle = new Label { Text = title, HorizontalOptions = LayoutOptions.Center, FontAttributes = FontAttributes.Bold };
|
||||||
|
var lblMessage = new Label { Text = description };
|
||||||
|
var txtInput = new Entry { Text = "" };
|
||||||
|
|
||||||
|
var btnOk = new Button
|
||||||
|
{
|
||||||
|
Text = Locale.OK,
|
||||||
|
WidthRequest = 100,
|
||||||
|
BackgroundColor = Color.FromRgb(0.8, 0.8, 0.8),
|
||||||
|
};
|
||||||
|
btnOk.Clicked += async (s, e) =>
|
||||||
|
{
|
||||||
|
// close page
|
||||||
|
var result = txtInput.Text;
|
||||||
|
await navigation.PopModalAsync();
|
||||||
|
// pass result
|
||||||
|
tcs.SetResult(result);
|
||||||
|
};
|
||||||
|
|
||||||
|
var btnCancel = new Button
|
||||||
|
{
|
||||||
|
Text = Locale.Cancel,
|
||||||
|
WidthRequest = 100,
|
||||||
|
BackgroundColor = Color.FromRgb(0.8, 0.8, 0.8)
|
||||||
|
};
|
||||||
|
btnCancel.Clicked += async (s, e) =>
|
||||||
|
{
|
||||||
|
// close page
|
||||||
|
await navigation.PopModalAsync();
|
||||||
|
// pass empty result
|
||||||
|
tcs.SetResult(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
var slButtons = new StackLayout
|
||||||
|
{
|
||||||
|
Orientation = StackOrientation.Horizontal,
|
||||||
|
Children = { btnOk, btnCancel },
|
||||||
|
};
|
||||||
|
|
||||||
|
var layout = new StackLayout
|
||||||
|
{
|
||||||
|
Padding = new Thickness(0, 40, 0, 0),
|
||||||
|
VerticalOptions = LayoutOptions.StartAndExpand,
|
||||||
|
HorizontalOptions = LayoutOptions.CenterAndExpand,
|
||||||
|
Orientation = StackOrientation.Vertical,
|
||||||
|
Children = { lblTitle, lblMessage, txtInput, slButtons },
|
||||||
|
};
|
||||||
|
|
||||||
|
// create and show page
|
||||||
|
var page = new ContentPage();
|
||||||
|
page.Content = layout;
|
||||||
|
navigation.PushModalAsync(page);
|
||||||
|
// open keyboard
|
||||||
|
txtInput.Focus();
|
||||||
|
|
||||||
|
// code is waiting her, until result is passed with tcs.SetResult() in btn-Clicked
|
||||||
|
// then proc returns the result
|
||||||
|
return tcs.Task;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,7 +17,19 @@ namespace PlantBox.Client.ViewModels
|
|||||||
{
|
{
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
public ObservableCollection<PlantInfo> Plants { get; set; }
|
private ObservableCollection<PlantInfo> _plants;
|
||||||
|
public ObservableCollection<PlantInfo> Plants
|
||||||
|
{
|
||||||
|
get => _plants;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value != _plants)
|
||||||
|
{
|
||||||
|
_plants = value;
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public HomeViewModel()
|
public HomeViewModel()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user