Add client add button

This commit is contained in:
2019-04-29 19:35:38 +02:00
parent 216daeac33
commit 6ca735b574
6 changed files with 198 additions and 5 deletions

View File

@@ -1,11 +1,15 @@
using PlantBox.Client.Extensions;
using PlantBox.Client.Forms.Plant;
using PlantBox.Client.Models;
using PlantBox.Client.Resources;
using PlantBox.Client.Util;
using PlantBox.Client.ViewModels;
using PlantBox.Shared.Communication;
using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
@@ -44,18 +48,58 @@ namespace PlantBox.Client.Forms
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)
{
var homeViewModel = (HomeViewModel)BindingContext;
await Task.Run(() => homeViewModel.Plants = homeViewModel.LoadPlants(App.Settings.IDs));
await Refresh();
List.EndRefresh();
}
private async Task Refresh()
{
var homeViewModel = (HomeViewModel)BindingContext;
await Task.Run(() => homeViewModel.Plants = homeViewModel.LoadPlants(App.Settings.IDs));
}
}
}

View File

@@ -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>
/// Recherche une chaîne localisée semblable à Captors.
/// </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>
/// Recherche une chaîne localisée semblable à General.
/// </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>
/// Recherche une chaîne localisée semblable à Changes will take effect at next start.
/// </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>
/// Recherche une chaîne localisée semblable à Server.
/// </summary>

View File

@@ -123,12 +123,18 @@
<data name="Author" xml:space="preserve">
<value>Auteur</value>
</data>
<data name="Cancel" xml:space="preserve">
<value>Annuler</value>
</data>
<data name="Captors" xml:space="preserve">
<value>Capteurs</value>
</data>
<data name="Day" xml:space="preserve">
<value>Jour</value>
</data>
<data name="Error" xml:space="preserve">
<value>Erreur</value>
</data>
<data name="General" xml:space="preserve">
<value>Général</value>
</data>
@@ -147,6 +153,9 @@
<data name="Interval" xml:space="preserve">
<value>Intervalle:</value>
</data>
<data name="InvalidID" xml:space="preserve">
<value>Id invalide</value>
</data>
<data name="LanguageChangedMessage" xml:space="preserve">
<value>Les changements prendront effet au prochain lancement</value>
</data>
@@ -177,6 +186,9 @@
<data name="NotificationsMuted" xml:space="preserve">
<value>Rendre muet les notifications</value>
</data>
<data name="OK" xml:space="preserve">
<value>OK</value>
</data>
<data name="Server" xml:space="preserve">
<value>Serveur</value>
</data>

View File

@@ -123,12 +123,18 @@
<data name="Author" xml:space="preserve">
<value>Author</value>
</data>
<data name="Cancel" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="Captors" xml:space="preserve">
<value>Captors</value>
</data>
<data name="Day" xml:space="preserve">
<value>Day</value>
</data>
<data name="Error" xml:space="preserve">
<value>Error</value>
</data>
<data name="General" xml:space="preserve">
<value>General</value>
</data>
@@ -147,6 +153,9 @@
<data name="Interval" xml:space="preserve">
<value>Interval:</value>
</data>
<data name="InvalidID" xml:space="preserve">
<value>Invalid id</value>
</data>
<data name="LanguageChangedMessage" xml:space="preserve">
<value>Changes will take effect at next start</value>
</data>
@@ -177,6 +186,9 @@
<data name="NotificationsMuted" xml:space="preserve">
<value>Mute notifications</value>
</data>
<data name="OK" xml:space="preserve">
<value>OK</value>
</data>
<data name="Server" xml:space="preserve">
<value>Server</value>
</data>

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

View File

@@ -17,7 +17,19 @@ namespace PlantBox.Client.ViewModels
{
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()
{