Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pro.izcali.testapptojo;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import pro.izcali.testapptojo.models.Negocios;
- import pro.izcali.testapptojo.models.Sucursal;
- import pro.izcali.testapptojo.models.User;
- import pro.izcali.testapptojo.models.administrador;
- public class MainActivity extends AppCompatActivity {
- TextView txNombre, txCorreo, txFecha, txHora;
- EditText edEntrada, edCorreo;
- Button btEnviar;
- private FirebaseAuth mAuth;
- FirebaseDatabase database;
- DatabaseReference myRef, refUsers;
- DataSnapshot datosUsuario, datosAuthor;
- ArrayList<String> negociosDeEmpresario;
- FirebaseUser user2;
- static final String user = "3nf8nf9n9n";
- static final String TAG = "Dummy Firebase";
- final String UID_Empresario = "9OvtbLbnJAQNeMqcJxPEqSiv6842";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- database = FirebaseDatabase.getInstance();
- mAuth = FirebaseAuth.getInstance();
- edEntrada = findViewById(R.id.edInput);
- edCorreo = findViewById(R.id.edCorreo);
- txNombre = findViewById(R.id.txNombre);
- txCorreo = findViewById(R.id.txCorreo);
- txFecha = findViewById(R.id.txFecha);
- txHora = findViewById(R.id.txHora);
- btEnviar = findViewById(R.id.btSave);
- btEnviar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- enviar();
- }
- });
- myRef = database.getReference("nodousers/");
- refUsers = database.getReference("populares");
- Log.d(TAG, "Ya jalo esta madre!!");
- setFBListener();
- }
- private void enviar() {
- /* String email = edEntrada.getText().toString();
- String pass = edCorreo.getText().toString();
- mAuth.signInWithEmailAndPassword(email, pass)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success, update UI with the signed-in user's information
- Log.d(TAG, "signInWithEmail:success");
- callMess("Succes");
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithEmail:failure", task.getException());
- callMess("Error");
- }
- // ...
- }
- }); */
- // Write a message to the database
- //myRef.push().setValue(new administrador(edEntrada.getText().toString(), edCorreo.getText().toString(), "ijklm")); //Envio correo
- //String pushhh = myRef.getKey();
- //txFecha.setText(pushhh);
- //Get the current articles in authors, and save it on ArrayList
- /*
- ArrayList<String> id_author = new ArrayList<>();
- //refUsers = database.getReference("authors/ijklm/articles");
- Log.d("IDE_PUSH", datosAuthor.getChildrenCount()+"");
- Log.d("IDE_PUSH", datosAuthor.getRef().toString());
- for (DataSnapshot tmp : datosAuthor.getChildren()){
- String idpush = tmp.getKey();
- Log.d("IDE_PUSH", idpush);
- Log.d("IDE_PUSH", datosAuthor.getChildrenCount()+"");
- }*/
- String uid_nuevo = myRef.push().getKey();
- myRef = database.getReference("nodousers/"+uid_nuevo);
- myRef.child("Nombre").setValue(txNombre.getText().toString());
- myRef.child("Correo").setValue(txCorreo.getText().toString());
- String uid_negocio = myRef.push().getKey();
- myRef = database.getReference("nodousers/"+uid_nuevo+"/Negocios/"+uid_negocio);
- myRef.setValue(new Negocios());
- String uid_sucursal;
- myRef = database.getReference("nodousers/"+uid_nuevo+"/Negocios/"+uid_negocio+"/Sucursales");
- for (int i = 0; i < 4; i++) {
- uid_sucursal = myRef.push().getKey();
- myRef.child(uid_sucursal).setValue(new Sucursal());
- }
- myRef = database.getReference("nodousers");
- }
- private void callMess(String s) {
- Toast.makeText(this, s, Toast.LENGTH_LONG).show();
- }
- private void setFBListener(){
- // Read from the database
- myRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- datosUsuario = dataSnapshot;
- //cambiarDatos();
- Log.d(TAG, "Inicia Llenado, ya leyo, yupi!!");
- llenarArrayNegociosPorID();
- }
- @Override
- public void onCancelled(DatabaseError error) {
- // Failed to read value
- Log.w(TAG, "Failed to read value.", error.toException());
- }
- });
- }
- private void llenarArrayNegociosPorID() {
- negociosDeEmpresario = new ArrayList<>();
- Log.d(TAG, "Ahora hara cosas de comparacion y asi !!");
- String neg = "";
- for (DataSnapshot tmp: datosUsuario.getChildren()){
- if(tmp.child("UID_Empresario").getValue(String.class)!=null){
- if (tmp.child("UID_Empresario").getValue(String.class).equals(UID_Empresario)){
- negociosDeEmpresario.add(tmp.getKey());
- }
- }
- }
- for (String clave:negociosDeEmpresario){
- Toast.makeText(this, clave, Toast.LENGTH_LONG).show();
- Log.d(TAG, clave);
- }
- }
- private void getArticleDate() {
- //administrador obj = datosAuthor.child("ijklm").child("articles").getValue(administrador.class);
- //txNombre.setText(obj.Nombre_A);
- //txCorreo.setText(obj.Correo_A);
- }
- private void cambiarDatos() {
- txNombre.setText(datosUsuario.child("Nombre_A").getValue(String.class));
- txCorreo.setText(datosUsuario.child("Correo_A").getValue(String.class));
- txFecha.setText(datosUsuario.child("Fecha_Registro_A").getValue(String.class));
- txHora.setText(datosUsuario.child("Hora_Registro_A").getValue(String.class));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement