예제 #1
0
            public Info(Service service, TabbedPage tabs, Verify verify)
            {
                _phoneNumberContainer                   = new StackLayout();
                _phoneNumberContainer.Orientation       = StackOrientation.Horizontal;
                _phoneNumberContainer.HorizontalOptions = LayoutOptions.FillAndExpand;
                _phoneNumber                   = new PhoneEntry();
                _phoneNumber.Placeholder       = Localize.GetString("TelegramPhoneNumber");
                _phoneNumber.HorizontalOptions = LayoutOptions.FillAndExpand;
                _phoneNumberContainer.Children.Add(_phoneNumber);
                var programmaticChange = false;

                _firstName             = new Entry();
                _firstName.Placeholder = Localize.GetString("TelegramFirstName");

                _lastName             = new Entry();
                _lastName.Placeholder = Localize.GetString("TelegramLastName");

                _loadConversations                 = new CheckBox();
                _loadConversations.DefaultText     = Localize.GetString("TelegramLoadConversations");
                _loadConversations.CheckedChanged += (sender, e) =>
                {
                    //TODO:
                };
                _loadConversations.Checked = true;

                _next = new Button();
                _next.HorizontalOptions = LayoutOptions.FillAndExpand;
                _next.Text            = Localize.GetString("TelegramNext");
                _next.TextColor       = Color.White;
                _next.BackgroundColor = Color.FromHex("77D065");
                _next.Clicked        += async(sender, e) =>
                {
                    if (String.IsNullOrWhiteSpace(_firstName.Text))
                    {
                        await DisplayAlert(Localize.GetString("TelegramInvalidFirstNameTitle"), Localize.GetString("TelegramInvalidFirstNameMessage"), Localize.GetString("TelegramOkay"));

                        return;
                    }

                    if (String.IsNullOrWhiteSpace(_lastName.Text))
                    {
                        await DisplayAlert(Localize.GetString("TelegramInvalidLastNameTitle"), Localize.GetString("TelegramInvalidLastNameMessage"), Localize.GetString("TelegramOkay"));

                        return;
                    }

                    Func <Task> invalidNumber = () =>
                    {
                        return(DisplayAlert(Localize.GetString("TelegramInvalidNumberTitle"),
                                            Localize.GetString("TelegramInvalidNumberMessage"), Localize.GetString("TelegramOkay")));
                    };

                    if (!PhoneBook.IsPossibleNumber(_phoneNumber.Text))
                    {
                        await invalidNumber();

                        return;
                    }

                    var number          = PhoneBook.TryGetPhoneNumberLegible(_phoneNumber.Text);
                    var formattedNumber = PhoneBook.FormatPhoneNumber(number);

                    if (formattedNumber == null)
                    {
                        await invalidNumber();

                        return;
                    }

                    var nationalNumber = new string(formattedNumber.Item2.Where(Char.IsDigit).ToArray());

                    if (!await DisplayAlert(Localize.GetString("TelegramConfirmNumberTitle"),
                                            Localize.GetString("TelegramConfirmNumberMessage").Replace("[number]", number),
                                            Localize.GetString("TelegramYes"),
                                            Localize.GetString("TelegramNo")))
                    {
                        return;
                    }

                    _progressBar.IsVisible = true;
                    _next.IsEnabled        = false;
                    _firstName.IsEnabled   = false;
                    _lastName.IsEnabled    = false;
                    _phoneNumber.IsEnabled = false;
                    DependencyService.Get <IPluginPageControls>().BackPressEnabled = false;

                    TelegramSettings settings = null;

                    var skipSave = false;
                    var state    = _settings.States.FirstOrDefault(x => x.NationalNumber == nationalNumber);
                    if (state != null && state.Settings != null)
                    {
                        skipSave = true;
                        settings = state.Settings;
                    }
                    else
                    {
                        settings = await Task <TelegramSettings> .Factory.StartNew(() => { return(Telegram.GenerateAuthentication(service)); });
                    }

                    var firstName = _firstName.Text.Trim();
                    var lastName  = _lastName.Text.Trim();

                    DependencyService.Get <IPluginPageControls>().BackPressEnabled = true;
                    _firstName.IsEnabled   = true;
                    _lastName.IsEnabled    = true;
                    _phoneNumber.IsEnabled = true;
                    _next.IsEnabled        = true;
                    _progressBar.IsVisible = false;

                    if (settings == null)
                    {
                        await DisplayAlert(Localize.GetString("TelegramAuthGenerationFailedTitle"),
                                           Localize.GetString("TelegramAuthGenerationFailedMessage"), Localize.GetString("TelegramOkay"));

                        return;
                    }

                    if (!skipSave)
                    {
                        _settings.States.Add(new Setup.TelegramSetupSettings.State
                        {
                            Settings       = settings,
                            NationalNumber = nationalNumber
                        });
                        SaveSettings();
                    }

                    verify.CountryCode    = formattedNumber.Item1;
                    verify.NationalNumber = nationalNumber;
                    verify.FirstName      = firstName;
                    verify.LastName       = lastName;
                    tabs.Children.Add(verify);
                    tabs.CurrentPage = verify;
                };

                _image        = new Image();
                _image.Source = ImageSource.FromUri(
                    new Uri("https://lh4.ggpht.com/fuvTtxbZ1-dkEmzUMfKcgMJwW8PyY4fhJJ_NKT-NpIQJukszEY2GfCkJUF5ch6Co3w=w300"));
                _image.WidthRequest  = 100;
                _image.HeightRequest = 100;

                _progressBar = new ActivityIndicator();
                _progressBar.VerticalOptions = LayoutOptions.EndAndExpand;
                _progressBar.IsRunning       = true;
                _progressBar.IsVisible       = false;

                var stackLayout = new StackLayout();

                stackLayout.Spacing         = 20;
                stackLayout.Padding         = 25;
                stackLayout.VerticalOptions = LayoutOptions.Start;
                var children = stackLayout.Children;

                children.Add(_image);
                children.Add(_firstName);
                children.Add(_lastName);
                children.Add(_phoneNumberContainer);
                children.Add(_loadConversations);
                var nextLayout = new StackLayout();

                nextLayout.Spacing     = 20;
                nextLayout.Orientation = StackOrientation.Horizontal;
                nextLayout.Children.Add(_next);
                nextLayout.Children.Add(_progressBar);
                children.Add(nextLayout);

                Content = new ScrollView {
                    Content = stackLayout
                };
                Title = Localize.GetString("TelegramInformationTitle");
            }