Skip to content

Commit

Permalink
Handling Exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Culhane committed Jul 7, 2020
1 parent 44be4f6 commit 639fa48
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
4 changes: 0 additions & 4 deletions resources/menus/menu.xml

This file was deleted.

2 changes: 0 additions & 2 deletions resources/strings/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@

<string id="prompt">Click the menu button</string>

<string id="menu_label_1">Item 1</string>
<string id="menu_label_2">Item 2</string>
</strings>
13 changes: 12 additions & 1 deletion source/ConnectIQLIFXApp.mc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ using Toybox.Application;
using Toybox.WatchUi;

class ConnectIQLIFXApp extends Application.AppBase {
var mSelection;

hidden var mView;
var lifx_api;
var main_delegate;
function initialize() {
AppBase.initialize();
}
Expand All @@ -19,8 +22,16 @@ class ConnectIQLIFXApp extends Application.AppBase {
// Return the initial view of your application here
function getInitialView() {
lifx_api = new LIFX_API();
main_delegate = new ConnectIQLIFXDelegate(lifx_api);
mView = new ConnectIQLIFXView(lifx_api);
return [mView, new ConnectIQLIFXDelegate(lifx_api, mView.method(:onReceive))];
return [mView, main_delegate];
}
function getSelection() {
return mSelection;
}

function setSelection(bool) {
mSelection = bool;
}

}
31 changes: 12 additions & 19 deletions source/ConnectIQLIFXDelegate.mc
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,22 @@ class ConnectIQLIFXDelegate extends WatchUi.BehaviorDelegate {
var notify;
var lifx_api;
// Set up the callback to the view
function initialize(api_obj, handler) {
function initialize(api_obj) {
WatchUi.BehaviorDelegate.initialize();
notify = handler;
lifx_api = api_obj;
}

// Handle menu button press
function onMenu() {
// // Use WatchUi.switchToView() ??
// // https://developer.garmin.com/downloads/connect-iq/monkey-c/doc/Toybox/WatchUi.html#switchToView-instance_method
// var menu = new WatchUi.Menu();
// var delegate;
// menu.setTitle("Select Scene");
// var num_scenes = lifx_api.scenes.size();
// System.println("Number of scenes to display: "+ num_scenes);
// for( var i = 0; i < num_scenes; i++ ) {
// menu.addItem(lifx_api.scenes[i]["name"], lifx_api.scenes[i]["scene_num"]);
// }
//
// delegate = new LifxSceneInputDelegate(self.lifx_api);
// WatchUi.pushView(menu, delegate, WatchUi.SLIDE_IMMEDIATE);
// return true;
// Handle back button press to exit safely
function onBack() {
System.println("back pressed");
WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
return false;
}

function onSelect() {
return true;
return false;
}

}

class LifxMainInputDelegate extends WatchUi.MenuInputDelegate {
Expand All @@ -46,6 +35,7 @@ class LifxMainInputDelegate extends WatchUi.MenuInputDelegate {

function onMenuItem(item) {
// Builds the sub menus for each option
Application.getApp().setSelection(true);

System.println("Recieved item: " + item);
if (item == :toggle_all_lights) {
Expand Down Expand Up @@ -75,6 +65,9 @@ class LifxMainInputDelegate extends WatchUi.MenuInputDelegate {

delegate = new LifxLightInputDelegate(self.lifx_api);
WatchUi.pushView(menu, delegate, WatchUi.SLIDE_IMMEDIATE);

} else if (item == :exit) {
System.exit();
} else {
System.println("Item not recognised: " + item);
}
Expand Down
34 changes: 26 additions & 8 deletions source/ConnectIQLIFXView.mc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class ConnectIQLIFXView extends WatchUi.View {
hidden var mMessage = "Press menu button";
hidden var mModel;
hidden var lifx_api;
var main_menu;
var main_menu_delegate;
function initialize(api_obj) {
WatchUi.View.initialize();
lifx_api = api_obj;
Expand All @@ -13,19 +15,21 @@ class ConnectIQLIFXView extends WatchUi.View {
// Load your resources here
function onLayout(dc) {
mMessage = "Loading data from LIFX...";
main_menu = gen_main_menu();
main_menu_delegate = new LifxMainInputDelegate(self.lifx_api);
}

// Restore the state of the app and prepare the view to be shown
function onShow() {
// Start initial view
var init_menu = new WatchUi.Menu();
var init_menu_delegate;
init_menu.setTitle("Select operation");
init_menu.addItem("Toggle all lights", :toggle_all_lights);
init_menu.addItem("Apply a Scene", :apply_scene);
init_menu.addItem("Toggle a light", :toggle_light);
init_menu_delegate = new LifxMainInputDelegate(self.lifx_api);
WatchUi.pushView(init_menu, init_menu_delegate, WatchUi.SLIDE_IMMEDIATE);
var selection = Application.getApp().getSelection();
if (selection == null) {
Application.getApp().setSelection(false);
WatchUi.pushView(main_menu, main_menu_delegate, WatchUi.SLIDE_IMMEDIATE);
}
else if (selection == false) {
WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
}
}

// Update the view
Expand All @@ -38,8 +42,22 @@ class ConnectIQLIFXView extends WatchUi.View {
// Called when this View is removed from the screen. Save the
// state of your app here.
function onHide() {
return true;
}


function gen_main_menu(){
// Generates the main menu
var init_menu = new WatchUi.Menu();
init_menu.setTitle("Select operation");
init_menu.addItem("Toggle all lights", :toggle_all_lights);
init_menu.addItem("Apply a Scene", :apply_scene);
init_menu.addItem("Toggle a light", :toggle_light);
init_menu.addItem("Exit", :exit);
return init_menu;
}


function onReceive(args) {
if (args instanceof Lang.String) {
mMessage = args;
Expand Down

0 comments on commit 639fa48

Please sign in to comment.