Пример #1
0
        private void loginBtn_Clicked(object sender, EventArgs e)
        {
            indicator.IsVisible = true;
            indicator.IsRunning = true;
            //Task.Run(() => {
            try{
                if (!client.Connected)
                {
                    client    = Connection.Connect(hostname, defaultPort, ref writer, ref reader);
                    sessionID = (long)reader.Read();
                }
                var username = usernameEntry.Text.Trim().ToUsername();
                var password = passwordEntry.Text.ToPassword();

                SignInRequest req = new SignInRequest(username, password, sessionID);
                writer.Write(req);
                //req.Send(writer);

                Response resp = (Response)reader.Read();
                if (resp.SessionID == sessionID)
                {
                    //switch ((ResponseType)Enum.Parse(typeof(ResponseType), (string)reader.Read()))
                    switch (resp.Type)
                    {
                    case ResponseType.AccountInfo:
                        AccountInfoResponse AIResp = (AccountInfoResponse)resp;                                 //.Read(reader, sessionID);
                        var homePage = new NavigationPage(new HomePage(app, username, writer, reader, AIResp, sessionID));
                        //Device.BeginInvokeOnMainThread(() => {
                        //indicator.IsVisible = false;
                        //indicator.IsRunning = false;
                        app.MainPage = homePage;
                        //});
                        break;

                    case ResponseType.Fail:
                        FailResponse FResp = (FailResponse)resp;                                         //.Read(reader, sessionID);
                        DisplayAlert(SignInFailTitle, FResp.Reason, DefaultCancel);
                        break;

                    default:
                        throw new InvalidResponseException();
                    }
                }
                else
                {
                    Connection.Disconnect(ref client, ref reader, ref writer);
                }
            }
            catch (SocketException)
            {
                DisplayAlert(ConnectionErrorTitle, ConnectionErrorMessage, DefaultCancel);
            }
            catch (InvalidResponseException)
            {
                //Task.Run( () =>
                DisplayAlert(ResponseErrorTitle, ResponseErrorMessage, DefaultCancel);
                //);
            }
            finally
            {
                indicator.IsVisible = false;
                indicator.IsRunning = false;
            }
            //});
        }
Пример #2
0
        private void createAccountBtn_Clicked(object sender, EventArgs e)
        {
            indicator.IsVisible = true;
            indicator.IsRunning = true;
            //Task.Run(() => {
            try{
                if (!client.Connected)
                {
                    client    = Connection.Connect(hostname, defaultPort, ref writer, ref reader);
                    sessionID = (long)reader.Read();
                }
                NewAccountRequest req = default;
                var email             = emailEntry.Text.Trim();
                var username          = usernameEntry.Text.Trim();
                var passwd            = passwordEntry.Text;
                //if (username.Length <= 7)
                //{
                //	DisplayAlert(FormatErrorTitle, UsernameFormatErrorMessage, ConnectionErrorCancel);
                //	return;
                //}
                //if (passwd.Length <= 7)
                //{
                //	DisplayAlert(FormatErrorTitle, PasswordFormatErrorMessage, ConnectionErrorCancel);
                //	return;
                //}
                req = new NewAccountRequest(email.ToEmail(),
                                            username.ToUsername(), passwd.ToPassword(), sessionID);
                writer.Write(req);
                //req.Send(writer);

                Response resp = (Response)reader.Read();
                if (resp.SessionID == sessionID)
                {
                    //switch ((ResponseType)Enum.Parse(typeof(ResponseType), (string)reader.Read()))
                    switch (resp.Type)
                    {
                    case ResponseType.Success:
                        loginBtn.IsVisible   = true;
                        emailEntry.IsVisible = false;
                        emailLabel.IsVisible = false;
                        btnGrid.IsVisible    = true;
                        btnStack.IsVisible   = false;

                        emailEntry.Text    = "";
                        usernameEntry.Text = "";
                        passwordEntry.Text = "";
                        DisplayAlert(AccountCreateSuccessTitle, AccountCreateSuccessMessage, DefaultCancel);
                        break;

                    case ResponseType.Fail:
                        FailResponse FResp = (FailResponse)resp;                                         //.Read(reader, sessionID);
                        DisplayAlert(AccountCreateFailTitle, FResp.Reason, DefaultCancel);
                        break;
                    }
                }
                else
                {
                    Connection.Disconnect(ref client, ref reader, ref writer);
                }
            }
            catch (SocketException)
            {
                DisplayAlert(ConnectionErrorTitle, ConnectionErrorMessage, DefaultCancel);
            }
            catch (FormatException)
            {
                DisplayAlert(FormatErrorTitle, EmailFormatErrorMessage, DefaultCancel);
            }
            //catch (InvalidSessionIDException)
            //{
            //	DisplayAlert(FormatErrorTitle, EmailFormatErrorMessage, EmailFormatErrorCancel);
            //}
            finally
            {
                indicator.IsVisible = false;
                indicator.IsRunning = false;
            }
            //});
        }