Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reflection and cleanup names #647

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Meadow.Peripherals.Displays;

namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Represents a base input menu item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// Represents a text display menu item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Represents a base Numeric input type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// A base time input type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// Text display MenuSelectedEventArgs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Age input type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu bool input type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu Date input type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
internal static class InputHelpers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
internal enum InputType
{
Age,
Boolean,
Date,
Numerical,
Onff,
Temperature,
Time,
TimeDetailed,
TimeShort,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu base List input type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu Numerical input item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu on/off input item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu Temperature input item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu Time input item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu TimeDetailed input item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu.InputTypes
namespace Meadow.Foundation.Displays.UI.InputTypes
{
/// <summary>
/// Text display menu TimeShort input item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Meadow.Peripherals.Displays;

namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// Text display Menu Input abstraction
/// Text display TextDisplayMenu Input abstraction
/// </summary>
public interface IMenuInputItem : IPage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// Text display menu page abstraction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;

namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// Text display MenuPage class
Expand All @@ -13,8 +12,9 @@ public class MenuPage : IPage
/// </summary>
public int ScrollPosition
{
get => scrollPosition;
set {
get => scrollPosition;
set
{
if (value > MenuItems.Count - 1 || value < 0)
{
Resolver.Log.Warn("Attempting to set a scroll position outside of item range: " + value.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// TextCharacters class for custom characters on LCD character displays
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Meadow.Foundation.Displays.TextDisplayMenu.InputTypes;
using Meadow.Foundation.Displays.UI.InputTypes;
using Meadow.Peripherals.Displays;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Meadow.Foundation.Displays.TextDisplayMenu
namespace Meadow.Foundation.Displays.UI
{
/// <summary>
/// TextDisplayMenu Menu class
/// TextDisplayMenu TextDisplayMenu class
/// </summary>
public class Menu
public class TextDisplayMenu
{
const string INPUT_TYPES_NAMESPACE = "Meadow.Foundation.Displays.TextDisplayMenu.InputTypes.";
ITextDisplay display;
Expand Down Expand Up @@ -46,25 +45,25 @@ public class Menu
public bool IsEnabled { get; protected set; } = false;

/// <summary>
/// Create a new Menu object
/// Create a new TextDisplayMenu object
/// </summary>
/// <param name="display">The display to render the menu</param>
/// <param name="menuJson">Json to define the menu structure</param>
/// <param name="showBackOnRoot">True to show Back item on root menu</param>
public Menu(ITextDisplay display, byte[] menuJson, bool showBackOnRoot = false)
public TextDisplayMenu(ITextDisplay display, byte[] menuJson, bool showBackOnRoot = false)
{
this.showBackOnRoot = showBackOnRoot;
var items = ParseMenuData(menuJson);
Init(display, CreateMenuPage(items, showBackOnRoot));
}

/// <summary>
/// Create a new Menu object
/// Create a new TextDisplayMenu object
/// </summary>
/// <param name="display">The display to render the menu</param>
/// <param name="menuItems">Menu items array</param>
/// <param name="menuItems">TextDisplayMenu items array</param>
/// <param name="showBackOnRoot">True to show Back item on root menu</param>
public Menu(ITextDisplay display, MenuItem[] menuItems, bool showBackOnRoot = false)
public TextDisplayMenu(ITextDisplay display, MenuItem[] menuItems, bool showBackOnRoot = false)
{
Init(display, CreateMenuPage(menuItems, showBackOnRoot));
}
Expand Down Expand Up @@ -109,7 +108,7 @@ public void Disable()
}

/// <summary>
/// Create a Menu page
/// Create a TextDisplayMenu page
/// </summary>
/// <param name="items">Items to populate page</param>
/// <param name="addBack">True to add a back item</param>
Expand All @@ -126,7 +125,7 @@ protected MenuPage CreateMenuPage(MenuItem[] items, bool addBack)
foreach (var item in items)
{
menuPage.MenuItems.Add(item);
}
}

return menuPage;
}
Expand All @@ -136,7 +135,8 @@ protected MenuPage CreateMenuPage(MenuItem[] items, bool addBack)
/// </summary>
protected void ShowCurrentPage()
{
if (!IsEnabled) {
if (!IsEnabled)
{
Resolver.Log.Warn("Render not enabled");
return;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ protected void ShowCurrentPage()
/// <returns>The item text</returns>
protected string GetItemText(MenuItem item, bool isSelected)
{
if(item == null)
if (item == null)
{
Resolver.Log.Warn("GetItemText: item is null");
return "no item";
Expand Down Expand Up @@ -216,7 +216,7 @@ protected string GetItemText(MenuItem item, bool isSelected)
string padding = string.Empty;
if (paddingLength > 0) { padding = new string(' ', paddingLength); }

itemText += padding + (isSelected?"*":">");
itemText += padding + (isSelected ? "*" : ">");
}

return itemText;
Expand All @@ -228,17 +228,17 @@ protected string GetItemText(MenuItem item, bool isSelected)
protected void UpdateCurrentMenuPage()
{
currentMenuPage = rootMenuPage;
}
}

/// <summary>
/// Next input - navigates down/forward in the list of items
/// </summary>
/// <returns>True if successful, false in menu is disabled</returns>
public bool Next()
{
if(IsEnabled == false) { return false; }
if (IsEnabled == false) { return false; }

if(currentInputItem != null)
if (currentInputItem != null)
{
currentInputItem.Next();
}
Expand Down Expand Up @@ -327,7 +327,7 @@ public bool Select()
Exited(this, new EventArgs());
return true;
}

// if currently on a subpage and user selects back, pop back to parent page.
if (currentMenuPage.ScrollPosition == 0 && pageStack.Count > 0)
{
Expand Down Expand Up @@ -359,19 +359,10 @@ public bool Select()
else if (menuItem.Type != string.Empty)
{
pageStack.Push(currentMenuPage);

isEditMode = true;

// create the new input type
var type = Type.GetType(INPUT_TYPES_NAMESPACE + menuItem.Type);

if (type == null)
{
throw new ArgumentException(menuItem.Type + " was not found");
}
isEditMode = true;

var constructor = type.GetConstructor(new Type[] { });
currentInputItem = constructor.Invoke(new object[] { }) as IMenuInputItem;
currentInputItem = GetMenuInputItemFromName(menuItem.Type);

// setup callback
currentInputItem.ValueChanged += delegate (object sender, ValueChangedEventArgs e)
Expand Down Expand Up @@ -400,6 +391,30 @@ public bool Select()
}
}

enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

IMenuInputItem GetMenuInputItemFromName(string name)
{
if (Enum.TryParse(name, out InputType inputType) == false)
{
throw new ArgumentException(name + " was not found");
}

return inputType switch
{
InputType.Age => new Age(),
InputType.Boolean => new InputTypes.Boolean(),
InputType.Date => new Date(),
InputType.Numerical => new Numerical(),
InputType.Onff => new OnOff(),
InputType.Temperature => new Temperature(),
InputType.Time => new Time(),
InputType.TimeDetailed => new TimeDetailed(),
InputType.TimeShort => new TimeShort(),
_ => null,
};
}

/// <summary>
/// Refresh / redraw the menu
/// </summary>
Expand Down Expand Up @@ -441,7 +456,7 @@ private void UpdateMenuItemValue(string id, object value)
MenuItem node = null;
foreach (var menuItem in rootMenuPage.MenuItems)
{
node = FindNodeById(menuItem as MenuItem, id);
node = FindNodeById(menuItem, id);
if (node != null) { break; }
}

Expand All @@ -465,7 +480,7 @@ private MenuItem FindNodeById(MenuItem menuItem, string id)
{
foreach (var subMenuItem in menuItem.SubItems)
{
var node = FindNodeById(subMenuItem as MenuItem, id);
var node = FindNodeById(subMenuItem, id);
if (node != null) return node;
}
}
Expand Down
Loading