Esempio n. 1
0
        public CadastroPage()
        {
            try
            {
                this.BindingContext = model = App.Container.Resolve<CadastroViewModel>();
                model.ConfiguraNavigation(this.Navigation);
                MessagingCenter.Subscribe<CategoriasPage,ICollection<Categoria>>(this, "gravarCategorias", (sender, arg) =>
                    {
                        this.model.AdicionaCategoriasSelecionadas(arg);
                        var _categorias = arg.Select(x => x.Nome).Aggregate((a, b) => a + ',' + b).TrimEnd(',');
                
                        if (_categorias.Length > 1)
                            this.btnCategorias.Text = String.Format("{0} Categorias selecionadas!", arg.Count);
                        else
                            this.btnCategorias.Text = String.Format("{0} Categoria selecionada!", arg.Count);
                    });
                
                var imgLogo = new Image
                {
                    Source = ImageSource.FromResource(RetornaCaminhoImagem.GetImagemCaminho("logo_mini.png")),
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                };
                
                sexoPicker = new PickerSexoMais();
                
                if (Device.OS == TargetPlatform.Android)
                    sexoPicker.Title = "Sexo";
                else if (Device.OS == TargetPlatform.iOS)
                {
                    sexoPicker.Focused += (sender, e) =>
                    {
                        ((PickerSexoMais)sender).Items.Add("Selecione uma opção");
                        ((PickerSexoMais)sender).Items.Add(AppResources.TextoSexoMasculino);
                        ((PickerSexoMais)sender).Items.Add(AppResources.TextoSexoFeminino);
                    };
                }
                sexoPicker.SetBinding<CadastroViewModel>(PickerSexoMais.SelectedIndexProperty, x => x.Usuario.Sexo, BindingMode.Default,
                    new SexoValueConverter(), null);
                
                var nascimentoPicker = new DatePickerMais();
                nascimentoPicker.SetBinding(DatePickerMais.DateProperty, "Usuario.DataNascimento");
                
                var entEmail = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderEmailCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                entEmail.SetBinding<CadastroViewModel>(Entry.TextProperty, x => x.Usuario.Email);

                
                entNome = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderNomeCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                entNome.SetBinding<CadastroViewModel>(Entry.TextProperty, x => x.Usuario.Nome);

                
                var entSenha = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderSenhaCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    IsPassword = true
                };
                entSenha.SetBinding<CadastroViewModel>(Entry.TextProperty, x => x.Usuario.Senha);

                
                var entDDD = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderDDDCadastro,
                };
                entDDD.SetBinding<CadastroViewModel>(Entry.TextProperty, x => x.Usuario.DDD);

                
                var entTelefone = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderTelefoneCadastro
                };
                entTelefone.SetBinding<CadastroViewModel>(Entry.TextProperty, x => x.Usuario.Telefone);

                
                var entMunicipio = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderMunicipioCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                entMunicipio.SetBinding<CadastroViewModel>(Entry.TextProperty, x => x.Usuario.Municipio);

                
                btnCategorias = new Button
                { 
                    Text = AppResources.TextoPlaceHolderCategoriasCadastro, 
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Style = Estilos._estiloPadraoButtonFonteMenor
                };
                btnCategorias.Clicked += async (sender, e) =>
                {
                    if (model != null && model.Usuario != null && (model.Usuario.Categorias != null && model.Usuario.Categorias.Any() || !String.IsNullOrEmpty(model.Usuario.CategoriaMobileSelection)))
                    {
                        var dbCategorias = new Repositorio<Categoria>();
                        var categorias = new List<Categoria>();
                
                        foreach (var item in model.Usuario.CategoriaMobileSelection.Split(';'))
                        {
                            var catId = Convert.ToInt32(item);
                            var c = await dbCategorias.RetornarPorId(catId);
                            categorias.Add(c);
                        }
                
                        await this.Navigation.PushModalAsync(new CategoriasPage(categorias));
                    }
                    else
                        await this.Navigation.PushModalAsync(new CategoriasPage(null));
                };
                
                var gridWrapSexoNascimento = new Grid();
                gridWrapSexoNascimento.Children.Add(sexoPicker, 0, 0);
                gridWrapSexoNascimento.Children.Add(nascimentoPicker, 1, 0);
                
                var gridWrapDDDTelefone = new Grid();
                gridWrapDDDTelefone.Children.Add(entDDD, 0, 0);
                gridWrapDDDTelefone.Children.Add(entTelefone, 1, 0);
                
                var btnCriar = new Button
                {
                    Style = Estilos._estiloPadraoButton,
                    Text = AppResources.TextoBotaoCriarConta,
                    HorizontalOptions = LayoutOptions.End,
                    VerticalOptions = LayoutOptions.Start
                };
                //btnCriar.SetBinding<CadastroViewModel>(Button.CommandProperty, x => x.btnCadastrar_Click);
                btnCriar.Clicked += async (sender, e) =>
                {
                    var _sexo = sexoPicker.SelectedIndex;
                    var _nascimento = nascimentoPicker.Date;
                    var _email = entEmail.Text;
                    var _nome = entNome.Text;
                    var _ddd = entDDD.Text;
                    var _tel = entTelefone.Text;
                    var _municipio = entMunicipio.Text;
                    var _senha = entSenha.Text;

                    await this.model.EfetuarCadastro(_sexo, _nascimento, _email, _nome, _ddd, _tel, _municipio, _senha);
                    
                };
                
                var btnVoltar = new Button
                {
                    Style = Estilos._estiloPadraoButton,
                    Text = AppResources.TextoBotaoVoltarTelaLogin,
                    HorizontalOptions = LayoutOptions.Start,
                    VerticalOptions = LayoutOptions.Start
                };
                //btnVoltar.SetBinding<CadastroViewModel>(Button.CommandProperty, x => x.btnVoltar_Click);
                btnVoltar.Clicked += async (sender, e) => await this.model.Voltar();
                
                var gridWrapButtons = new Grid
                {
                    ColumnDefinitions =
                    {
                        new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) },
                        new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) }
                    },
                    RowDefinitions =
                    {
                        new RowDefinition { Height = GridLength.Auto }
                    }
                };
                gridWrapButtons.Children.Add(btnVoltar, 0, 0);
                gridWrapButtons.Children.Add(btnCriar, 1, 0);
                
                var mainLayout = 
                    new StackLayout
                    {
                        Children =
                        { 
                            imgLogo,
                            gridWrapSexoNascimento,
                            btnCategorias,
                            entEmail,
                            entNome,
                            entSenha,
                            gridWrapDDDTelefone,
                            entMunicipio,
                            gridWrapButtons
                        },
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        Spacing = 5,
                        Padding = new Thickness(5, 50, 5, 50)
                    };
                
                this.Content = new ScrollView { Content = mainLayout };
            }
            catch (Exception ex)
            {
                Insights.Report(ex);
            }
        }
        public CadastroEdicaoPage()
        {
            try
            {
                this.BindingContext = model = App.Container.Resolve<CadastroViewModel>();
                model.ConfiguraNavigation(this.Navigation);
                MessagingCenter.Subscribe<CategoriasPage,ICollection<Categoria>>(this, "gravarCategorias", (sender, arg) =>
                    {
                        this.model.AdicionaCategoriasSelecionadas(arg);
                
                        if (arg.Count > 1)
                            this.btnCategorias.Text = String.Format("{0} Categorias selecionada", arg.Count);
                        else
                            this.btnCategorias.Text = String.Format("{0} Categoria selecionadas", arg.Count);
                    });
                
                var imgLogo = new Image
                {
                    Source = ImageSource.FromResource(RetornaCaminhoImagem.GetImagemCaminho("logo_mini.png")),
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                };
                
                sexoPicker = new PickerSexoMais();
                
                if (Device.OS == TargetPlatform.Android)
                    sexoPicker.Title = "Sexo";
                
                sexoPicker.SetBinding<CadastroViewModel>(PickerSexoMais.SelectedIndexProperty, x => x.Usuario.Sexo, BindingMode.Default,
                    new SexoValueConverter(), null);
                
                var nascimentoPicker = new DatePickerMais();

                var entEmail = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderEmailCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                entEmail.SetBinding(Entry.TextProperty, "Email");
                
                var entNome = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderNomeCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                entNome.SetBinding(Entry.TextProperty, "Nome");
                
                var entDDD = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderDDDCadastro,
                };
                entDDD.SetBinding(Entry.TextProperty, "DDD");
                
                var entTelefone = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderTelefoneCadastro
                };
                entTelefone.SetBinding(Entry.TextProperty, "Telefone");
                
                var entMunicipio = new Entry
                { 
                    Placeholder = AppResources.TextoPlaceHolderMunicipioCadastro,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                entMunicipio.SetBinding(Entry.TextProperty, "Municipio");
                
                btnCategorias = new Button
                { 
                    Text = AppResources.TextoPlaceHolderCategoriasCadastro, 
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Style = Estilos._estiloPadraoButtonFonteMenor
                };

                dataNasc = new DatePicker();
                sexoPickerDois = new PickerSexoMais();

                gridWrapSexoNascimento = new Grid();
                gridWrapSexoNascimento.Children.Add(sexoPicker, 0, 0);
                gridWrapSexoNascimento.Children.Add(nascimentoPicker, 1, 0);

                gridWrapSexoNascimentoEdicao = new Grid();
                gridWrapSexoNascimentoEdicao.Children.Add(sexoPickerDois, 0, 0);
                gridWrapSexoNascimentoEdicao.Children.Add(dataNasc, 1, 0);

                var gridWrapDDDTelefone = new Grid();
                gridWrapDDDTelefone.Children.Add(entDDD, 0, 0);
                gridWrapDDDTelefone.Children.Add(entTelefone, 1, 0);
                
                var btnCriar = new Button
                {
                    Style = Estilos._estiloPadraoButton,
                    Text = "Atualizar",
                    HorizontalOptions = LayoutOptions.End,
                    VerticalOptions = LayoutOptions.Start
                };
                btnCriar.Clicked += async (sender, e) =>
                {
                    var result = await model.AtualizarCadastro(model.Usuario, this.Navigation, gridWrapSexoNascimento.IsVisible ? nascimentoPicker.Date : dataNasc.Date, gridWrapSexoNascimento.IsVisible ? sexoPicker.SelectedIndex : sexoPickerDois.SelectedIndex);
                
                    if (result)
                        await this.Navigation.PushModalAsync(new MainPage());
                };
                
                var gridWrapButtons = new Grid
                {
                    ColumnDefinitions =
                    {
                        new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) },
                        new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) }
                    },
                    RowDefinitions =
                    {
                        new RowDefinition { Height = GridLength.Auto }
                    }
                };
                //gridWrapButtons.Children.Add(btnVoltar, 0, 0);
                gridWrapButtons.Children.Add(btnCriar, 1, 0);
                
                var mainLayout = 
                    new StackLayout
                    {
                        Children =
                        { 
                            imgLogo,
                            gridWrapSexoNascimento,
                            gridWrapSexoNascimentoEdicao,
                            btnCategorias,
                            entEmail,
                            entNome,
                            gridWrapDDDTelefone,
                            entMunicipio,
                            gridWrapButtons
                        },
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        Spacing = 5,
                        Padding = new Thickness(5, 50, 5, 50)
                    };
                
                this.Content = new ScrollView { Content = mainLayout };
            }
            catch (Exception ex)
            {
                Insights.Report(ex);
            }
        }