Tuesday 5 August 2014

File explorer v002

I did a bit more work on the file explorer.
You might think I made it worse.

The new layout makes it easier to tell which files are folders.

The new spk is at http://hudl.sgarman.net/public/spk/SJGexplorer_002.spk

The code looks like this:

//Simple file explorer by Steve Garman

var basePath = "/mnt"

//Called when application is started.
function OnStart()
{
    //Make sure our path is valid
    if( ! app.FolderExists( basePath ) ) basePath = "/sdcard"
    //Probably works best in portrait
    //but don't force the issue
    if( app.GetOrientation() == "Landscape" ){
      //var goPortrait = app.CreateYesNoDialog( "Change to portrait?" );
      //goPortrait.SetOnTouch( goPortrait_OnTouch )
      if (confirm("Set to Portrait") == true) {
          app.SetOrientation("Portrait");
      }
    }
    //Create a layout with objects vertically centered.
    lay = app.CreateLayout( "linear", "Vertical,FillXY" );    

    //Create a text label and add it to layout.
    currPath = app.CreateText( basePath );
    lay.AddChild( currPath );
    //create a list view for folders (directories)
    folds = app.CreateList( "abc,zxc,qwe,sdf,ghj,nbv,zzz", 1.0, 0.4 );
    folds.SetBackGradient( "#222222", "#444444", "#888888" );
    folds.SetOnTouch( foldsTouch);
    lay.AddChild( folds );

    //create detail list view for files
    detail = app.CreateList( "", 1.0, 0.6 );
    detail.SetOnTouch( detailTouch);
    populate_detail();
    lay.AddChild( detail );

    //Add layout to app.    
    app.AddLayout( lay );

    //make back key available for use
    app.EnableBackKey( false );

}

function populate_detail(){
  //called whenever current path changed
  filelist = app.ListFolder( currPath.GetText() ).split(",") ;
  filelist.sort();
  //my array handling is a mess but I cant get filelist.Unshift to work
  filelist.reverse();
  var ths = filelist.pop();
  var pth;
  var dirs = [];
  var fils = [];
  while( undefined != ths ){
    pth = currPath.GetText() + "/" + ths;
    if( testFolder( pth ) ) dirs.push(ths);
    else fils.push( ths );
    ths = filelist.pop();
  }
  folds.SetList(dirs.join(","),",");
  detail.SetList(fils.join(","),",");
}

function detailTouch(name){
  //user has touched a name in detail list
  var fullPath = currPath.GetText() + "/" + name
  if( app.FileExists(fullPath) ){
    deal_with_file( fullPath ); 
  }
}

function foldsTouch(name){
  //user has touched a name in folders list
  var fullPath = currPath.GetText() + "/" + name
  if( app.FolderExists(fullPath) ){
    //show it
    currPath.SetText( fullPath );
    populate_detail();
  }
}
function testFolder( fullPath ){
  //no proper test for folders
  //in this app, it's safe to treat an empty folder like a file
  //if we need to create folders later, 
  //this will need fixing
  var tst = app.ListFolder( fullPath );
  if( tst.length > 0 ) return true;
  else return false;
};

function deal_with_file( fullpath ){
  //still very basic
  if( fullpath.indexOf(".js" ) >= 0 ||
      fullpath.indexOf(".txt" ) >= 0 )
      app.OpenFile( fullpath,"text/plain", "Choose Editor" );
   else app.ShowPopup("No action defined for " + fullpath);
}

function OnBack(){
  //user pressed back key
  var tmp = currPath.GetText();
  if( tmp == basePath ) app.Exit();
  else {
    //not yet back at beginning
    //move up one level
    var tst = currPath.GetText().split("/");
    tmp = tst.pop();
    currPath.SetText( tst.join("/") );
    populate_detail();
  }
}

function goPortrait_OnTouch( result ){
  if( result=="Yes" ) app.SetOrientation( "Portrait" );
}


No comments:

Post a Comment