Friday, 25 May 2012

Be motivated to live life

Problem:

I want to be motivated to do things.

Solution:

Just remember, everyone has a 100% chance of dying, so you may as well live while you are alive.

For those who say that they do not have enough time, time is the only thing we are all given the same amount of, (when compared to other things such as opportunity or resources).

For those who say that they have never done something before, everyone who has ever lived did everything they're good at for the very first time at one point in their life. Even the world's best. We were all born knowing nothing.

So with this in mind, just ask yourself, "when can I get started", and always keep moving forward.

Dedication:

I wanted to dedicate this post to a friend of mine who passed away in a plane crash a few months ago near Mt. Everest. I just came back from his memorial unveiling earlier today. Although young, this friend experienced, and accomplished a lot in a very short amount of time, and made many friends along the way. I wish everyone who reads this post the very best in your future :)

Saturday, 19 May 2012

JQuery animation: execute callback only once when all completed

Problem:

When using the animate function after selecting more than one item, my callback function is executed as many times as there are items. How do I get the callback function to execute only once when the entire animation is done?

Problem Example:

If, for example, you have five items with the class 'do-something', and you try the following code:

var ctr = 0;
var selected = $(".do-something");

var mycallback = function(){
  ctr++;
  alert(ctr);
}

selected.animate({
  // ... your animation stuff ...
}, 200, mycallback);

Rather than the hoped for single alert with the number 1, you'll receive five alerts with the numbers 1 through 5.

Solution:

As of JQuery version 1.6 you can use the promise() and done() functions to fire off your callback once after all the animations are completed.

For example, again if you have five items with the class 'do-something' the following code should now produce a single alert with the number 1:

var ctr = 0;
var selected = $(".do-something");

var mycallback = function(){
  ctr++;
  alert(ctr);
}

selected.animate({
  // ... your animation stuff ...
}, 200);

selected.promise().done(mycallback);

Other notes:

This was tested to work using JQuery 1.7.2 and Firefox 12.0.

References:

Sunday, 13 May 2012

Copy files in order using Linux or OS X

Problem:

How do I copy files in sorted order using the Linux or OS X Terminal?

Solution:

In the terminal, you can use a combination of the "find", "sort", "xargs", and "cp" commands to copy files in sorted order.

For instance, first switch to the directory that has the files you would like to copy:

cd /path/to/media/directory

Then copy the files in sorted order:

find . -print0 | sort -z | xargs -0 cp --parents
  --target-directory=/path/to/destination -v

Note that the above command is a single line, (but it appears split into two in order to fit into this page.)

In the cp command, the -v flag will help you see if files are being copied in the desired order. This can be omitted if you do not wish to monitor the files being copied. To customize the order of your sort, simply customize the flags used with the 'sort' command (see the manual pages for 'sort' using the command 'man sort', if you're unsure of how to do this).

Why copying files in sorted order can be useful:

Some MP3 players, digital media players, and other devices play back media in the order that files were copied to it. With some file managers, files can be copied out of order, in reverse-sorted order, or in other ways that copy files out of the order that is desired (such as copying multiple files in parallel). There are also other situations where copying files in a particular sorted order would be useful. For instance, copying files in some required order to a custom-built robot's flash memory, etc.

Other thoughts:

Although the command 'cp -R' can also recursively copy things in order, the method mentioned above in this post should give a bit more flexibility customizing the sorted order of the copied files.

Friday, 11 May 2012

Fullscreen PhoneGap app with XCode

Problem:

How can I get rid of the status bar on top of my PhoneGap application on my iOS device?

Solution:

There are several ways to do this, and although I won't go into all of the ones I found, one of the easiest ways to do this is to just edit your project's Info.plist file. This is generally named in the form of "YourAppName-Info.plist".

In your plist file, secondary-click and add a row. Name this row UIStatusBarHidden, and change its type to Boolean. Note that if a drop-down menu appears and gives you the option to select "Status bar is initially hidden", pick that. In both cases, set the value to YES.

The next screenshot is an example where the choice is given to initially hide the status bar:

The next time you run your PhoneGap application, you'll now notice that it is fullscreen. This will continue to be the case until your application code does anything to change this, (for instance, if you try to send the user to a different page).


Other thoughts:

This was tested with PhoneGap version 1.5.0 (codename Cordova) on iOS 5.1, using XCode 4.3.2. Your mileage might vary with different versions. Also note that these instructions can be used to initially hide the status bar in any iOS application, not necessarily just PhoneGap applications.

Wednesday, 9 May 2012

String character whitelist

Problem:

How do I determine if a string contains only allowed characters?

Solution (code snippet):

The following PHP example code will check if a string contains only characters in a white-list. It should be pretty easy to convert this to other languages.

function containsOnly($string, $whiteliststring){
    $char_arr = str_split($string);
    $whitelist_arr = str_split($whiteliststring);
    
    foreach($char_arr as $char){
        if(in_array($char, $whitelist_arr)==false) return false;
    }
    return true;
}

Example usage of this would be, for instance, checking for numbers only:

$numbers = "0123456789";
$not_only_numbers = "abcd123";
$only_numbers = "123";

echo containsOnly($not_only_numbers, $numbers)?"yes":"no"; // no
echo containsOnly($only_numbers, $numbers)?"yes":"no";     // yes

Code explained:

The containsOnly function uses str_split to convert strings into arrays containing their characters. The loop checks to see if any of the test string characters is absent from the white-list.

This code can generally be sped up by using an implementation of a set rather than arrays.

Tuesday, 8 May 2012

Clearing an array in Javascript

There are a few ways to clear an array in Javascript. Two particularly useful ways are as follows:
  1. Assigning a new array:
    myArray = [];
  2. Refactoring the current array:
    myArray.length = 0;

The first method will assign a new array to the myArray variable. If there are any other pointers to the previous array, these will still point to the old array.

The second method changes the array itself. If there are any other pointers to the previous array, they will all be updated to the cleared array.