Method in a web service runs multiple times (depending on the button click)

kmmpalizakmmpaliza Member ✭✭
edited December 2018 in Xamarin.Android

I am consuming a method in a web service soap. It runs smoothly when the button is being tapped once, a dialog box will be popped up. On the second button tap, the dialog box will be showed twice. On the third button tap, the dialog box will be showed thrice. And so on....

    case Resource.Id.btnLogin:

                    hud = KK.Create(this.Activity)
                         .SetSize(75, 75);
                    hud.Show();

                    loginClick(); //Function for button click

                       break;


      private void loginClick(){
            hud.Dismiss();
            if (TextUtils.IsEmpty(txtID.Text.ToString()) || TextUtils.IsEmpty(txtPass.Text.ToString()))
            {
                AlertMessageView.showAlertDialog(this.Activity, "Error", GetString(Resource.String.Msg33), "OK", null, null, null);
            }
            else if (txtID.Text.ToString().Trim().Length < 12)
            {
                tilID.Error = GetString(Resource.String.Msg8);
            }
            else
            {

                service.VerifyLoginAsync("U$er", "[email protected]", txtID.Text.ToString(), txtPass.Text.ToString());
                service.VerifyLoginCompleted += Service_VerifyLoginCompleted;
            }
    }

Consuming web service method Verify Login:

  private void Service_VerifyLoginCompleted(object sender, IDWebService.VerifyLoginCompletedEventArgs e)
  {
       Console.WriteLine("IsValid" + e.Result.isValid);

    if (e.Result.isValid == true)
    {

        #region firstLogin
        if (e.Result.isfirstlogin == "1")
        {
            Console.WriteLine("Redirect Verification page");

            AlertMessageView.showAlertDialog(this.Activity, "Invalid login", "Is it your first time to login?", "YES", "NO", dialogFTL, null);

            //var intent = new Intent(this.Activity, typeof(OTPActivity));
            //StartActivity(intent);
            //Activity.Finish();


        }
        else
        {
            #region lockedout
            if (e.Result.islockedout == "1")
            {
                Console.WriteLine("Error Msg");
                AlertMessageView.showAlertDialog(this.Activity, "Account has been locked", "Kindly coordinate with employer", "OK", null, null, null);

            }

            else {

                #region forgotPass
                if (e.Result.isforgotpass == "1")
                {
                    AlertMessageView.showAlertDialog(this.Activity, "Invalid Password", "Please change your password.", "YES", "CANCEL", dialogFTL, null);

                }
                else
                {
                    #region enabled
                    if (e.Result.isenabled == "1")
                    {
                        #region validpass
                        if (e.Result.isValidPassword == true)
                        {
                            Console.WriteLine("Number:" + e.Result.number);
                            Console.WriteLine("First Name:" + e.Result.fname);

                            fname = e.Result.fname;
                            number = e.Result.number;
                            GenerateOTP();

                            var intent = new Intent(this.Activity, typeof(OTPLoginActivity));
                            StartActivity(intent);
                            Activity.Finish();
                        }
                        else
                        {
                            Console.WriteLine("Failed Date Login");
                            #region morethan24
                            if (e.Result.isMoreThan24Hrs == true)
                            {

                                loginCounter = 0;
                                MemberFailedLogin(loginCounter.ToString());
                            }
                            else
                            {
                                loginCounter = Convert.ToInt32(e.Result.failedattempt) + 1;

                                if (loginCounter < 3)
                                {
                                    MemberFailedLogin(loginCounter.ToString());
                                    Console.WriteLine("Invalid Login" + loginCounter);
                                }
                                else
                                {
                                    Console.WriteLine("Account has been locked" + loginCounter);

                                    MemberLockAccount();


                                    AlertMessageView.showAlertDialog(this.Activity, "Account has been locked", "Kindly coordinate with employer.", "OK", null, null, null);
                                }


                            }
                            #endregion

                        }
                        #endregion 
                    }
                    else
                    {

                        AlertMessageView.showAlertDialog(this.Activity, "Account disabled", "Please contact your employer.", "OK", null, null, null);

                    }
                    #endregion

                }
                #endregion
            }
            #endregion

        }
        #endregion
    }
    else
    {
        Console.WriteLine("Invalid");

        AlertMessageView.showAlertDialog(this.Activity, null, "Invalid Credentials", "OK", null, null, null);

    }
}

Answers

  • XigmondXigmond USMember ✭✭

    I would create a bool Variable like this:

    public bool InProgress { get; set; }

    in your service class

    then set it to true when your service process starts.

    now in your Button Click event Handler method check the InProgress Value :

        private void loginClick() {
    
            if ( myService.Inprogress )
                return;
        ...
    

    this is the logic, you may need to change it to fit your needs.

  • XigmondXigmond USMember ✭✭

    Another Approach could be Disabling the button untill you get the result of your service's process and re enabling it after that.

    good luck :wink:

  • kmmpalizakmmpaliza Member ✭✭

    @Xigmond I tried the same thing, but still the same :(

Sign In or Register to comment.