Exemplo n.º 1
0
            public Code(Service service, TabbedPage tabs, Password password, UserInformation userInformation)
            {

                Action<Label, ActivationResult.ActivationType> setLabelText = (label, type) =>
                {
                    switch (type)
                    {
                        case ActivationResult.ActivationType.PhoneCall:
                            label.Text = Localize.GetString("TelegramCodeSentPhoneCall");
                            break;
                        case ActivationResult.ActivationType.Telegram:
                            label.Text = Localize.GetString("TelegramCodeSentTelegramApp");
                            break;
                        case ActivationResult.ActivationType.Text:
                            label.Text = Localize.GetString("TelegramCodeSentText");
                            break;
                        case ActivationResult.ActivationType.Unknown:
                            label.Text = "";
                            break;
                    }
                };

                Action<Label, ActivationResult.ActivationType> setLabelNextText = (label, type) =>
                {
                    switch (type)
                    {
                        case ActivationResult.ActivationType.PhoneCall:
                            label.Text = Localize.GetString("TelegramCodeResendViaPhoneCall");
                            break;
                        case ActivationResult.ActivationType.Text:
                            label.Text = Localize.GetString("TelegramCodeResendViaText");
                            break;
                        case ActivationResult.ActivationType.Unknown:
                            label.Text = "";
                            break;
                    }
                };

                Action<bool> doVerify = async (reVerify) =>
                {
                    _error.IsVisible = false;
                    _progressBar.IsVisible = true;
                    _code.IsEnabled = false;
                    _submit.IsEnabled = false;
                    _verify.IsEnabled = false;

                    tabs.IsEnabled = false;
                    DependencyService.Get<IPluginPageControls>().BackPressEnabled = false;

                    var result = await DoVerify(reVerify);

                    setLabelText(_label, result.CurrentType);

                    setLabelNextText(_label2, result.NextType);

                    _error.IsVisible = true;
                    _progressBar.IsVisible = false;
                    _code.IsEnabled = true;
                    _submit.IsEnabled = true;
                    if(result.NextType != ActivationResult.ActivationType.Unknown)
                        _verify.IsEnabled = true;
                    
                    tabs.IsEnabled = true;
                    DependencyService.Get<IPluginPageControls>().BackPressEnabled = true;

                    if (!result.Success)
                    {
                        _error.IsVisible = true;
                        _error.Text = result.ErrorMessage;
                        return;
                    }

                };

                _service = service;

                _label = new Label();
                _label.Font = Font.SystemFontOfSize(18);
                _label.XAlign = TextAlignment.Center;
                _label.Text = Localize.GetString("TelegramCodeIntitalText");

                _label2 = new Label();
                _label2.Font = Font.SystemFontOfSize(18);
                _label2.XAlign = TextAlignment.Center;

                _code = new Entry();
                _code.Placeholder = Localize.GetString("TelegramCode");
                _code.IsEnabled = false;

                _verify = new Button();
                _verify.Text = Localize.GetString("TelegramVerify");
                _verify.TextColor = Color.White;
                _verify.BackgroundColor = Color.FromHex("77D065");
                _verify.Clicked += (sender, e) => 
                {
                    if (_verificationAttempt == 0)
                    {
                        doVerify(false);
                    }
                    else
                    {
                        doVerify(true);
                    }
                    _verificationAttempt++;
                }; 


                _submit = new Button();
                _submit.Text = Localize.GetString("TelegramSubmit");
                _submit.TextColor = Color.White;
                _submit.BackgroundColor = Color.FromHex("77D065");
                _submit.IsEnabled = false;
                _submit.Clicked += async (sender, e) =>
                    {
                        Func<Task> invalidCode = () =>
                        {
                            return DisplayAlert(Localize.GetString("TelegramCodeInvalidTitle"),
                                Localize.GetString("TelegramCodeInvalidMessage"), Localize.GetString("TelegramOkay"));
                        };

                        if (String.IsNullOrWhiteSpace(_code.Text))
                        {
                            await invalidCode();
                            return;
                        }

                        var code = new string(_code.Text.Where(Char.IsDigit).ToArray());

                        if (String.IsNullOrWhiteSpace(code))
                        {
                            await invalidCode();
                            return;
                        }

                        _progressBar.IsVisible = true;
                        _error.IsVisible = false;

                        if (!GetSettingsRegistered(NationalNumber))
                        {
                            userInformation.CountryCode = CountryCode;
                            userInformation.NationalNumber = NationalNumber;
                            userInformation.Code = code;
                            tabs.Children.Add(userInformation);
                            tabs.CurrentPage = userInformation;
                            return;
                        }

                        DependencyService.Get<IPluginPageControls>().BackPressEnabled = false;
                        _code.IsEnabled = false;
                        _submit.IsEnabled = false;
                        tabs.IsEnabled = false;

                        var result = await Register(service, NationalNumber, CountryCode, code);

                        DependencyService.Get<IPluginPageControls>().BackPressEnabled = true;
                        _code.IsEnabled = true;
                        _submit.IsEnabled = true;
                        tabs.IsEnabled = true;

                        _progressBar.IsVisible = false;

                        if (!result.Success && result.PasswordNeeded)
                        {
                            //add a tab for the password selection
                            password.CountryCode = CountryCode;
                            password.NationalNumber = NationalNumber;
                            password.Code = code;
                            password.SetFields(result.PasswordInformation);
                            tabs.Children.Add(password);
                            tabs.CurrentPage = password;
                            return;
                        }

                        if (!result.Success)
                        {
                            _error.IsVisible = true;
                            _error.Text = result.ErrorMessage;
                            return;
                        }

                        Save(service, result.AccountId, GetSettingsTelegramSettings(NationalNumber));
                        DependencyService.Get<IPluginPageControls>().Finish();
                    };


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

                _error = new Label();
                _error.VerticalOptions = LayoutOptions.EndAndExpand;
                _error.IsVisible = false;
                _error.Font = Font.SystemFontOfSize(18);
                _error.XAlign = TextAlignment.Center;
                _error.TextColor = Color.Red;

                var stackLayout = new StackLayout();
                stackLayout.Spacing = 20;
                stackLayout.Padding = 25;
                stackLayout.VerticalOptions = LayoutOptions.Start;
                var children = stackLayout.Children;
                children.Add(_label);
                children.Add(_verify);
                children.Add(_label2);
                children.Add(_code);
                children.Add(_submit);
                children.Add(_progressBar);
                children.Add(_error);

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