An improved version of OutlinePicker

I’ve promised to present an improved version of the OutlinePicker shown in this post and here it is. What did I do?

  1. The parameter list changed a little bit. Now there’s a special title for „No Selection“ and we do not need the isExpanded parameter anymore since we want to expand the nodes automatically according to the selection.
  2. I’ve introduced the new array nodesToExpand. I’ll come back to that a little bit later.
  3. Instead of a sheet the selection view is presented as a popover (WithPopover from this post). Using that we do not need a close button anymore.
  4. The NodeOutlineGroup from this post is used to accept the array of expanded nodes.
  5. Instead of a Form I use a List here with a plain listStyle. That looks better in the popover view.
  6. The „No Selection“ section is now separated from the list in a similar manner as a Divider would do it in a Picker view.

Here is the coding:

So what about the nodesToExpand array? This was my main motivation to develop this improved version of OutlinePicker. If the currently selected node is deep in the tree of nodes it would be nice if the tree is expanded on the path to this node and only there.

For that we have to traverse through the tree of nodes until we find the selected one and have to remember the parent nodes up to that point. I’ve achieved this by building up the array of nodes to expand recursively with the method checkNode. It returns true if the selected one is found in the current node or in one of its children and is building up the array of parents as a side effect.

All this functionality is implemented in the internal class NodesToExpand.

Here you can see how this new version of OutlinePicker looks like in the same example as in this previous post. Have fun.

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert