Garmaine Staff asked 1 year ago

Right, so the app basically connects a driver to a user. When a user makes a request it searches for the nearest driver, however my problem is what if there are no drivers available. It currently will just continue searching unless the user cancels the request themselves.

What I tried to implement in the onGeoQueryReady method was if radius <= 5 findClosestTradesman else endRequest() but it didn't seem to work.

I was hoping someone could see how I could end a request after a given time of searching say, or if a maximum radius is reached then end request.

 private void getClosestTradesman() {
    DatabaseReference tradesmanLocation = FirebaseDatabase.getInstance().getReference().child("TradesmanAvailable");

    GeoFire geoFire = new GeoFire(tradesmanLocation);
    geoQuery = geoFire.queryAtLocation(new GeoLocation(customerLocation.latitude, customerLocation.longitude), radius);
    geoQuery.removeAllListeners();

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            if (!tradesmanFound && requestBol) {
                DatabaseReference customerDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(key);
                customerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists() && dataSnapshot.getChildrenCount() > 0) {
                            Map<String, Object> tradesmanMap = (Map<String, Object>) dataSnapshot.getValue();
                            if (tradesmanFound) {
                                return;
                            }

                            if (tradesmanMap.get("Trade").equals(tradeType)) {
                                tradesmanFound = true;
                                tradesmanFoundID = dataSnapshot.getKey();

                                DatabaseReference tradesmanRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(tradesmanFoundID).child("CustomerRequest");
                                String customerID = FirebaseAuth.getInstance().getCurrentUser().getUid();
                                HashMap map = new HashMap();
                                map.put("CustomerRequestID", customerID);
                                tradesmanRef.updateChildren(map);

                                getTradesmanLocation();
                                getTradesmanInfo();
                                isRequestFinished();
                                requestTradesmanBtn.setText("Looking for Tradesman's Location...");
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        }

        @Override
        public void onKeyExited(String key) {

        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }

        @Override
        public void onGeoQueryReady() {
            if (!tradesmanFound) {
                radius++;
                getClosestTradesman();
            }
            //else{
              //  endRequest();
                //Toast.makeText(getApplicationContext(), "No Tradesman Found.", Toast.LENGTH_LONG).show();


        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });
}

As you can see it currently increments the radius so I thought maybe if it reaches a maximum radius e.g. 10 miles then it stops searching and ends request (endRequest() will cancel the request).