DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:visitor_log

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
sample_code:visitor_log [2014/10/18 21:49]
stevegarman created
sample_code:visitor_log [2014/12/06 21:05] (current)
stevegarman change curly brace style
Line 2: Line 2:
 This is the first working draft of a visitor sign-in log for my workplace. 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 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 ===== ===== The code =====
 <code javascript visitors.js>​ <code javascript visitors.js>​
 //Visitor sign-in app by Steve Garman //Visitor sign-in app by Steve Garman
 //saves one day's data as a JSON string in a file //saves one day's data as a JSON string in a file
- 
 //Global variables //Global variables
-var recArray = [], fileFolder = "/​sdcard/​sjgApps/",​ jsonPath;+var recArray = [], 
 +  ​fileFolder = "/​sdcard/​sjgApps/",​ 
 +  ​jsonPath;
 //Global layouts and controls //Global layouts and controls
-var scrollr, layScroll, edtName, edtOrg, edtVehicle,​edtReason;​+var scrollr, layScroll, edtName, edtOrg, edtVehicle, edtReason;
  
 //Called when application is started. //Called when application is started.
-function OnStart(){ +function OnStart() 
-  //file variables +  ​
-  var d = new Date(); +    if (!app.IsTablet()) app.SetOrientation("​Landscape"​);​ 
-  jsonPath = fileFolder + "​vistorBook"​ + +    ​//file variables 
-     ​d.toISOString().slice(0,​10).replace(/​-/​gi,"​_"​) +    var d = new Date(); 
-     + "​.json";​ +    jsonPath = fileFolder + "​vistorBook"​ + 
-  app.MakeFolder(fileFolder) +      d.toISOString().slice(0,​ 10).replace(/​-/​g, "​_"​) + "​.json";​ 
-  //Create main layout. +    app.MakeFolder(fileFolder) 
-  var lay = app.CreateLayout( "​linear",​ "​Left"​ ); +      //Create main layout. 
-  //Create a scroller and add it to main layout +    var lay = app.CreateLayout("​linear",​ "​Left"​);​ 
-  scrollr = app.CreateScroller( 1.0, 0.8 );  +    //Create a scroller and add it to main layout 
-  lay.AddChild( scrollr );   +    scrollr = app.CreateScroller(1.0,​ 0.8); 
-  //Create a layout inside scroller.  +    lay.AddChild(scrollr);​ 
-  layScroll = app.CreateLayout( "​Linear",​ "​Left"​);​ +    //Create a layout inside scroller.  
-  scrollr.AddChild( layScroll ​); +    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.     +    ​//Create a sign-in ​layout 
-  app.AddLayoutlay ); +    var layNew = app.CreateLayout("​linear",​ "​Horizontal,​Left"​);​ 
-  //​check if today'​s visitor book already active+    edtName = app.CreateTextEdit("",​ .16); 
-  if(app.FileExists(jsonPath)){ +    ​edtName.SetHint("​Name"​);​ 
-    ​loadJSONfile(); +    layNew.AddChild(edtName);​ 
-  } +    edtOrg = app.CreateTextEdit("",​ .16); 
-}//OnStart+    ​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);​
  
-function btnCreate_OnClick( ){ +    ​//Add sign-in ​layout ​to main layout 
-  app.HideKeyboard();​ +    lay.AddChild(layNew);
-  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( ){ +    ​//Add main layout to app.    ​ 
-  ​//has this visitor already signed out? +    app.AddLayout(lay); 
-  if (this.GetText() != "​Out"​) return+    //check if today'​s visitor book already active. 
-  //not yet, so record time out +    ​if ​(app.FileExists(jsonPath)) 
-  var d = new Date(); +    { 
-  this.obj.timeOut = d.toTimeString().slice(0,5); +      loadJSONfile(); 
-  ​this.SetText(this.obj.timeOut); +    } 
-  //​update copy of recArray on file +  } //OnStart
-  ​saveJSONfile();​ +
-}//btnOut_OnClick+
  
-function ​recordLayout(obj){ +function ​btnCreate_OnClick() 
-  ​//create a new horizontal layout to display this record +  ​{ 
-  var newlay = app.CreateLayout"​Linear",​ "​Horizontal,​FillXY" ​); +    app.HideKeyboard(); 
-  var txt app.CreateText(obj.name, 0.15); +    var name edtName.GetText(); 
-  ​newlay.AddChild(txt); +    var org = edtOrg.GetText(); 
-  ​txt ​app.CreateText(obj.org, 0.15); +    var reg edtVehicle.GetText(); 
-  ​newlay.AddChild(txt); +    var reason = edtReason.GetText(); 
-  ​txt ​app.CreateText(obj.reg, 0.15); +    //​check required fields 
-  ​newlay.AddChild(txt); +    var errs "";​ 
-  ​txt ​= app.CreateText(obj.reason, 0.15); +    if (name == ""​errs = "'​Name'​ "
-  ​newlay.AddChild(txt); +    ​if ​(reason == ""​errs = errs + "'​Reason for visit' "
-  txt = app.CreateText(obj.timeIn, 0.15)+    if (errs !""​) 
-  ​newlay.AddChild(txt); +    { 
-  var btn app.CreateButton(obj.timeOut, ​0.15); +      //bail out 
-  btn.obj = obj+      ​app.Alert(errs + "​cannot be empty"​);​ 
-  btn.SetOnTouch(btnOut_OnClick); +      return; 
-  ​newlay.AddChild(btn); +    } 
-  ​return newlay+    // clear sign-in form for next visitor 
-}//recordLayout+    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 ​saveJSONfile(){ +function ​btnOut_OnClick() 
-  //update copy of recArray on file +  ​
-  var JSON.stringify(recArray); +    ​//has this visitor already signed out? 
-  app.WriteFile(jsonPath+    if (this.GetText() != "​Out"​) return; 
-}//​saveJSONfile+    //not yet, so record time out 
 +    ​var new Date(); 
 +    this.obj.timeOut = d.toTimeString().slice(05); 
 +    ​this.SetText(this.obj.timeOut);​ 
 +    ​//update copy of recArray on file 
 +    ​saveJSONfile(); 
 +  } //​btnOut_OnClick
  
-function ​loadJSONfile(){ +function ​recordLayout(obj) 
-  //read copy of recArray from file +  ​{ 
-  var s = app.ReadFile(jsonPath) +    //create a new horizontal layout to display this record 
-  recArray = JSON.parse(s) +    var newlay = app.CreateLayout("​Linear",​ "​Horizontal,​FillXY"​);​ 
-  var len = recArray.length;​ +    var txt = app.CreateText(obj.name,​ 0.15); 
-  for (var i=0; i<​len;​i++){ +    newlay.AddChild(txt);​ 
-    var obj = recArray[i];​ +    txt = app.CreateText(obj.org,​ 0.15); 
-    layScroll.AddChild( recordLayout(obj) ); +    newlay.AddChild(txt);​ 
-  +    txt = app.CreateText(obj.reg,​ 0.15); 
-  scrollr.ScrollTo(0,​ layScroll.GetHeight());​ +    newlay.AddChild(txt);​ 
-}//​loadJSONfile+    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
  
 </​code>​ </​code>​
 +===== 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.1413668984.txt.gz · Last modified: 2014/10/18 21:49 by stevegarman