Esempio n. 1
0
        private void SelectItem(object sender, AdapterView.ItemClickEventArgs e)
        {
            Fragment fragment = null;

            switch (e.Position)
            {
            case 0:
                fragment = new PlannerDailyFragment();
                break;

            case 1:
                fragment = new PlannerWeeklyFragment();
                break;

            case 2:
                fragment = new SettingsFragment();
                break;

            case 3:
                //TODO logout here
                if (!_Preferences.DemoMode)
                {
                    // Make sure we're not running in demo mode, then logout and redirect to the login fragment.
                    Logout();

                    // Destroy the local cookie.
                    _Preferences.AuthenticationCookie = default(Cookie);
                    fragment = new LoginFragment();
                }
                break;
            }

            if (fragment != null)
            {
                // Load the requested fragment.
                FragmentManager.BeginTransaction()
                .Replace(Resource.Id.content, fragment)
                .AddToBackStack(null)
                .Commit();
            }
        }
        private async void Register_OnClick(object sender, EventArgs e)
        {
            // Collect all of the data from the UI.
            string firstname = View.FindViewById <EditText>(Resource.Id.etFirstName).Text;
            string lastname  = View.FindViewById <EditText>(Resource.Id.etLastName).Text;
            string username  = View.FindViewById <EditText>(Resource.Id.etUsername).Text;
            string password  = View.FindViewById <EditText>(Resource.Id.etPassword).Text;

            if (firstname == "" || lastname == "" || username == "" || password == "")
            {
                // At least one field is empty.
                View.FindViewById <TextView>(Resource.Id.tvErrors).Text = GetString(Resource.String.register_required_fields);
                return;
            }

            try
            {
                // Create an instance of the backend and register the user.
                BackendUser backend = new BackendUser();
                await backend.Register(firstname, lastname, username, password);

                // Switch back to the login activity
                Fragment fragment = new LoginFragment();

                // Load the fragment.
                FragmentManager.BeginTransaction()
                .Replace(Resource.Id.content, fragment)
                .Commit();
            }
            catch (WebException ex)
            {
                // Use WebException to get the status code returned from the API.
                if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Forbidden)
                {
                    // One of the fields is empty.
                    View.FindViewById <TextView>(Resource.Id.tvErrors).Text = GetString(Resource.String.register_required_fields);
                }
                else if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Conflict)
                {
                    // Username is taken.
                    View.FindViewById <TextView>(Resource.Id.tvErrors).Text = GetString(Resource.String.register_username_taken);
                }
                else
                {
                    View.FindViewById <TextView>(Resource.Id.tvErrors).Text = GetString(Resource.String.unknown_error);
                }
            }
            catch (BackendTimeoutException)
            {
                // Display a popup.
                DisplayAlert(GetString(Resource.String.timeout), GetString(Resource.String.timeout_message));
            }
            catch (AggregateException ex)
            {
                // Catch the aggregate exception, this might be thrown by the asynchronous tasks in the backend.
                // Handle any of the types that we are aware of.
                // Managing of aggregate exceptions modified from code found here: https://msdn.microsoft.com/en-us/library/dd537614%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

                ex.Handle((x) =>
                {
                    if (x is WebException)
                    {
                        // Check for an inner exception of Socket Exception
                        if (x.InnerException is System.Net.Sockets.SocketException)
                        {
                            // There is an issue connecting to the backend.
                            DisplayAlert(GetString(Resource.String.connection_failed), GetString(Resource.String.connection_failed_message));
                        }

                        // This exception matched, return true.
                        return(true);
                    }

                    // Was not able to handle the exception.
                    return(false);
                });
            }
        }