SAVING A PICKER SELECTION

Hi Guys, 

I've gone back to try and fix my watch - manual data entry problems, of trying to enter or modify a few GPS positions ( the name and latitude or longitude ) That will be saved in the app Storage.set data  

I thought using the Picker factory would suit the name entry seeing as it may be letter or numbers, however when I implemented the sample factory, 

I cannot figure out how to exit the picker screen and save the string I have created. all the buttons are being used for up / down, enter value or cancel value and there does not seem to be a way to stop adding to the string and exit the page...

Any ideas?

using Toybox.Application;
using Toybox.Graphics;
using Toybox.WatchUi;

class StringPicker extends WatchUi.Picker {
    const mCharacterSet = " ABCDEFGHIJKLMNOPQRSTUVWXYZ -0123456789";
    hidden var mTitleText;
    hidden var mFactory;

    function initialize() {
        mFactory = new CharacterFactory(mCharacterSet, {:addOk=>true});
        mTitleText = "";

        var string = Application.getApp().getProperty("string");
        var defaults = null;
        var titleText = Rez.Strings.stringPickerTitle;

        if(string != null) {
            mTitleText = string;
            titleText = string;
            defaults = [mFactory.getIndex(string.substring(string.length()-1, string.length()))];
        }

        mTitle = new WatchUi.Text({:text=>titleText, :locX =>WatchUi.LAYOUT_HALIGN_CENTER, :locY=>WatchUi.LAYOUT_VALIGN_BOTTOM, :color=>Graphics.COLOR_WHITE});

        Picker.initialize({:title=>mTitle, :pattern=>[mFactory], :defaults=>defaults});
    }

    function onUpdate(dc) {
        dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK);
        dc.clear();
        Picker.onUpdate(dc);
    }

    function addCharacter(character) {
        mTitleText += character;
        mTitle.setText(mTitleText);
    }

    function removeCharacter() {
        mTitleText = mTitleText.substring(0, mTitleText.length() - 1);

        if(0 == mTitleText.length()) {
            mTitle.setText(WatchUi.loadResource(Rez.Strings.stringPickerTitle));
        }
        else {
            mTitle.setText(mTitleText);
        }
    }

    function getTitle() {
        return mTitleText.toString();
    }

    function getTitleLength() {
        return mTitleText.length();
    }

    function isDone(value) {
        return mFactory.isDone(value);
    }
}

class StringPickerDelegate extends WatchUi.PickerDelegate {
    hidden var mPicker;

    function initialize(picker) {
        PickerDelegate.initialize();
        mPicker = picker;
    }

    function onCancel() {
        if(0 == mPicker.getTitleLength()) {
            WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
        }
        else {
            mPicker.removeCharacter();
        }
    }

    function onAccept(values) {
        if(!mPicker.isDone(values[0])) {
            mPicker.addCharacter(values[0]);
        }
        else {
            if(mPicker.getTitle().length() == 0) {
                Application.getApp().deleteProperty("string");
            }
            else {
                Application.getApp().setProperty("string", mPicker.getTitle());
            }
            WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
        }
    }

}

  • If you look at the code, you will see that the picker is popped from view when

    1. StringPickerDelegate.onCancel() is called and StringPicker.getTitleLength() returns 0 (you press back and there are no characters to remove)
    2. StringPickerDelegate.onAccept() is called and StringPicker.isDone() returns true (which returns true if CharacterFactory.isDone() returns true for the given character)... i.e., the 'Done' item is selected in the factory.
  • Ok, the text that is displayed when using the picker,  to confirm the selection... is displayed as

    "     'actrp    "    with the letter p half occluded.

    I have no idea where it comes from or how to set it..... but I need to change it to SAVE so it makes sense for the user

    Any ideas?  I'm not trying to get rid of the picker, I'm trying to save the string that has been created by the picker and at the moment the save characters displayed make no sense....

  • The issue it the confirm text is "     'actrp    "  

    anyone know where this is set or how to change?? 

  • I'm pretty sure the code worked just fine as provided, so any problem that exists is something that you should be able to track back to changes you've made.

    As for where to find where the text displayed for the 'Done' option comes from, I'd think you'd need to look at CharacterFactory.getDrawable(). The original code uses the string resource Rez.Strings.characterPickerOk.

  • Ok, I'll give that a go.

  • I found it... The value I had in Strings.characterPickerOK  was too big to fit inside the Display region, so it was displaying the partial word with ' at the beginning. Thanks for the help