Init

Before using any of the apis, you should first initialize the SDK.
The SDK could either be initialized on application start up or just before a screen appears that needs the SDK.
Init should be called once.

import { initBringgDriverSDK } from 'react-native-bringg-driver-sdk';

async function startUp() {
  await initBringgDriverSDK();
}
startUp().catch(console.error);
import { initBringgDriverSDK } from 'react-native-bringg-driver-sdk';

const state: {
  didInitialize: boolean;
  initializingPromise: Promise<void> | null;
} = { didInitialize: false, initializingPromise: null };

// initBringgDriverSDK should be called once.
// initBringgDriverSDK initializes the SDK including initializing the DB and updating its local state.
// You could call it on app startup but that might make startup time longer.
// If the sdk functionallity is only needed for specific parts of the hosting app, it should probably be called when those parts appear.
export default function initBringgDriverSDKIfNeeded(): Promise<void> {
  if (state.didInitialize) {
    return Promise.resolve();
  }
  if (state.initializingPromise) {
    return state.initializingPromise;
  }

  const initializingPromise = initBringgDriverSDK();
  state.initializingPromise = initializingPromise;
  return initializingPromise;
}

ActiveCustomerManagerType Interface

The interface for actions performed by an active customer.
We will now go over each API.

export type ActiveCustomerManagerType = {
  // login related
  isLoggedIn: BehaviorSubject<boolean>;
  loginWithToken(token: string, secret: string, region: string): Promise<void>;
  logout(): Promise<void>;

  // task related
  startTask(taskId: number): Promise<void>;
  arriveAtWaypoint(): Promise<void>;
  leaveWaypoint(): Promise<void>;
  updateWaypointETA(eta: Date): Promise<void>;

  activeTask: BehaviorSubject<Task | null>;

  setUserTransportType(transportType: TransportType): Promise<void>;
};

Login related

Observe current login state

A rxjs BehaviorSubject for login state.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { isLoggedIn } = activeCustomerManager;

isLoggedIn: BehaviorSubject<boolean>;
function useIsLoggedIn(): boolean {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  useEffect(() => {
    let subscription = activeCustomerManager.isLoggedIn.subscribe(
      setIsLoggedIn
    );
    return () => subscription.unsubscribe();
  }, []);
  return isLoggedIn;
}

export default function App() {
  const isLoggedIn = useIsLoggedIn();
	if (isLoggedIn) {
    return <Text>Logged in</Text>;
  } else {
    return <Text>Not logged in</Text>
  }
}

Login

Call this method once, to authenticate the user with Bringg and allow access to various capabilities, such as changing task status and reporting location.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { loginWithToken } = activeCustomerManager;

loginWithToken(token: string, secret: string, region: string): Promise<void>;

📘

Obtaining token, secret and region params

The above params can be obtained by calling the following service:
"Generate Customer One Time Code". See reference: generate customer one-time code .

Logout

Logs the user out of the SDK. Should be called when the user logs out from the hosting app.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { logout } = activeCustomerManager;

logout(): Promise<void>;

Task related

Observe current active task state

A rxjs BehaviorSubject for active task.
The active task will be null unless the user is logged in and started a task. Once task is done, activeTask is once again set to null.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { activeTask } = activeCustomerManager;

activeTask: BehaviorSubject<Task | null>;

Start task

Changes the task status with the given id to "started".
This will also move the user to the online state - the SDK will begin active tracking of the customer (given user permission).

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { startTask } = activeCustomerManager;

startTask(id: number): Promise<void>;

📘

Location tracking

When startTask is called, the SDK will try to begin tracking the location of the customer (given user permission).
Location tracking is done natively and so the behavior of requesting user permission depends on the operating system.

iOS - The SDK will create a CLLocationManager and start tracking.
Android - The hosting app should request the user for location permissions prior to calling startTask. More information

Location tracking will stop when the active task is done (either by the user using leaveWaypoint or by any other remote way - for example task is done by the store).

Arrive to waypoint

Changes the active task status (the one started with startTask( to "checked-in." This indicates that the customer arrived at the pick-up location.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { arriveAtWaypoint } = activeCustomerManager;

arriveAtWaypoint(): Promise<void>;
function useActiveTask(): Task | null {
  const [activeTask, setActiveTask] = useState<Task | null>(null);
  useEffect(() => {
    let subscription = activeCustomerManager.activeTask.subscribe(
      setActiveTask
    );
    return () => subscription.unsubscribe();
  }, []);
  return activeTask;
}

Leave waypoint

leaveWaypoint will move the task to the 'done' state by the customer using the SDK.

If someone in the store or pickup location using the Bringg platform marks the order as 'done', the SDK will get a real time event that changes the task state to 'done'.
In some use cases where the task is marked as 'done' by the store, there is no need to use this method.

When the active task is moved to the 'done' state, activeTask will be set to null and the SDK will move to the offline state where there is no active location tracking.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { leaveWaypoint } = activeCustomerManager;

leaveWaypoint(): Promise<void>;

Changing user transport type and ETA

When there is an active task that is in the "onTheWay" status, you might want to give the user the option to change the transport type or manually update the estimated time to arrival to the pickup.
In most cases, if the user gave location tracking permission, you might want to allow him to change the transport type so that the ETA could be calculated more precisely, When the user doesn't give location tracking permissions, you might want to give the option to manually input the ETA or give a list of optional ETA time slots the user can choose from.

import { activeCustomerManager } from 'react-native-bringg-driver-sdk';
const { updateWaypointETA, setUserTransportType } = activeCustomerManager;

export enum TransportType {
  Unknown = 0,
  Stationary = 1,
  Walking = 2,
  Running = 3,
  Bicycle = 4,
  Driving = 5,
  PublicTransportation = 6,
}


updateWaypointETA(eta: Date): Promise<void>;
setUserTransportType(transportType: TransportType): Promise<void>;