예제 #1
0
        /// <summary>
        /// Saves the user data.
        /// </summary>
        private void Save()
        {
            // If already saving information
            if (this.IsBusy)
            {
                return;
            }

            // Validate that the notification type is valid
            if (this.NotificationTypeIndex < 0)
            {
                App.DisplayAlert(
                    Localization.ErrorDialogTitle,
                    Localization.ErrorInvalidNotificationTypes,
                    Localization.DialogDismiss);
                return;
            }

            // Prepare the data to be send to the server
            var request = new Json.JsonObject {
                { "notification_types", this.NotificationTypeIndex }
            };

            // Send request to the server
            this.IsBusy = true;
            WebHelper.SendAsync(
                Uris.GetUpdateUserInfoUri(),
                request.AsHttpContent(),
                this.ProcessSaveResult,
                () => this.IsBusy = false);
        }
        /// <summary>
        /// Logs in the user with the provided credentials.
        /// </summary>
        private void Login()
        {
            // Login the user
            System.Diagnostics.Debug.WriteLine("{0}:{1}", this.UserName, this.Password);

            // If already logging in
            if (this.IsBusy)
            {
                return;
            }

            // Validate that the user name is a valid email
            var user    = this.UserName.Trim().ToLower();
            var isEmail = Regex.IsMatch(
                user,
                @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z",
                RegexOptions.IgnoreCase);

            if (!isEmail)
            {
                App.DisplayAlert(
                    Localization.ErrorDialogTitle,
                    Localization.ErrorInvalidUserLogin,
                    Localization.DialogDismiss);
                return;
            }

            // Validate that the password is present
            if (this.Password.Length <= 2)
            {
                App.DisplayAlert(
                    Localization.ErrorDialogTitle,
                    Localization.ErrorInvalidPassword,
                    Localization.DialogDismiss);
                return;
            }

            // Prepare the data to be send to the server
            var deviceId = ((App)Application.Current).DeviceId;
            var request  = new Json.JsonObject
            {
                { "grant_type", "password" },
                { "username", user },
                { "password", this.Password },
                { "scope", "user submit-report" },
                { "device", deviceId }
            };

            // If push token exists
            var pushToken = ((App)Application.Current).PushToken;

            if (!string.IsNullOrEmpty(pushToken))
            {
                request.Add("push_token", pushToken);
            }

            // Setup error handlers
            // - If session is already opened by another device, request user consent
            var handlers = new Dictionary <System.Net.HttpStatusCode, Action <JsonValue> >
            {
                { System.Net.HttpStatusCode.Conflict, resp => this.RetryLogin(request, resp) }
            };

            // Send request to the server
            this.IsBusy = true;
            WebHelper.SendAsync(
                Uris.GetLoginUri(),
                request.AsHttpContent(),
                this.ProcessLoginResult,
                () => this.IsBusy = false,
                handlers);
        }