package br.com.alura.agenda;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import br.com.alura.agenda.dao.AlunoDAO;
import br.com.alura.agenda.modelo.Aluno;
public class FormularioActivity extends AppCompatActivity {
public static final int CODIGO_CAMERA = 567;
private FormularioHelper helper;
private String caminhoFoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
helper = new FormularioHelper(this);
Intent intent = getIntent();
Aluno aluno = (Aluno) intent.getSerializableExtra("aluno");
if (aluno != null) {
helper.preencheFormulario(aluno);
}
Button botaoFoto = (Button) findViewById(R.id.formulario_botao_foto);
botaoFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
caminhoFoto = getExternalFilesDir(null) + "/" + System.currentTimeMillis() + ".jpg";
File arquivoFoto = new File(caminhoFoto);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(arquivoFoto));
startActivityForResult(intentCamera, CODIGO_CAMERA);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CODIGO_CAMERA) {
helper.carregaImagem(caminhoFoto);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_formulario, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_formulario_ok:
Aluno aluno = helper.pegaAluno();
AlunoDAO dao = new AlunoDAO(this);
if (aluno.getId() != null) {
dao.altera(aluno);
} else {
dao.insere(aluno);
}
dao.close();
Toast.makeText(FormularioActivity.this, "Aluno " + aluno.getNome() + " salvo!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
public class FormularioHelper {
private final EditText campoNome;
private final EditText campoEndereco;
private final EditText campoTelefone;
private final EditText campoSite;
private final RatingBar campoNota;
private final ImageView campoFoto;
private Aluno aluno;
public FormularioHelper(FormularioActivity activity) {
campoNome = (EditText) activity.findViewById(R.id.formulario_nome);
campoEndereco = (EditText) activity.findViewById(R.id.formulario_endereco);
campoTelefone = (EditText) activity.findViewById(R.id.formulario_telefone);
campoSite = (EditText) activity.findViewById(R.id.formulario_site);
campoNota = (RatingBar) activity.findViewById(R.id.formulario_nota);
campoFoto = (ImageView) activity.findViewById(R.id.formulario_foto);
aluno = new Aluno();
}
public Aluno pegaAluno() {
aluno.setNome(campoNome.getText().toString());
aluno.setEndereco(campoEndereco.getText().toString());
aluno.setTelefone(campoTelefone.getText().toString());
aluno.setSite(campoSite.getText().toString());
aluno.setNota(Double.valueOf(campoNota.getProgress()));
aluno.setCaminhoFoto((String) campoFoto.getTag());
return aluno;
}
public void preencheFormulario(Aluno aluno) {
campoNome.setText(aluno.getNome());
campoEndereco.setText(aluno.getEndereco());
campoTelefone.setText(aluno.getTelefone());
campoSite.setText(aluno.getSite());
campoNota.setProgress(aluno.getNota().intValue());
carregaImagem(aluno.getCaminhoFoto());
this.aluno = aluno;
}
public void carregaImagem(String caminhoFoto) {
if (caminhoFoto != null) {
Bitmap bitmap = BitmapFactory.decodeFile(caminhoFoto);
Bitmap bitmapReduzido = Bitmap.createScaledBitmap(bitmap, 300, 300, true);
campoFoto.setImageBitmap(bitmapReduzido);
campoFoto.setScaleType(ImageView.ScaleType.FIT_XY);
campoFoto.setTag(caminhoFoto);
}
}
}