konstantinbueschel
1/11/2016 - 11:15 PM

Titanium Settings Dialog Examples

Titanium Settings Dialog Examples

var _isAndroid = Ti.Platform.osname === 'android';

var DIALOG_TYPE = {
	NETWORK : 0,
	LOCATION_SERVICES :1
};

exports.SETTINGS_TYPE = DIALOG_TYPE;

var getActionByType = function(settingsType){
	if(settingsType ===DIALOG_TYPE.NETWORK){
		return "android.settings.WIRELESS_SETTINGS";
	}else{
		return "android.settings.LOCATION_SOURCE_SETTINGS";
	}
};

exports.prompt = function(args, callback){
	
	if(!args.hasOwnProperty("settingsType")){
		throw "settingsType property is required";
	}
	if(!args.hasOwnProperty("settingsIndex")){
		throw "settingsIndex property is required";
	}
	
	var ew = Ti.UI.createAlertDialog(args);	
	
	ew.addEventListener("click",function(e){
		e.settingsSelected = (e.index == args.settingsIndex);
		if(e.settingsSelected){
			if(_isAndroid){
				var actionType = getActionByType(args.settingsType);
				var intent = Ti.Android.createIntent({action: actionType});
				Ti.Android.currentActivity.startActivity(intent);
			}else{
				Ti.Platform.openURL(Ti.App.iOS.applicationOpenSettingsURL);	
			}
		}
		return callback(e);
	});
	ew.show();						
};
Ti.UI.setBackgroundColor('#000');

var alertSettings = require("settings-dialog");

var win  = Ti.UI.createWindow({  
    title:'Example', backgroundColor:'#fff', layout:"vertical"
});

win.add(Ti.UI.createLabel({
	text:"Example on now to prompt user to change settings",
	left:15, right:15, height:45, top:50, color:"#000", font:{fontSize:18, fontWeight:"bold"}
}));

var networkButton = Ti.UI.createButton({
	title:"Ask to check network settings",
	left:15, right:15, height:45, top:50, color:"#000"
});
win.add(networkButton);

networkButton.addEventListener("click", function(){
	alertSettings.prompt({
		title:"Information", 
		message:"We can't find a network connection. Please check your settings.",
		buttonNames:["Settings", "Continue"],
		settingsType : alertSettings.SETTINGS_TYPE.NETWORK, //The type of prompt 
		settingsIndex : 0 //What button index should launch the settings
	}, function(d){
		console.log("prompt results = " + JSON.stringify(d));
	});
});

var locationButton = Ti.UI.createButton({
	title:"Ask to check Location settings",
	left:15, right:15, height:45, top:50, color:"#000"	
});
win.add(locationButton);

locationButton.addEventListener("click", function(){
	alertSettings.prompt({
		title:"Information", 
		message:"Please enable location services to use this feature.",
		buttonNames:["Settings", "Continue"],
		settingsType : alertSettings.SETTINGS_TYPE.LOCATION_SERVICES, //The type of prompt 
		settingsIndex : 0 //What button index should launch the settings
	}, function(d){
		console.log("prompt results = " + JSON.stringify(d));
	});	
});

win.open();