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

This is an old revision of the document!


Visitor log

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

The final copy 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(){
  //file variables
  var d = new Date();
  jsonPath = fileFolder + "vistorBook" +
     d.toISOString().slice(0,10).replace(/-/gi,"_")
     + ".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
sample_code/visitor_log.1413668984.txt.gz · Last modified: 2014/10/19 05:49 (external edit)