1
\$\begingroup\$

I am trying to write a method that can find the child item in a menu. The below code works but is not exactly elegant or scaleable. Is there another way to find the path to child menu item that could be used?

public class Item
{
    public string Name { get; set; }
    public string Parent { get; set; } 
}


public void OpenMenuItem(string name)
{
    var items = new List<Item>
    {
        new Item  { Name = "Test", Parent = null },
        new Item  { Name = "TestChild1", Parent = "Test" },
        new Item  { Name = "TestChild2", Parent = "Test" },
        new Item  { Name = "TestGranChild1", Parent = "TestChild1" },
        new Item  { Name = "TestGranChild2", Parent = "TestChild1" },
        new Item  { Name = "TestGranChild3", Parent = "TestChild2" },
        new Item  { Name = "TestGranChild4", Parent = "TestChild2" }
    };
    var menuItem = items.Where(x => x.Name == name);
    if (menuItem.FirstOrDefault().Parent != null)
    {
        var parent1 = items.Where(x => x.Name == menuItem.FirstOrDefault().Parent);
        if (parent1.FirstOrDefault().Parent != null)
        {
            var parent2 = items.Where(x => x.Name == parent1.FirstOrDefault().Parent);
            app.Tap(x => x.Marked(parent2.FirstOrDefault().Name));
        }
        app.Tap(x => x.Marked(parent1.FirstOrDefault().Name));
    }
    app.Tap(x => x.Marked(menuItem.FirstOrDefault().Name));
}
\$\endgroup\$
1
\$\begingroup\$

Make parent a list of ancestors as a path to the item:

new Item  { Name = "TestGranChild4", Path = {"Test", "TestChild2"} },


var menuItem = items.Where(x => x.Name == name);
menuItem.FirstOrDefault().Path.ForEach(a => app.tap(x=>x.Marked(a)));
app.Tap(x => x.Marked(menuItem.FirstOrDefault().Name));
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.