Search in sources :

Example 11 with ChildEventListener

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);
}
Also used : Query(com.google.firebase.database.Query) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DatabaseError(com.google.firebase.database.DatabaseError) Conv(com.example.singhkshitiz.letschat.Conv) FirebaseRecyclerAdapter(com.firebase.ui.database.FirebaseRecyclerAdapter) ValueEventListener(com.google.firebase.database.ValueEventListener) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 12 with ChildEventListener

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;
}
Also used : RequestAdapter(com.example.singhkshitiz.letschat.RequestAdapter) DatabaseError(com.google.firebase.database.DatabaseError) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 13 with ChildEventListener

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);
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 14 with 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) {
        }
    });
}
Also used : NavigationView(android.support.design.widget.NavigationView) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser) RadioButton(android.widget.RadioButton) DataSnapshot(com.google.firebase.database.DataSnapshot) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) TextView(android.widget.TextView) Paint(android.graphics.Paint) DatabaseError(com.google.firebase.database.DatabaseError) TextView(android.widget.TextView) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 15 with ChildEventListener

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) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) LatLng(com.google.android.gms.maps.model.LatLng) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) HashMap(java.util.HashMap) GoogleMap(com.google.android.gms.maps.GoogleMap) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

ChildEventListener (com.google.firebase.database.ChildEventListener)38 DataSnapshot (com.google.firebase.database.DataSnapshot)38 DatabaseError (com.google.firebase.database.DatabaseError)38 DatabaseReference (com.google.firebase.database.DatabaseReference)11 Intent (android.content.Intent)7 View (android.view.View)5 Product (com.example.asus.onlinecanteen.model.Product)5 Map (java.util.Map)5 ListView (android.widget.ListView)4 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)4 BrixxEventModel (com.manan.dev.ec2018app.Models.BrixxEventModel)4 TextView (android.widget.TextView)3 Transaction (com.example.asus.onlinecanteen.model.Transaction)3 LatLng (com.google.android.gms.maps.model.LatLng)3 Query (com.google.firebase.database.Query)3 HashMap (java.util.HashMap)3 AlertDialog (android.support.v7.app.AlertDialog)2 Button (android.widget.Button)2 ImageView (android.widget.ImageView)2 RadioButton (android.widget.RadioButton)2