Advertisement
rachmadi

Bleed Post

Aug 14th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.66 KB | None | 0 0
  1. package info.rekayasa.donordarah.activity;
  2.  
  3. import android.content.Intent;
  4. import android.support.design.widget.FloatingActionButton;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.support.v7.widget.LinearLayoutManager;
  8. import android.support.v7.widget.RecyclerView;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.ProgressBar;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.firebase.ui.database.FirebaseRecyclerAdapter;
  18. import com.google.firebase.auth.FirebaseAuth;
  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.Query;
  24. import com.google.firebase.database.ServerValue;
  25. import com.google.firebase.database.ValueEventListener;
  26. import com.google.firebase.iid.FirebaseInstanceId;
  27.  
  28. import info.rekayasa.donordarah.R;
  29. import info.rekayasa.donordarah.adapter.BleedViewHolder;
  30. import info.rekayasa.donordarah.application.BleedApp;
  31. import info.rekayasa.donordarah.entity.Bleed;
  32. import info.rekayasa.donordarah.utility.DatabaseUtil;
  33. import info.rekayasa.donordarah.utility.Utility;
  34.  
  35. import static android.R.attr.id;
  36. import static info.rekayasa.donordarah.utility.DatabaseUtil.getDatabase;
  37.  
  38. public class BleedActivity extends BaseActivity {
  39.  
  40.     FirebaseDatabase database;
  41.     DatabaseReference rootRef, userRef, testRef;
  42.     DatabaseReference mDatabase, mRef;
  43.     FirebaseAuth auth;
  44.     LinearLayoutManager mManager;
  45.  
  46.     ProgressBar progressBar;
  47.     Utility util = new Utility();
  48.     String username, fullName;
  49.  
  50.     RecyclerView mRecyclerView;
  51.     FirebaseRecyclerAdapter<Bleed, BleedViewHolder> mAdapter;
  52.  
  53.     String TAG = "BleedActivity";
  54.  
  55.     @Override
  56.     public void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.  
  59.         // get firebase auth instance
  60.         auth = FirebaseAuth.getInstance();
  61.         if (auth.getCurrentUser() == null) {
  62.             startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
  63.             finish();
  64.         } else {
  65.             setContentView(R.layout.activity_bleed);
  66.             mDatabase = FirebaseDatabase.getInstance().getReference();
  67.  
  68.             mRecyclerView = (RecyclerView) findViewById(R.id.bleed_list);
  69.             mRecyclerView.setHasFixedSize(true);
  70.             mManager = new LinearLayoutManager(this);
  71.             mManager.setReverseLayout(true);
  72.             mManager.setStackFromEnd(true);
  73.             mRecyclerView.setLayoutManager(mManager);
  74.  
  75. //            mRef = FirebaseDatabase.getInstance().getReference().child("bleeds");
  76.  
  77.             progressBar = (ProgressBar) findViewById(R.id.progressBar);
  78.  
  79. //            getUsername();
  80. //            getFullName();
  81. //            getConnectionStatus();
  82.  
  83.  
  84.             FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  85.             fab.setOnClickListener(new View.OnClickListener() {
  86.                 @Override
  87.                 public void onClick(View view) {
  88.                     Intent intent = new Intent(getApplicationContext(), PostActivity.class);
  89.                     intent.putExtra("username", username);
  90.                     intent.putExtra("fullName", fullName);
  91.                     startActivity(intent);
  92.                 }
  93.             });
  94.         }
  95.     }
  96.  
  97.     @Override
  98.     protected void onStart() {
  99.         super.onStart();
  100.  
  101.         boolean status = util.isConnected(BleedActivity.this);
  102. //        setLastActive();
  103. //        if (BleedApp.wasInBackground) {
  104. //            Toast.makeText(getApplicationContext(),
  105. //                    "Application came to foreground", Toast.LENGTH_LONG)
  106. //                    .show();
  107. //            BleedApp.wasInBackground = false;
  108. ////            getConnectionStatus();
  109. //        }
  110.  
  111.  
  112.         Log.d(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());
  113.  
  114. //        if (!status) {
  115. //            Toast.makeText(getApplicationContext(), "Jaringan tak terhubung", Toast.LENGTH_SHORT).show();
  116. //            progressBar.setVisibility(View.GONE);
  117. //        } else {
  118. //            mRef.addValueEventListener(new ValueEventListener() {
  119. //                @Override
  120. //                public void onDataChange(DataSnapshot dataSnapshot) {
  121. //                    System.out.println("dataSnapshot: ");
  122. //
  123. //                }
  124. //
  125. //                @Override
  126. //                public void onCancelled(DatabaseError databaseError) {
  127. //
  128. //                }
  129. //            });
  130.  
  131. //            Log.d(TAG, "mDatabase: " + mDatabase.toString());
  132.             // bagian yg dimodif
  133. //            mRef = FirebaseDatabase.getInstance().getReference().child("bleeds");
  134.             mRef = mDatabase.child("bleeds");
  135.  
  136.             mAdapter = new FirebaseRecyclerAdapter<Bleed, BleedViewHolder>(
  137.                     Bleed.class,
  138.                     R.layout.item_bleed,
  139.                     BleedViewHolder.class, mRef) {
  140.                 @Override
  141.                 protected void populateViewHolder(BleedViewHolder viewHolder, Bleed model, int position) {
  142.  
  143.                     DatabaseReference postRef = getRef(position);
  144.                     final String postKey = postRef.getKey();
  145.  
  146.                     viewHolder.bindToPost(model);
  147.                     progressBar.setVisibility(View.GONE);
  148.  
  149.                     viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
  150.                         @Override
  151.                         public void onClick(View view) {
  152. //                            System.out.println("postKey: " + postKey);
  153.                             Intent intent = new Intent(getApplicationContext(), CommentActivity.class);
  154.                             intent.putExtra("postKey", postKey);
  155.                             intent.putExtra("fullName", fullName);
  156.                             startActivity(intent);
  157.                         }
  158.                     });
  159.                 }
  160.             };
  161.  
  162.             mRecyclerView.setAdapter(mAdapter);
  163.  
  164.             getUsername();
  165.             getFullName();
  166.         }
  167. //    }
  168.  
  169.     public String getUid() {
  170.         return FirebaseAuth.getInstance().getCurrentUser().getUid();
  171.     }
  172.  
  173.     private void signout() {
  174.         auth.signOut();
  175.     }
  176.  
  177.     public void setLastActive() {
  178.         mDatabase.child("users").child(getUid()).child("lastActive").setValue(ServerValue.TIMESTAMP);
  179.     }
  180.  
  181.     @Override
  182.     public boolean onCreateOptionsMenu(Menu menu) {
  183.         // Inflate the menu; this adds items to the action bar if it is present.
  184.         getMenuInflater().inflate(R.menu.menu_bleed, menu);
  185.         return true;
  186.     }
  187.  
  188.     @Override
  189.     public boolean onOptionsItemSelected(MenuItem item) {
  190.         // Handle action bar item clicks here. The action bar will
  191.         // automatically handle clicks on the Home/Up button, so long
  192.         // as you specify a parent activity in AndroidManifest.xml.
  193.         int id = item.getItemId();
  194.  
  195.         //noinspection SimplifiableIfStatement
  196.         if (id == R.id.profile) {
  197.             Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
  198.             intent.putExtra("userId", getUid());
  199.             startActivity(intent);
  200.             return true;
  201.         } else if (id == R.id.donor) {
  202.             startActivity(new Intent(getApplicationContext(), DonorListActivity.class));
  203.             return true;
  204.         } else if (id == R.id.message) {
  205.             startActivity(new Intent(getApplicationContext(), MessageActivity.class));
  206.             return true;
  207.         } else if (id == R.id.about) {
  208.             startActivity(new Intent(getApplicationContext(), About.class));
  209.             return true;
  210.         } else if (id == R.id.signout) {
  211.             signout();
  212.             finish();
  213.             startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
  214.             return true;
  215.         }
  216.  
  217.         return super.onOptionsItemSelected(item);
  218.     }
  219.  
  220.     // contoh mencari data tertentu
  221.     private String getUsername() {
  222.  
  223.         Query query = mDatabase.child("users").child(getUid()).child("username");
  224.         query.addValueEventListener(new ValueEventListener() {
  225.             @Override
  226.             public void onDataChange(DataSnapshot dataSnapshot) {
  227.                 username = dataSnapshot.getValue(String.class);
  228. //                System.out.println("getUsername: " + username);
  229.             }
  230.  
  231.             @Override
  232.             public void onCancelled(DatabaseError databaseError) {
  233.  
  234.             }
  235.         });
  236.         return username;
  237.  
  238.     }
  239.  
  240.     private String getFullName() {
  241.  
  242.         Query query = mDatabase.child("users").child(getUid()).child("fullName");
  243.         query.addValueEventListener(new ValueEventListener() {
  244.             @Override
  245.             public void onDataChange(DataSnapshot dataSnapshot) {
  246.                 fullName = dataSnapshot.getValue(String.class);
  247. //                System.out.println("getFullName: " + fullName);
  248.             }
  249.  
  250.             @Override
  251.             public void onCancelled(DatabaseError databaseError) {
  252.  
  253.             }
  254.         });
  255.         return fullName;
  256.  
  257.     }
  258.  
  259.     @Override
  260.     protected void onUserLeaveHint() {
  261.         super.onUserLeaveHint();
  262.         Log.d(TAG, "User preses home button...");
  263. //        BleedApp.wasInBackground = true;
  264.     }
  265.  
  266.     @Override
  267.     protected void onPause() {
  268.         super.onPause();
  269.         Log.d(TAG, "User pauses...");
  270. //        mDatabase.child("users").onDisconnect()
  271.     }
  272.  
  273.     @Override
  274.     protected void onStop() {
  275.         super.onStop();
  276.         Log.d(TAG, "Activity stopped...");
  277.     }
  278.  
  279.     @Override
  280.     protected void onResume() {
  281.         super.onResume();
  282.         Log.d(TAG, "User resumes....");
  283. //        BleedApp.wasInBackground = true;
  284.         getConnectionStatus();
  285.     }
  286.  
  287.     public void getConnectionStatus() {
  288.         // since I can connect from multiple devices, we store each connection instance separately
  289.         // any time that connectionsRef's value is null (i.e. has no children) I am offline
  290.         final FirebaseDatabase database = FirebaseDatabase.getInstance();
  291.         final DatabaseReference myConnectionsRef = database.getReference().child("users").child(getUid()).child("connection");
  292.  
  293.         // stores the timestamp of my last disconnect (the last time I was seen online)
  294.         final DatabaseReference lastOnlineRef = database.getReference().child("users").child(getUid()).child("lastActive");
  295.  
  296.         final DatabaseReference connectedRef = database.getReference(".info/connected");
  297.         connectedRef.addValueEventListener(new ValueEventListener() {
  298.             @Override
  299.             public void onDataChange(DataSnapshot snapshot) {
  300.                 boolean connected = snapshot.getValue(Boolean.class);
  301.                 if (connected) {
  302.                     // add this device to my connections list
  303.                     // this value could contain info about the device or a timestamp too
  304.                     DatabaseReference con = myConnectionsRef.push();
  305.                     con.setValue(Boolean.TRUE);
  306.  
  307.                     // when this device disconnects, remove it
  308.                     con.onDisconnect().removeValue();
  309.  
  310.                     // when I disconnect, update the last time I was seen online
  311.                     lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
  312.                 }
  313.             }
  314.  
  315.             @Override
  316.             public void onCancelled(DatabaseError error) {
  317.                 System.err.println("Listener was cancelled at .info/connected");
  318.             }
  319.         });
  320.     }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement