User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

sample_code:visitor_log

Visitor log

This is the first working draft of a visitor sign-in log for my workplace.

The live version includes more error-checking for signing out, extra code to deal with the possibility that the app is left running over midnight and a fair bit of prettying up.

The code

visitors.js
//Visitor sign-in app by Steve Garman
//saves one day's data as a JSON string in a file
//Global variables
var recArray = [],
  fileFolder = "/sdcard/sjgApps/",
  jsonPath;
//Global layouts and controls
var scrollr, layScroll, edtName, edtOrg, edtVehicle, edtReason;
 
//Called when application is started.
function OnStart()
  {
    if (!app.IsTablet()) app.SetOrientation("Landscape");
    //file variables
    var d = new Date();
    jsonPath = fileFolder + "vistorBook" +
      d.toISOString().slice(0, 10).replace(/-/g, "_") + ".json";
    app.MakeFolder(fileFolder)
      //Create main layout.
    var lay = app.CreateLayout("linear", "Left");
    //Create a scroller and add it to main layout
    scrollr = app.CreateScroller(1.0, 0.8);
    lay.AddChild(scrollr);
    //Create a layout inside scroller. 
    layScroll = app.CreateLayout("Linear", "Left");
    scrollr.AddChild(layScroll);
 
    //Create a sign-in layout
    var layNew = app.CreateLayout("linear", "Horizontal,Left");
    edtName = app.CreateTextEdit("", .16);
    edtName.SetHint("Name");
    layNew.AddChild(edtName);
    edtOrg = app.CreateTextEdit("", .16);
    edtOrg.SetHint("Organization");
    layNew.AddChild(edtOrg);
    edtVehicle = app.CreateTextEdit("", .16);
    edtVehicle.SetHint("Vehicle reg.");
    layNew.AddChild(edtVehicle);
    edtReason = app.CreateTextEdit("", .16);
    edtReason.SetHint("Reason for visit");
    layNew.AddChild(edtReason);
    var btnCreate = app.CreateButton("Ok");
    btnCreate.SetOnTouch(btnCreate_OnClick);
    layNew.AddChild(btnCreate);
 
    //Add sign-in layout to main layout
    lay.AddChild(layNew);
 
    //Add main layout to app.    
    app.AddLayout(lay);
    //check if today's visitor book already active.
    if (app.FileExists(jsonPath))
    {
      loadJSONfile();
    }
  } //OnStart
 
function btnCreate_OnClick()
  {
    app.HideKeyboard();
    var name = edtName.GetText();
    var org = edtOrg.GetText();
    var reg = edtVehicle.GetText();
    var reason = edtReason.GetText();
    //check required fields
    var errs = "";
    if (name == "") errs = "'Name' ";
    if (reason == "") errs = errs + "'Reason for visit' ";
    if (errs != "")
    {
      //bail out
      app.Alert(errs + "cannot be empty");
      return;
    }
    // clear sign-in form for next visitor
    edtName.SetText("");
    edtOrg.SetText("");
    edtVehicle.SetText("");
    edtReason.SetText("");
    // create a record
    var obj = {};
    obj.name = name;
    obj.org = org;
    obj.reg = reg;
    obj.reason = reason;
    var d = new Date();
    obj.timeIn = d.toTimeString().slice(0, 5);
    obj.timeOut = "Out";
    // add this record to the array
    recArray.push(obj);
    saveJSONfile();
    // display record on scroller
    layScroll.AddChild(recordLayout(obj));
    scrollr.ScrollTo(0, layScroll.GetHeight());
  } //btnCreate_OnClick
 
function btnOut_OnClick()
  {
    //has this visitor already signed out?
    if (this.GetText() != "Out") return;
    //not yet, so record time out
    var d = new Date();
    this.obj.timeOut = d.toTimeString().slice(0, 5);
    this.SetText(this.obj.timeOut);
    //update copy of recArray on file
    saveJSONfile();
  } //btnOut_OnClick
 
function recordLayout(obj)
  {
    //create a new horizontal layout to display this record
    var newlay = app.CreateLayout("Linear", "Horizontal,FillXY");
    var txt = app.CreateText(obj.name, 0.15);
    newlay.AddChild(txt);
    txt = app.CreateText(obj.org, 0.15);
    newlay.AddChild(txt);
    txt = app.CreateText(obj.reg, 0.15);
    newlay.AddChild(txt);
    txt = app.CreateText(obj.reason, 0.15);
    newlay.AddChild(txt);
    txt = app.CreateText(obj.timeIn, 0.15);
    newlay.AddChild(txt);
    var btn = app.CreateButton(obj.timeOut, 0.15);
    btn.obj = obj;
    btn.SetOnTouch(btnOut_OnClick);
    newlay.AddChild(btn);
    return newlay;
  } //recordLayout
 
function saveJSONfile()
  {
    //update copy of recArray on file
    var s = JSON.stringify(recArray);
    app.WriteFile(jsonPath, s)
  } //saveJSONfile
 
function loadJSONfile()
  {
    //read copy of recArray from file
    var s = app.ReadFile(jsonPath)
    recArray = JSON.parse(s)
    var len = recArray.length;
    for (var i = 0; i < len; i++)
    {
      var obj = recArray[i];
      layScroll.AddChild(recordLayout(obj));
    }
    scrollr.ScrollTo(0, layScroll.GetHeight());
  } //loadJSONfile

Notes

This app was designed for a tablet. It is a replacement for an old paper form and is designed to be similar.

The final version is in use on a 7-inch tablet, which is mainly kept in landscape mode, though sometimes they rotate the tablet to make it easier to log-out a visitor who is way back the list.

At the suggestion of Jorge Ramirez, I have set the orientation to Landscape if it is being used on a phone.

The final version includes code to save previous files to a network server and the internal code to deal with date/time is significantly more robust, so that visitors who have not been logged out can be copied across to the live list as part of the backup process.

Please note. If this code is used with large numbers of records, the slow bit is displaying them all. For sizeable databases, the method of creating hundreds of layouts will need to be avoided.

sample_code/visitor_log.txt · Last modified: 2014/12/07 05:05 (external edit)