diff --git a/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs b/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs index e7a15e0..35192aa 100644 --- a/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs +++ b/PlantBox.Client/PlantBox.Client/Forms/HomePage.xaml.cs @@ -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 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(); + + 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)); + } } } \ No newline at end of file diff --git a/PlantBox.Client/PlantBox.Client/Resources/Locale.Designer.cs b/PlantBox.Client/PlantBox.Client/Resources/Locale.Designer.cs index 64895cf..dd8a845 100644 --- a/PlantBox.Client/PlantBox.Client/Resources/Locale.Designer.cs +++ b/PlantBox.Client/PlantBox.Client/Resources/Locale.Designer.cs @@ -78,6 +78,15 @@ namespace PlantBox.Client.Resources { } } + /// + /// Recherche une chaîne localisée semblable à Cancel. + /// + internal static string Cancel { + get { + return ResourceManager.GetString("Cancel", resourceCulture); + } + } + /// /// Recherche une chaîne localisée semblable à Captors. /// @@ -96,6 +105,15 @@ namespace PlantBox.Client.Resources { } } + /// + /// Recherche une chaîne localisée semblable à Error. + /// + internal static string Error { + get { + return ResourceManager.GetString("Error", resourceCulture); + } + } + /// /// Recherche une chaîne localisée semblable à General. /// @@ -150,6 +168,15 @@ namespace PlantBox.Client.Resources { } } + /// + /// Recherche une chaîne localisée semblable à Invalid id. + /// + internal static string InvalidID { + get { + return ResourceManager.GetString("InvalidID", resourceCulture); + } + } + /// /// Recherche une chaîne localisée semblable à Changes will take effect at next start. /// @@ -240,6 +267,15 @@ namespace PlantBox.Client.Resources { } } + /// + /// Recherche une chaîne localisée semblable à OK. + /// + internal static string OK { + get { + return ResourceManager.GetString("OK", resourceCulture); + } + } + /// /// Recherche une chaîne localisée semblable à Server. /// diff --git a/PlantBox.Client/PlantBox.Client/Resources/Locale.fr.resx b/PlantBox.Client/PlantBox.Client/Resources/Locale.fr.resx index 55ae3c9..8df7e22 100644 --- a/PlantBox.Client/PlantBox.Client/Resources/Locale.fr.resx +++ b/PlantBox.Client/PlantBox.Client/Resources/Locale.fr.resx @@ -123,12 +123,18 @@ Auteur + + Annuler + Capteurs Jour + + Erreur + Général @@ -147,6 +153,9 @@ Intervalle: + + Id invalide + Les changements prendront effet au prochain lancement @@ -177,6 +186,9 @@ Rendre muet les notifications + + OK + Serveur diff --git a/PlantBox.Client/PlantBox.Client/Resources/Locale.resx b/PlantBox.Client/PlantBox.Client/Resources/Locale.resx index 85c6db4..b77347a 100644 --- a/PlantBox.Client/PlantBox.Client/Resources/Locale.resx +++ b/PlantBox.Client/PlantBox.Client/Resources/Locale.resx @@ -123,12 +123,18 @@ Author + + Cancel + Captors Day + + Error + General @@ -147,6 +153,9 @@ Interval: + + Invalid id + Changes will take effect at next start @@ -177,6 +186,9 @@ Mute notifications + + OK + Server diff --git a/PlantBox.Client/PlantBox.Client/Util/FormsDialog.cs b/PlantBox.Client/PlantBox.Client/Util/FormsDialog.cs new file mode 100644 index 0000000..7492eb4 --- /dev/null +++ b/PlantBox.Client/PlantBox.Client/Util/FormsDialog.cs @@ -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 InputBox(INavigation navigation, string title, string description) + { + // wait in this proc, until user did his input + var tcs = new TaskCompletionSource(); + + 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; + } + } +} diff --git a/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs b/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs index 244a4b3..294a6e4 100644 --- a/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs +++ b/PlantBox.Client/PlantBox.Client/ViewModels/HomeViewModel.cs @@ -17,7 +17,19 @@ namespace PlantBox.Client.ViewModels { public event PropertyChangedEventHandler PropertyChanged; - public ObservableCollection Plants { get; set; } + private ObservableCollection _plants; + public ObservableCollection Plants + { + get => _plants; + set + { + if (value != _plants) + { + _plants = value; + OnPropertyChanged(); + } + } + } public HomeViewModel() {