Search in sources :

Example 66 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project BloodHub by kazijehangir.

the class SplashActivity method setView.

private void setView(String userId, final String email) {
    final String uid = userId;
    FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            String account_type = user.account_type;
            // showProgress(false);
            if (account_type.equals("individual")) {
                Intent intent;
                intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            } else if (account_type.equals("organization")) {
                Intent intent;
                intent = new Intent(SplashActivity.this, MainActivityOrg.class);
                startActivity(intent);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return;
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser) User(com.jexapps.bloodhub.m_Model.User) DatabaseError(com.google.firebase.database.DatabaseError) Intent(android.content.Intent) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 67 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project BloodHub by kazijehangir.

the class MyAppointmentDataAdapter method onBindViewHolder.

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    Appointment appointment = appointments.get(position);
    holder.cv.setTag(keys.get(position));
    if (appointment.transport) {
        holder.mTransport.setText("Yes");
    } else {
        holder.mTransport.setText("No");
    }
    if (appointment.confirmed.equals(false)) {
        holder.mStatus.setText("Pending confirmation");
    } else {
        holder.mStatus.setText("Confirmed");
    }
    holder.mDate.setText(DateFormat.getDateInstance().format(new Date(appointment.date)));
    holder.mTime.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date(appointment.date)));
    FirebaseDatabase.getInstance().getReference().child("users").child(appointment.orgid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User usr = dataSnapshot.getValue(User.class);
            holder.mName.setText(usr.username);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : Appointment(com.jexapps.bloodhub.m_Model.Appointment) User(com.jexapps.bloodhub.m_Model.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) Date(java.util.Date)

Example 68 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project Firebase-Helper by AtifAbbAsi19.

the class FireBaseHelper method addListenerForSingleValueEvent.

public void addListenerForSingleValueEvent(DatabaseReference mDatabaseRef, FirebaseCallInterface dataCallInterface) {
    if (context != null && isNetworkAvailable(context) && mDatabaseRef != null && dataCallInterface != null) {
        this.firebaseCallInterface = dataCallInterface;
        mDatabaseRef.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                firebaseCallInterface.onFirebaseCallComplete(dataSnapshot);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                firebaseCallInterface.onFirebaseCallFailure(databaseError);
            }
        });
    } else {
        if (context != null) {
            Toast.makeText(this.context, "No Internet Connection.!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this.context, "Unable To Detect Context.!", Toast.LENGTH_SHORT).show();
        }
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 69 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project GogoNew by kuldeep725.

the class MapsActivity method pickMe.

public void pickMe(View view) {
    showInternetStatus();
    if (!isInternetOn())
        return;
    if (!isWindowReady)
        return;
    if (null != mCurrentLocation && checkBusSelection != 0) {
        Log.d(TAG, "pickMe fired...");
        Log.d(TAG, "theKey = " + theKey);
        BUS = "b" + checkBusSelection;
        pickMeRadioButton = radiobutton;
        pickMeBusDistance = distance.getText().toString().trim();
        Log.e(TAG, "BUS (pickme before) = " + BUS);
        if (pickMeRadioButton != null) {
            pickMeRadioButton.setTypeface(Typeface.DEFAULT_BOLD);
            pickMeRadioButton.setTextColor(getResources().getColor(R.color.primeColor));
        }
        String lat = String.valueOf(mCurrentLocation.getLatitude());
        String lng = String.valueOf(mCurrentLocation.getLongitude());
        findNearestBusStop();
        Log.d(TAG, "findNearestBusStop has now STOPPED");
        // Log.e(TAG, "BUS (pickme after) = " + BUS);
        DatabaseReference routeDatabase = FirebaseDatabase.getInstance().getReference().child(USER).child(BUS).child("route");
        /*NOTE : addListenerForSingleValueEvent runs only after all the instances of onChildAdded have been run*/
        DatabaseReference vehicleDatabase = FirebaseDatabase.getInstance().getReference().child(VEHICLE).child(BUS);
        // routeDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        vehicleDatabase.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (minName != null) {
                    Log.d(TAG, "minName = " + minName);
                    status = (String) dataSnapshot.child(minName).child("status").getValue();
                    Log.d(TAG, "status = " + status);
                    if (checkBusSelection != 0) {
                        // Log.d(TAG, "WE ARE INSIDE NOW....");
                        userDatabase = mDatabase.child(VEHICLE).child(BUS);
                        Map<String, String> userData = new HashMap<>();
                        if (minLocation == null) {
                            Log.d(TAG, "minLocation is NULL :(");
                            minLocation = mCurrentPosition;
                        }
                        minMarker = markerMap.get(minLocation);
                        if (minMarker != null) {
                            minMarker.remove();
                            if (mMap != null) {
                                minMarker = mMap.addMarker(new MarkerOptions().position(minLocation).title(markerNameList.get(minLocation)));
                                minMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.green_stop_10));
                            }
                        }
                        if (minName == null) {
                            minName = "bus stop";
                        }
                        Log.d(TAG, "minLocation.latitude = " + minLocation.latitude);
                        Log.d(TAG, "minLocation.longitude = " + minLocation.longitude);
                        Log.d(TAG, "minDistance  = " + minDistance);
                        userData.put(LATITUDE, String.valueOf(minLocation.latitude));
                        userData.put(LONGITUDE, String.valueOf(minLocation.longitude));
                        if (theKey == null)
                            theKey = userDatabase.push().getKey();
                        Map<String, Map<String, String>> mSendingData = new HashMap<>();
                        mSendingData.put("LOCATION", userData);
                        /*Map<String, Map<String, Map<String, String>>> mFinalData = new HashMap<>();
                                mFinalData.put(INTERMEDIATE, mSendingData);*/
                        Log.e(TAG, "status = " + status);
                        if (status == null)
                            userDatabase.child(minName).child("status").setValue("0");
                        userDatabase.child(minName).child(theKey).setValue(mSendingData);
                        double difference = CalculationByDistance(new LatLng(latitudeBus, longitudeBus), minLocation);
                        Log.e(TAG, "difference = " + difference);
                        String differenceStr = "Bus " + checkBusSelection + " is at distance " + String.format(Locale.US, "%.2f", difference) + " km" + " from " + markerNameList.get(minLocation);
                        String toastString = "Request sent for " + markerNameList.get(minLocation);
                        Toast.makeText(MapsActivity.this, toastString, Toast.LENGTH_SHORT).show();
                        Toast.makeText(MapsActivity.this, differenceStr, Toast.LENGTH_SHORT).show();
                        pickMeBusDistance = "";
                        // floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.primeColorOld)));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            // floatingButton.setBackground(getResources().getDrawable(R.drawable.ic_cancel_white_24dp));
                            floatingButton.setImageResource(R.drawable.ic_cancel_white_24dp);
                            // floatingButton.setBackgroundColor(Color.parseColor("#ff0000"));
                            floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.myRed)));
                        }
                        pickMeDone = true;
                        Log.d(TAG, "findNearestBusStop has STOPPED");
                    } else {
                        Log.d(TAG, "location is null ...............");
                    }
                // }
                // @Override
                // public void onCancelled(DatabaseError databaseError) {
                // 
                // }
                // });
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) 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)

