<commit_before>var Animation = require("ui/animation").Animation;
var Color = require("color").Color;
var dialogsModule = require("ui/dialogs");
var Observable = require("data/observable").Observable;

var statusBarUtil = require("../../shared/utils/status-bar-util");
var formUtil = require("../../shared/utils/form-util");
var navigation = require("../../shared/navigation");
var UserViewModel = require("../../shared/view-models/user-view-model");

var pageData;
var user;
var page;
var email;
var password;
var submitButton;

exports.loaded = function(args) {
	page = args.object;

	user = new UserViewModel({
		email: "<EMAIL>",
		password: "password",
	});
	pageData = new Observable({
		user: user,
		authenticating: false,
		isLogin: true
	});
	page.bindingContext = pageData;
	statusBarUtil.configure();

	email = page.getViewById("email");
	password = page.getViewById("password");
	submitButton = page.getViewById("submit-button");
	formUtil.hideKeyboardOnBlur(page, [email, password]);

	handleAndroidFocus();
	addLetterSpacing();
	setHintColors();

	// Create the parallax background effect by scaling the background image
	page.getViewById("background").animate({
		scale: { x: 1.2, y: 1.2 },
		duration: 8000
	});
};

// Prevent the first textfield from receiving focus on Android
// See http://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus
function handleAndroidFocus() {
	var container = page.getViewById("container");
	if (container.android) {
		container.android.setFocusableInTouchMode(true);
		container.android.setFocusable(true);
		email.android.clearFocus();
	}
}

function addLetterSpacing() {
	var mainLabel = page.getViewById("main-label");
	var initialLabel = page.getViewById("initial-label");
	var androidLetterSpacing = 0.2;

	if (mainLabel.android && mainLabel.android.setLetterSpacing) {
		mainLabel.android.setLetterSpacing(androidLetterSpacing);
		initialLabel.android.setLetterSpacing(androidLetterSpacing);
	}
	if (mainLabel.ios) {
		var text = mainLabel.ios.text;
		var attributedString = NSMutableAttributedString.alloc().initWithString(text);
		attributedString.addAttributeValueRange(NSKernAttributeName, 5.0, NSMakeRange(0, text.length));
		mainLabel.ios.attributedText = attributedString;
		initialLabel.ios.attributedText = attributedString;
	}
}

function setHintColors() {
	var placeHolderColor = pageData.get("isLogin") ? "#ACA6A7" : "#C4AFB4";

	if (email.android) {
		var color = android.graphics.Color.parseColor(placeHolderColor);
		email.android.setHintTextColor(color);
		password.android.setHintTextColor(color);
	}
	if (email.ios) {
		var dictionary = new NSDictionary([new Color(placeHolderColor).ios], [NSForegroundColorAttributeName]);
		email.ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(
			email.hint, dictionary);
		password.ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(
			password.hint, dictionary);
	}
}

exports.focusPassword = function() {
	password.focus();
};

function disableForm() {
	email.isEnabled = false;
	password.isEnabled = false;
	submitButton.isEnabled = false;
	pageData.set("authenticating", true);
}
function enableForm() {
	email.isEnabled = true;
	password.isEnabled = true;
	submitButton.isEnabled = true;
	pageData.set("authenticating", false);
}

exports.submit = function() {
	if (!user.isValidEmail()) {
		dialogsModule.alert({
			message: "Enter a valid email address.",
			okButtonText: "OK"
		});
		return;
	}

	disableForm();

	if (pageData.get("isLogin")) {
		login();
	} else {
		register();
	}
};

function login() {
	user.login()
		.catch(function() {
			dialogsModule.alert({
				message: "Unfortunately we could not find your account.",
				okButtonText: "OK"
			});
			enableForm();
			return Promise.reject();
		})
		.then(enableForm)
		.then(navigation.goToListPage);
}

function register() {
	user.register()
		.then(function() {
			dialogsModule
				.alert("Your account was successfully created.")
				.then(function() {
					pageData.set("isLogin", true);
					enableForm();
				});
		}).catch(function() {
			dialogsModule
				.alert({
					message: "Unfortunately we were unable to create your account.",
					okButtonText: "OK"
				});
		}).then(enableForm);
}

exports.forgotPassword = function() {
	if (!pageData.get("isLogin")) {
		return;
	}
	navigation.goToPasswordPage();
}

