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
sample_code:visitor_log [2014/11/11 21:56]
stevegarman [Notes] larger databàses
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 +    //Create a sign-in layout 
-  var layNew = app.CreateLayout( "​linear",​ "​Horizontal,​Left"​);​ +    var layNew = app.CreateLayout("​linear",​ "​Horizontal,​Left"​);​ 
-  edtName = app.CreateTextEdit("",​ .16); +    edtName = app.CreateTextEdit("",​ .16); 
-  edtName.SetHint("​Name"​);​ +    edtName.SetHint("​Name"​);​ 
-  layNew.AddChild(edtName);​ +    layNew.AddChild(edtName);​ 
-  edtOrg = app.CreateTextEdit("",​ .16); +    edtOrg = app.CreateTextEdit("",​ .16); 
-  edtOrg.SetHint("​Organization"​);​ +    edtOrg.SetHint("​Organization"​);​ 
-  layNew.AddChild(edtOrg);​ +    layNew.AddChild(edtOrg);​ 
-  edtVehicle = app.CreateTextEdit("",​ .16); +    edtVehicle = app.CreateTextEdit("",​ .16); 
-  edtVehicle.SetHint("​Vehicle reg."​);​ +    edtVehicle.SetHint("​Vehicle reg."​);​ 
-  layNew.AddChild(edtVehicle);​ +    layNew.AddChild(edtVehicle);​ 
-  edtReason = app.CreateTextEdit("",​ .16); +    edtReason = app.CreateTextEdit("",​ .16); 
-  edtReason.SetHint("​Reason for visit"​);​ +    edtReason.SetHint("​Reason for visit"​);​ 
-  layNew.AddChild(edtReason);​ +    layNew.AddChild(edtReason);​ 
-  var btnCreate = app.CreateButton("​Ok"​);​ +    var btnCreate = app.CreateButton("​Ok"​);​ 
-  btnCreate.SetOnTouch(btnCreate_OnClick);​ +    btnCreate.SetOnTouch(btnCreate_OnClick);​ 
-  layNew.AddChild(btnCreate);​ +    layNew.AddChild(btnCreate);​ 
-   + 
-  //Add sign-in layout to main layout +    //Add sign-in layout to main layout 
-  lay.AddChild(layNew);​+    lay.AddChild(layNew);​
  
-  ​//Add main layout to app.     +    ​//Add main layout to app.     
-  app.AddLayout( lay ); +    app.AddLayout(lay);​ 
-  //check if today'​s visitor book already active. +    //check if today'​s visitor book already active. 
-  if(app.FileExists(jsonPath)){ +    if (app.FileExists(jsonPath)) 
-    loadJSONfile();​ +    ​
-  +      ​loadJSONfile();​ 
-}//OnStart+    
 +  } //OnStart
  
-function btnCreate_OnClick( ){ +function btnCreate_OnClick() 
-  app.HideKeyboard();​ +  ​
-  var name = edtName.GetText();​ +    ​app.HideKeyboard();​ 
-  var org = edtOrg.GetText();​ +    var name = edtName.GetText();​ 
-  var reg = edtVehicle.GetText();​ +    var org = edtOrg.GetText();​ 
-  var reason = edtReason.GetText();​ +    var reg = edtVehicle.GetText();​ 
-  //check required fields +    var reason = edtReason.GetText();​ 
-  var errs = "";​ +    //check required fields 
-  if ( name == ""​ ) errs = "'​Name'​ "; +    var errs = "";​ 
-  if ( reason == ""​ ) errs = errs + "'​Reason for visit' "; +    if (name == ""​) errs = "'​Name'​ "; 
-  if ( errs != ""​){ +    if (reason == ""​) errs = errs + "'​Reason for visit' "; 
-    //bail out +    if (errs != ""​) 
-    app.Alert( errs + "​cannot be empty"​);​ +    ​
-    return; +      ​//bail out 
-  +      app.Alert(errs + "​cannot be empty"​);​ 
-  // clear sign-in form for next visitor +      return; 
-  edtName.SetText(""​);​ +    
-  edtOrg.SetText(""​);​ +    // clear sign-in form for next visitor 
-  edtVehicle.SetText(""​);​ +    edtName.SetText(""​);​ 
-  edtReason.SetText(""​);​ +    edtOrg.SetText(""​);​ 
-  // create a record +    edtVehicle.SetText(""​);​ 
-  var obj = {}; +    edtReason.SetText(""​);​ 
-  obj.name = name; +    // create a record 
-  obj.org = org; +    var obj = {}; 
-  obj.reg = reg; +    obj.name = name; 
-  obj.reason = reason; +    obj.org = org; 
-  var d = new Date(); +    obj.reg = reg; 
-  obj.timeIn = d.toTimeString().slice(0,​5);​ +    obj.reason = reason; 
-  obj.timeOut = "​Out";​ +    var d = new Date(); 
-  // add this record to the array +    obj.timeIn = d.toTimeString().slice(0,​ 5); 
-  recArray.push( obj ); +    obj.timeOut = "​Out";​ 
-  saveJSONfile();​ +    // add this record to the array 
-  // display record on scroller +    recArray.push(obj);​ 
-  layScroll.AddChild( recordLayout(obj) ); +    saveJSONfile();​ 
-  scrollr.ScrollTo(0,​ layScroll.GetHeight());​ +    // display record on scroller 
-}//​btnCreate_OnClick+    layScroll.AddChild(recordLayout(obj));​ 
 +    scrollr.ScrollTo(0,​ layScroll.GetHeight());​ 
 +  } //​btnCreate_OnClick
  