Example 70 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project GogoNew by kuldeep725.

the class MapsActivity method showDistanceInBetween.

private void showDistanceInBetween() {
    Log.e(TAG, "showDistanceInBetween fired...");
    String BUS = "b" + checkBusSelection;
    mDatabase = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference userDatabase = mDatabase.child(USER).child(BUS).child(LOCATION);
    showInternetStatus();
    if (!isInternetOn())
        return;
    userDatabase.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String bus = "b" + checkBusSelection;
            if (!userDatabase.toString().equals(mDatabase.child(USER).child(bus).child(LOCATION).toString()))
                return;
            GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
            };
            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
            // Log.d(TAG, "Data : " + dataSnapshot.getValue());
            if (mCurrentLocation == null)
                return;
            assert map != null;
            String latitudeStr = map.get("latitude");
            String longitudeStr = map.get("longitude");
            latitudeBus = Double.parseDouble(latitudeStr);
            longitudeBus = Double.parseDouble(longitudeStr);
            // Log.d(TAG, "mCurrentLocation = "+mCurrentLocation);
            if (flagShowDistanceInBetween == 0) {
                if (mCurrentLocation != null) {
                    flagShowDistanceInBetween = 1;
                    String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude() + "&destination=" + latitudeStr + "," + longitudeStr + // + R.string.google_direction_api_key;
                    "&key=AIzaSyChXllnUaESuRZPDpSHtb3oyXgL1edHITg";
                    /*String url = "https://maps.googleapis.com/maps/api/directions/json?origin="
                                                + "40.81649,-73.907807&destination=40.819585,-73.90177"+ "&key=AIzaSyChXllnUaESuRZPDpSHtb3oyXgL1edHITg";// + R.string.google_direction_api_key;*/
                    // Log.d(TAG, "URL : " + url);
                    Log.d(TAG, url);
                    DownloadTask downloadTask = new DownloadTask();
                    // Log.d(TAG, "@busDistance (SphericalUtil.computeDistanceBetween) = " + SphericalUtil.computeDistanceBetween(mCurrentPosition, new LatLng(Double.parseDouble(latitudeStr), Double.parseDouble(longitudeStr))));
                    // Start downloading json data from Google Directions API
                    downloadTask.execute(url);
                }
            } else {
                // double dist = SphericalUtil.computeDistanceBetween(mCurrentPosition,  new LatLng(latitudeBus, longitudeBus));
                double dist = CalculationByDistance(mCurrentPosition, new LatLng(latitudeBus, longitudeBus)) * 1000;
                // Log.d(TAG, "dist = "+ dist);
                long distLong = Math.round(dist);
                String timeStr;
                if (dist < 1000) {
                    String distStr = String.valueOf(distLong) + " m";
                    // Log.d(TAG, "distStr = " + distStr);
                    distance.setText(distStr);
                    distance.setPaintFlags(distance.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    // long timeLong =Math.round(9 * distLong / 100);          //      40km/hr into m/s
                    // 30km/hr into m/s
                    long timeLong = Math.round(12 * distLong / 100);
                    if (timeLong > 60) {
                        timeLong = Math.round(timeLong / 60);
                        timeStr = String.valueOf(timeLong) + " min";
                    } else {
                        timeStr = String.valueOf(timeLong) + " s";
                    }
                    // Log.d(TAG, "timeStr = " +timeStr);
                    duration.setText(timeStr);
                    duration.setPaintFlags(duration.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                } else {
                    distLong = Math.round(distLong / 1000);
                    String distStr = String.valueOf(distLong + " km");
                    // Log.d(TAG, "distStr = " + distStr);
                    distance.setText(distStr);
                    distance.setPaintFlags(distance.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    // long timeLong = Math.round(3 * distLong / 2);           //      40km/hr into km/min
                    // 30km/hr into km/min
                    long timeLong = Math.round(2 * distLong);
                    if (timeLong > 60) {
                        timeLong = Math.round(timeLong / 60);
                        timeStr = String.valueOf(timeLong) + " hr";
                    } else {
                        timeStr = String.valueOf(timeLong) + " min";
                    }
                    // Log.d(TAG, "timeStr = " +timeStr);
                    duration.setText(timeStr);
                    duration.setPaintFlags(duration.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                }
            }
        /*LatLng bus1_location = new LatLng(latitude,  longitude);
                                LatLng myLocation = new LatLng(mCurrentLocation.getLatitude(),  mCurrentLocation.getLongitude());
                                String DIFFERENCE = CalculationByDistance(myLocation, bus1_location);
                                String dist = DIFFERENCE + " Km";*/
        // distance.setText(dist);
        /*TextView time = (TextView) findViewById(R.id.time);
                String theTime = latitudeStr + ", " +  longitudeStr;
                time.setText(theTime);*/
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
// return myClass.getLongitude();
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) ValueEventListener(com.google.firebase.database.ValueEventListener) 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)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)211 ValueEventListener (com.google.firebase.database.ValueEventListener)211 DatabaseError (com.google.firebase.database.DatabaseError)210 DatabaseReference (com.google.firebase.database.DatabaseReference)62 View (android.view.View)47 Intent (android.content.Intent)43 TextView (android.widget.TextView)30 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)24 RecyclerView (android.support.v7.widget.RecyclerView)20 FirebaseUser (com.google.firebase.auth.FirebaseUser)20 HashMap (java.util.HashMap)20 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)19 Bundle (android.os.Bundle)16 ImageView (android.widget.ImageView)15 ArrayList (java.util.ArrayList)15 User (com.jexapps.bloodhub.m_Model.User)11 Map (java.util.Map)11 Date (java.util.Date)10 Query (com.google.firebase.database.Query)9 User (com.polito.mad17.madmax.entities.User)9