Advertisement
urksiful

Test Push Firebase

May 17th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. package pro.izcali.testapptojo;
  2.  
  3. import android.support.annotation.NonNull;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.google.android.gms.tasks.OnCompleteListener;
  14. import com.google.android.gms.tasks.OnSuccessListener;
  15. import com.google.android.gms.tasks.Task;
  16. import com.google.firebase.auth.AuthResult;
  17. import com.google.firebase.auth.FirebaseAuth;
  18. import com.google.firebase.auth.FirebaseUser;
  19. import com.google.firebase.database.DataSnapshot;
  20. import com.google.firebase.database.DatabaseError;
  21. import com.google.firebase.database.DatabaseReference;
  22. import com.google.firebase.database.FirebaseDatabase;
  23. import com.google.firebase.database.ValueEventListener;
  24.  
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30.  
  31. import pro.izcali.testapptojo.models.Negocios;
  32. import pro.izcali.testapptojo.models.Sucursal;
  33. import pro.izcali.testapptojo.models.User;
  34. import pro.izcali.testapptojo.models.administrador;
  35.  
  36.  
  37. public class MainActivity extends AppCompatActivity {
  38.     TextView txNombre, txCorreo, txFecha, txHora;
  39.     EditText edEntrada, edCorreo;
  40.     Button btEnviar;
  41.  
  42.     private FirebaseAuth mAuth;
  43.     FirebaseDatabase database;
  44.     DatabaseReference myRef, refUsers;
  45.     DataSnapshot datosUsuario, datosAuthor;
  46.  
  47.     ArrayList<String> negociosDeEmpresario;
  48.  
  49.     FirebaseUser user2;
  50.  
  51.  
  52.     static final String user = "3nf8nf9n9n";
  53.     static final String TAG = "Dummy Firebase";
  54.     final String UID_Empresario = "9OvtbLbnJAQNeMqcJxPEqSiv6842";
  55.  
  56.     @Override
  57.     protected void onCreate(Bundle savedInstanceState) {
  58.         super.onCreate(savedInstanceState);
  59.         setContentView(R.layout.activity_main);
  60.  
  61.         database = FirebaseDatabase.getInstance();
  62.         mAuth = FirebaseAuth.getInstance();
  63.  
  64.         edEntrada = findViewById(R.id.edInput);
  65.         edCorreo = findViewById(R.id.edCorreo);
  66.         txNombre = findViewById(R.id.txNombre);
  67.         txCorreo = findViewById(R.id.txCorreo);
  68.         txFecha = findViewById(R.id.txFecha);
  69.         txHora = findViewById(R.id.txHora);
  70.  
  71.  
  72.         btEnviar = findViewById(R.id.btSave);
  73.  
  74.         btEnviar.setOnClickListener(new View.OnClickListener() {
  75.             @Override
  76.             public void onClick(View v) {
  77.                 enviar();
  78.             }
  79.         });
  80.  
  81.  
  82.         myRef = database.getReference("nodousers/");
  83.         refUsers = database.getReference("populares");
  84.  
  85.         Log.d(TAG, "Ya jalo esta madre!!");
  86.         setFBListener();
  87.     }
  88.  
  89.     private void enviar() {
  90.        /* String email = edEntrada.getText().toString();
  91.         String pass = edCorreo.getText().toString();
  92.  
  93.         mAuth.signInWithEmailAndPassword(email, pass)
  94.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  95.                     @Override
  96.                     public void onComplete(@NonNull Task<AuthResult> task) {
  97.                         if (task.isSuccessful()) {
  98.                             // Sign in success, update UI with the signed-in user's information
  99.                             Log.d(TAG, "signInWithEmail:success");
  100.                             callMess("Succes");
  101.                         } else {
  102.                             // If sign in fails, display a message to the user.
  103.                             Log.w(TAG, "signInWithEmail:failure", task.getException());
  104.                             callMess("Error");
  105.                         }
  106.  
  107.                         // ...
  108.                     }
  109.                 }); */
  110.         // Write a message to the database
  111.  
  112.         //myRef.push().setValue(new administrador(edEntrada.getText().toString(), edCorreo.getText().toString(), "ijklm")); //Envio correo
  113.         //String pushhh = myRef.getKey();
  114.         //txFecha.setText(pushhh);
  115.  
  116.         //Get the current articles in authors, and save it on ArrayList
  117. /*
  118.         ArrayList<String> id_author = new ArrayList<>();
  119.         //refUsers = database.getReference("authors/ijklm/articles");
  120.         Log.d("IDE_PUSH", datosAuthor.getChildrenCount()+"");
  121.         Log.d("IDE_PUSH", datosAuthor.getRef().toString());
  122.         for (DataSnapshot tmp : datosAuthor.getChildren()){
  123.             String idpush = tmp.getKey();
  124.             Log.d("IDE_PUSH", idpush);
  125.             Log.d("IDE_PUSH", datosAuthor.getChildrenCount()+"");
  126.         }*/
  127.         String uid_nuevo = myRef.push().getKey();
  128.  
  129.  
  130.         myRef = database.getReference("nodousers/"+uid_nuevo);
  131.         myRef.child("Nombre").setValue(txNombre.getText().toString());
  132.         myRef.child("Correo").setValue(txCorreo.getText().toString());
  133.  
  134.  
  135.         String uid_negocio = myRef.push().getKey();
  136.  
  137.         myRef = database.getReference("nodousers/"+uid_nuevo+"/Negocios/"+uid_negocio);
  138.  
  139.         myRef.setValue(new Negocios());
  140.  
  141.         String uid_sucursal;
  142.         myRef = database.getReference("nodousers/"+uid_nuevo+"/Negocios/"+uid_negocio+"/Sucursales");
  143.         for (int i = 0; i < 4; i++) {
  144.             uid_sucursal = myRef.push().getKey();
  145.             myRef.child(uid_sucursal).setValue(new Sucursal());
  146.         }
  147.  
  148.         myRef = database.getReference("nodousers");
  149.  
  150.     }
  151.  
  152.     private void callMess(String s) {
  153.         Toast.makeText(this, s, Toast.LENGTH_LONG).show();
  154.     }
  155.  
  156.     private void setFBListener(){
  157.         // Read from the database
  158.         myRef.addValueEventListener(new ValueEventListener() {
  159.             @Override
  160.             public void onDataChange(DataSnapshot dataSnapshot) {
  161.                 datosUsuario = dataSnapshot;
  162.                 //cambiarDatos();
  163.                 Log.d(TAG, "Inicia Llenado, ya leyo, yupi!!");
  164.                 llenarArrayNegociosPorID();
  165.             }
  166.  
  167.             @Override
  168.             public void onCancelled(DatabaseError error) {
  169.                 // Failed to read value
  170.                 Log.w(TAG, "Failed to read value.", error.toException());
  171.             }
  172.         });
  173.  
  174.  
  175.     }
  176.  
  177.     private void llenarArrayNegociosPorID() {
  178.         negociosDeEmpresario = new ArrayList<>();
  179.         Log.d(TAG, "Ahora hara cosas de comparacion y asi !!");
  180.         String neg = "";
  181.  
  182.  
  183.         for (DataSnapshot tmp: datosUsuario.getChildren()){
  184.  
  185.             if(tmp.child("UID_Empresario").getValue(String.class)!=null){
  186.  
  187.                 if (tmp.child("UID_Empresario").getValue(String.class).equals(UID_Empresario)){
  188.                     negociosDeEmpresario.add(tmp.getKey());
  189.                 }
  190.  
  191.             }
  192.  
  193.  
  194.         }
  195.  
  196.         for (String clave:negociosDeEmpresario){
  197.  
  198.             Toast.makeText(this, clave, Toast.LENGTH_LONG).show();
  199.             Log.d(TAG, clave);
  200.         }
  201.  
  202.     }
  203.  
  204.     private void getArticleDate() {
  205.         //administrador obj = datosAuthor.child("ijklm").child("articles").getValue(administrador.class);
  206.  
  207.         //txNombre.setText(obj.Nombre_A);
  208.         //txCorreo.setText(obj.Correo_A);
  209.  
  210.     }
  211.  
  212.     private void cambiarDatos() {
  213.         txNombre.setText(datosUsuario.child("Nombre_A").getValue(String.class));
  214.         txCorreo.setText(datosUsuario.child("Correo_A").getValue(String.class));
  215.         txFecha.setText(datosUsuario.child("Fecha_Registro_A").getValue(String.class));
  216.         txHora.setText(datosUsuario.child("Hora_Registro_A").getValue(String.class));
  217.     }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement