Wednesday, June 19, 2013

Showing a Loading Spinner for long-running form submits in ASP.net

If a form submit takes long time due to any underground operations such as executing heavy database queries, XML parsing, etc , you may need to show the user that some processing is going on. Studies shows that average maximum waiting time of an website user is 10 seconds before he will dispose the page or try to do something. Well we don't need that.  At old days we used to show a hour glass animation to do this, but nowadays spinner is quite popular.

Simplest way to do this is, creating a div and put a <img> control inside of the div and set its source to a gif image of a spinner. It works out in most of the browsers but unfortunately not in IE 9 or in any older IE versions. Then I found out about spin.js which works in all major browsers, even in IE6. So instead of placing a gif in the div, I targeted that div for the spinner.


Reference to spin.js libaray -

<script type="text/javascript" src="@Url.Content("~/Content/spin.min.js")"></script>

HTML for loading div -

    <div id="processing">
        <div id="content">
            <p id="loadingspinner">
                Creating SPs...
            </p>
        </div>
    </div>

CSS styling -

#processing
{
    display: none;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(255,255,255,0.6);
    z-index: 1000;
}

#content
{
    display: table;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}

#loadingspinner
{
    display: table-cell;
    vertical-align: middle;
    width: 100%;
    text-align: center;
    font-size: larger;
    padding-top: 75px;
}

Script to show div when proceed button clicked -

 $(function () {
        $("#proceedButton").click(function () {
            $("#processing").fadeIn();
        });
    });

Script to display the spinner to indicate loading -

    $(function () {
        $("#proceedButton").click(function () {
            $("#processing").fadeIn();
            var opts = {
                lines: 12, // The number of lines to draw
                length: 7, // The length of each line
                width: 4, // The line thickness
                radius: 10, // The radius of the inner circle
                color: '#000', // #rgb or #rrggbb
                speed: 1, // Rounds per second
                trail: 60, // Afterglow percentage
                shadow: false, // Whether to render a shadow
                hwaccel: false // Whether to use hardware acceleration
            };
            var target = document.getElementById('processing');
            var spinner = new Spinner(opts).spin(target);
        });
    });

That's it. Works like magic.. ;)

But if you wan't to show the spinner when you are doing some work through a ajax postback, found a nice little article to achieve that too.

Monday, June 17, 2013

Solving "The Path already exists!" error when creating a new project in PhoneGap 2.8.1

After you installed Android SDK  and PhoneGap Cordova and  setup your environment variables -JAVA_HOME, ANT_HOME, and PATH variables, you will go for setting up a new project. In the guid it says, 
  • In a terminal window, navigate to the bin directory within the android subfolder of the Cordova distribution.
  • Type in ./create <project_folder_path> <package_name> <project_name> then press "Enter"
        where, 
        <project_folder_path> is the path to your new Cordova Android project
        <package_name> is the package name, e.g com.YourCompany.AppName
       <project_name> is the project name, e.g. YourApp (not contain spaces and dashes)

Therefore I typed in console,

create D:\ECLIPSE\eclipse-jee-helios-SR2-win32-x86_64\eclipse\workplace\phonegap\exampleApp com.geveo.android.example exampleApp 

It gives the error,
-Project path already exists!

By some testings I found out the issue and then solution was obvious. The problem is 'create' command create the destination folder you specified in the project_folder_path. In my case 'exampleApp' folder which I created just before executing the 'create' command. That's why it says project path is exists. Therefore do not create the destination folder, create command will do that for you. 

After deleting exampleApp folder I executed the create command again.

C:\Users\Asanka\Desktop\phonegap-2.8.1\phonegap-2.8.1\lib\android\bin>create D:\ECLIPSE\eclipse-jee-helios-SR2-win32 x86_64\eclipse\workplace\phonegap\exampleApp com.geveo.android.example exampleApp
Creating new android project...
Copying template files...
Copying js, jar & config.xml files...
Copying cordova command tools...
Updating AndroidManifest.xml and Main Activity...

C:\Users\Asanka\Desktop\phonegap-2.8.1\phonegap-2.8.1\lib\android\bin>



Problem solved... :)