Soon after I completed my first HR home theater remote controller design, I found that making changes to add or delete functionality was a monstrous pain. I had shoveled so much “functionality” into individual buttons that every change I made rippled through nearly the entire layout. After a little experimentation with Scenes, I decided to redesign the remote controller as a state machine. With this state machine approach, I designed my first plugin as the state machine controller, and constructed Scenes as the elements of the state machine executive. Now, each button only performs 2 operations when pressed: 1) Load a new page into the page browser and 2) Update a plugin attribute with the ID of the pressed button.
My first pass at the remote controller redesign (Auto_Lock_OFF.hrp) followed the conventional wisdom that once started, the HR app would run 24/7 without interruption. My host is an iPad Mini (Gen 5 with a HOME button), so I set Auto Lock to “Never”. On the HR MainPage.xaml settings, I set IdleDim to True and IdleTimeout to 120 (arbitrary, but I just followed the iPad 2-minute minimum). I initialized all my plugin attributes within the onConnect() function and everything worked well. Until it didn’t. When the iPad mini screen dims, everyone in my household reflexively hits the HOME button - which suspends the app and returns the iPad to the home page. When I tap the app to restart, onConnect() executes, re-initializing all my attributes. I lose the state of my system. Newer-generation iPads that don’t use a HOME button probably wouldn’t be affected, and could probably use this version just fine.
I took a second pass at the redesign, assuming Auto Lock was ON. I needed to find a mechanism to save my plugin attribute information through a sleep/wakeup cycle. I’ve known for some time that Device Variables persist through a sleep/wakeup cycle, so I took an ugly approach and duplicated my plugin attributes as Device Variables. This accomplishes two things. First, whether the app comes up initially or through the HOME reflex response, I use onConnect() to trigger a special RestoreState Scene. RestoreState copies the contents of specific Device Variables into their corresponding plugin Attributes. Second, whenever a button press occurs, I force the plugin to trigger another special SaveState Scene. SaveState copies the newly-changed plugin Attributes into their corresponding Device Variables. SaveState ensures the latest state machine state variables persist through a sleep/wake cycle, and RestoreState ensures the plugin state machine controller is always working with the freshest information.
Both controller versions, their respective plugins, and additional details on the state machine can be found in the following Dropbox link: Dropbox - Home_Theater_State_Machine.zip - Simplify your life