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

Both sides previous revision Previous revision
Next revision
Previous revision
sample_code:visitor_log [2014/10/19 10:50]
stevegarman Set to Landscape
sample_code:visitor_log [2014/12/06 21:05] (current)
stevegarman change curly brace style
Line 7: Line 7:
 //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() 
-  if( ! app.IsTablet()) app.SetOrientation("​Landscape"​);​ +  ​
-  //file variables +    ​if (!app.IsTablet()) app.SetOrientation("​Landscape"​);​ 
-  var d = new Date(); +    //file variables 
-  jsonPath = fileFolder + "​vistorBook"​ + +    var d = new Date(); 
-     ​d.toISOString().slice(0,​10).replace(/​-/​g,"​_"​) +    jsonPath = fileFolder + "​vistorBook"​ + 
-     + "​.json";​ +      d.toISOString().slice(0,​ 10).replace(/​-/​g,​ "​_"​) + "​.json";​ 
-  app.MakeFolder(fileFolder) +    app.MakeFolder(fileFolder) 
-  //Create main layout. +      //Create main layout. 
-  var lay = app.CreateLayout( "​linear",​ "​Left"​ ); +    var lay = app.CreateLayout("​linear",​ "​Left"​);​ 
-  //Create a scroller and add it to main layout +    //Create a scroller and add it to main layout 
-  scrollr = app.CreateScroller( 1.0, 0.8 );  +    scrollr = app.CreateScroller(1.0,​ 0.8); 
-  lay.AddChild( scrollr );   +    lay.AddChild(scrollr);​ 
-  //Create a layout inside scroller.  +    //Create a layout inside scroller.  
-  layScroll = app.CreateLayout( "​Linear",​ "​Left"​);​ +    layScroll = app.CreateLayout("​Linear",​ "​Left"​);​ 
-  scrollr.AddChild( layScroll ​); +    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>​
Line 155: Line 164:
  
 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. 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.1413715801.txt.gz · Last modified: 2014/10/19 10:50 by stevegarman