简体   繁体   中英

Foreach, With only one DisplayAlert Popup?

Hi guy's i'm trying to figure out a way to display a DisplayAlert Popup when Someone entered the Wrong details into the Login Process.

Issue is, The login's a little "Weird" I'm not comparing against a Database i'm comparing against a List of Customers I get from a APi So I have to loop threw to then find if the detail's are correct.

I have a login Phase 1() which Checks against the Wordpress API but I want to have this be able to notify on Phase 1 and 2, Phase 1 is a Username = Username Password = Password Where phase 2 is just a Username = Username , I know it's not secure The Login is more of a Formality then anything.


  public async void Login_Phase1()
        {
            try
            {


                #region Login Phase 1

                var client = new WordPressClient("http://XXX.co.za/wp-json/");
                client.AuthMethod = AuthMethod.JWT;
                try
                {
                    await client.RequestJWToken(Usernamelabel.Text, Password.Text);
                }
                catch (Exception e)
                {
                    await App.Current.MainPage.DisplayAlert("Something Went wrong", e.ToString(), "OK");

                }


                var x = client;
                var isValidToken = await client.IsValidJWToken();

                WpApiCredentials.token = client.GetToken();

                if (isValidToken)
                {

                   Login_Phase2();
                }
                else
                {
                 await App.Current.MainPage.DisplayAlert("Detail's are Incorect", "Token not Found", "OK");
                }
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
            #endregion
        }


public void Login_Phase2()
        {
            try
            {


                #region login Phase 2
                var list = FetchCustomers.customers.ToList();

                foreach (var user in list)
                {

                    if (user.username == Usernamelabel.Text)
                    {
                        Preferences.Set("CId", user.id.ToString());
                        Preferences.Set("Token", WpApiCredentials.token);
                        Preferences.Set("CUsername", user.username);
                        Preferences.Set("CEmail", user.email);
                        Users.Loggedin = true;
                        Application.Current.SavePropertiesAsync();
                        App.Current.MainPage.DisplayAlert("Complete", "Login Process Complete, Enjoy", "OK");
                        App.Current.MainPage = new Home("Mica Market");
                    }
                   //Want to add a Display popup Here to say the information is entered incorrectly, but 
                      not have it repeat 200 Time's 
                }
            }

            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
            #endregion
        }

Fetching all the Customers to check against


private async void ExtractWooData(List<Customer> x)
        {
            try
            {


                #region FetchC
                RestAPI rest = new RestAPI("http://xxxxxx/wp-json/wc/v3/", "ck_a25xxxxxxxxxxx0", "cs_8xxxxxxxx8xxxx");
                WCObject wc = new WCObject(rest);


                int pageNum = 1;
                var isNull = false;
                List<Customer> oldlist;

                while (!isNull)
                {

                    var page = pageNum.ToString();
                    x = await wc.Customer.GetAll(new Dictionary<string, string>() {
                {
                    "page", page
                }, {
                    "per_page", "100"
                }
            });
                    oldlist = FetchCustomers.customers ?? new List<Customer>();
                    if (x.Count == 0)
                    {


                        break;
                    }
                    else
                    {
                        //1st loop customers needs to = 100
                        //2nd loop oldist needs to = 40+
                        //If count = 0 then => Combine Customers + Oldist
                        pageNum++;

                        FetchCustomers.customers = oldlist.Union(x).ToList();


                    }


                }
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
            #endregion
        }

Any Advice?

you can replace the foreach with a LINQ query

// find the first match
var found = list.Where(user => user.username == Usernamelabel.Text).FirstOrDefault();

if (found != null) 
{
  // set preferences and navigation
}
else 
{
  // DisplayAlert
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM