Created
May 8, 2020 16:20
-
-
Save vinnom/84cea4b24e0fb76d232555d57659d240 to your computer and use it in GitHub Desktop.
Consigo editar o aluno, mas a lista não recupera o valor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.alura.agenda.ui.asynctask; | |
import android.os.AsyncTask; | |
import br.com.alura.agenda.model.Aluno; | |
import br.com.alura.agenda.room.AlunoDao; | |
public class AtualizaAlunosAsyncTask extends AsyncTask<Void, Void, Void> { | |
private final AlunoDao alunoDao; | |
private final Aluno aluno; | |
public AtualizaAlunosAsyncTask(AlunoDao alunoDao, Aluno aluno){ | |
this.alunoDao = alunoDao; | |
this.aluno = aluno; | |
} | |
@Override | |
protected Void doInBackground(Void... voids){ | |
alunoDao.atualiza(aluno); | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.alura.agenda.ui.asynctask; | |
import android.os.AsyncTask; | |
import java.util.List; | |
import br.com.alura.agenda.model.Telefone; | |
import br.com.alura.agenda.room.TelefoneDao; | |
public class AtualizaTelefonesAsyncTask extends AsyncTask<Void, Void, Void> { | |
private final TelefoneDao telefoneDao; | |
private final Telefone telefoneFixo; | |
private final Telefone telefoneCelular; | |
private final DefineTelefonesListener listener; | |
private final List<Telefone> telefones; | |
private final FinalizaAtividadeListener finalizaAtividadeListener; | |
public AtualizaTelefonesAsyncTask(TelefoneDao telefoneDao, | |
Telefone telefoneFixo, Telefone telefoneCelular, | |
List<Telefone> telefones, DefineTelefonesListener listener, | |
FinalizaAtividadeListener finalizaAtividadeListener){ | |
this.telefoneDao = telefoneDao; | |
this.telefoneFixo = telefoneFixo; | |
this.telefoneCelular = telefoneCelular; | |
this.telefones = telefones; | |
this.listener = listener; | |
this.finalizaAtividadeListener = finalizaAtividadeListener; | |
} | |
@Override | |
protected Void doInBackground(Void... voids){ | |
telefoneDao.atualiza(telefoneFixo, telefoneCelular); | |
listener.defineNumerosTelefone(telefones); | |
finalizaAtividadeListener.finaliza(); | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.alura.agenda.ui.activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import androidx.appcompat.app.AppCompatActivity; | |
import br.com.alura.agenda.R; | |
import br.com.alura.agenda.model.Aluno; | |
import br.com.alura.agenda.ui.activity.component.FormularioAlunoActivityComponent; | |
import static br.com.alura.agenda.ui.activity.ListaAlunosActivity.CHAVE_ALUNO; | |
public class FormularioAlunoActivity extends AppCompatActivity{ | |
private static final String TITULO_APPBAR_NOVO_ALUNO = "Novo Aluno"; | |
private static final String TITULO_APPBAR_EDITA_ALUNO = "Edita Aluno"; | |
private FormularioAlunoActivityComponent component; | |
@Override | |
protected void onCreate(Bundle savedInstanceState){ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_formulario_aluno); | |
component = new FormularioAlunoActivityComponent(this, this); | |
defineCampos(); | |
} | |
private void defineCampos(){ | |
component.setCampoNome(findViewById(R.id.activity_formulario_aluno_nome)); | |
component.setCampoTelefoneFixo(findViewById(R.id.activity_formulario_aluno_telefone_fixo)); | |
component.setCampoTelefoneCelular(findViewById(R.id.activity_formulario_aluno_telefone_celular)); | |
component.setCampoEmail(findViewById(R.id.activity_formulario_aluno_email)); | |
Intent dadosAluno = getIntent(); | |
defineFormularioEditaOuSalva(dadosAluno); | |
} | |
private void defineFormularioEditaOuSalva(Intent dadosAluno){ | |
if(dadosAluno.hasExtra(CHAVE_ALUNO)){ | |
setTitle(TITULO_APPBAR_EDITA_ALUNO); | |
component.recupera(dadosAluno); | |
} else{ | |
setTitle(TITULO_APPBAR_NOVO_ALUNO); | |
component.setAluno(new Aluno()); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu){ | |
getMenuInflater().inflate(R.menu.activity_formulario_aluno_menu, menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item){ | |
if(item.getItemId() == R.id.activity_formulario_aluno_menu_salvar){ | |
component.finalizaFormulario(); | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.alura.agenda.ui.activity.component; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.util.List; | |
import br.com.alura.agenda.model.Aluno; | |
import br.com.alura.agenda.model.Telefone; | |
import br.com.alura.agenda.room.AgendaBD; | |
import br.com.alura.agenda.room.AlunoDao; | |
import br.com.alura.agenda.room.TelefoneDao; | |
import br.com.alura.agenda.ui.asynctask.AtualizaAlunosAsyncTask; | |
import br.com.alura.agenda.ui.asynctask.AtualizaTelefonesAsyncTask; | |
import br.com.alura.agenda.ui.asynctask.RecuperaTelefonesAsyncTask; | |
import br.com.alura.agenda.ui.asynctask.SalvaAlunoAsyncTask; | |
import static br.com.alura.agenda.model.TipoTelefone.CELULAR; | |
import static br.com.alura.agenda.model.TipoTelefone.FIXO; | |
import static br.com.alura.agenda.ui.activity.ListaAlunosActivity.CHAVE_ALUNO; | |
public class FormularioAlunoActivityComponent { | |
private static final String AVISO_CAMPO_OBRIGATORIO = "Campo nome é obrigatório!"; | |
private final AlunoDao alunoDao; | |
private final TelefoneDao telefoneDao; | |
private EditText campoNome; | |
private EditText campoTelefoneFixo; | |
private EditText campoEmail; | |
private EditText campoTelefoneCelular; | |
private Aluno aluno; | |
private Activity activity; | |
private Context context; | |
private List<Telefone> telefones; | |
private String numeroFixo; | |
private String numeroCelular; | |
public FormularioAlunoActivityComponent(Activity activity, Context context){ | |
this.activity = activity; | |
this.context = context; | |
this.alunoDao = AgendaBD.getInstance(context).getRoomAlunoDao(); | |
this.telefoneDao = AgendaBD.getInstance(context).getTelefoneDao(); | |
} | |
public void finalizaFormulario(){ | |
defineAluno(); | |
if(aluno.naoEhNulo()){ | |
if(aluno.temIdValido()){ | |
atualizaAluno(); | |
} else{ | |
salvaAluno(); | |
} | |
} | |
Toast.makeText(context, AVISO_CAMPO_OBRIGATORIO, Toast.LENGTH_SHORT).show(); | |
} | |
private void defineAluno(){ | |
String nome = campoNome.getText().toString(); | |
String email = campoEmail.getText().toString(); | |
aluno.setNome(nome); | |
aluno.setEmail(email); | |
defineTelefones(); | |
} | |
public void recupera(Intent dadosAluno){ | |
aluno = (Aluno) dadosAluno.getSerializableExtra(CHAVE_ALUNO); | |
campoNome.setText(aluno.getNome()); | |
campoEmail.setText(aluno.getEmail()); | |
recuperaTelefones(); | |
} | |
private void salvaAluno(){ | |
new SalvaAlunoAsyncTask(alunoDao, telefoneDao, aluno, numeroFixo, numeroCelular, () -> { | |
activity.finish(); | |
}).execute(); | |
Toast.makeText(context, "Aluno " + aluno.getNome() + " salvo!", Toast.LENGTH_SHORT).show(); | |
} | |
private void atualizaAluno(){ | |
new AtualizaAlunosAsyncTask(alunoDao, aluno).execute(); | |
defineTelefones(); | |
Telefone telefoneFixo = new Telefone(numeroFixo, FIXO, aluno.getId()); | |
Telefone telefoneCelular = new Telefone(numeroCelular, CELULAR, aluno.getId()); | |
new AtualizaTelefonesAsyncTask(telefoneDao, telefoneFixo, telefoneCelular, telefones, listaTelefones -> { | |
for(Telefone telefone : listaTelefones){ | |
if(telefone.getTipo() == FIXO){ | |
telefoneFixo.setId(telefone.getId()); | |
} else{ | |
telefoneCelular.setId(telefone.getId()); | |
} | |
} | |
}, () -> { | |
activity.finish(); | |
}).execute(); | |
Toast.makeText(context, "Aluno " + aluno.getNome() + " editado!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
private void defineTelefones(){ | |
numeroFixo = campoTelefoneFixo.getText().toString(); | |
numeroCelular = campoTelefoneCelular.getText().toString(); | |
} | |
private void recuperaTelefones(){ | |
new RecuperaTelefonesAsyncTask(telefones, telefoneDao, aluno, listaTefones -> { | |
telefones = listaTefones; | |
for(Telefone telefone : listaTefones){ | |
if(telefone.getTipo() == FIXO){ | |
campoTelefoneFixo.setText(telefone.getNumero()); | |
} else{ | |
campoTelefoneCelular.setText(telefone.getNumero()); | |
} | |
} | |
}).execute(); | |
} | |
public void setCampoNome(EditText campoNome){ | |
this.campoNome = campoNome; | |
} | |
public void setCampoTelefoneFixo(EditText campoTelefoneFixo){ | |
this.campoTelefoneFixo = campoTelefoneFixo; | |
} | |
public void setCampoTelefoneCelular(EditText campoTelefoneCelular){ | |
this.campoTelefoneCelular = campoTelefoneCelular; | |
} | |
public void setCampoEmail(EditText campoEmail){ | |
this.campoEmail = campoEmail; | |
} | |
public void setAluno(Aluno aluno){ | |
this.aluno = aluno; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.alura.agenda.ui.asynctask; | |
import android.os.AsyncTask; | |
import java.util.List; | |
import br.com.alura.agenda.model.Aluno; | |
import br.com.alura.agenda.model.Telefone; | |
import br.com.alura.agenda.room.TelefoneDao; | |
public class RecuperaTelefonesAsyncTask extends AsyncTask<Void, Void, List<Telefone>> { | |
private final TelefoneDao telefoneDao; | |
private final Aluno aluno; | |
private final DefineTelefonesListener listener; | |
public RecuperaTelefonesAsyncTask(List<Telefone> telefones, TelefoneDao telefoneDao, Aluno aluno, DefineTelefonesListener listener){ | |
this.telefoneDao = telefoneDao; | |
this.aluno = aluno; | |
this.listener = listener; | |
} | |
@Override | |
protected List<Telefone> doInBackground(Void... voids){ | |
List<Telefone> telefones = telefoneDao.devolveTodosTelefones(aluno.getId()); | |
return telefones; | |
} | |
@Override | |
protected void onPostExecute(List<Telefone> telefones){ | |
super.onPostExecute(telefones); | |
listener.defineNumerosTelefone(telefones); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment