public void OnDataChange(DataSnapshot snapshot)
        {
            if (snapshot.Value != null)
            {
                if (snapshot.Child("driver_id").Value.ToString() != "waiting")
                {
                    string status = "";
                    double fares  = 0;

                    if (!isDriverAccepted)
                    {
                        AcceptedDriver acceptedDriver = new AcceptedDriver();
                        acceptedDriver.ID       = snapshot.Child("driver_id").Value.ToString();
                        acceptedDriver.fullname = snapshot.Child("driver_name").Value.ToString();
                        acceptedDriver.phone    = snapshot.Child("driver_phone").Value.ToString();
                        isDriverAccepted        = true;
                        DriverAccepted.Invoke(this, new DriverAcceptedEventArgs {
                            acceptedDriver = acceptedDriver
                        });
                    }

                    //Gets Status
                    if (snapshot.Child("status").Value != null)
                    {
                        status = snapshot.Child("status").Value.ToString();
                    }

                    //Get Fares
                    if (snapshot.Child("fares").Value != null)
                    {
                        fares = double.Parse(snapshot.Child("fares").Value.ToString());
                    }

                    if (isDriverAccepted)
                    {
                        //Get Driver Location Updates
                        double driverLatitude       = double.Parse(snapshot.Child("driver_location").Child("latitude").Value.ToString());
                        double driverLongitude      = double.Parse(snapshot.Child("driver_location").Child("longitude").Value.ToString());
                        LatLng driverLocationLatLng = new LatLng(driverLatitude, driverLongitude);
                        TripUpdates.Invoke(this, new TripUpdatesEventArgs {
                            DriverLocation = driverLocationLatLng, Status = status, Fares = fares
                        });
                    }
                }
            }
        }
Esempio n. 2
0
        public void OnDriverResponse(AcceptedDriver driverInfor)
        {
            if (!string.IsNullOrEmpty(driverInfor.fullname) && !isDriverAccepted)
            {
                isDriverAccepted = true;
                DriverAccepted.Invoke(this, new DriverAcceptedEventArgs {
                    acceptedDriver = driverInfor
                });
                LatLng driverLocationLatLng = new LatLng(driverInfor.Latitude, driverInfor.Longitude);
                TripUpdates.Invoke(this, new TripUpdatesEventArgs
                {
                    DriverLocation = driverLocationLatLng,
                    Status         = "accepted",
                    Fares          = Convert.ToDouble(rides.RidesInfo.TotalCost)
                });
            }
            else if (!string.IsNullOrEmpty(driverInfor.Status) && isDriverAccepted)
            {
                LatLng driverLocationLatLng = new LatLng(driverInfor.Latitude, driverInfor.Longitude);
                TripUpdates.Invoke(this, new TripUpdatesEventArgs
                {
                    DriverLocation = driverLocationLatLng,
                    Status         = driverInfor.Status,
                    Fares          = Convert.ToDouble(rides.RidesInfo.TotalCost)
                });
            }
            else
            {
                isDriverRejected = true;
            }
            //if(snapshot.Value != null)
            //{
            //    if(snapshot.Child("driver_id").Value.ToString() != "waiting")
            //    {
            //        string status = "";
            //        double fares = 0;

            //        if (!isDriverAccepted)
            //        {
            //            AcceptedDriver acceptedDriver = new AcceptedDriver();
            //            acceptedDriver.ID = snapshot.Child("driver_id").Value.ToString();
            //            acceptedDriver.fullname = snapshot.Child("driver_name").Value.ToString();
            //            acceptedDriver.phone = snapshot.Child("driver_phone").Value.ToString();
            //            isDriverAccepted = true;
            //            DriverAccepted.Invoke(this, new DriverAcceptedEventArgs { acceptedDriver = acceptedDriver });
            //        }

            //        //Gets Status
            //        if(snapshot.Child("status").Value != null)
            //        {
            //            status = snapshot.Child("status").Value.ToString();
            //        }

            //        //Get Fares
            //        if(snapshot.Child("fares").Value != null)
            //        {
            //            fares = double.Parse(snapshot.Child("fares").Value.ToString());
            //        }

            //        if (isDriverAccepted)
            //        {
            //            //Get Driver Location Updates
            //            double driverLatitude = double.Parse(snapshot.Child("driver_location").Child("latitude").Value.ToString());
            //            double driverLongitude = double.Parse(snapshot.Child("driver_location").Child("longitude").Value.ToString());
            //            LatLng driverLocationLatLng = new LatLng(driverLatitude, driverLongitude);
            //            TripUpdates.Invoke(this, new TripUpdatesEventArgs { DriverLocation = driverLocationLatLng , Status = status, Fares = fares});
            //        }
            //    }
            //}
        }
        public void CreateRequest()
        {
            requestReference      = Database.DefaultInstance.GetRootReference().GetChild("rideRequest").GetChildByAutoId();
            newTripDetails.RideID = requestReference.Key;

            var locationNode = new NSDictionary
                               (
                "latitude", newTripDetails.PickupLat.ToString(),
                "longitude", newTripDetails.PickupLng.ToString()
                               );

            var destinationNode = new NSDictionary
                                  (
                "latitude", newTripDetails.DestinationLat.ToString(),
                "longitude", newTripDetails.DestinationLng.ToString()
                                  );

            var tripDetailsNode = new NSDictionary
                                  (
                "location", locationNode,
                "destination", destinationNode,
                "destination_address", newTripDetails.DestinationAddress,
                "pickup_address", newTripDetails.PickupAddress,
                "rider_id", AppDataHelper.GetUserID(),
                "rider_name", AppDataHelper.GetFullName(),
                "rider_phone", AppDataHelper.GetPhone(),
                "created_at", newTripDetails.TimeStamp.ToString()
                                  );

            requestReference.SetValue <NSDictionary>(tripDetailsNode);
            requestReference.ObserveEvent(DataEventType.Value, (DataSnapshot snapshot) =>
            {
                //Driver has been assigned
                if (snapshot.GetChildSnapshot("driver_id").GetValue <NSObject>() != NSNull.Null)
                {
                    if (snapshot.GetChildSnapshot("driver_id").GetValue <NSObject>().ToString() != "waiting")
                    {
                        if (!isDriverAccepted)
                        {
                            // Fetch Driver Details from the snapshot
                            AcceptedDriver acceptedDriver = new AcceptedDriver();
                            acceptedDriver.ID             = snapshot.GetChildSnapshot("driver_id").GetValue <NSObject>().ToString();
                            acceptedDriver.Fullname       = snapshot.GetChildSnapshot("driver_name").GetValue <NSObject>().ToString();
                            acceptedDriver.phone          = snapshot.GetChildSnapshot("driver_phone").GetValue <NSObject>().ToString();

                            isDriverAccepted = true;
                            DriverAccepted?.Invoke(this, new DriverAcceptedEventArgs {
                                acceptedDriver = acceptedDriver
                            });
                        }


                        // Gets Trip Status

                        if (snapshot.GetChildSnapshot("status").GetValue <NSObject>() != NSNull.Null)
                        {
                            status = snapshot.GetChildSnapshot("status").GetValue <NSObject>().ToString();
                        }

                        // Get fares
                        if (snapshot.GetChildSnapshot("fares").GetValue <NSObject>() != NSNull.Null)
                        {
                            fares = double.Parse(snapshot.GetChildSnapshot("fares").GetValue <NSObject>().ToString());
                        }


                        if (isDriverAccepted)
                        {
                            // Get driver location upadtes

                            double driverLatitude  = 0;
                            double driverLongitude = 0;

                            driverLatitude  = double.Parse(snapshot.GetChildSnapshot("driver_location").GetChildSnapshot("latitude").GetValue <NSObject>().ToString());
                            driverLongitude = double.Parse(snapshot.GetChildSnapshot("driver_location").GetChildSnapshot("longitude").GetValue <NSObject>().ToString());

                            CLLocationCoordinate2D driverLocationLatLng = new CLLocationCoordinate2D(driverLatitude, driverLongitude);
                            TripUpdates?.Invoke(this, new TripUpdatesEventArgs {
                                DriverLocation = driverLocationLatLng, Status = status, Fares = fares
                            });
                        }
                    }
                }
            });
        }