UNPKG

react-native-here-mobility-sdk

Version:

Offical Here Mobility SDK for react native to access Here's marketplace features

230 lines (212 loc) 6.98 kB
// @flow /********************************************************** * Copyright © 2018 HERE Global B.V. All rights reserved. * **********************************************************/ import { NativeModules, NativeEventEmitter } from "react-native"; import { Address, BookingConstraints, CancellationPolicy, HereMobilityDemandError, LatLng, Price, PriceEstimate, PriceRange, RideOfferRequestSortType, RideOffersRequest, RideWaypoints, StatusFilter, Supplier, TransitOptions, TransitType, Waypoint, RidePreferences } from "./models/Demand"; import { CancellationInfo, CancellationStatus, CancellingParty, DriverDetails, PassengerDetails, Ride, RideRecord, RideLocation, RideStatus, RideStatusLog, Vehicle, VehicleType, RideQuery } from "./models/Ride"; import { RideOffers } from "./models/RideOffers"; import { PublicTransportRouteLeg, RideOfferBase, TaxiRideOffer, TransportMode } from "./models/RideOffers"; // Event emitter, use to receive ride status updates. const eventEmitter = new NativeEventEmitter(NativeModules.HereSDKDemand); /** * HereMobilitySDKDemand is the client wrapper for accessing the Demand API, used for requesting, booking and managing rides. * HERE Mobility offers a mobility platform solution for transportation service providers, businesses, and consumers. * The platform consists of the HERE Mobility Marketplace and API/SDK packages. * The HERE Mobility Marketplace is a "broker" between transportation suppliers and consumers, which matches up ride requests with ride offers. * The HERE Mobility SDK enables developers to create apps with a variety of mobility features, while connecting to the HERE Mobility Marketplace. */ class HereMobilitySDKDemand { /** * Request ride offers that match a given request. * * @param request Ride offers request parameters. * @param callback Callback which will be called with the result. The first parameter is the ride offers array. The second parameter is the error (if any). */ static getRideOffers( request: RideOffersRequest, callback: (response: ?RideOffers, error: ?Object) => void ) { NativeModules.HereSDKDemand.getRideOffers(request, function( offersJSON, err ) { if (err) { callback(null, JSON.parse(err)); } else { callback(new RideOffers(JSON.parse(offersJSON)), null); } }); } /** * Book a ride and create a new ride object with the given offer ID. * * @param rideOfferId The ride offer ID received from getRideOffers * @param passengerDetails Details about the passenger booking the ride (name, phone number, photo URL). * @param callback Callback which will be called with the result. The first parameter is the ride object. The second parameter is the error (if any). */ static createRide( rideOfferId: string, passengerDetails: PassengerDetails, ridePreferences: ?RidePreferences, callback: (response: ?Ride, error: ?Object) => void ) { NativeModules.HereSDKDemand.createRide( rideOfferId, passengerDetails, ridePreferences, function(ride, err) { if (err) { callback(null, JSON.parse(err)); } else { callback(JSON.parse(ride), null); } } ); } /** * Get a ride by rideId. * * @param rideId The ID of the ride to retrieve. * @param callback Callback which will be called with the result. The first parameter is the ride object. The second parameter is the error (if any). */ static getRide( rideId: string, callback: (response: ?Ride, error: ?Object) => void ) { NativeModules.HereSDKDemand.getRide(rideId, function(ride, err) { if (err) { callback(null, JSON.parse(err)); } else { callback(JSON.parse(ride), null); } }); } /** * Cancel a ride * * @param rideId The ID of the ride to be cancelled. * @param reason The cancellation reason. * @param callback A callback which returns information about the cancellation request (e.g. whether the request was accepted or rejected) and errors if any. */ static cancelRide( rideId: string, reason: string, callback: (cancellationInfo: ?CancellationInfo, err: ?Object) => void ) { NativeModules.HereSDKDemand.cancelRide(rideId, reason, function( cancellationInfo, err ) { if ((cancellationInfo, err)) { callback(null, JSON.parse(err)); } else { callback(JSON.parse(cancellationInfo), null); } }); } /** * Query for rides with the given statuses and update time. * * @param rideQuery A RideQuery object that contains fields specifying ride update times and statuses with which you want to filter results. * @param callback Callback which will be called with the query results. The first parameter is an array of rides, the second is the error(if any). */ static getRides( rideQuery: ?RideQuery, callback: (response: ?RideQueryResponse, error: ?Object) => void ) { NativeModules.HereSDKDemand.getRides(rideQuery, function(rides, err) { if (err) { callback(null, JSON.parse(err)); } else { callback(JSON.parse(rides), null); } }); } /** * Register for ride updates. * NOTE: Only one simultaneous registration for all types of updates is possible from this module. * * @param rideStatusChanged A callback for handling changes to the ride status * @param rideStatusChanged A callback for handling changes to the ride location * @param rideStatusChanged A callback for handling errors */ static registerForRidesUpdates( rideStatusChanged: (ride: Ride, rideStatusLog: RideStatusLog) => void, rideLocationChanged: (ride: Ride, rideLocation: RideLocation) => void, error: (error: ?HereMobilityDemandError) => void ) { // Register via native module. NativeModules.HereSDKDemand.registerForRidesUpdates(); // Add rides updates events eventEmitter.addListener("ride_status_changed", message => rideStatusChanged( JSON.parse(message).ride, JSON.parse(message).rideStatusLog ) ); eventEmitter.addListener("ride_location_changed", message => rideLocationChanged( JSON.parse(message).ride, JSON.parse(message).rideLocation ) ); // Add rides update error event. eventEmitter.addListener("ride_updates_error", message => error(JSON.parse(message)) ); } /** * Unregister for ride updates. */ static unregisterFromRidesUpdates() { // Unregister client module. NativeModules.HereSDKDemand.unregisterFromRidesUpdates(); //Remove events [ "ride_updates_error", "ride_status_changed", "ride_location_changed" ].forEach(eventName => { eventEmitter.removeListener(eventName); }); } } module.exports = HereMobilitySDKDemand;