Purpose
This self contained screen can be used as a stand-alone app or a service routine to offer up a browsable Table of Contents, using only an App Inventor List Picker, an internal pre-defined list of subtopic lists, and a history stack.
It can be used as a standalone document, a tutorial text, or context-sensitive help for another app, using an optional starter topic string parameter.
No permissions are required.
Screen Shots
The code
The GUI
The GUI consists of a plain vanilla scrollable screen, with a List Picker. Nothing else is needed.
The blocks
The Docs variable definition
The Docs variable is a two-level list of lists. Each List in Docs is meant to be shown in the List Picker at one time or another, starting with the first list. Notice how the first item value in each list from the second list onwards appears as a list item value in a preceding list. That’s because they are used as link targets, and their parent lists can reference them by including a copy of their link. I used brackets [] in my sample Docs link values, to highlight that they’re links, but the brackets mean nothing to the code.
Here’s the Docs variable, as supplied by me to self-document this app …
The other variables
Viewed is a list used as a stack for previously visited topic numbers.
Selection is used to hold the current string returned by the List Picker.
Selection_Index is used to hold the number of the sublist of Docs whose first element matches Selection. (or zero, if none matches.)
The Processing
Screen1.Initialize
The first time in, we check if Screen1 received a start value, and try to use it as a starting topic in Docs. If we can’t match it or if none was given, we use the first topic in Docs. We stack our topic number in the Viewed list, load the List Picker from it, and open the List Picker.
ListPicker1.AfterPicking
After the user has picked an item from the List Picker, we try to find the selected value in our Docs list, either refresh the List Picker with a new topic list, back out and show a prior viewed topic, possibly close the screen, or leave the List Picker contents alone, then re-open the List Picker.
We assume that if the person chooses the header of a topic, he wants to climb back out of the topic to a higher level topic, like following bread crumbs back.
Index_of_Topic
This service routine is called to look up the topic number in Docs of a text string, either passed into this app screen or previously selected from the List Picker. It does it by a dumb search thru the first element of each list in Docs.
Because this is being done in a small list, and once per interaction, we can afford to use a complete scan for simplicity’s sake, without much noticeable slowness.
I am committing a cardinal sin of programming here by modifying a global variable, Selection_Index, because App Inventor does not provide local variables.
In a larger app, this could cause serious bugs.
Load_List_Picker
This routine loads our List Picker from the appropriate topic number in Docs. Feel free to argue that Docs should have been a parameter too.
If we have no topic to load, we leave the List Picker alone.
last element function
This routine returns the last element of a list, or the value 0 if the list is empty. The choice of 0 as a return value is presumptious, but it matches the expectations of the rest of this app. Beware of that, if you copy this code for other uses. Maybe I should have called the routine last_or_zero, instead of last.
Popping and Pushing the Viewed Stack
I didn’t bother to pass the Viewed list as a parameter to these routines, because it’s the only stack in this app, I named the routines for their single purposes, and I wanted to avoid any possible bugs that might arise if it turned out that the Viewed list was being copied into a parameter, rather than being passed directly.
Last, pop_viewed, and push_viewed are the only routines that access the Viewed stack.
Downloads
(App Inventor 1 is no longer supported, so don’t bother downloading these.)
The .apk comes pre-loaded with documentation on the Documenter screen, and can be run standalone.
댓글 없음:
댓글 쓰기