From 2a74c047dd928d3684c3a70317785acae4888d29 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Mon, 25 Jan 2021 21:08:38 +0100 Subject: [PATCH] Add User edit --- Akari.Provider.WaveshareUART.csproj | 1 + Views/EditUserDisplay.cs | 201 ++++++++++++++++++++++++++++ Views/ManageUsersDisplay.cs | 2 +- 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 Views/EditUserDisplay.cs diff --git a/Akari.Provider.WaveshareUART.csproj b/Akari.Provider.WaveshareUART.csproj index e4e6c13..8426d4e 100644 --- a/Akari.Provider.WaveshareUART.csproj +++ b/Akari.Provider.WaveshareUART.csproj @@ -94,6 +94,7 @@ + diff --git a/Views/EditUserDisplay.cs b/Views/EditUserDisplay.cs new file mode 100644 index 0000000..67f8c63 --- /dev/null +++ b/Views/EditUserDisplay.cs @@ -0,0 +1,201 @@ +using Akari.Provider.WaveshareUART.Users; +using NStack; +using System; +using System.Linq; +using System.Threading.Tasks; +using Terminal.Gui; +using WaveshareUARTFingerprintSensor; + +namespace Akari.Provider.WaveshareUART.Views +{ + public class EditUserDisplay : Toplevel + { + public UsersManager UsersManager => WaveshareUARTProvider.Instance.UsersManager; + public FingerprintSensor FingerprintSensor => WaveshareUARTProvider.Instance.FingerprintSensor; + + private readonly User _user; + private Label _infoLabel; + private Label _fingerprintsCountLabel; + private Label _startLabel; + + public EditUserDisplay(User user) + { + _user = user; + + Init(); + } + + private void Init() + { + Modal = true; + + ColorScheme = Colors.Error; + + // Creates the top-level window to show + var win = new Window("Manage Users") + { + X = 0, + Y = 0, + + // By using Dim.Fill(), it will automatically resize without manual intervention + Width = Dim.Fill(), + Height = Dim.Fill() + }; + + win.ColorScheme = Colors.ColorSchemes["Dialog"]; + + Add(win); + + // Window Content + _infoLabel = new Label($"Name: {_user.Name}") + { + X = 2, + Y = 1, + Width = Dim.Fill() + }; + + _startLabel = new Label($"First ID: {_user.Start}") + { + X = Pos.Left(_infoLabel), + Y = Pos.Bottom(_infoLabel), + Width = Dim.Fill() + }; + + _fingerprintsCountLabel = new Label($"Fingerprints: {_user.Count}") + { + X = Pos.Left(_startLabel), + Y = Pos.Bottom(_startLabel), + Width = Dim.Fill() + }; + + var addButton = new Button("_Add Fingerprint") + { + X = Pos.Right(this) - 69, + Y = Pos.Bottom(this) - 3 + }; + addButton.Clicked += AddButton_Clicked; + + var editTokenButton = new Button("Edit Token") + { + X = Pos.Right(addButton) + 1, + Y = Pos.Top(addButton) + }; + editTokenButton.Clicked += EditTokenButton_Clicked; + + var clearButton = new Button("Clear Fingerprints") + { + X = Pos.Right(editTokenButton) + 1, + Y = Pos.Top(editTokenButton) + }; + clearButton.Clicked += ClearButton_Clicked; + + var quitButton = new Button("_Quit") + { + X = Pos.Right(clearButton) + 1, + Y = Pos.Top(clearButton) + }; + quitButton.Clicked += () => { Application.RequestStop(); }; + + win.Add(_infoLabel, _startLabel, _fingerprintsCountLabel, addButton, editTokenButton, clearButton, quitButton); + } + + private void UpdateCount() + { + _fingerprintsCountLabel.Text = $"Fingerprints: {_user.Count}"; + } + + private void AddButton_Clicked() + { + if (_user.IsFull()) + { + MessageBox.ErrorQuery("Add Fingerprint", "Can't add more fingerprints to this user", "Ok"); + return; + } + + var dialog = new FingerprintDialog("Add Fingerprint", "Can't add fingerprint, try to place your finger flat on the sensor"); + + Task.Run(() => + { + // In case an external program messed with the fingerprints + FingerprintSensor.DeleteUser(_user.Position); + + switch (FingerprintSensor.AddFingerprint(_user.Position, UserPermission.Level1)) + { + case ResponseType.Success: + dialog.Cancel(); + + lock (_user) + { + Console.WriteLine(_user.IncrementPosition()); + } + + Application.MainLoop.Invoke(() => MessageBox.Query("Add Fingerprint", "Successfully added fingerprint", "Ok")); + + break; + case ResponseType.Full: + dialog.ErrorMessage = "Sensor full, can't add more users"; + + dialog.CancelAndShowError(); + + break; + case ResponseType.NoUser: + dialog.ErrorMessage = "Can't add fingerprint, invalid id"; + + dialog.CancelAndShowError(); + + break; + case ResponseType.FingerOccupied: + dialog.ErrorMessage = "Can't add fingerprint, finger already registered"; + + dialog.CancelAndShowError(); + + break; + case ResponseType.UserOccupied: + dialog.ErrorMessage = "Can't add fingerprint, id already used"; + + dialog.CancelAndShowError(); + + break; + default: + dialog.CancelAndShowError(); + break; + } + }); + + dialog.Show(); + + UsersManager.Save(); + + UpdateCount(); + } + + private void EditTokenButton_Clicked() + { + if (new EntryDialog("Edit Token", s => !string.IsNullOrWhiteSpace(s.ToString()), "Token cannot be empty").TryShow(out ustring input)) + { + _user.Token = input.ToString(); + + UsersManager.Save(); + } + } + + private void ClearButton_Clicked() + { + if (MessageBox.Query("Delete Users", "Do you really to clear all the user fingerprints?", "yes", "No") != 0) + { + return; + } + + for (ushort i = _user.Start; i < _user.Position; i++) + { + FingerprintSensor.DeleteUser(i); + } + + _user.ResetPosition(); + + UsersManager.Save(); + + UpdateCount(); + } + } +} diff --git a/Views/ManageUsersDisplay.cs b/Views/ManageUsersDisplay.cs index a4a191b..e818dc8 100644 --- a/Views/ManageUsersDisplay.cs +++ b/Views/ManageUsersDisplay.cs @@ -89,7 +89,7 @@ namespace Akari.Provider.WaveshareUART.Views private void ListView_OpenSelectedItem(ListViewItemEventArgs item) { - + Application.Run(new EditUserDisplay((User)item.Value)); } private void AddButton_Clicked()