示例#1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Parse.Initialize(this, ParseCredentials.ApplicationID, ParseCredentials.ClientKey);
            DensityExtensions.Initialize(this);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            var signInBtn     = FindViewById <Button> (Resource.Id.SignInButton);
            var signUpBtn     = FindViewById <Button> (Resource.Id.SignUpButton);
            var userEntry     = FindViewById <EditText> (Resource.Id.EmailEntry);
            var passwordEntry = FindViewById <EditText> (Resource.Id.PasswordEntry);

            /* If the user is already logged in, we show a blank landing page
             * (as there is a bit of delay when acquiring the TabPerson
             * so this activity content is still shown).
             */
            if (ParseUser.CurrentUser != null)
            {
                signInBtn.Visibility = signUpBtn.Visibility = userEntry.Visibility = passwordEntry.Visibility = ViewStates.Invisible;
                ParseUser.CurrentUser.RefreshInBackground(null);
                LaunchApp(this, ParseUser.CurrentUser, null);
            }

            profile = UserProfile.Instantiate(this);

            SignupTimer timer = null;

            userEntry.AfterTextChanged += (sender, e) => {
                var login = userEntry.Text;
                if (string.IsNullOrEmpty(login))
                {
                    return;
                }
                if (timer != null)
                {
                    timer.Cancel();
                }
                timer = new SignupTimer(1000, 1000, () => {
                    var usernameChecker = CheckLoginDisponibility(login);
                    usernameChecker.ContinueWith(t => {
                        if (userEntry.Text == login)
                        {
                            signUpBtn.Enabled = t.Result;
                        }
                    }, TaskContinuationOptions.ExecuteSynchronously);
                });
                timer.Start();
            };
            var initialEmail = profile.PrimayAddress ?? (profile.Emails == null ? null : profile.Emails.FirstOrDefault()) ?? null;

            if (!string.IsNullOrEmpty(initialEmail))
            {
                userEntry.Text = initialEmail;
            }
            if (!string.IsNullOrEmpty(userEntry.Text))
            {
                passwordEntry.RequestFocus();
            }

            ProgressDialog spinDialog = new ProgressDialog(this)
            {
                Indeterminate = true
            };

            spinDialog.SetCancelable(false);

            Action <ParseUser, ParseException> callback = (user, err) => {
                if (user == null || err != null)
                {
                    Android.Util.Log.Debug("Login",
                                           "User not recognized: {0}",
                                           (err != null) ? err.Message : string.Empty);
                    spinDialog.Dismiss();
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage(Resource.String.login_error);
                    builder.SetPositiveButton("OK", (a, b) => passwordEntry.Text = string.Empty);
                    builder.Create().Show();

                    return;
                }

                Android.Util.Log.Debug("Login", "User {0} successfully logged. New? {1}", user.Username, user.IsNew);

                LaunchApp(this, user, spinDialog.Dismiss);
            };

            signInBtn.Click += (sender, e) => {
                string email;
                if (!TryExtractEmailFromRawInput(userEntry.Text, out email))
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage(Resource.String.invalid_email);
                    builder.SetPositiveButton("OK", (a, b) => userEntry.Text = string.Empty);
                    builder.Create().Show();
                    return;
                }
                spinDialog.SetMessage("Signing in...");
                spinDialog.Show();
                ParseUser.LogInInBackground(email,
                                            passwordEntry.Text,
                                            new TabLoginCallback(callback));
            };
            signUpBtn.Click += (sender, e) => {
                string email;
                if (!TryExtractEmailFromRawInput(userEntry.Text, out email))
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage(Resource.String.invalid_email);
                    builder.SetPositiveButton("OK", (a, b) => userEntry.Text = string.Empty);
                    builder.Create().Show();
                    return;
                }

                spinDialog.SetMessage("Signing up...");
                spinDialog.Show();

                var user = new ParseUser()
                {
                    Username = email,
                    Email    = email
                };
                user.SetPassword(passwordEntry.Text);
                user.SignUpInBackground(new TabSignUpCallback(user, callback));
            };
        }