Announcement

Collapse
No announcement yet.

How to use WatchUi.Menu() with a dynamic array?

Collapse
X
  • Time
  • Show
Clear All
new posts

  • How to use WatchUi.Menu() with a dynamic array?

    I get an array from a json message. The number of items in this array is dynamic.
    I want to use this array to populate a WatchUi.Menu().

    This is the addItem method on a menu class from the API Docs.

    addItem(name, id)
    id (Toybox::Lang::Symbol) ? A Symbol representing the Menu item value

    I want to do something like this but I get stuck on how to handle the id (Symbol)

    var array = getMyJsonMessage(); // Response something like this [[1, "Jan"], [2, "Feb"], [3, "Mar"], ...]
    var menu = new WatchUi.Menu();
    menu.setTitle("My Menu");
    for(var item = 0; item < array.size(); item++) {
    menu.addItem(array[item][1], ???);
    }

    And then how to implement the WatchUi::MenuInputDelegate class.

    function onMenuItem(item) {
    if (item == ???) {
    System.println("Item 1");
    } else if ....
    }

    The Array that I use have a unique value for every item. If I can use this as Symbol the problem is solved.
    But I dont understand how.




  • #2
    Is there an upper limit to the number of menu items (say, 12)? And is the set of all possible IDs known beforehand? If so you could create two dictionaries which map from numerical IDs to symbols and vice versa (it's not ideal, but oh well).

    Code:
    // IDs do not need to be contiguous
    var idToSymbolMap = {
      1 => :menu_item_1,
      2 => :menu_item_2,
      // ...
      12 => :menu_item_12
    };
    
    var symbolToIdMap = {
      :menu_item_1 => 1,
      :menu_item_2 => 2,
      //. ...
      :menu_item_12 => 12
    }
    
    // ==============================================
    var array = getMyJsonMessage(); // Response something like this [[1, "Jan"], [2, "Feb"], [3, "Mar"], ...]
    var menu = new WatchUi.Menu();
    menu.setTitle("My Menu");
    for(var item = 0; item < array.size(); item++) {
    var itemSymbol = idToSymbolMap[array[item][0]];
      if (itemSymbol != null) {
    menu.addItem(array[item][1], itemSymbol);
      }
    }
    // ==============================================
    function onMenuItem(item) {
    var id = symbolToIdMap[item];
      System.println("item ID = " + id);
    }
    A more generic solution, if the set of all IDs is not known beforehand, but the max array size is. (Again I will assume the max size is 12). I think having a max array size is realistic in any case, since there has to be a practical limit to the number of menu items you are willing to display.

    Code:
    const maxArraySize = 12;
    
    // indexes need to be contiguous (from 0 to max array size - 1)
    var indexToSymbolMap = {
      0 => :menu_item_0,
      1 => :menu_item_1,
      // ...
      11 => :menu_item_11
    };
    
    var symbolToIndexMap = {
      :menu_item_0 => 0,
      :menu_item_1 => 1,
      //. ...
      :menu_item_11 => 11
    }
    
    // ==============================================
    // responseArray needs to be available to onMenuItem somehow (e.g. global var, class variable)
    var responseArray; // at global scope
    
    // function scope:
    responseArray = getMyJsonMessage(); // Response something like this [[1, "Jan"], [2, "Feb"], [3, "Mar"], ...]
    var menu = new WatchUi.Menu();
    menu.setTitle("My Menu");
    for(var item = 0; item < array.size() && item < maxArraySize; item++) {
      var itemSymbol = indexToSymbolMap[item];
      if (itemSymbol != null) {
       menu.addItem(array[item][1], itemSymbol);
      }
    }
    // ==============================================
    function onMenuItem(item) {
    var index = symbolToIndexMap[item];
      var id = responseArray[index][0];
      System.println("item ID = " + id);
    }
    Last edited by FlowState; 10-11-2017, 07:44 PM.

    Comment


    • #3
      The best place to ask stuff like this is in the CIQ Developer's Forum
      My Connect IQ Apps in the Store
      Facebook - Instagram -
      Twitter

      Comment


      • #4
        Sorry! I thought this was the place to ask general developer questions. I will start using the forum you mention.

        Comment

        Working...
        X