use of com.google.firebase.database.ChildEventListener in project Lets-Chat by kshitiz1007.
the class ChatFragment method onStart.
@Override
public void onStart() {
super.onStart();
// ---ADDING THE RECYCLERVIEW TO FIREBASE DATABASE DIRECTLY----
// --ORDERING THE MESSAGE BY TIME----
Query conversationQuery = mConvDatabase.orderByChild("time_stamp");
FirebaseRecyclerAdapter<Conv, ConvViewHolder> friendsConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(// --CLASS FETCHED FROM DATABASE-- LAYOUT OF THE SINGLE ITEM--- HOLDER CLASS(DEFINED BELOW)---QUERY
Conv.class, R.layout.recycle_list_single_user, ConvViewHolder.class, conversationQuery) {
// ---- GETTING DATA FROM DATABSE AND ADDING TO VIEWHOLDER-----
@Override
protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int position) {
final String list_user_id = getRef(position).getKey();
Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);
// ---IT WORKS WHENEVER CHILD OF mMessageDatabase IS CHANGED---
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
convViewHolder.setMessage(data, conv.isSeen());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// ---ADDING NAME , IMAGE, ONLINE FEATURE , AND OPENING CHAT ACTIVITY ON CLICK----
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
if (dataSnapshot.hasChild("online")) {
String userOnline = dataSnapshot.child("online").getValue().toString();
convViewHolder.setUserOnline(userOnline);
}
convViewHolder.setName(userName);
convViewHolder.setUserImage(userThumb, getContext());
// --OPENING CHAT ACTIVITY FOR CLICKED USER----
convViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("user_id", list_user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mConvList.setAdapter(friendsConvAdapter);
}
use of com.google.firebase.database.ChildEventListener in project Lets-Chat by kshitiz1007.
the class RequestFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
mMainView = inflater.inflate(R.layout.fragment_request, container, false);
mReqList = (RecyclerView) mMainView.findViewById(R.id.recyclerViewRequestList);
mAuth = FirebaseAuth.getInstance();
mCurrent_user_id = mAuth.getCurrentUser().getUid();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("friend_request");
mDatabaseReference.keepSynced(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
mReqList.setHasFixedSize(true);
mReqList.setLayoutManager(linearLayoutManager);
requestList.clear();
mRequestAdapter = new RequestAdapter(requestList);
mReqList.setAdapter(mRequestAdapter);
mDatabaseReference.child(mCurrent_user_id).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String userId = dataSnapshot.getKey();
requestList.add(userId);
mRequestAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return mMainView;
}
use of com.google.firebase.database.ChildEventListener in project Pepper_v0 by SamDaQueen.
the class MenuActivity method attachDatabaseReadListener.
private void attachDatabaseReadListener() {
if (childEventListener == null) {
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MenuItem menuItem = dataSnapshot.getValue(MenuItem.class);
menuAdapter.add(menuItem);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
databaseReference.addChildEventListener(childEventListener);
}
}
use of com.google.firebase.database.ChildEventListener in project GogoNew by kuldeep725.
the class MapsActivity method onStart.
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart fired ..............");
if (!checkPermissions()) {
requestPermissions();
}
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
Intent i = new Intent(MapsActivity.this, Login.class);
startActivity(i);
finish();
}
showInternetStatus();
SharedPreferences loginPrefs = getSharedPreferences("userId", MODE_PRIVATE);
userEmail = loginPrefs.getString("email", "User id");
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView tv = (TextView) headerView.findViewById(R.id.user_id);
tv.setText(userEmail);
SharedPreferences prefs = getSharedPreferences("onStop", MODE_PRIVATE);
// boolean floatingClickable = prefs.getBoolean(floatingClickableState, Boolean.TRUE);
radiobuttonId = prefs.getInt(radioButtonString, 0);
flagSendOrCancel = prefs.getInt("flagSendOrCancel", 0);
int bus = prefs.getInt(whichBus, 0);
theKey = prefs.getString(keyString, null);
pickMeRadioButtonId = prefs.getInt("pickMeRadioButton", -1);
minName = prefs.getString(minNameStr, null);
Log.e(TAG, "onStart-> pickMeRadioButtonId = " + pickMeRadioButtonId);
Log.d(TAG, "flagSendOrCancel" + " = " + flagSendOrCancel);
Log.d(TAG, "radiobuttonId" + " = " + radiobuttonId);
Log.d(TAG, whichBus + " = " + bus);
Log.d(TAG, "theKey = " + theKey);
Log.d(TAG, "minName = " + minName);
checkBusSelection = radiobuttonId;
BUS = prefs.getString("pickMeBus", null);
if (BUS == null) {
BUS = "b" + checkBusSelection;
}
if (flagSendOrCancel != 0) {
floatingButton.setImageResource(R.drawable.ic_cancel_white_24dp);
floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.myRed)));
pickMeDone = true;
}
if (checkPermissions()) {
mGoogleApiClient.connect();
Log.e(TAG, "mGoogleApiClient is connected");
} else {
Log.e(TAG, "mGoogleApiClient is not connected");
}
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "dataSnapshot.getKey() = " + dataSnapshot.getKey());
Log.d(TAG, "dataSnapshot.getChildrenCount() = " + dataSnapshot.getChildrenCount());
/*check whether onChildAdded has already run once**/
if (flagOnChild == 1)
return;
noOfBuses = dataSnapshot.getChildrenCount();
Log.d(TAG, "noOfBuses = " + noOfBuses);
// Log.d(TAG, "String s = " + s);
createRadioButtons();
if (radiobuttonId != 0) {
// Log.d(TAG, "MAKING radiobutton setChecked(true)...");
radiobutton = (RadioButton) findViewById(radiobuttonId);
Log.d(TAG, "radiobutton @onStart = " + radiobutton.toString());
radiobutton.setTextColor(Color.parseColor(primeColorString));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
radiobutton.setBackground(getDrawable(R.drawable.underline));
} else {
radiobutton.setPaintFlags(radiobutton.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
radiobutton.setClickable(true);
radiobutton.setChecked(false);
lastButton = radiobutton;
Log.d(TAG, "checkbusSelection = " + checkBusSelection);
}
if (pickMeRadioButtonId != -1) {
pickMeRadioButton = (RadioButton) findViewById(pickMeRadioButtonId);
pickMeRadioButton.setTypeface(Typeface.DEFAULT_BOLD);
pickMeRadioButton.setTextColor(getResources().getColor(R.color.primeColor));
}
flagOnChild = 1;
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.ChildEventListener in project GogoNew by kuldeep725.
the class MapsActivity method findNearestBusStop.
private void findNearestBusStop() {
showInternetStatus();
if (!isInternetOn())
return;
DatabaseReference routeDatabase = FirebaseDatabase.getInstance().getReference().child(USER).child(BUS).child("route");
routeDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Log.d(TAG, "onChildAdded findNearestBusStop() fired .. .. ");
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
};
// Log.d(TAG, " dataSnapshot.getValue(genericTypeIndicator) : " + dataSnapshot.getValue(genericTypeIndicator));
// Log.d(TAG, "postDataSnapshot.getKey() = " + dataSnapshot.getKey());
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
assert map != null;
String latitudeStr = map.get("latitude");
String longitudeStr = map.get("longitude");
// Log.d(TAG, "Latitude = " + latitudeStr);
// Log.d(TAG, "Longitude = " + longitudeStr);
double latitude = Double.parseDouble(latitudeStr);
double longitude = Double.parseDouble(longitudeStr);
LatLng busStop = new LatLng(latitude, longitude);
// String busName = "BUS " + checkBusSelection;
// double diff = SphericalUtil.computeDistanceBetween(mCurrentPosition, busStop);
double diff = CalculationByDistance(mCurrentPosition, busStop) * 1000;
// Log.d(TAG, "diff = " + diff);
if (diff < minDistance) {
minDistance = diff;
minLocation = new LatLng(latitude, longitude);
minName = dataSnapshot.getKey();
}
// Log.d(TAG, "minLocation.toString() = " + minLocation.toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Aggregations