What's the best way to do

Hello.

What's the best way to call functions between files?

my file teste4app.mc

import Toybox.Application;
import Toybox.Lang;
import Toybox.WatchUi;
import Toybox.ActivityRecording;
import Toybox.Application.Storage;
import Toybox.Position;

class teste4App extends Application.AppBase {
  function getInitialView() as Array<Views or InputDelegates>? {

            var view = new teste4View(); 
            var delegate = new teste4Delegate(); 
            return [ new teste4View(), new teste4Delegate(view) ]; 


    }

 function onKey(evt) { 

    if (evt.getKey() == 4) { System.println("start session"); 
        StartRecordSession(); 
    }
    
}

my file teste4view.mc

import Toybox.Graphics;
import Toybox.WatchUi;

import Toybox.Lang;
using Toybox.Activity;
using Toybox.ActivityRecording;
using Toybox.Timer;
using Toybox.Attention;
import Toybox.Position;

class teste4View extends WatchUi.View {

function StartRecordSession() {
    System.println("lets start");
    
}

}

I already tried this:

teste4app.mc

import Toybox.Application;
import Toybox.Lang;
import Toybox.WatchUi;
import Toybox.ActivityRecording;
import Toybox.Application.Storage;
import Toybox.Position;

class teste4App extends Application.AppBase {
  function getInitialView() as Array<Views or InputDelegates>? {

            var view = new teste4View(); 
            var delegate = new teste4Delegate(); 
            return [ new teste4View(), new teste4Delegate(view) ]; 


    }
}

testeview4.mc

import Toybox.Graphics;
import Toybox.WatchUi;

import Toybox.Lang;
using Toybox.Activity;
using Toybox.ActivityRecording;
using Toybox.Timer;
using Toybox.Attention;
import Toybox.Position;

class teste4View extends WatchUi.View {

function StartRecordSession() {
    System.println("lets start");
    
}

}

class teste4Delegate extends WatchUi.BehaviorDelegate {


    function onKey(evt) { 
    if (evt.getKey() == 4) { System.println("Vamos começar a gravar a sessão"); 
        //StartRecordSession(); 
        StartRecordSession(); 
    
    
    }
    
 }

but always got:

Class 'teste4Delegate' does not initialize its super class, 'BehaviorDelegate'

Thanks!!

btw, why OnKey()  doesn't work on view file ? but work on app file ??

thanks!!
  • Look at the recordsample in the SDK

    the onKey in your AppBase is never called.  It's the one in the delegate that is

    Your delegate is missing:

        function initialize() {
            BehaviorDelegate.initialize();
        }

    which is the reason for the message.

  • ok, all my code

    app.mc

    import Toybox.Application;
    import Toybox.Lang;
    import Toybox.WatchUi;
    import Toybox.ActivityRecording;
    import Toybox.Application.Storage;
    import Toybox.Position;
    
    class teste4App extends Application.AppBase {
    
         private var _recordSampleView as RecordSampleView?;
    
        function initialize() {
            AppBase.initialize();
        }
    
        // onStart() is called on application start up
        function onStart(state as Dictionary?) as Void {
    //EXPLORAR MELHOR, SE NAO TER POSICAO QUER DIZER QUE NÃO ESTÁ NENHUMA SESSÃO A GRAVAR... OU SE É O CONTRARIO...
            System.println("gpsOn, has position: " + (Toybox has :Postition)); 
            if( Toybox has :Postition ) { 
            System.println("Toybox has :Postition"); 
            Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition)); 
            } else { System.println("não temos posição GPS");}
    
        }
    
        // onStop() is called when your application is exiting
        function onStop(state as Dictionary?) as Void {
            var recordSampleView = _recordSampleView;
            if (recordSampleView != null) {
                recordSampleView.stopRecording();
            }
            Position.enableLocationEvents(Position.LOCATION_DISABLE, method(:onPosition));
            System.println("DESLIGOU O GPS");
        }
    
        public function onPosition(info as Info) as Void {
            System.println("segundo a segundo");
        }
    