-function btnOut_OnClick( ){ +function btnOut_OnClick() 
-  //has this visitor already signed out? +  ​
-  if (this.GetText() != "​Out"​) return; +    ​//has this visitor already signed out? 
-  //not yet, so record time out +    if (this.GetText() != "​Out"​) return; 
-  var d = new Date(); +    //not yet, so record time out 
-  this.obj.timeOut = d.toTimeString().slice(0,​5);​ +    var d = new Date(); 
-  this.SetText(this.obj.timeOut);​ +    this.obj.timeOut = d.toTimeString().slice(0,​ 5); 
-  //update copy of recArray on file +    this.SetText(this.obj.timeOut);​ 
-  saveJSONfile();​ +    //update copy of recArray on file 
-}//​btnOut_OnClick+    saveJSONfile();​ 
 +  } //​btnOut_OnClick
  
-function recordLayout(obj){ +function recordLayout(obj) 
-  //create a new horizontal layout to display this record +  ​
-  var newlay = app.CreateLayout( "​Linear",​ "​Horizontal,​FillXY"​ ); +    ​//create a new horizontal layout to display this record 
-  var txt = app.CreateText(obj.name,​ 0.15); +    var newlay = app.CreateLayout("​Linear",​ "​Horizontal,​FillXY"​);​ 
-  newlay.AddChild(txt);​ +    var txt = app.CreateText(obj.name,​ 0.15); 
-  txt = app.CreateText(obj.org,​ 0.15); +    newlay.AddChild(txt);​ 
-  newlay.AddChild(txt);​ +    txt = app.CreateText(obj.org,​ 0.15); 
-  txt = app.CreateText(obj.reg,​ 0.15); +    newlay.AddChild(txt);​ 
-  newlay.AddChild(txt);​ +    txt = app.CreateText(obj.reg,​ 0.15); 
-  txt = app.CreateText(obj.reason,​ 0.15); +    newlay.AddChild(txt);​ 
-  newlay.AddChild(txt);​ +    txt = app.CreateText(obj.reason,​ 0.15); 
-  txt = app.CreateText(obj.timeIn,​ 0.15); +    newlay.AddChild(txt);​ 
-  newlay.AddChild(txt);​ +    txt = app.CreateText(obj.timeIn,​ 0.15); 
-  var btn = app.CreateButton(obj.timeOut,​ 0.15); +    newlay.AddChild(txt);​ 
-  btn.obj = obj; +    var btn = app.CreateButton(obj.timeOut,​ 0.15); 
-  btn.SetOnTouch(btnOut_OnClick);​ +    btn.obj = obj; 
-  newlay.AddChild(btn);​ +    btn.SetOnTouch(btnOut_OnClick);​ 
-  return newlay; +    newlay.AddChild(btn);​ 
-}//​recordLayout+    return newlay; 
 +  } //​recordLayout
  
-function saveJSONfile(){ +function saveJSONfile() 
-  //update copy of recArray on file +  ​
-  var s = JSON.stringify(recArray);​ +    ​//update copy of recArray on file 
-  app.WriteFile(jsonPath,​ s ) +    var s = JSON.stringify(recArray);​ 
-}//​saveJSONfile+    app.WriteFile(jsonPath,​ s) 
 +  } //​saveJSONfile
  
-function loadJSONfile(){ +function loadJSONfile() 
-  //read copy of recArray from file +  ​
-  var s = app.ReadFile(jsonPath) +    ​//read copy of recArray from file 
-  recArray = JSON.parse(s) +    var s = app.ReadFile(jsonPath) 
-  var len = recArray.length;​ +    recArray = JSON.parse(s) 
-  for (var i=0; i<​len;​i++){ +    var len = recArray.length;​ 
-    var obj = recArray[i];​ +    for (var i = 0; i < len; i++) 
-    layScroll.AddChild( recordLayout(obj) ); +    ​
-  +      ​var obj = recArray[i];​ 
-  scrollr.ScrollTo(0,​ layScroll.GetHeight());​ +      layScroll.AddChild(recordLayout(obj));​ 
-}//​loadJSONfile+    
 +    scrollr.ScrollTo(0,​ layScroll.GetHeight());​ 
 +  } //​loadJSONfile
  
 </​code>​ </​code>​
sample_code/visitor_log.txt · Last modified: 2014/12/06 21:05 by stevegarman