simplified progress reporting

This commit is contained in:
Javier Abadia 2014-01-13 13:56:00 +01:00
parent 4dae3ebd61
commit ddbf16194b
2 changed files with 116 additions and 108 deletions

View File

@ -88,13 +88,6 @@ define([
return tileid;
};
layer.estimateTileSize = function()
{
var tileInfo = this.tileInfo;
return 14000; // TODO - come up with a more precise estimation method
};
layer.getLevelEstimation = function(extent, level)
{
var tilingScheme = new TilingScheme(this,geometry);
@ -110,7 +103,7 @@ define([
return levelEstimation;
};
layer.prepareForOffline = function(minLevel, maxLevel, extent, reportProgress, finishedDownloading)
layer.prepareForOffline = function(minLevel, maxLevel, extent, reportProgress)
{
/* create list of tiles to store */
var tilingScheme = new TilingScheme(this,geometry);
@ -133,32 +126,9 @@ define([
}
/* launch tile download */
this.downloadTile(0, cells, reportProgress, finishedDownloading);
this.doNextTile(0, cells, reportProgress);
};
layer.downloadTile = function(i,cells, reportProgress, finishedDownloading)
{
var cell = cells[i];
var cancelRequested = reportProgress({countNow:i, countMax:cells.length});
this.storeTile(cell.level,cell.row,cell.col, function(success, error)
{
if(!success)
{
console.log("error storing tile", cell, error);
reportProgress({countNow:i, countMax:cells.length, error: { cell:cell, msg:error}});
}
if( cancelRequested )
finishedDownloading(true);
else if( i==cells.length-1 )
finishedDownloading(false);
else
this.downloadTile(i+1, cells, reportProgress, finishedDownloading);
}.bind(this))
}
layer.goOffline = function()
{
this.offline.online = false;
@ -170,6 +140,74 @@ define([
this.refresh();
};
layer.deleteAllTiles = function(callback)
{
var store = this.offline.store;
store.deleteAll(callback);
}
layer.getOfflineUsage = function(callback)
{
var store = this.offline.store;
store.size(callback);
};
layer.getTilePolygons = function(callback)
{
var store = this.offline.store;
var tilingScheme = new TilingScheme(this,geometry);
store.getAllTiles(function(url,img,err)
{
if(url)
{
var components = url.split("/");
var level = parseInt(components[ components.length - 3]);
var col = parseInt(components[ components.length - 2]);
var row = parseInt(components[ components.length - 1]);
var cellId = [row,col];
var polygon = tilingScheme.getCellPolygonFromCellId(cellId, level);
//if( level == 15)
callback(polygon);
}
else
{
callback(null,err);
}
});
}
/* internal methods */
layer.estimateTileSize = function()
{
var tileInfo = this.tileInfo;
return 14000; // TODO - come up with a more precise estimation method
};
layer.doNextTile = function(i,cells, reportProgress)
{
var cell = cells[i];
var error;
this.storeTile(cell.level,cell.row,cell.col, function(success, error)
{
if(!success)
{
console.log("error storing tile", cell, error);
error = { cell:cell, msg:error};
}
var cancelRequested = reportProgress({countNow:i, countMax:cells.length, error: error, finishedDownloading:false});
if( cancelRequested || i==cells.length-1 )
reportProgress({ finishedDownloading: true, cancelRequested: cancelRequested})
else
this.doNextTile(i+1, cells, reportProgress);
}.bind(this))
}
layer.storeTile = function(level,row,col,callback)
{
var store = this.offline.store;
@ -208,42 +246,6 @@ define([
}
req.send(null);
};
layer.deleteAllTiles = function(callback)
{
var store = this.offline.store;
store.deleteAll(callback);
}
layer.getOfflineUsage = function(callback)
{
var store = this.offline.store;
store.size(callback);
};
layer.getTilePolygons = function(callback)
{
var store = this.offline.store;
var tilingScheme = new TilingScheme(this,geometry);
store.getAllTiles(function(url,img,err)
{
if(url)
{
var components = url.split("/");
var level = parseInt(components[ components.length - 3]);
var col = parseInt(components[ components.length - 2]);
var row = parseInt(components[ components.length - 1]);
var cellId = [row,col];
var polygon = tilingScheme.getCellPolygonFromCellId(cellId, level);
//if( level == 15)
callback(polygon);
}
else
{
callback(null,err);
}
});
}
}
}
});

View File

@ -260,7 +260,7 @@ require(["esri/map",
/* launch offline preparation process */
var minLevel = parseInt(dojo.byId('minLevel').value);
var maxLevel = parseInt(dojo.byId('maxLevel').value);
basemapLayer.prepareForOffline(minLevel, maxLevel, map.extent, reportProgress, finishedDownloading);
basemapLayer.prepareForOffline(minLevel, maxLevel, map.extent, reportProgress);
}
function cancel()
@ -270,50 +270,56 @@ require(["esri/map",
function reportProgress(progress)
{
var pbar = query('#download-progress [role=progressbar]')[0];
var percent = progress.countMax? (progress.countNow / progress.countMax * 100) : 0;
pbar.style.width = percent+"%";
if( progress.error )
if( progress.finishedDownloading )
{
query('#download-progress [role=progressbar]')
.removeClass('progress-bar-success')
.addClass('progress-bar-warning');
if( progress.cancelRequested )
showAlert('alert-warning', 'Cancelled');
else if (errorList.length == 0)
showAlert('alert-success', 'All tiles downloaded and stored');
else
showAlert('alert-warning', "Finished downloading tiles, " + errorList.length + " tiles couldn't be downloaded");
errorList.push(progress.error.msg);
showAlert('alert-warning', progress.error.msg);
setTimeout(function()
{
esri.show(dojo.byId('ready-to-download-ui'));
esri.hide(dojo.byId('downloading-ui'));
updateOfflineUsage();
showStoredTiles(showTiles);
}, 1000);
}
if( progress.countNow > 5 )
{
var currentTime = new Date();
var elapsedTime = currentTime - startTime;
var remainingTime = (elapsedTime / progress.countNow) * (progress.countMax - progress.countNow);
var sec = 1 + Math.floor(remainingTime / 1000);
var min = Math.floor(sec / 60);
sec -= (min * 60);
dojo.byId('remaining-time').innerHTML = ((min<10)? "0" + min : min) + ":" + ((sec<10)? "0" + sec : sec);
}
return cancelRequested;
}
function finishedDownloading(cancelled)
{
if( cancelled )
showAlert('alert-warning', 'Cancelled');
else if (errorList.length == 0)
showAlert('alert-success', 'All tiles downloaded and stored');
else
showAlert('alert-warning', "Finished downloading tiles, " + errorList.length + " tiles couldn't be downloaded");
{
// progress bar
var pbar = query('#download-progress [role=progressbar]')[0];
var percent = progress.countMax? (progress.countNow / progress.countMax * 100) : 0;
pbar.style.width = percent+"%";
setTimeout(function()
{
esri.show(dojo.byId('ready-to-download-ui'));
esri.hide(dojo.byId('downloading-ui'));
updateOfflineUsage();
showStoredTiles(showTiles);
}, 1000);
// any errors?
if( progress.error )
{
query('#download-progress [role=progressbar]')
.removeClass('progress-bar-success')
.addClass('progress-bar-warning');
errorList.push(progress.error.msg);
showAlert('alert-warning', progress.error.msg);
}
// remaining time
if( progress.countNow > 5 )
{
var currentTime = new Date();
var elapsedTime = currentTime - startTime;
var remainingTime = (elapsedTime / progress.countNow) * (progress.countMax - progress.countNow);
var sec = 1 + Math.floor(remainingTime / 1000);
var min = Math.floor(sec / 60);
sec -= (min * 60);
dojo.byId('remaining-time').innerHTML = ((min<10)? "0" + min : min) + ":" + ((sec<10)? "0" + sec : sec);
}
return cancelRequested;
}
}
function toggleShowStoredTiles()