package br.com.alura.agenda.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable; //NÃO FUNCIONA
import android.support.design.widget.FloatingActionButton; //NÃO FUNCIONA
import android.support.v7.app.AppCompatActivity;//NÃO FUNCIONA
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import br.com.alura.agenda.R;
import br.com.alura.agenda.model.Aluno;
import br.com.alura.agenda.ui.ListaAlunosView;
import static br.com.alura.agenda.ui.activity.ConstantesActivities.CHAVE_ALUNO;
public class ListaAlunosActivity extends AppCompatActivity {
private static final String TITULO_APPBAR = "Lista de alunos";
private final ListaAlunosView listaAlunosView = new ListaAlunosView(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
setTitle(TITULO_APPBAR);
configuraFabNovoAluno();
configuraLista();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater()
.inflate(R.menu.activity_lista_alunos_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.activity_lista_alunos_menu_remover) {
listaAlunosView.confirmaRemocao(item);
}
return super.onContextItemSelected(item);
}
private void configuraFabNovoAluno() {
FloatingActionButton botaoNovoAluno = findViewById(R.id.activity_lista_alunos_fab_novo_aluno);
botaoNovoAluno.setOnClickListener(view -> abreFormularioModoInsereAluno());
}
private void abreFormularioModoInsereAluno() {
startActivity(new Intent(this, FormularioAlunoActivity.class));
}
@Override
protected void onResume() {
super.onResume();
listaAlunosView.atualizaAlunos();
}
private void configuraLista() {
ListView listaDeAlunos = findViewById(R.id.activity_lista_alunos_listview);
listaAlunosView.configuraAdapter(listaDeAlunos);
configuraListenerDeCliquePorItem(listaDeAlunos);
registerForContextMenu(listaDeAlunos);
}
private void configuraListenerDeCliquePorItem(ListView listaDeAlunos) {
listaDeAlunos.setOnItemClickListener((adapterView, view, posicao, id) -> {
Aluno alunoEscolhido = (Aluno) adapterView.getItemAtPosition(posicao);
abreFormularioModoEditaAluno(alunoEscolhido);
});
}
private void abreFormularioModoEditaAluno(Aluno aluno) {
Intent vaiParaFormularioActivity = new Intent(ListaAlunosActivity.this, FormularioAlunoActivity.class);
vaiParaFormularioActivity.putExtra(CHAVE_ALUNO, aluno);
startActivity(vaiParaFormularioActivity);
}
}