Implement AddUser in ManageUserDisplay

This commit is contained in:
2021-01-23 19:11:50 +01:00
parent f820c4c331
commit 5d9b01e313
3 changed files with 105 additions and 0 deletions

View File

@@ -99,6 +99,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\ManageUsersDisplay.cs" />
<Compile Include="Views\NewUserDialog.cs" />
<Compile Include="Views\SettingsDisplay.cs" />
<Compile Include="Views\SleepDisplay.cs" />
<Compile Include="Views\TUIManager.cs" />

View File

@@ -94,7 +94,14 @@ namespace Akari.Provider.WaveshareUART.Views
private void AddButton_Clicked()
{
var dialog = new NewUserDialog();
if (dialog.TryShow(out var name, out var token))
{
UsersManager.Add(name.ToString(), token.ToString());
}
UpdateList();
}
private void DeleteButton_Clicked()

97
Views/NewUserDialog.cs Normal file
View File

@@ -0,0 +1,97 @@
using NStack;
using System;
using Terminal.Gui;
namespace Akari.Provider.WaveshareUART.Views
{
public class NewUserDialog : Dialog
{
private bool _success;
private TextField _nameEntry;
private TextField _tokenEntry;
public NewUserDialog() : base("New User", 60, 8)
{
ColorScheme = Colors.ColorSchemes["Menu"];
}
public bool TryShow(out ustring name, out ustring token)
{
_success = false;
var nameLabel = new Label("Name: ")
{
X = 1,
Y = 2
};
_nameEntry = new TextField("")
{
X = Pos.Right(nameLabel),
Y = Pos.Top(nameLabel),
Width = Dim.Fill() - 1,
Height = 1
};
var tokenLabel = new Label("Token: ")
{
X = 1,
Y = 3
};
_tokenEntry = new TextField("")
{
X = Pos.Right(tokenLabel),
Y = Pos.Top(tokenLabel),
Width = Dim.Fill() - 1,
Height = 1
};
var cancelButton = new Button("_Cancel")
{
X = Pos.Percent(82),
Y = Pos.Percent(95)
};
cancelButton.Clicked += () => Application.RequestStop();
var okButton = new Button("_Ok")
{
X = Pos.Percent(70),
Y = Pos.Percent(95)
};
okButton.Clicked += () => CheckInput();
Add(nameLabel, _nameEntry, tokenLabel, _tokenEntry, okButton, cancelButton);
_nameEntry.SetFocus();
Application.Run(this);
if (_success)
{
name = _nameEntry.Text;
token = _tokenEntry.Text;
return true;
}
name = default;
token = default;
return false;
}
private void CheckInput()
{
if (!string.IsNullOrWhiteSpace(_nameEntry.Text.ToString()) && !string.IsNullOrWhiteSpace(_tokenEntry.Text.ToString()))
{
_success = true;
Application.RequestStop();
}
else
{
MessageBox.ErrorQuery("New User", "Name and Token can't be empty", "Ok");
}
}
}
}