Skip to content

Instantly share code, notes, and snippets.

@mauropm
Created July 4, 2013 00:22
Show Gist options
  • Save mauropm/5923980 to your computer and use it in GitHub Desktop.
Save mauropm/5923980 to your computer and use it in GitHub Desktop.
An example of generic app with different actions according to the button. It's showing how to manage the destruction of the window after you use it, and other nice things. To use: Create a new classic mobile project in Titanium Studio, and copy all this files to Resources directory. This is intended to work with Titanium Mobile SDK 3.1.1.GA, wit…
// INcluding memory management utils.
Ti.include('utils.js');
// root window.
var win = Ti.UI.createWindow({
backgroundColor:'white',
exitOnclose:true,
});
// we want this to look centered.
var img_center = Math.floor(Ti.Platform.displayCaps.platformWidth/2)-100;
var image = Ti.UI.createView({
backgroundImage:'KS_nav_ui.png',
width:200,
height:200,
top:10,
left:img_center,
});
win.add(image);
// creating buttons
var rh = Ti.UI.createButton({
title:'Recursos humanos',
width:200,
height:44,
top: 215,
left: img_center,
});
// Include functionality of rh.
Ti.include('rh.js');
var checkin = Ti.UI.createButton({
title:'Entrada a tienda',
width:200,
height:44,
top: 269,
left: img_center,
});
Ti.include('checkin.js');
var price = Ti.UI.createButton({
title:'Checar precio',
width:200,
height:44,
top:323,
left: img_center,
});
Ti.include('price.js');
var checkout = Ti.UI.createButton({
title:'Salida de tienda',
width:200,
height:44,
top:377,
left: img_center,
});
Ti.include('checkout.js');
//add buttons to the window
win.add(rh);
win.add(checkin);
win.add(price);
win.add(checkout);
win.open();
// HR functionality
var win2 = Ti.UI.createWindow({
backgroundColor:'white',
});
function checkin_build(){
var picture = Ti.UI.createButton({
title:'Tomar fotografia',
width:200,
height:44,
top:25,
left:img_center,
});
var registro = Ti.UI.createButton({
title:'Registrarse en tienda',
width:200,
height:44,
top:79,
left:img_center,
});
win2.add(picture);
win2.add(registro);
function print_gps(){
var geolocation = '';
if (Titanium.Geolocation.locationServicesEnabled === false){
geolocation = 'No disponible';
} else {
Titanium.Geolocation.distanceFilter = 10;
Titanium.Geolocation.getCurrentPosition(function(e){
if (!e.success || e.error){
geolocation = 'error: ' + JSON.stringify(e.error);
} else {
geolocation = e.coords.longitude +", "+e.coords.latitude;
}
});
}
var currentDate = new Date();
var mensaje = "Entrada: "+currentDate.getDate()+"-"+currentDate.getMonth()+"-"+currentDate.getFullYear()+" "+currentDate.getHours()+":"+currentDate.getMinutes()+"\nGeolocalizacion: "+geolocation;
var label = Ti.UI.createLabel({
text:mensaje,
width:200,
height:88,
top:133,
left:img_center,
});
win2.add(label);
}
function get_picture(){
Titanium.Media.showCamera({
success:function(event)
{
var cropRect = event.cropRect;
var image = event.media;
Ti.API.debug('Our type was: '+event.mediaType);
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
{
var t = Ti.UI.create2DMatrix();
t = t.rotate(90);
var imageView = Ti.UI.createImageView({
width:150,
height:150,
image:event.media,
top:231,
left:img_center,
transform:t,
});
win2.add(imageView);
}
else
{
alert("got the wrong type back ="+event.mediaType);
}
},
cancel:function()
{
},
error:function(error)
{
// create alert
var a = Titanium.UI.createAlertDialog({title:'Camera'});
// set message
if (error.code == Titanium.Media.NO_CAMERA)
{
a.setMessage('Please run this test on device');
}
else
{
a.setMessage('Unexpected error: ' + error.code);
}
// show alert
a.show();
},
saveToPhotoGallery:true,
allowEditing:true,
mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
});
}
registerEventListener(picture, {event: 'click', callback: get_picture});
registerEventListener(registro, {event: 'click', callback: print_gps});
}
checkin.addEventListener('click',function(){
win2 = win2 || Ti.UI.createWindow({
backgroundColor:'white',
});
registerEventListener(win2, {event: 'androidback', callback: win2Close});
Ti.App.addEventListener('win2:close',win2Clean);
checkin_build();
win2.open();
});
// HR functionality
var win2 = Ti.UI.createWindow({
backgroundColor:'white',
});
function checkout_build(){
var registro = Ti.UI.createButton({
title:'Salida de tienda',
width:200,
height:44,
top:25,
left:img_center,
});
win2.add(registro);
function print_gps(){
var geolocation = '';
if (Titanium.Geolocation.locationServicesEnabled === false){
geolocation = 'No disponible';
} else {
Titanium.Geolocation.distanceFilter = 10;
Titanium.Geolocation.getCurrentPosition(function(e){
if (!e.success || e.error){
geolocation = 'error: ' + JSON.stringify(e.error);
} else {
geolocation = e.coords.longitude +", "+e.coords.latitude;
}
});
}
var currentDate = new Date();
var mensaje = "Salida: "+currentDate.getDate()+"-"+currentDate.getMonth()+"-"+currentDate.getFullYear()+" "+currentDate.getHours()+":"+currentDate.getMinutes()+"\nGeolocalizacion: "+geolocation;
var label = Ti.UI.createLabel({
text:mensaje,
width:200,
height:88,
top:79,
left:img_center,
});
win2.add(label);
}
registerEventListener(registro, {event: 'click', callback: print_gps});
}
checkout.addEventListener('click',function(){
win2 = win2 || Ti.UI.createWindow({
backgroundColor:'white',
});
registerEventListener(win2, {event: 'androidback', callback: win2Close});
Ti.App.addEventListener('win2:close',win2Clean);
checkout_build();
win2.open();
});
// HR functionality
var win2 = Ti.UI.createWindow({
backgroundColor:'white',
});
function price_build(){
var picture = Ti.UI.createButton({
title:'Tomar fotografia',
width:200,
height:44,
top:25,
left:img_center,
});
var precio = Ti.UI.createLabel({
text:'Teclee el precio',
width:200,
height:44,
top:79,
left:img_center,
});
var textfield = Ti.UI.createTextField({
width:200,
height:44,
top:143,
left:img_center,
});
win2.add(picture);
win2.add(precio);
win2.add(textfield);
function get_picture(){
Titanium.Media.showCamera({
success:function(event)
{
var cropRect = event.cropRect;
var image = event.media;
Ti.API.debug('Our type was: '+event.mediaType);
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
{
var t = Ti.UI.create2DMatrix();
t = t.rotate(90);
var imageView = Ti.UI.createImageView({
width:150,
height:150,
image:event.media,
top:231,
left:img_center,
transform:t,
});
win2.add(imageView);
}
else
{
alert("got the wrong type back ="+event.mediaType);
}
},
cancel:function()
{
},
error:function(error)
{
// create alert
var a = Titanium.UI.createAlertDialog({title:'Camera'});
// set message
if (error.code == Titanium.Media.NO_CAMERA)
{
a.setMessage('Please run this test on device');
}
else
{
a.setMessage('Unexpected error: ' + error.code);
}
// show alert
a.show();
},
saveToPhotoGallery:true,
allowEditing:true,
mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
});
}
registerEventListener(picture, {event: 'click', callback: get_picture});
}
price.addEventListener('click',function(){
win2 = win2 || Ti.UI.createWindow({
backgroundColor:'white',
});
registerEventListener(win2, {event: 'androidback', callback: win2Close});
Ti.App.addEventListener('win2:close',win2Clean);
price_build();
win2.open();
});
// HR functionality
var win2 = Ti.UI.createWindow({
backgroundColor:'white',
});
function rh_build(){
var entrada = Ti.UI.createButton({
title:'Entrada',
width:200,
height:44,
top:25,
left:img_center,
});
var falta = Ti.UI.createButton({
title:'Inasistencia',
width:200,
height:44,
top:79,
left:img_center,
});
var salida = Ti.UI.createButton({
title:'Salida',
width:200,
height:44,
top:133,
left:img_center,
});
var label = Ti.UI.createLabel({
text:'Comentario',
width:200,
height:44,
top:187,
left:img_center,
});
var textfield = Ti.UI.createTextField({
width:200,
height:44,
top:241,
left:img_center,
});
win2.add(entrada);
win2.add(salida);
win2.add(falta);
win2.add(label);
win2.add(textfield);
function print_alert(){
var geolocation = '';
if (Titanium.Geolocation.locationServicesEnabled === false){
geolocation = 'No disponible';
} else {
Titanium.Geolocation.distanceFilter = 10;
Titanium.Geolocation.getCurrentPosition(function(e){
if (!e.success || e.error){
geolocation = 'error: ' + JSON.stringify(e.error);
} else {
geolocation = e.coords.longitude +", "+e.coords.latitude;
}
});
}
var currentDate = new Date();
if (textfield.value){
var mensaje = currentDate.getDate()+"-"+currentDate.getMonth()+"-"+currentDate.getFullYear()+" "+currentDate.getHours()+":"+currentDate.getMinutes()+"\nGeolocalizacion: "+geolocation+" \nComentario: "+textfield.value;
} else {
var mensaje = currentDate.getDate()+"-"+currentDate.getMonth()+"-"+currentDate.getFullYear()+" "+currentDate.getHours()+":"+currentDate.getMinutes()+"\nGeolocalizacion: "+geolocation;
}
alert(mensaje);
}
registerEventListener(entrada, {event: 'click', callback: print_alert});
registerEventListener(salida, {event: 'click', callback: print_alert});
registerEventListener(falta, {event: 'click', callback: print_alert});
}
rh.addEventListener('click',function(){
win2 = win2 || Ti.UI.createWindow({
backgroundColor:'white',
});
registerEventListener(win2, {event: 'androidback', callback: win2Close});
Ti.App.addEventListener('win2:close',win2Clean);
rh_build();
win2.open();
});
/// Recursive Clean of Memory
// by Mauro Parra
// https://gist.github.com/mauropm/2655813
// Begin recursive clean of memory
function do_clean(e,c){
clean(c);
e.remove(c);
Ti.API.info( 'Deleted child at do_clean' );
return;
}
function clean(e){
if (e!=null){
if(e.children){
Ti.API.info( 'Number of children: ' + e.children.length );
for(var i = 0; i<e.children.length;i++){
do_clean(e, e.children[0]);
}
} else {
return;
}
}
}
// End recursive clean of memory
/// <<< Register & UnRegister Event Listeners
// Original from: https://gist.github.com/minhnc/2333095
// Thanks minhnc!
/**
* params: {event: 'event', callback: eventCallback}
*/
function registerEventListener(obj, params) {
if ( typeof obj._eventListeners == 'undefined' ) {
obj._eventListeners = [];
}
obj.addEventListener(params.event, params.callback);
var eventListeners = obj._eventListeners;
eventListeners.push(params);
obj._eventListeners = eventListeners;
Ti.API.info( JSON.stringify(obj._eventListeners) );
}
function unRegisterAllEventListeners(obj) {
if ( typeof obj._eventListeners == 'undefined' || obj._eventListeners.length == 0 ) {
return;
}
for(var i = 0, len = obj._eventListeners.length; i < len; i++) {
var e = obj._eventListeners[i];
obj.removeEventListener(e.event, e.callback);
}
obj._eventListeners = [];
}
/// Register & UnRegister Event Listeners >>>
// Begin win2 clean (all secondary windows are named win2)
// Function to clean the win2 (i.e., remove all the attached listeners, close it, null it)
function win2Clean(){
if(win2!=null){
win2.close();
clean(win2);
unRegisterAllEventListeners(win2);
win2 = null;
}
Ti.App.removeEventListener('win2:close',win2Clean);
}
function win2Close(){
Ti.App.fireEvent('win2:close');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment