Beispiel #1
0
 public UserAccount(UserAccount anotherAccount)
 {
     Email = anotherAccount.Email;
     FirstName = anotherAccount.FirstName;
     LastName = anotherAccount.LastName;
     Birthday = anotherAccount.Birthday;
     Phone = anotherAccount.Phone;
     AddressLine1 = anotherAccount.AddressLine1;
     AddressLine2 = anotherAccount.AddressLine2;
     City = anotherAccount.City;
     Province = anotherAccount.Province;
     PostalCode = anotherAccount.PostalCode;
     Country = anotherAccount.Country;
     PreferedContactMethod = anotherAccount.PreferedContactMethod;
     PreferedContactMethodCharity = anotherAccount.PreferedContactMethodCharity;
 }
        public async Task GetAccountInfo()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(c_serverBaseAddress);

                HttpResponseMessage response = null;
                try // should be more fine grain maybe !!
                {
                    if (DatabaseManager.Token == null)
                    {
                        throw new Exception("Token is null while trying to retrieve data!");
                    }

                    var url = c_userApiAddress + "?token_id=" + DatabaseManager.Token.Value;
                    response = await client.GetAsync(url);

                    var json = response.Content.ReadAsStringAsync().Result;

                    UserAccount = JsonConvert.DeserializeObject<UserAccount>(json);

                    // Translate from code to full name
                    var country = DatabaseManager.DbConnection.Table<Country>().Where(x => x.CountryCode == UserAccount.CountryCode).FirstOrDefault();
                    if (country != null)
                    {
                        UserAccount.Country = country.CountryName;
                    }

                    var province = DatabaseManager.DbConnection.Table<Province>().Where(x => x.ProvinceAbbreviation == UserAccount.ProvinceAbbreviation).FirstOrDefault();
                    if (province != null)
                    {
                        UserAccount.Province = province.ProvinceName;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("Error while retrieving account info:" + e.Message);
                }
            }
        }
        public RegistrationPage(bool isUpdate, Page parent, UserAccount userAccount = null)
        {
            InitializeComponent();
            if (!isUpdate)
            {
                Title = "Registration";
            }
            else
            {
                Title = "Update Information";
            }
            _tableView.Intent = TableIntent.Menu;

            if (userAccount == null)
            {
                _viewModel = new AccountInfoViewModel();
            }
            else
            {
                _viewModel = new AccountInfoViewModel(userAccount);
            }
            this.BindingContext = _viewModel;

            #region Birthday
            var birthdayCell = new ViewCell();
            _birthdaySection.Add(birthdayCell);

            var birthdayLayout = new StackLayout
            {
                Padding = new Thickness(10, 0, 10, 0),
            };
            birthdayCell.View = birthdayLayout;

            var datePicker = new DatePicker
            {
                Format = "MMM d yyyy",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };
            birthdayLayout.Children.Add(datePicker);
            datePicker.SetBinding(DatePicker.DateProperty, "UserAccount.Birthday", BindingMode.TwoWay);
            #endregion

            #region Province
            var provinceCellLayout = new StackLayout
            {
                Padding = new Thickness(10, 0, 10, 0),
            };
            _provinceCell.View = provinceCellLayout;

            var provincePicker = new Picker
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };
            provinceCellLayout.Children.Add(provincePicker);

            var firstCountry = DatabaseManager.DbConnection.Table<Country>().First();
            var provinces = DatabaseManager.DbConnection.Table<Province>().Where(x => x.CountryCode == firstCountry.CountryCode).ToList();
            foreach (var province in provinces)
            {
                provincePicker.Items.Add(province.ProvinceName);
            }
            provincePicker.SetBinding(Picker.SelectedIndexProperty, new Binding("UserAccount.Province", BindingMode.TwoWay, new PickerProvinceToIndexConverter(), provinces));
            #endregion

            #region Country
            var countryCellLayout = new StackLayout
            {
                Padding = new Thickness(10, 0, 10, 0),
            };
            _countryCell.View = countryCellLayout;

            var countryPicker = new Picker
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };
            countryCellLayout.Children.Add(countryPicker);

            var countries = DatabaseManager.DbConnection.Table<Country>().ToList();
            foreach (var country in countries)
            {
                countryPicker.Items.Add(country.CountryName);
            }
            countryPicker.SetBinding(Picker.SelectedIndexProperty, new Binding("UserAccount.Country", BindingMode.TwoWay, new PickerCountryToIndexConverter(), countries));

            countryPicker.SelectedIndexChanged += (sender, e) =>
            {
                provincePicker.Items.Clear();

                var countryCode = countries[countryPicker.SelectedIndex].CountryCode;
                var newProvinces = DatabaseManager.DbConnection.Table<Province>().Where(x => x.CountryCode == countryCode).ToList();

                // If there is no province, use N/A
                if ((newProvinces == null) || (newProvinces.Count == 0))
                {
                    newProvinces = new List<Province>
                    {
                        new Province
                        {
                            ProvinceName = "N/A",
                        }
                    };
                }

                foreach (var province in newProvinces)
                {
                    provincePicker.Items.Add(province.ProvinceName);
                }
                provincePicker.SetBinding(Picker.SelectedIndexProperty, new Binding("UserAccount.Province", BindingMode.TwoWay, new PickerProvinceToIndexConverter(), newProvinces));
            };
            #endregion

            #region Raffle Result
            var raffleResultsViewCell = new ViewCell();
            _raffleResultsSection.Add(raffleResultsViewCell);

            var raffleResultsLayout = new StackLayout
            {
                Padding = new Thickness(10, 0, 10, 0),
            };
            raffleResultsViewCell.View = raffleResultsLayout;

            var raffleResultsPicker = new Picker
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };
            raffleResultsLayout.Children.Add(raffleResultsPicker);

            foreach (var item in c_contactMethods)
            {
                raffleResultsPicker.Items.Add(item);
            }
            raffleResultsPicker.SetBinding(Picker.SelectedIndexProperty, new Binding("UserAccount.PreferedContactMethod", BindingMode.TwoWay, new PickerContactMethodsConverter()));
            #endregion

            #region Charity Message
            var charityMessagesViewCell = new ViewCell();
            _charityMessagesSection.Add(charityMessagesViewCell);

            var charityMessagesLayout = new StackLayout
            {
                Padding = new Thickness(10, 0, 10, 0),
            };
            charityMessagesViewCell.View = charityMessagesLayout;

            var charityMessagesPicker = new Picker
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };
            charityMessagesLayout.Children.Add(charityMessagesPicker);

            foreach (var item in c_contactMethods)
            {
                charityMessagesPicker.Items.Add(item);
            }
            charityMessagesPicker.SetBinding(Picker.SelectedIndexProperty, new Binding("UserAccount.PreferedContactMethodCharity", BindingMode.TwoWay, new PickerContactMethodsConverter()));
            #endregion

            if (!isUpdate)
            {
                var createAccountButtonViewCell = new ViewCell();
                _createAccountButtonSection.Add(createAccountButtonViewCell);

                var createAccountButtonLayout = new StackLayout
                {
                    Padding = new Thickness(5, 0, 5, 0),
                    BackgroundColor = Color.Accent,
                };
                createAccountButtonViewCell.View = createAccountButtonLayout;

                var createAccountButton = new Button
                {
                    Text = "Create Account",
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    TextColor = Color.White,
                };
                createAccountButtonLayout.Children.Add(createAccountButton);

                createAccountButton.Clicked += async (sender, e) =>
                {
                    var result = await _viewModel.CreateAccount();
                    if (result.Item1)
                    {
                        this.Navigation.PopAsync();
                        var answer = await DisplayAlert("Register Phone Number", "Send registration code to your phone now?", "Later", "Yes");
                        if (!answer) // use !answer because the negative choice has a bigger font
                        {
                            parent.Navigation.PushAsync(new VerifyPhonePage(_viewModel.UserAccount.Email, _viewModel.UserAccount.Phone, _viewModel.UserAccount.CountryCode)); //Country code is updated after calling _viewModel.CreateAccount()
                        }
                    }
                    else
                    {
                        DisplayAlert("Server request failed", "", "OK");
                    }
                };
            }
            else
            {
                // Edit Account Page
                _toolbarItemIsEnabled = true;

                _onDoneButtonClicked = new Command(async () =>
                    {
                        if (_toolbarItemIsEnabled)
                        {
                            if (_viewModel.InfoHasNotChanged())
                            {
                                DisplayAlert("Warning", "Your information has not changed.", "Retry");
                            }
                            else
                            {
                                _toolbarItemIsEnabled = false; // Disable the button while await
                                var result = await _viewModel.UpdateAccountInfo();
                                _toolbarItemIsEnabled = true;
                                if (result.Item1)
                                {
                                    this.Navigation.InsertPageBefore(new AccountInfoPage(), parent);
                                    this.Navigation.PopAsync(false);
                                    this.Navigation.PopAsync(false);
                                }
                                else
                                {
                                    DisplayAlert("Error", result.Item2, "Retry");
                                }
                            }
                        }
                    });

                var toolbarItem = new ToolbarItem
                {
                    Text = "Done",
                    Command = _onDoneButtonClicked,
                };
                this.ToolbarItems.Add(toolbarItem);
            }
        }
Beispiel #4
0
        // Should override Equals instead!! -> Later
        public static bool Equals(UserAccount first, UserAccount second)
        {
            var rs = (first.Email == second.Email)
                     && (first.FirstName == second.FirstName)
                     && (first.LastName == second.LastName)
                     && (first.Birthday.Date == second.Birthday.Date)// Ignore hour:minute is important. Date picker for some reasons use 12:00:00 AM while the default value after deserialization is 06:00:00 AM
                     && (first.Phone == second.Phone)
                     && (first.AddressLine1 == second.AddressLine1)
                     && (first.AddressLine2 == second.AddressLine2)
                     && (first.City == second.City)
                     && (first.Province == second.Province)
                     && (first.PostalCode == second.PostalCode)
                     && (first.Country == second.Country)
                     && (first.PreferedContactMethod == second.PreferedContactMethod)
                     && (first.PreferedContactMethodCharity == second.PreferedContactMethodCharity);

            return rs;
        }