Skip to content

Commit

Permalink
update watchface with same code as datafield
Browse files Browse the repository at this point in the history
  • Loading branch information
avouspierre committed May 5, 2023
1 parent cdf1554 commit aef3e13
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
16 changes: 12 additions & 4 deletions iAPSGarminWatchface/source/iAPSBGServiceDelegate.mc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ class iAPSBGServiceDelegate extends System.ServiceDelegate {

function initialize(){
ServiceDelegate.initialize();
//for fenix 5
phoneCallback = method(:onReceiveMessage) as Communications.PhoneMessageCallback;
Communications.registerForPhoneAppMessages(phoneCallback);
//for fenix 5 if required

if (Background has :registerForPhoneAppMessageEvent) {
// nothing to do
} else {
Communications.registerForPhoneAppMessages(method(:onReceiveMessage) as Communications.PhoneMessageCallback);
System.println("****add the registerForPhoneAppMessages done****");
}
}

function onReceiveMessage(msg)
{
System.println("a message from onReceiveMessage! ");
System.println(msg);
Background.exit(msg.data);
}

Expand All @@ -36,7 +43,8 @@ class iAPSBGServiceDelegate extends System.ServiceDelegate {
}

function onPhoneAppMessage(msg) {
System.println(msg.data);
System.println("****onPhoneAppMessage*****");
System.println(msg);
Application.Storage.setValue("status", msg.data as Dictionary);
Background.exit(null);
}
Expand Down
41 changes: 27 additions & 14 deletions iAPSGarminWatchface/source/iAPSWatchfaceApp.mc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import Toybox.Communications;

(:background)
class iAPSWatchfaceApp extends Application.AppBase {

var inBackground=false;

function initialize() {
AppBase.initialize();
}
Expand All @@ -29,32 +32,40 @@ class iAPSWatchfaceApp extends Application.AppBase {
if (Background has :registerForPhoneAppMessageEvent) {
Background.registerForPhoneAppMessageEvent();
System.println("****background is ok****");
}
System.println("****background is ok****");
} else {
System.println("****registerForPhoneAppMessageEvent is not available****");
}

} else {
System.println("****background not available on this device****");
}

}

function onBackgroundData(data) {
// for Fenix5
if (Background has :registerForPhoneAppMessageEvent) {

} else {
if (data instanceof Number) {
System.println("Not a dictionary");
if (data instanceof Number || data == null) {
System.println("Not a dictionary");
} else {
Application.Storage.setValue("status", data as Dictionary);
Background.registerForTemporalEvent(new Time.Duration(5 * 60));
}
}

WatchUi.requestUpdate();
System.println("try to update the status");
if (Background has :registerForPhoneAppMessageEvent) {
System.println("updated with registerForPhoneAppMessageEvent");
// Application.Storage.setValue("status", data as Dictionary);
} else {
System.println("update status");
Application.Storage.setValue("status", data as Dictionary);
Background.registerForTemporalEvent(new Time.Duration(5 * 60));
}
}
System.println("requestUpdate");
WatchUi.requestUpdate();
}

// onStop() is called when your application is exiting
function onStop(state as Dictionary?) as Void {
if(!inBackground) {
System.println("stop temp event");
Background.deleteTemporalEvent();
}
}

// Return the initial view of your application here
Expand All @@ -68,6 +79,8 @@ class iAPSWatchfaceApp extends Application.AppBase {
}

function getServiceDelegate() {
inBackground=true;
System.println("start background");
return [new iAPSBGServiceDelegate()];
}
}
Expand Down

0 comments on commit aef3e13

Please sign in to comment.