        // Return the initial view of your application here
        function getInitialView() as Array<Views or InputDelegates>? {
            
           return [ new teste4View(), new teste4Delegate() ] as Array<Views or InputDelegates>;
    
           /*
                var view = new teste4View(); 
                var delegate = new teste4Delegate(); 
                return [ new teste4View(), new teste4Delegate(view) ]; 
            */
    
        }
    
        function getApp() as teste4App {
            return Application.getApp() as teste4App;
        }
        
    
    }

    delegate.mc

    import Toybox.Lang;
    import Toybox.WatchUi;
    
    using Toybox.Graphics;
    
    
    class teste4Delegate extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
    
    /*
    
        function onBack() {
    
            WatchUi.popView(SLIDE_IMMEDIATE);
    
        }
    */
        function onMenu() as Boolean {
    
            var menu = new WatchUi.Menu2({:title=>"Wind Surf"});
              
              menu.addItem(new WatchUi.IconMenuItem("Sails", null, "item_sail", new CustomIcon(Rez.Drawables.LauncherIcon), null));
              menu.addItem(new WatchUi.IconMenuItem("Boards", null, "item_board", new CustomIcon(Rez.Drawables.LauncherIcon), null));
              
            WatchUi.pushView(menu, new teste4Menu2Delegate(), WatchUi.SLIDE_IMMEDIATE);
    
            return true;
        }
    
    /*
        function onKey(evt) { 
        //getKey dá o botão que foi carregado, 4 é o botão começar, o vermelho
        if (evt.getKey() == 4) { System.println("Vamos começar a gravar a sessão"); }
    
        // return false é como se fosse o elseif, se ele não entrar no if retorna falso
        return false;
    
        } 
    */
    
    }
    
    class CustomIcon extends WatchUi.Drawable {
        var bitmapResource = null;
    
        public function initialize(resourceId) {
            Drawable.initialize({});
            bitmapResource = WatchUi.loadResource(resourceId);
        }
    
        public function draw(dc as Dc) as Void {
            dc.setColor(Graphics.COLOR_TRANSPARENT, Graphics.COLOR_TRANSPARENT);
            dc.clear();
            dc.drawBitmap(5, 20, bitmapResource);
        }
    
       
        
    }

    menudelegate.mc

    import Toybox.Lang;
    import Toybox.System;
    import Toybox.WatchUi;
    
    class teste4Menu2Delegate extends WatchUi.Menu2InputDelegate {
    
        function initialize() {
            Menu2InputDelegate.initialize();
        }
    
        function onSelect(item) {
    
            if (item.getId().equals("item_sail")) { 
    
                System.println("VELAS");
    
                //CREATE SUB MENU
                var menu_sub = new WatchUi.Menu2({:title=>"Sails Size"});
    
                menu_sub.addItem(new WatchUi.MenuItem("7.8",null,"sail1",null));
                menu_sub.addItem(new WatchUi.MenuItem("7.1", null,"sail2", null));
                menu_sub.addItem(new WatchUi.MenuItem("6", null,"sail3", null));
    
                WatchUi.pushView(menu_sub, new teste4Menu2Delegate(), WatchUi.SLIDE_IMMEDIATE);
    
                return true;
    
            } 
                else if (item.getId().equals("sail1")) { System.println("you chose sail 7.8"); }
                else if (item.getId().equals("sail2")) { System.println("you chose sail 7.1"); }
                else if (item.getId().equals("sail3")) { System.println("you chose sail 6"); }
    
    
            return false;
    
        }
    
    
        /*
            function onMenuItem(item as Symbol) as Void {
    
    
            }
        */
    
    
    }

    view.mc

  • view.mc 

    import Toybox.Graphics;
    import Toybox.WatchUi;

    import Toybox.Lang;
    using Toybox.Activity;
    using Toybox.ActivityRecording;
    using Toybox.Timer;
    using Toybox.Attention;
    import Toybox.Position;

    class teste4View extends WatchUi.View {

    var WatchLenghtX = System.getDeviceSettings().screenWidth;
    var WatchLenghtY = System.getDeviceSettings().screenHeight;
    var WatchCenterX = WatchLenghtX / 2;
    var WatchCenterY = WatchLenghtY / 2;
    var battery = System.getSystemStats().battery;
    var myTime = System.getClockTime(); // ClockTime object
    var hour = myTime.hour.format("%02d");
    var minutes = myTime.min.format("%02d");
    var seconds = myTime.sec.format("%02d");
    var completeHours = hour + ":" + minutes;
    var session = null;
    var GPSQuality = Position.QUALITY_GOOD;

    function initialize() {
    View.initialize();
    // iniciar gps já foi iniciado no OnStart
    //Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, self.method(:onPosition));
    }


    // 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
    function onUpdate(dc as Dc) as Void {
    // Call the parent onUpdate function to redraw the layout
    View.onUpdate(dc);
    drawLines(dc);
    drawLayoutWindSurf(dc);
    drawLayoutBars(dc);
    }

    // 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 {
    }

    function drawLines(dc as Dc) as Void {
    // var barLenght = 60;
    // dc.drawLine(WatchCenterX, 10, WatchCenterX, barLenght);
    //dc.drawLine(mCenterX - 1, System.getDeviceSettings().screenHeight - LINE_HEIGHT, mCenterX - 1, System.getDeviceSettings().screenHeight - 10);
    }

    hidden static var bailoutCounter as Number = 0;
    function onPosition(info as Position.Info) as Void {

    System.println("entrou no onPosition");
    /*
    if (info == null || info.accuracy == null) {
    return;
    }

    if (info.accuracy == Position.QUALITY_USABLE) {
    bailoutCounter++;
    }

    if (info.accuracy == Position.QUALITY_GOOD or bailoutCounter > 10) {
    System.println("qualidade boa");
    // Either have a Good quality or a usable for 10 times
    if ($.session == null) {
    System.println("Position usable. Start recording.");
    $.session = ActivityRecording.createSession({
    :name=>"Windsurf",
    :sport=>43 as ActivityRecording.Sport1, // SPORT_WINDSURFING 43
    });
    $.session.start();
    }
    }*/
    }

    function StartRecordSession() {
    System.println("entrou na função a partir do delegate");

    if (info.accuracy == Position.QUALITY_GOOD) {
    if (Toybox has :ActivityRecording) { // check device for activity recording
    if ((session == null) || (session.isRecording() == false)) {
    session = ActivityRecording.createSession({ // set up recording session
    :name=>"Windsurf", // set session name
    :sport=>ActivityRecording.SPORT_WINDSURFING, // set sport type
    //:subSport=>ActivityRecording.SUB_SPORT_GENERIC // set sub sport type
    });
    session.start(); // call start session
    }
    else if ((session != null) && session.isRecording()) {
    session.stop(); // stop the session
    session.save(); // save the session
    session = null; // set session control variable to null
    }
    }
    }
    return true; // return true for onSelect function
    }

    //! Stop the recording if necessary
    public function stopRecording() as Void {
    // var session = _session;
    if ((Toybox has :ActivityRecording) && isSessionRecording() && (session != null)) {
    session.stop();
    session.save();
    session = null;
    WatchUi.requestUpdate();
    }
    }
    public function isSessionRecording() as Boolean {

    if (session != null) {
    return session.isRecording();
    }
    return false;
    }

    function drawLayoutBars(dc as Dc) as Void {

    var ThicknessBar = 3;
    var ThicknessHalfBar = ThicknessBar / 2.0;
    var WatchCenterXBar = WatchCenterX - ThicknessHalfBar;
    var WatchCenterYBar = WatchCenterY - ThicknessHalfBar;

    // barra em azul
    dc.setColor(Graphics.COLOR_BLUE, Graphics.COLOR_TRANSPARENT);
    // barra horizontal
    dc.fillRectangle(0, WatchCenterYBar, WatchLenghtY, ThicknessBar);

    // barra vertical de baixo
    dc.fillRectangle(WatchCenterXBar, WatchCenterY+20, ThicknessBar, 50);

    // vertical bar
    // dc.fillRectangle(WatchCenterXBar, 0, ThicknessBar, WatchLenghtY);

    }

    function drawLayoutWindSurf(dc as Dc) as Void {

    //definir fundo do relogio branco
    dc.clearClip();
    dc.setColor(Graphics.COLOR_TRANSPARENT, Graphics.COLOR_WHITE);
    dc.clear();

    //definir letras pretas
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);

    // escrever horas
    dc.drawText(WatchCenterX, WatchCenterY-95 , Graphics.FONT_SYSTEM_NUMBER_THAI_HOT, completeHours, Graphics.TEXT_JUSTIFY_CENTER);

    //function GPSsignal()

    // escrever icons ou sinal gps ou outra informação mas pequena
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_DK_GREEN);
    dc.drawText(WatchCenterX-50, WatchCenterY-32, Graphics.FONT_XTINY, "GPS", Graphics.TEXT_JUSTIFY_RIGHT);
    // escrever percentagem bateria
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);
    dc.drawText(WatchCenterX, WatchCenterY-32, Graphics.FONT_XTINY, battery.format("%02d") + "%", Graphics.TEXT_JUSTIFY_CENTER);

    // mostrar se sessão está a ser gravada ou não
    dc.setColor(Graphics.COLOR_DK_GREEN, Graphics.COLOR_TRANSPARENT);
    dc.drawText(WatchCenterX+60, WatchCenterY-32, Graphics.FONT_XTINY, "Rec..", Graphics.TEXT_JUSTIFY_CENTER);

    //definir letras pretas
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);

    // km percorridos
    dc.drawText(WatchCenterX-50, WatchCenterY , Graphics.FONT_XTINY, "km", Graphics.TEXT_JUSTIFY_CENTER);
    dc.drawText(WatchCenterX-50, WatchCenterY+33 , Graphics.FONT_NUMBER_MEDIUM, "35.4", Graphics.TEXT_JUSTIFY_CENTER);
    // jibes
    dc.drawText(WatchCenterX+60, WatchCenterY , Graphics.FONT_XTINY, "jibes", Graphics.TEXT_JUSTIFY_CENTER);
    dc.drawText(WatchCenterX+50, WatchCenterY+33 , Graphics.FONT_NUMBER_MEDIUM, "12", Graphics.TEXT_JUSTIFY_CENTER);
    // alguma cooisa
    dc.drawText(WatchCenterX, WatchCenterY+75 , Graphics.FONT_LARGE, "algo", Graphics.TEXT_JUSTIFY_CENTER);

    var positionInfo = Position.getInfo();
    /*
    if (positionInfo.accuracy >= Position.QUALITY_USABLE){
    GPSready=true;
    }*/
    System.println("qualidade GPS " + positionInfo.accuracy);
    }


    }

    class teste4Delegate extends WatchUi.BehaviorDelegate {


    function onKey(evt) {

    //getKey dá o botão que foi carregado, 4 é o botão começar, o vermelho
    if (evt.getKey() == 4) { System.println("Vamos começar a gravar a sessão");
    //StartRecordSession();
    StartRecordSession();
    }

    // return false é como se fosse o elseif, se ele não entrar no if retorna falso
    return false;

    }
    }


  • add initialise in teste4Delegate and call inside BehaviourDelegate.initalise()

  • Thanks to both! now its working. MANY THANKS!!

  • and voilá :D 

    app.mc

    function getInitialView() as Array<Views or InputDelegates>? {
            
           return [ new teste4View(), new teste4DelegatePassar() ] as Array<Views or InputDelegates>;
    
        }

    view.mc

    class teste4DelegatePassar extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
        function onKey(evt) { 
        //getKey dá o botão que foi carregado, 4 é o botão começar, o vermelho
        if (evt.getKey() == 4) { System.println("Vamos começar a gravar a sessão"); 
     
            StartRecordSession();   
    }
    
    }