exports.toggleDisplay = function() {
	var isLogin = !pageData.get("isLogin");
	pageData.set("isLogin", isLogin);

	setHintColors();

	// Animate the background color of the main container
	var container = page.getViewById("container");
	var backgroundColor = isLogin ? "white" : "#301217";
	// Workaround https://github.com/NativeScript/NativeScript/issues/1770
	if (container.android) {
		container.style.backgroundColor = backgroundColor;
	} else {
		container.animate({
			backgroundColor: backgroundColor,
			duration: 200
		});
	}

	// Update the UIButton text without an animation.<commit_msg>Making the Android color animation work by using the color module<commit_after>var Animation = require("ui/animation").Animation;
var Color = require("color").Color;
var dialogsModule = require("ui/dialogs");
var Observable = require("data/observable").Observable;

var statusBarUtil = require("../../shared/utils/status-bar-util");
var formUtil = require("../../shared/utils/form-util");
var navigation = require("../../shared/navigation");
var UserViewModel = require("../../shared/view-models/user-view-model");

var pageData;
var user;
var page;
var email;
var password;
var submitButton;

exports.loaded = function(args) {
	page = args.object;

	user = new UserViewModel({
		email: "<EMAIL>",
		password: "password",
	});
	pageData = new Observable({
		user: user,
		authenticating: false,
		isLogin: true
	});
	page.bindingContext = pageData;
	statusBarUtil.configure();

	email = page.getViewById("email");
	password = page.getViewById("password");
	submitButton = page.getViewById("submit-button");
	formUtil.hideKeyboardOnBlur(page, [email, password]);

	handleAndroidFocus();
	addLetterSpacing();
	setHintColors();

	// Create the parallax background effect by scaling the background image
	page.getViewById("background").animate({
		scale: { x: 1.2, y: 1.2 },
		duration: 8000
	});
};

// Prevent the first textfield from receiving focus on Android
// See http://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus
function handleAndroidFocus() {
	var container = page.getViewById("container");
	if (container.android) {
		container.android.setFocusableInTouchMode(true);
		container.android.setFocusable(true);
		email.android.clearFocus();
	}
}

function addLetterSpacing() {
	var mainLabel = page.getViewById("main-label");
	var initialLabel = page.getViewById("initial-label");
	var androidLetterSpacing = 0.2;

	if (mainLabel.android && mainLabel.android.setLetterSpacing) {
		mainLabel.android.setLetterSpacing(androidLetterSpacing);
		initialLabel.android.setLetterSpacing(androidLetterSpacing);
	}
	if (mainLabel.ios) {
		var text = mainLabel.ios.text;
		var attributedString = NSMutableAttributedString.alloc().initWithString(text);
		attributedString.addAttributeValueRange(NSKernAttributeName, 5.0, NSMakeRange(0, text.length));
		mainLabel.ios.attributedText = attributedString;
		initialLabel.ios.attributedText = attributedString;
	}
}

function setHintColors() {
	var placeHolderColor = pageData.get("isLogin") ? "#ACA6A7" : "#C4AFB4";

	if (email.android) {
		var color = android.graphics.Color.parseColor(placeHolderColor);
		email.android.setHintTextColor(color);
		password.android.setHintTextColor(color);
	}
	if (email.ios) {
		var dictionary = new NSDictionary([new Color(placeHolderColor).ios], [NSForegroundColorAttributeName]);
		email.ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(
			email.hint, dictionary);
		password.ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(
			password.hint, dictionary);
	}
}

exports.focusPassword = function() {
	password.focus();
};

function disableForm() {
	email.isEnabled = false;
	password.isEnabled = false;
	submitButton.isEnabled = false;
	pageData.set("authenticating", true);
}
function enableForm() {
	email.isEnabled = true;
	password.isEnabled = true;
	submitButton.isEnabled = true;
	pageData.set("authenticating", false);
}

exports.submit = function() {
	if (!user.isValidEmail()) {
		dialogsModule.alert({
			message: "Enter a valid email address.",
			okButtonText: "OK"
		});
		return;
	}

	disableForm();

	if (pageData.get("isLogin")) {
		login();
	} else {
		register();
	}
};

function login() {
	user.login()
		.catch(function() {
			dialogsModule.alert({
				message: "Unfortunately we could not find your account.",
				okButtonText: "OK"
			});
			enableForm();
			return Promise.reject();
		})
		.then(enableForm)
		.then(navigation.goToListPage);
}

function register() {
	user.register()
		.then(function() {
			dialogsModule
				.alert("Your account was successfully created.")
				.then(function() {
					pageData.set("isLogin", true);
					enableForm();
				});
		}).catch(function() {
			dialogsModule
				.alert({
					message: "Unfortunately we were unable to create your account.",
					okButtonText: "OK"
				});
		}).then(enableForm);
}

exports.forgotPassword = function() {
	if (!pageData.get("isLogin")) {
		return;
	}
	navigation.goToPasswordPage();
}

exports.toggleDisplay = function() {
	var isLogin = !pageData.get("isLogin");
	pageData.set("isLogin", isLogin);

	setHintColors();

	// Animate the background color of the main container
	var container = page.getViewById("container");
	container.animate({
		backgroundColor: isLogin ? new Color("white") : new Color("#301217"),
		duration: 200
	});

	// Update the UIButton text without an animation.
{'fraction_non_alphanumeric': 0.14330692667072922, 'fraction_numerical': 0.007109486085720089, 'mean_word_length': 4.823181549379066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '737553', 'n_tokens_mistral': 3311, 'n_tokens_neox': 3000, 'n_words': 627}