I'm want to register a user in Cloud Firestore
, for that i have 2 diferent screens in Android Studio (FormCadastro1
and FormCadastro2
) the problem is import the selected item of every spinner in FormCadastro1
and bring then to FormCadastro2
, and then send then to Cloud Firestore.
I'm thinking about to import class FormCadastro1
to FormCadastro2
, if there is another way to do it please tell me.
FormCadastro1
public class FormCadastro1 extends AppCompatActivity {
Spinner edit_spempr, edit_sparea, edit_spsetor, edit_spcargos;
Button bt_cadastrar;
Usuarios usuarios;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_cadastro1);
getSupportActionBar().hide();
IniciarComponentes();
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spItemCargos, android.R.layout.simple_spinner_item);
edit_spcargos.setAdapter(adapter);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.spItemEmpresa, android.R.layout.simple_spinner_item);
edit_spempr.setAdapter(adapter1);
ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this, R.array.spItemArea, android.R.layout.simple_spinner_item);
edit_sparea.setAdapter(adapter3);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.spItemSetor, android.R.layout.simple_spinner_item);
edit_spsetor.setAdapter(adapter2);
String Empr = edit_spempr.getSelectedItem().toString();
String Area = edit_sparea.getSelectedItem().toString();
String Setor = edit_spsetor.getSelectedItem().toString();
String Cargo = edit_spcargos.getSelectedItem().toString();
usuarios = new Usuarios();
bt_cadastrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(FormCadastro1.this, FormCadastro2.class);
startActivity(intent);
}
});
}
private void IniciarComponentes() {
edit_sparea = findViewById(R.id.spArea);
edit_spempr = findViewById(R.id.spEmpresa);
edit_spsetor = findViewById(R.id.spSetor);
edit_spcargos = findViewById(R.id.spCargos);
bt_cadastrar = findViewById(R.id.btnCadastrar);
}
}
FormCadastro 2
public class FormCadastro2 extends AppCompatActivity{
EditText edit_nome, edit_senha, edit_email, edit_confsenha, edit_tel;
Button bt_Próximo;
String[] mensagens = {"Preencha todos os campos", "Cadastro realizado com sucesso"};
String UID;
String ID;
TextView MainActivityPasswordError, MainActivityConfirmPassError;
Usuarios usuarios;
FirebaseFirestore db = FirebaseFirestore.getInstance();
//1.Qual layout será exibido
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_cadastro2);
getSupportActionBar().hide();
IniciarComponentes();
usuarios = new Usuarios();
CollectionReference reff = db.collection("Usuarios");
bt_Próximo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//3.1.Convertendo o texto em string ao clicar no botão
String nome = edit_nome.getText().toString().trim();
String email = edit_email.getText().toString().trim();
String senha = edit_senha.getText().toString().trim();
String tel = edit_tel.getText().toString().trim();
//3.2.Tratando exceções
if (nome.isEmpty() || email.isEmpty() || senha.isEmpty() || tel.isEmpty()) {
Snackbar snackbar = Snackbar.make(view, mensagens[0], Snackbar.LENGTH_SHORT);
snackbar.setBackgroundTint(Color.WHITE);
snackbar.setTextColor(Color.BLACK);
snackbar.show();
} else {
CadastrarUsuario(view);
Intent intent = new Intent(FormCadastro2.this, FormLogin.class);
startActivity(intent);
}
}
});
}
public void SalvarDadosUsuario() {
String nome = edit_nome.getText().toString().trim();
String email = edit_email.getText().toString().trim();
String senha = edit_senha.getText().toString().trim();
String tel = edit_tel.getText().toString().trim();
String empresa = //edit_spempresa selected item
String area = //edit_sparea selected item
String setor = //edit_spsetor selected item
String cargo = //edit_spcargos selected item
UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
//5.2.Cria a variável
Map<String, Object> usuarios = new HashMap<>();
usuarios.put("nome", nome);
usuarios.put("email", email);
usuarios.put("senha", senha);
usuarios.put("Tel", tel);
usuarios.put("empresa", empresa);
usuarios.put("area", area);
usuarios.put("setor", setor);
usuarios.put("cargo", cargo);
usuarios.put("Login", UID);
usuarios.put("ID", "");
db.collection("Usuarios").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
Integer IDS = task.getResult().size();
DocumentReference documentReference = db.collection("Usuarios").document(ID = String.valueOf(IDS + 1));
documentReference.set(usuarios);
db.collection("Usuarios").document(ID).update("ID", ID);
}
}
});
}
private void IniciarComponentes() {
edit_nome = findViewById(R.id.editNome);
edit_email = findViewById(R.id.editEmail);
edit_senha = findViewById(R.id.editSenha);
edit_confsenha = findViewById(R.id.editConfSenha);
edit_tel = findViewById(R.id.editTel);
bt_Próximo = findViewById(R.id.btnPróximo);
}
private boolean ValidaçãoDeSenha() {
String passwordInput = edit_senha.getText().toString().trim();
String ConfitmpasswordInput = edit_confsenha.getText().toString().trim();
if (passwordInput.isEmpty()) {
MainActivityPasswordError.setText("Field can't be empty");
return false;
}
if (passwordInput.length() < 5) {
MainActivityPasswordError.setText("Password must be at least 5 characters");
return false;
}
if (!passwordInput.equals(ConfitmpasswordInput)) {
MainActivityConfirmPassError.setText("Password Would Not be matched");
return false;
} else {
MainActivityConfirmPassError.setText("Password Matched");
return true;
}
}
public void CadastrarUsuario(View view) {
String email = edit_email.getText().toString();
String senha = edit_senha.getText().toString();
//4.1.FirebaseAuth coletando as informações necessárias para o cadastro e as autenticando
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, senha).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//4.2.Caso a tarefa seja completa
if (task.isSuccessful()) {
//4.3.Oque vai acontecer
SalvarDadosUsuario();
Snackbar snackbar = Snackbar.make(view, mensagens[1], Snackbar.LENGTH_SHORT);
snackbar.setBackgroundTint(Color.WHITE);
snackbar.setTextColor(Color.BLACK);
snackbar.show();
} else {
//4.4.Criando String erro
String erro;
try {
throw task.getException();
} catch (FirebaseAuthWeakPasswordException e) {
erro = "Digite um senha com no mínimo 6 carácteres";
} catch (FirebaseAuthUserCollisionException e) {
erro = "Esta conta já foi cadastrada";
} catch (FirebaseAuthInvalidCredentialsException e) {
erro = "E-mail inválido";
} catch (Exception e) {
erro = "Erro ao cadastrar usuário";
}
//4.5.Cor da mensagem
Snackbar snackbar = Snackbar.make(view, erro, Snackbar.LENGTH_SHORT);
snackbar.setBackgroundTint(Color.WHITE);
snackbar.setTextColor(Color.BLACK);
snackbar.show();
}
}
});
}
}