protected internal void SincronizarClick(object sender, EventArgs e)
        {
            CloudData = new CloudDataStore();
            db        = new DataBase();
            TextView TVResult = FindViewById <TextView>(Resource.Id.TVResultado);

            ProgressDialog pbar = new ProgressDialog(this);

            pbar.SetCancelable(false);
            pbar.SetMessage("Sincronizando dados do sistema...");
            pbar.SetProgressStyle(ProgressDialogStyle.Horizontal);
            pbar.Progress = 0;
            pbar.Max      = 100;
            pbar.Show();

            new Thread(new ThreadStart(async delegate
            {
                // Baixa o conteúdo da Web pro App;
                if (await CloudData.BaixarPlantio(null))
                {
                    pbar.Progress += 5;
                }

                if (await CloudData.BaixarInstalacao(null))
                {
                    pbar.Progress += 5;
                }

                if (await CloudData.BaixarManutencao(null))
                {
                    pbar.Progress += 10;
                }

                if (await CloudData.BaixarPlanejamentoAplic(null) &&
                    await CloudData.BaixarAplicacao(null))
                {
                    pbar.Progress += 10;
                }

                if (await CloudData.BaixarPlanejamentoAval(null) &&
                    await CloudData.BaixarAvaliacao(null))
                {
                    pbar.Progress += 10;
                }

                if (await CloudData.MunicipiosSync(null) &&
                    await CloudData.LocalidadeSync(null))
                {
                    pbar.Progress += 5;
                    if (await CloudData.UsuarioSync(null))
                    {
                        pbar.Progress += 5;
                    }
                }
                if (await CloudData.BaixarCultura(null) &&
                    await CloudData.BaixarEquipamento(null))
                {
                    pbar.Progress += 10;
                }
                if (await CloudData.BaixarEstudos(null) &&
                    await CloudData.BaixarAplicPlan(null) &&
                    await CloudData.BaixarAvalPlan(null))
                {
                    pbar.Progress += 5;
                }
                if (await CloudData.BaixarVariedade(null) &&
                    await CloudData.BaixarTipoAvaliacao(null))
                {
                    pbar.Progress += 10;
                }
                if (await CloudData.BaixarSafra(null) &&
                    await CloudData.BaixarAlvo(null))
                {
                    pbar.Progress += 5;
                }
                if (await CloudData.BaixarUmidade(null) &&
                    await CloudData.BaixarGleba(null))
                {
                    pbar.Progress += 5;
                }
                if (await CloudData.BaixarProdutos(null) &&
                    await CloudData.BaixarBBCHEstagio(null) /* &&
                                                             * await CloudData.BaixarBBCH(null)*/)
                {
                    pbar.Progress += 5;
                }
                if (await CloudData.BaixarSolo(null) &&
                    await CloudData.BaixarCobertura(null))
                {
                    pbar.Progress += 5;
                }
                if (await CloudData.BaixarManutencaoTipo(null) &&
                    await CloudData.BaixarManutencaoObj(null) &&
                    await CloudData.BaixarUnidadeMedida(null))
                {
                    pbar.Progress += 5;
                }


                if (pbar.Progress >= 100)
                {
                    Thread.Sleep(800);
                    RunOnUiThread(() => {
                        TVResult.Text = "Sincronização efetuada com sucesso!";
                        TVResult.SetTextColor(Android.Graphics.Color.DarkGreen);
                    });
                    RunOnUiThread(() => { Toast.MakeText(this, "Dados importados com sucesso.", ToastLength.Long).Show(); });

                    pbar.Dismiss();
                }
                else
                {
                    RunOnUiThread(() => {
                        TVResult.Text = "Erro ao baixar os dados do servidor!";
                        TVResult.SetTextColor(Android.Graphics.Color.Red);
                    });

                    pbar.Dismiss();
                }


                RunOnUiThread(() => { pbar.SetMessage("Dados importados..."); });
            })).Start();
        }
Esempio n. 2
0
        void SaveButton_Click(object sender, EventArgs e)
        {
            CloudData = new CloudDataStore();
            db        = new DataBase();

            ProgressDialog pbar = new ProgressDialog(this);

            pbar.SetCancelable(false);
            pbar.SetMessage("Validando chave...");
            pbar.SetProgressStyle(ProgressDialogStyle.Horizontal);
            pbar.Progress = 0;
            pbar.Max      = 100;
            pbar.Show();

            bool sucesso = false;

            new Thread(new ThreadStart(async delegate
            {
                if (await CloudData.ValidarChave(licenca.Text))
                {
                    sucesso = true;
                }

                if (sucesso)
                {
                    RunOnUiThread(() => { Toast.MakeText(this, "Chave validada com sucesso.", ToastLength.Long).Show(); });
                    RunOnUiThread(() => { pbar.SetMessage("Sincronizando dados básicos..."); });
                    pbar.Progress += 25;
                    if (await CloudData.MunicipiosSync(licenca.Text) &&
                        await CloudData.LocalidadeSync(licenca.Text))
                    {
                        pbar.Progress += 25;
                        bool result    = await CloudData.UsuarioSync(licenca.Text);
                        if (result)
                        {
                            pbar.Progress += 25;
                            var conf2      = new Config
                            {
                                Descricao = "carga_inicial",
                                Valor     = "1"
                            };
                            if (db.InserirConfig(conf2))
                            {
                                pbar.Progress += 25;
                            }
                        }
                    }

                    Thread.Sleep(400);

                    if (pbar.Progress >= 100)
                    {
                        var intent = new Intent(this, typeof(LoginActivity));;
                        StartActivity(intent);
                        Finish();
                        pbar.Dismiss();
                    }

                    RunOnUiThread(() => { pbar.SetMessage("Dados importados..."); });
                    RunOnUiThread(() => { Toast.MakeText(this, "Dados importados com sucesso.", ToastLength.Long).Show(); });
                    // SyncInstall();
                }
                else
                {
                    pbar.Dismiss();
                    RunOnUiThread(() => { Toast.MakeText(this, "Chave não encontrada.", ToastLength.Long).Show(); });
                }
            })).Start();



            //  Finish();
        }