예제 #1
0
        async void Login()
        {
            if (!Validate())
            {
                return;
            }
            ProgressBar.Visibility = ViewStates.Visible;
            LoginBtn.Enabled       = false;
            var pwdHash = User.GetPasswordHash(UserNameText.Text.Trim().ToLower(), PasswordText.Text);
            var client  = SessionManager.GetHttpClient();
            var content = new FormUrlEncodedContent(new KeyValuePair <string, string>[]
            {
                new KeyValuePair <string, string>("grant_type", "password"),
                new KeyValuePair <string, string>("username", UserNameText.Text.Trim().ToLower()),
                new KeyValuePair <string, string>("password", pwdHash),
            });

            try
            {
                var response = await client.PostAsync($"{SessionManager.ServerAddress}/Token", content);

                var responseMsg = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrWhiteSpace(responseMsg))
                {
                    var json = JsonConvert.DeserializeObject <dynamic>(responseMsg);
                    if (response.IsSuccessStatusCode)
                    {
                        SessionManager.User.Sync(json);
                        SessionManager.SaveSettings();
                        SessionManager.User.Hash = pwdHash;
                        // SyncDataAsync();
                        if (SessionManager.IsFirstRun)
                        {
                            StartActivity(typeof(AboutUsActivity));
                            Finish();
                            OverridePendingTransition(Resource.Animation.side_in_right, Resource.Animation.side_out_left);
                        }
                        else
                        {
                            StartActivity(typeof(MainActivity));
                            Finish();
                            OverridePendingTransition(Resource.Animation.side_in_right, Resource.Animation.side_out_left);
                        }
                        //DataSync.ProgressChanged += (o, e) =>
                        //{
                        //    ProgressBar.Visibility = ViewStates.Visible;
                        //};
                        //DataSync.DataSyncCompleted += (o, e) =>
                        //{
                        //    ProgressBar.Visibility = ViewStates.Gone;
                        //    Finish();
                        //    OverridePendingTransition(Resource.Animation.side_in_right, Resource.Animation.side_out_left);
                        //};
                        //DataSync.DataSyncFailed += (o, e) =>
                        //{
                        //    MessageBox.Show(this, e.Message, "Sync failed");
                        //    ProgressBar.Visibility = ViewStates.Gone;
                        //};
                    }
                    else
                    {
                        MessageBox.Show(this, "Login Error", (string)json.error_description);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, "Login Error", e.Message);
            }
            LoginBtn.Enabled       = true;
            ProgressBar.Visibility = ViewStates.Gone;
            this.HideSoftKeyboard();
        }