Now that we have support for multiple routes and we can choose one of them, it's important to be able to remember which one we've chosen so that we can show it the moment the widget is selected. Monkey C provides Device Storage for this, so let's use that now to store the value of currRoute.
This is a basic singleton class that essentially has methods for getting and setting values in the application storage. As you might expect, getValue returns null if the information is not there.
So we can easily add setValue to our next and previous methods:
function previousRoute() {And then in the constructor we can retrieve it. If it isn't there, we default to 0:
self.currRoute --;
if (self.currRoute < 0) {
self.currRoute = self.routes.size()-1;
}
Storage.setValue("currRoute", currRoute);
reset();
}
function nextRoute() {
self.currRoute ++;
if (self.currRoute >= self.routes.size()) {
self.currRoute = 0;
}
Storage.setValue("currRoute", currRoute);
reset();
}
function initialize(routes as Array<Route>) {That's checked in as METROLINK_MC_ROUTE_STORAGE and it's time to deploy to the watch again.
self.showWait = true;
self.routes = routes;
var stored = Storage.getValue("currRoute");
if (stored) {
self.currRoute = stored;
} else {
self.currRoute = 0;
}
View.initialize();
}
On the Main View
So this works in the way in which I designed it, but one thing I can do on the watch that I haven't yet been able to do in the simulator is "leave" the nested view (I push the physical button). I notice that when I do this, it "forgets" the selected route and goes back to the route it was previously thinking of. Looking at the code, presumably the initialize method is not called again when a view is "popped", but onShow is. So the simple fix is to move the recovery of currRoute into onShow. It's also necessary to make sure that the query is fired again, since the data we have on hand may be out of date (it would probably be possible to get the child to pass back its current data, but I'm not sure I'm up to that).function onShow() as Void {This is then tagged in as METROLINK_MC_CURRROUTE_ONSHOW.
var stored = Storage.getValue("currRoute");
if (stored) {
self.currRoute = stored;
} else {
self.currRoute = 0;
}
self.timer = null;
self.showWait = true;
}
No comments:
Post a Comment