Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

BB10: Date Picker class

[ Edited ]

In case someone needs one:

package controls
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import qnx.fuse.ui.picker.Picker;
	import qnx.ui.data.DataProvider;
	
	public class DatePicker extends Picker
	{
		
		////////////////////////////////////////////////////////////////////////////////////
		public function DatePicker( years_back : int = 20 )
		{
			super();
			
			// year col
			var yrar    : Array = [];
			var moar    : Array = [ { data : 1, label : 'Jan' },
					{ data : 2, label : 'Feb' },
					{ data : 3, label : 'Mar' },
					{ data : 4, label : 'Apr' },
					{ data : 5, label : 'May' },
					{ data : 6, label : 'Jun' },
					{ data : 7, label : 'Jul' },
					{ data : 8, label : 'Aug' },
					{ data : 9, label : 'Sep' },
					{ data : 10, label : 'Oct' },
					{ data : 11, label : 'Nov' },
					{ data : 12, label : 'Dec' } ];
			var datear  : Array = [];
			var today   : Date = new Date();
			var yr      : int;
			for( yr = today.fullYear - years_back; yr <= today.fullYear + 1; yr++ )
			{
				yrar.push( { data : yr, label : yr } );
			}
			
			var day : int;
			for( day=1; day <= 31; day++ )
			{
				datear.push( { data : day, label : day } );
			}
			
			this.dataProvider = new DataProvider( [ new DataProvider( yrar ), new DataProvider( moar ), new DataProvider( datear ) ] );
			
			this.valueFunction = DateDisplay;
			
			this.addEventListener(FocusEvent.FOCUS_IN, FocusIn );
			this.addEventListener(Event.SELECT, ItemSelected );
		}
		
		/////////////////////////////////////////////////////////////////////////
		private function DateDisplay( items : Array ) : String
		{
			return  items[1]['label'] + ' ' + items[2]['label'] + ', ' + items[0]['label'];
		}
		
		////////////////////////////////////////////////////////////////////////
		public function setDateIndex( year : int, month : int, date : int ) : void
		{
			var yrdata :DataProvider = this.getItemAt( 0 ) as DataProvider;
			var modata :DataProvider = this.getItemAt( 1 ) as DataProvider;
			var dtdata :DataProvider = this.getItemAt( 2 ) as DataProvider;
			var item   : Object;
			var yridx  : int = 0;
			var moidx  : int = 0;
			var dtidx  : int = 0;
			var count  : int;
			
			count = 0;
			for each( item in yrdata.data )
			{
				if( item.data == year )
				{
					yridx = count;
					break;
				}
				count++;
			}
			
			count = 0;
			for each( item in modata.data )
			{
				if( item.data == month )
				{
					moidx = count;
					break;
				}
				count++;
			}
			
			count = 0;
			for each( item in dtdata.data )
			{
				if( item.data == date )
				{
					dtidx = count;
					break;
				}
				count++;
			}
			
			this.selectedIndices = [ yridx, moidx, dtidx ];
		}
		
		////////////////////////////////////////////////////////////////////////
		public function setDate( value : Date ) : void
		{
			this.setDateIndex( value.fullYear, value.month+1, value.date );
		}
		
		////////////////////////////////////////////////////////////////////////////
		private function FocusIn( event : Event ) : void
		{
			this.parent.setChildIndex( this, this.parent.numChildren - 1 );
		}
		
		////////////////////////////////////////////////////////////////////////////
		public final function get year() : int
		{
			var ar : Array = this.selectedItems;
			return ar[0].data;
		}
		
		////////////////////////////////////////////////////////////////////////////
		public final function get month() : int
		{
			var ar : Array = this.selectedItems;
			return ar[1].data;
		}
		
		////////////////////////////////////////////////////////////////////////////
		public final function get date() : int
		{
			var ar : Array = this.selectedItems;
			return ar[2].data;
		}
		
		////////////////////////////////////////////////////////////////////////////
		private function ItemSelected( event : Event ) : void
		{	
			var ar : Array = this.selectedItems;
			var yr : int = ar[0].data;
			var mo : int = ar[1].data;
			var dt : int = ar[2].data;
			
			//trace( 'item selected ' + yr + ' ' + mo + ' ' + dt );
			
			var date :Date = new Date( yr, mo-1, 1 );
			// advance 1 month
			if( date.month < 11 )
				date.month++;
			else
				date.month = 0;
			
			// back a day
			date.time = date.time - (1000*60*60*24 );
			
			// date will be the number of days in that month
			//trace( 'max ' + date.date );
			
			var d        : int;
			var maxd     : int = date.date;
			var newdates : Array = [];
			for( d=1; d <= maxd; d++ )
			{
				newdates.push( { data : d, label : d } );
			}
			this.replaceItemAt( new DataProvider( newdates ), 2 );

			if( dt > maxd )dt = maxd;
			
			this.setDateIndex( yr, mo, dt );
		}
		
		//////////////////////////////////////////////////////////////////////////
		public final function get bottom() : int
		{
			return this.y + this.height;
		}
	}
}

 

Developer
Posts: 133
Registered: ‎12-02-2010
My Device: Z10 & Dev Alpha C
My Carrier: T

Re: BB10: Date Picker class

Thank you!!! Saved me lots of time!! Buying Magellan Compass as a small thanks!

---------------------------------------------------------------------
Developer of Stocks for Blackberry 10
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: BB10: Date Picker class

It would be nice if the date picker they use on BB10 is a core class. I really like how they also include the day of the week and seems very fluid.