I’m creating a simple swim-app for my Vivoactive5.
If the top-button is clicked, or the top-half of the screen is tapped, the lap-counter advances.
If the bottom-button is clicked, or the bottom-half of the screen is tapped, the app closes.
On land it works fine, but as soon as I hold it submerged, the app stops.
How to prevent this stopping?
Attached my mc-file (all-in-one file), and the zipped project directory.
import Toybox.Application;
import Toybox.Lang;
import Toybox.WatchUi;
import Toybox.Time;
import Toybox.System;
import Toybox.Graphics;
import Toybox.Time.Gregorian;
public var _lap0 = 0;
public var _tim1, _tim1a, _tim2, _tim2a;
public function toHMS(secs) {
var hr = secs/3600;
var min = (secs-(hr*3600))/60;
var sec = secs%60;
var ret=min.format("%02d")+":"+sec.format("%02d");
if(hr!=0) {ret=hr.format("%d")+":"+ret;}
return ret;
}
class LapSwim1App extends Application.AppBase {
function initialize() {AppBase.initialize();}
// onStart() is called on application start up
function onStart(state as Dictionary?) as Void {
System.println("SwimApp started");
_tim1 = Time.now();
_tim1a = new Time.Duration(0);
_tim2 = _tim1;
_tim2a = _tim1a;
}
// onStop() is called when your application is exiting
function onStop(state as Dictionary?) as Void {
System.println("SwimApp stopped");
}
// Return the initial view of your application here
function getInitialView() as [Views] or [Views, InputDelegates] {
return [ new LapSwim1View(), new LapSwim1Delegate() ];
}
}
function getApp() as LapSwim1App {
return Application.getApp() as LapSwim1App;
}
class LapSwim1View extends WatchUi.View {
public var _lap1 = 0;
function initialize() {View.initialize();}
// Load your resources here
function onLayout(dc as Dc) as Void {
setLayout(Rez.Layouts.MainLayout(dc));
}
// Called when this View is brought to the foreground.
// Restore the state of this View and prepare it to be shown.
// This includes loading resources into memory.
function onShow() as Void {}
// Update the view
public function onUpdate(dc as Dc) as Void {
dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK);
dc.clear();
dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);
var x1 = dc.getWidth() / 2;
var y1 = (dc.getHeight() / 2) - 50;
var delta_y = 100;
var just1 = Graphics.TEXT_JUSTIFY_CENTER;
var font1 = Graphics.FONT_NUMBER_THAI_HOT;
dc.drawText(x1, y1 - delta_y, font1, toHMS(_tim2a.value()), just1);
dc.drawText(x1, y1, font1, toHMS(_tim1a.value()), just1);
dc.drawText(x1, y1 + delta_y, font1, _lap0, just1);
}
// Called when this View is removed from the screen.
// Save the state of this View here.
// This includes freeing resources from memory.
function onHide() as Void {}
}
class LapSwim1MenuDelegate extends WatchUi.MenuInputDelegate {
function initialize() {MenuInputDelegate.initialize();}
function onMenuItem(item as Symbol) as Void {}
}
class LapSwim1Delegate extends WatchUi.BehaviorDelegate {
function initialize() {BehaviorDelegate.initialize();}
function NewLap() {
_lap0++;
_tim1a = _tim1.subtract(Time.now());
_tim2a = _tim2.subtract(Time.now());
_tim1 = Time.now();
WatchUi.requestUpdate();
}
function onKey(keyEvent) {
// 4 = top key , 5 = bottom key
System.println("onKey: " + keyEvent.getKey() + " " +
keyEvent.getType() + " " + _lap0);
if (keyEvent.getKey() == 4) {NewLap();}
if (keyEvent.getKey() == 5) {System.exit();}
return true;
}
function onTap(clickEvent) {
System.println("onTap: "
+ clickEvent.getType() + " " +
+ clickEvent.getCoordinates());
if (clickEvent.getCoordinates()[1] < 200) {
NewLap(); // Top half of screen
} else {
System.exit(); // Bottom half of screen
}
return true;
}
function onMenu() as Boolean {
System.println("onMenu started");
WatchUi.pushView(
new Rez.Menus.MainMenu(),
new LapSwim1MenuDelegate(),
WatchUi.SLIDE_UP
);
return true;
}
}