Advertisement
mythoc

Untitled

Aug 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1. package kosmoglou.antogkou.learninganalytics;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.support.annotation.NonNull;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.DefaultItemAnimator;
  9. import android.support.v7.widget.LinearLayoutManager;
  10. import android.support.v7.widget.RecyclerView;
  11. import android.support.v7.widget.Toolbar;
  12. import android.util.Log;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19.  
  20. import com.google.android.gms.tasks.OnFailureListener;
  21. import com.google.android.gms.tasks.OnSuccessListener;
  22. import com.google.firebase.auth.FirebaseAuth;
  23. import com.google.firebase.database.DatabaseReference;
  24. import com.google.firebase.database.FirebaseDatabase;
  25. import com.google.firebase.firestore.EventListener;
  26. import com.google.firebase.firestore.FirebaseFirestore;
  27. import com.google.firebase.firestore.FirebaseFirestoreException;
  28. import com.google.firebase.firestore.QuerySnapshot;
  29. import com.google.firebase.firestore.SetOptions;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. public class NotificationFeedActivity extends AppCompatActivity {
  34.  
  35.     private Button return_button;
  36.     Button submit,fetch;
  37.     EditText fullname,username;
  38.     RecyclerView recyclerView;
  39.     ArrayList<FavModel> favModelArrayList;
  40.     private ArrayList<String> userName = new ArrayList<>();
  41.     private ArrayList<String> fullName = new ArrayList<>();
  42.     private RecyclerView.LayoutManager layoutManager;
  43.     public static String  TAG = "MainActivity";
  44.     private FirebaseFirestore mFirestore;
  45.     private DatabaseReference UsersRef;
  46.     String currentUserID;
  47.     private FirebaseAuth mAuth;
  48.  
  49.     private void SendUserToMainActivity() {
  50.         Intent mainIntent = new Intent (NotificationFeedActivity.this, MainActivity.class);
  51.         mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  52.         startActivity(mainIntent);
  53.         finish();
  54.     }
  55.  
  56.  
  57.     @Override
  58.     protected void onStart() {
  59.         mAuth = FirebaseAuth.getInstance();
  60.         currentUserID = mAuth.getCurrentUser().getUid();
  61.         UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
  62.         super.onStart();
  63.         mFirestore.collection("Users").addSnapshotListener(this,new EventListener<QuerySnapshot>() {
  64.             @Override
  65.             public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
  66.                 if(documentSnapshots.isEmpty()){
  67.                     Log.w(TAG,"Exec "+e);
  68.                 }else{
  69.                     List<FavModel> favModel  = documentSnapshots.toObjects(FavModel.class);
  70.                     CustomAdapter customAdapter = new CustomAdapter(favModel);
  71.                     recyclerView.setAdapter(customAdapter);
  72.  
  73.                     Log.w(TAG,"Size onstart "+favModel.size());
  74.                 }
  75.             }
  76.         });
  77.     }
  78.  
  79.     @Override
  80.     protected void onCreate(Bundle savedInstanceState) {
  81.         super.onCreate(savedInstanceState);
  82.         setContentView(R.layout.content_notification_feed);
  83.  
  84.  
  85.         /*return_button = (Button) findViewById(R.id.return_button);
  86.  
  87.         return_button.setOnClickListener(new View.OnClickListener() {
  88.             @Override
  89.             public void onClick(View view)
  90.             {
  91.                 SendUserToMainActivity();
  92.             }
  93.         });*/
  94.         //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  95.      //   setSupportActionBar(toolbar);
  96.  
  97.         mFirestore = FirebaseFirestore.getInstance();
  98.  
  99.         initviews();
  100.         //fetchdata();
  101.         //final CollectionReference samplesref = mFirestore.collection("sampleData");
  102.  
  103.         submit.setOnClickListener(new View.OnClickListener() {
  104.             @Override
  105.             public void onClick(View view) {
  106.                 insertdata();
  107.             }
  108.         });
  109.         fetch.setOnClickListener(new View.OnClickListener() {
  110.             @Override
  111.             public void onClick(View view) {
  112.                 fetchdata();
  113.             }
  114.         });
  115.     }
  116.  
  117.     private void fetchdata() {
  118.         //DocumentReference userRef = mFirestore.collection("cities").document("SF");
  119.  
  120.         mFirestore.collection("Users")
  121.                 .get()
  122.                 .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
  123.                     @Override
  124.                     public void onSuccess(QuerySnapshot documentSnapshots) {
  125.  
  126.                         List<FavModel> favModel  = documentSnapshots.toObjects(FavModel.class);
  127.                         Log.w(TAG,"fetch successful : "+favModel.size());
  128.                         for(int i=0;i<favModel.size();i++){
  129.                             Log.w(TAG,"data"+favModel.get(i).getFullname()+" "+favModel.get(i).getUsername());
  130.                         }
  131.                         CustomAdapter customAdapter = new CustomAdapter(favModel);
  132.                         recyclerView.setAdapter(customAdapter);
  133.                     }
  134.                 }).addOnFailureListener(new OnFailureListener() {
  135.             @Override
  136.             public void onFailure(@NonNull Exception e) {
  137.                 Log.w(TAG,"failed to fetch");
  138.             }
  139.         });
  140.  
  141.     }
  142.  
  143.  
  144.     private void insertdata() {
  145.  
  146.  
  147.  
  148.         String sfullname = fullname.getText().toString();
  149.         String susername = username.getText().toString();
  150.         if(sfullname.isEmpty()|| susername.isEmpty()){
  151.             Toast.makeText(NotificationFeedActivity.this, "Both fields Required", Toast.LENGTH_SHORT).show();
  152.         }else {
  153.             FavModel favModel = new FavModel(sfullname,susername);
  154.             mFirestore.collection("Users").document(currentUserID)
  155.                     .set(favModel, SetOptions.merge())
  156.                     .addOnSuccessListener(new OnSuccessListener<Void>() {
  157.                         @Override
  158.                         public void onSuccess(Void aVoid) {
  159.                             Log.w(TAG,"submitted");
  160.                             // fetchdata();
  161.                             fullname.setText("");
  162.                             username.setText("");
  163.                         }
  164.                     }).addOnFailureListener(new OnFailureListener() {
  165.                 @Override
  166.                 public void onFailure(@NonNull Exception e) {
  167.                     Log.w(TAG,"not submitted");
  168.  
  169.                 }
  170.             });
  171.  
  172.         }
  173.     }
  174.  
  175.  
  176.     private void initviews() {
  177.  
  178.         fullname = (EditText) findViewById(R.id.fullname);
  179.         username = (EditText)findViewById(R.id.username);
  180.         submit = (Button)findViewById(R.id.submit);
  181.         fetch = (Button)findViewById(R.id.fetch);
  182.  
  183.         recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
  184.         recyclerView.setHasFixedSize(true);
  185.  
  186.         layoutManager = new LinearLayoutManager(this);
  187.         recyclerView.setLayoutManager(layoutManager);
  188.         recyclerView.setItemAnimator(new DefaultItemAnimator());
  189.  
  190.     }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.     boolean doubleBackToExitPressedOnce = false;
  198.     @Override
  199.     public void onBackPressed() {
  200.         if (doubleBackToExitPressedOnce) {
  201.             super.onBackPressed();
  202.             return;
  203.         }
  204.  
  205.         this.doubleBackToExitPressedOnce = true;
  206.         Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
  207.  
  208.         new Handler().postDelayed(new Runnable() {
  209.  
  210.             @Override
  211.             public void run() {
  212.                 doubleBackToExitPressedOnce=false;
  213.             }
  214.         }, 2000);
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement