I have been tasked with creating what on paper seems a simple mobile application. Because of the portability, we have chosen the Intel XDK as our development tool utilising HTML5/JQuery.
Basically, the mobile app will have three options:
1. Download data - stock, operators, accounts etc.
2. Process transactions - record items allocated to the end user and by whom.
3. Upload transactions - pass transaction details back to Back Office
system.
I have made a start on the first of the options, Download data, where a number of text files need to be copied to the device, read in turn and imported into a database for use with the second option.
My code below is an attempt to show 3 buttons on the first 'page'. The app swaps to another page on pressing the download data button, which shows some blurb and Yes/No buttons. On pressing Yes, the app swaps to a further page where I would like to show the progress of each file being downloaded.
- <!DOCTYPE html>
- <html>
- <!--
- * Copyright © 2012-2015, Intel Corporation. All rights reserved.
- * Please see the included LICENSE.md file for license terms and conditions.
- -->
- <head>
- <title>Gift of Kit</title>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8">
-
- <!-- This is a blank template, include any desired javascript frameworks, libraries, CSS here and start developing your app -->
-
- <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=no">
-
- <style>
- @-ms-viewport { width: 100vw ; min-zoom: 100% ; zoom: 100% ; } @viewport { width: 100vw ; min-zoom: 100%; zoom: 100% ; }
- @-ms-viewport { user-zoom: fixed ; min-zoom: 100% ; } @viewport { user-zoom: fixed ; min-zoom: 100% ; }
- </style>
-
- <!-- Recommended location for your JavaScript libraries -->
- <script src="cordova.js" id="xdkJScordova_"></script>
- <link rel="stylesheet" href="themes/giftofkit.css" />
- <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
- <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
- <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
-
- <script src="js/main.js"></script>
- <script>
- $(document).ready(function() {
- var bodyHeight = $("body").height();
- var vwptHeight = $(window).height();
- if (vwptHeight > bodyHeight) {
- $("div.pgFooter").css("position","absolute").css("bottom",0);
- }
- });
-
- </script>
-
- <script id="xdkJSonDeviceReady_">
- function onDeviceReady() { // ignored by "Standard HTML5 web app"
- if( navigator.splashscreen && navigator.splashscreen.hide ) { // Cordova hide splashscreen detected, use it
- navigator.splashscreen.hide() ;
- }
- }
- document.addEventListener("deviceready", onDeviceReady, false) ; // ignored by "Standard HTML5 web app"
- </script>
- <link rel="stylesheet" href="css/main.css" type="text/css">
-
- </head>
- <body>
- <div data-role="page" id="pgMenu" class="pgMenu">
- <div data-role="header" id="pgMenuHeader" class="pgHeader">
- <b>Gift of Kit</b>
- </div>
-
- <div data-role="main" class="pgContent">
- <div data-role="controlgroup" data-type="vertical">
- <a href="#pgDownload" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Download Data</a>
- <a href="#pgProcess" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Process Transactions</a>
- <a href="#pgUpload" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Upload Transactions</a>
- </div>
- </div>
-
- <div data-role="footer" id="pgMenuFooter" class="pgFooter">
- <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">© Medoc Computers</b>
- </div>
- </div>
- <div data-role="page" id="pgDownload">
- <div data-role="header" id="pgDownloadHeader" class="pgHeader">
- <b>Gift of Kit</b>
- </div>
-
- <div data-role="main" class="pgContent">
- <p>You are about to download multiple files to this device and refresh the database.</p>
- <p>Please confirm you wish to continue.</p>
- <div data-role="controlgroup" data-type="horizontal">
- <a href="#pgImport" class="ui-btn ui-corner-all ui-shadow" data-transition="flip" id="btnYes">Yes</a>
- <a href="#" class="ui-btn ui-corner-all ui-shadow" data-rel="back">No</a>
- </div>
- </div>
-
- <div data-role="footer" id="pgDownloadFooter" class="pgFooter">
- <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">© Medoc Computers</b>
- </div>
- </div>
- <div data-role="page" id="pgImport">
- <div data-role="header" id="pgImportHeader" class="pgHeader">
- <b>Gift of Kit</b>
- </div>
-
- <div data-role="main" class="pgContent">
- <progress value="0" min="0" max="100" id="progress"></progress>
- <div id="size"></div>
- <div id="downloaded"></div>
-
- <a href="#pgMenu" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Go Back</a>
- </div>
-
- <div data-role="footer" id="pgImportFooter" class="pgFooter">
- <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">© Medoc Computers</b>
- </div>
- </div>
- <div data-role="page" id="pgProcess">
- <div data-role="header" id="pgProcessHeader" class="pgHeader">
- <b>Gift of Kit</b>
- </div>
-
- <div data-role="main" class="pgContent">
- <a href="#" class="ui-btn ui-corner-all ui-shadow" data-rel="back">Go Back</a>
- </div>
-
- <div data-role="footer" id="pgProcessFooter" class="pgFooter">
- <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">© Medoc Computers</b>
- </div>
- </div>
- <div data-role="page" id="pgUpload">
- <div data-role="header" id="pgUploadHeader" class="pgHeader">
- <b>Gift of Kit</b>
- </div>
-
- <div data-role="main" class="pgContent">
- <p id="temp"></p>
- <a href="#" class="ui-btn ui-corner-all ui-shadow" data-rel="back">Go Back</a>
- </div>
-
- <div data-role="footer" id="pgUploadFooter" class="pgFooter">
- <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">© Medoc Computers</b>
- </div>
- </div>
- <script>
- $( "#pgImport" ).focus(function( event ) {
- var file1State = downloadFile("S\zcaug13");
- var file2State = downloadFile("S\safc_2feb");
- var file3State = downloadFile("S\sales_query.sql");
-
- if ( file1State == 0 || file2State == 0 || file3State == 0 ) {
- alert("Downloading Cancelled");
- } else if ( file1State == 1 || file2State == 1 || file3State == 1 ) {
- alert("Downloading Failed");
- } else {
- alert("Downloading Successful");
- }
-
- });
-
- function downloadFile(fn) {
- var fileName = fn;
- var xhr = new XMLHttpRequest();
- var stat;
-
- xhr.addEventListener("progress", updateProgress, false);
- xhr.addEventListener("load", transferComplete, false);
- xhr.addEventListener("error", transferFailed, false);
- xhr.addEventListener("abort", transferCanceled, false);
-
- xhr.open("GET", fileName);
-
- function updateProgress(oEvent)
- {
- if (oEvent.lengthComputable)
- {
- var percentComplete = (oEvent.loaded / oEvent.total) * 100;
- document.getElementById("progress").setAttribute("value", percentComplete);//just a random value
- document.getElementById("size").innerHTML = oEvent.total;
- document.getElementById("downloaded").innerHTML = oEvent.loaded;
- }
- else
- {
- document.getElementById("progress").setAttribute("value", 30);//just a random value
- document.getElementById("size").innerHTML = "unknown";
- document.getElementById("downloaded").innerHTML = "unknown";
- }
- }
-
- function transferComplete(oEvent)
- {
- return 2;
- }
-
- function transferFailed(oEvent)
- {
- return 1;
- }
-
- function transferCanceled(oEvent)
- {
- return 0;
- }
-
- xhr.send();
- }
- </script>
- </body>
- </html>
However my knowledge of HTML5/JQuery is not good and I have already hit my first hurdle.
The file download is happening before the progress page is displayed.
I have a couple of questions:
1. How do I make the file download happen after the progress page as been displayed?
2. How do I make it show progress for each file in turn?
Any help greatly appreciated.