Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.rekayasa.donordarah.activity;
- import android.content.Intent;
- import android.support.design.widget.FloatingActionButton;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.firebase.ui.database.FirebaseRecyclerAdapter;
- import com.google.firebase.auth.FirebaseAuth;
- 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.Query;
- import com.google.firebase.database.ServerValue;
- import com.google.firebase.database.ValueEventListener;
- import com.google.firebase.iid.FirebaseInstanceId;
- import info.rekayasa.donordarah.R;
- import info.rekayasa.donordarah.adapter.BleedViewHolder;
- import info.rekayasa.donordarah.application.BleedApp;
- import info.rekayasa.donordarah.entity.Bleed;
- import info.rekayasa.donordarah.utility.DatabaseUtil;
- import info.rekayasa.donordarah.utility.Utility;
- import static android.R.attr.id;
- import static info.rekayasa.donordarah.utility.DatabaseUtil.getDatabase;
- public class BleedActivity extends BaseActivity {
- FirebaseDatabase database;
- DatabaseReference rootRef, userRef, testRef;
- DatabaseReference mDatabase, mRef;
- FirebaseAuth auth;
- LinearLayoutManager mManager;
- ProgressBar progressBar;
- Utility util = new Utility();
- String username, fullName;
- RecyclerView mRecyclerView;
- FirebaseRecyclerAdapter<Bleed, BleedViewHolder> mAdapter;
- String TAG = "BleedActivity";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // get firebase auth instance
- auth = FirebaseAuth.getInstance();
- if (auth.getCurrentUser() == null) {
- startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
- finish();
- } else {
- setContentView(R.layout.activity_bleed);
- mDatabase = FirebaseDatabase.getInstance().getReference();
- mRecyclerView = (RecyclerView) findViewById(R.id.bleed_list);
- mRecyclerView.setHasFixedSize(true);
- mManager = new LinearLayoutManager(this);
- mManager.setReverseLayout(true);
- mManager.setStackFromEnd(true);
- mRecyclerView.setLayoutManager(mManager);
- // mRef = FirebaseDatabase.getInstance().getReference().child("bleeds");
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- // getUsername();
- // getFullName();
- // getConnectionStatus();
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(getApplicationContext(), PostActivity.class);
- intent.putExtra("username", username);
- intent.putExtra("fullName", fullName);
- startActivity(intent);
- }
- });
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- boolean status = util.isConnected(BleedActivity.this);
- // setLastActive();
- // if (BleedApp.wasInBackground) {
- // Toast.makeText(getApplicationContext(),
- // "Application came to foreground", Toast.LENGTH_LONG)
- // .show();
- // BleedApp.wasInBackground = false;
- //// getConnectionStatus();
- // }
- Log.d(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());
- // if (!status) {
- // Toast.makeText(getApplicationContext(), "Jaringan tak terhubung", Toast.LENGTH_SHORT).show();
- // progressBar.setVisibility(View.GONE);
- // } else {
- // mRef.addValueEventListener(new ValueEventListener() {
- // @Override
- // public void onDataChange(DataSnapshot dataSnapshot) {
- // System.out.println("dataSnapshot: ");
- //
- // }
- //
- // @Override
- // public void onCancelled(DatabaseError databaseError) {
- //
- // }
- // });
- // Log.d(TAG, "mDatabase: " + mDatabase.toString());
- // bagian yg dimodif
- // mRef = FirebaseDatabase.getInstance().getReference().child("bleeds");
- mRef = mDatabase.child("bleeds");
- mAdapter = new FirebaseRecyclerAdapter<Bleed, BleedViewHolder>(
- Bleed.class,
- R.layout.item_bleed,
- BleedViewHolder.class, mRef) {
- @Override
- protected void populateViewHolder(BleedViewHolder viewHolder, Bleed model, int position) {
- DatabaseReference postRef = getRef(position);
- final String postKey = postRef.getKey();
- viewHolder.bindToPost(model);
- progressBar.setVisibility(View.GONE);
- viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // System.out.println("postKey: " + postKey);
- Intent intent = new Intent(getApplicationContext(), CommentActivity.class);
- intent.putExtra("postKey", postKey);
- intent.putExtra("fullName", fullName);
- startActivity(intent);
- }
- });
- }
- };
- mRecyclerView.setAdapter(mAdapter);
- getUsername();
- getFullName();
- }
- // }
- public String getUid() {
- return FirebaseAuth.getInstance().getCurrentUser().getUid();
- }
- private void signout() {
- auth.signOut();
- }
- public void setLastActive() {
- mDatabase.child("users").child(getUid()).child("lastActive").setValue(ServerValue.TIMESTAMP);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_bleed, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.profile) {
- Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
- intent.putExtra("userId", getUid());
- startActivity(intent);
- return true;
- } else if (id == R.id.donor) {
- startActivity(new Intent(getApplicationContext(), DonorListActivity.class));
- return true;
- } else if (id == R.id.message) {
- startActivity(new Intent(getApplicationContext(), MessageActivity.class));
- return true;
- } else if (id == R.id.about) {
- startActivity(new Intent(getApplicationContext(), About.class));
- return true;
- } else if (id == R.id.signout) {
- signout();
- finish();
- startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- // contoh mencari data tertentu
- private String getUsername() {
- Query query = mDatabase.child("users").child(getUid()).child("username");
- query.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- username = dataSnapshot.getValue(String.class);
- // System.out.println("getUsername: " + username);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- return username;
- }
- private String getFullName() {
- Query query = mDatabase.child("users").child(getUid()).child("fullName");
- query.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- fullName = dataSnapshot.getValue(String.class);
- // System.out.println("getFullName: " + fullName);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- return fullName;
- }
- @Override
- protected void onUserLeaveHint() {
- super.onUserLeaveHint();
- Log.d(TAG, "User preses home button...");
- // BleedApp.wasInBackground = true;
- }
- @Override
- protected void onPause() {
- super.onPause();
- Log.d(TAG, "User pauses...");
- // mDatabase.child("users").onDisconnect()
- }
- @Override
- protected void onStop() {
- super.onStop();
- Log.d(TAG, "Activity stopped...");
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.d(TAG, "User resumes....");
- // BleedApp.wasInBackground = true;
- getConnectionStatus();
- }
- public void getConnectionStatus() {
- // since I can connect from multiple devices, we store each connection instance separately
- // any time that connectionsRef's value is null (i.e. has no children) I am offline
- final FirebaseDatabase database = FirebaseDatabase.getInstance();
- final DatabaseReference myConnectionsRef = database.getReference().child("users").child(getUid()).child("connection");
- // stores the timestamp of my last disconnect (the last time I was seen online)
- final DatabaseReference lastOnlineRef = database.getReference().child("users").child(getUid()).child("lastActive");
- final DatabaseReference connectedRef = database.getReference(".info/connected");
- connectedRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- boolean connected = snapshot.getValue(Boolean.class);
- if (connected) {
- // add this device to my connections list
- // this value could contain info about the device or a timestamp too
- DatabaseReference con = myConnectionsRef.push();
- con.setValue(Boolean.TRUE);
- // when this device disconnects, remove it
- con.onDisconnect().removeValue();
- // when I disconnect, update the last time I was seen online
- lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
- }
- }
- @Override
- public void onCancelled(DatabaseError error) {
- System.err.println("Listener was cancelled at .info/connected");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement