适配器FireStore中的ProgressDialog

时间:2017-10-24 16:16:15

标签: java android android-recyclerview recycler-adapter google-cloud-firestore

问题:

我有一个屏幕,显示某个用户的日程安排。

但与此同时,我需要检查是否已经占用了任何时间表,并将背景颜色设置为较暗的颜色。

打印1:带有计划的RecyclerView屏幕。

打印2:它是相同的屏幕,但在检查可用的时间表后。

注意:如果我们在Wi-Fi网络上,充电速度很快,但如果是移动互联网,则速度非常慢。

Print 1 Print 2

我需要一些方法,在我的适配器中添加一个ProgressDialog,正好在检查时间表的那一刻。

在这里:

/*Checar Horario disponivel*/
            final FirebaseFirestore mDB = FirebaseFirestore.getInstance();
            CollectionReference mDBHorarios = mDB.collection("Empresas").document(empresa).collection("Agendamentos");

            mDBHorarios.whereEqualTo("profissional_id", ref.getId_Prof()).whereEqualTo("agenda_hora", ref.getHora())
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                for (DocumentSnapshot document : task.getResult()) {

                                    Agendamentos doc = document.toObject(Agendamentos.class);

                                    String horaAgenda = doc.getAgenda_hora();
                                    /*Cor do fundo*/
                                    if ( horaAgenda.equals(ref.getHora()) ){

                                        fundoHora.setBackgroundColor(Color.parseColor("#E0E0E0"));
                                        statusHoraAgenda = true;


                                    }

                                }
                            } else {

                            }
                        }
                    });

注意:这可能不是一个好方法。我接受建议,如果有更好的方法在RecyclerView中执行此查询。

代码已满 - 适配器:

public class HorariosProfissionalAdapter extends FirestoreAdapter<HorariosProfissionalAdapter.ViewHolder>{

    public interface OnHorariosProfissionaisListner {

        void onHorarioProfissionaisSelected(DocumentSnapshot horariosProfissional);

    }

    private OnHorariosProfissionaisListner mListner;

    public HorariosProfissionalAdapter(Query query, OnHorariosProfissionaisListner listner){

        super(query);
        mListner = listner;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        return new ViewHolder(inflater.inflate(R.layout.card_horarios_profissional, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(getSnapshot(position), mListner);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private Boolean statusHoraAgenda = false;

        @BindView(R.id.tvHoraProf)
        TextView hora;

        @BindView(R.id.fundoHorario)
        LinearLayout fundoHora;





        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        public void bind(final DocumentSnapshot snapshot,
                         final OnHorariosProfissionaisListner listener) {

            final HorariosProfissionais ref = snapshot.toObject(HorariosProfissionais.class);
            String empresa = ref.getId_Emp();


            /*Checar Horario disponivel*/
            final FirebaseFirestore mDB = FirebaseFirestore.getInstance();
            CollectionReference mDBHorarios = mDB.collection("Empresas").document(empresa).collection("Agendamentos");

            mDBHorarios.whereEqualTo("profissional_id", ref.getId_Prof()).whereEqualTo("agenda_hora", ref.getHora())
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                for (DocumentSnapshot document : task.getResult()) {

                                    Agendamentos doc = document.toObject(Agendamentos.class);

                                    String horaAgenda = doc.getAgenda_hora();
                                    /*Cor do fundo*/
                                    if ( horaAgenda.equals(ref.getHora()) ){

                                        fundoHora.setBackgroundColor(Color.parseColor("#E0E0E0"));
                                        statusHoraAgenda = true;


                                    }

                                }
                            } else {

                            }
                        }
                    });



            hora.setText(ref.getHora());


            // Click listener
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null && !statusHoraAgenda) {
                        listener.onHorarioProfissionaisSelected(snapshot);
                    }
                }
            });
        }

    }
}

代码已满 - 活动:

public class Agenda_profissionais extends AppCompatActivity implements HorariosProfissionalAdapter.OnHorariosProfissionaisListner {

    private String mId_Empresa = null;
    private String mId_Profissional = null;
    private String mNome_Profissional = null;

    private RecyclerView mCardHorarios;
    private HorariosProfissionalAdapter mAdapter;
    private FirebaseFirestore mFirestore;
    private Query mQuery;

    private ProgressDialog mProgress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agenda_profissionais);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Horario");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        /* CONFIGURAÇÃO TOOLBAR*/
        Bundle extra = getIntent().getExtras();
        if ( extra != null ){

            mId_Empresa = extra.getString("id_empresa");
            mId_Profissional = extra.getString("id_profissional");
            mNome_Profissional = extra.getString("nome_profissional");

        }



        /*Firebase*/
        mFirestore = FirebaseFirestore.getInstance();
        mQuery = mFirestore.collection("Empresas").document(mId_Empresa).collection("Profissionais").document(mId_Profissional).collection("Horarios");

        mCardHorarios = (RecyclerView) findViewById(R.id.cardListaHorariosProf);

        mAdapter = new HorariosProfissionalAdapter(mQuery, this){

            @Override
            protected void onDataChanged() {

                if (getItemCount() == 0) {
                    mCardHorarios.setVisibility(View.GONE);
                    //mTxtVazio.setVisibility(View.VISIBLE);

                } else {
                    mCardHorarios.setVisibility(View.VISIBLE);


                }

            }
        };

        mCardHorarios.setLayoutManager(new LinearLayoutManager(this));
        mCardHorarios.setAdapter(mAdapter);


    }



    @Override
    public void onStart() {
        super.onStart();

        // Start listening for Firestore updates
        if (mAdapter != null) {
            mAdapter.startListening();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAdapter != null) {
            mAdapter.stopListening();
        }
    }

    @Override
    public void onHorarioProfissionaisSelected(DocumentSnapshot horario) {


        HorariosProfissionais idHorario = horario.toObject(HorariosProfissionais.class);
        Toast.makeText(Agenda_profissionais.this, "Horario selecionado: " +idHorario.getHora(), Toast.LENGTH_LONG).show();


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_agendamento, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onBackPressed() {

        finish();

    }

}

1 个答案:

答案 0 :(得分:0)

解决方案

public void bind(final DocumentSnapshot snapshot,
                         final OnHorariosProfissionaisListner listener) {

            final HorariosProfissionais ref = snapshot.toObject(HorariosProfissionais.class);
            String empresa = ref.getId_Emp();

            mProgress = new ProgressDialog(itemView.getContext());
            mProgress.setMessage("testando....");
            mProgress.show();

            /*Checar Horario disponivel*/
            final FirebaseFirestore mDB = FirebaseFirestore.getInstance();
            CollectionReference mDBHorarios = mDB.collection("Empresas").document(empresa).collection("Agendamentos");

            mDBHorarios.whereEqualTo("profissional_id", ref.getId_Prof()).whereEqualTo("agenda_hora", ref.getHora())
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {

                            if (task.isSuccessful()) {

                                for (DocumentSnapshot document : task.getResult()) {

                                    Agendamentos doc = document.toObject(Agendamentos.class);

                                    String horaAgenda = doc.getAgenda_hora();
                                    /*Cor do fundo*/
                                    if ( horaAgenda.equals(ref.getHora()) ){

                                        fundoHora.setBackgroundColor(Color.parseColor("#E0E0E0"));
                                        statusHoraAgenda = true;



                                    }

                                }

                                mProgress.dismiss();

                            } else {

                            }
                        }
                    });