ActiveCustomerManager

Interface

The interface for actions performed by an active customer are detailed in ActiveCustomerManagerProtocol and ActiveCustomerManagerDelegate.
We will now go over each API in this protocol.

public protocol ActiveCustomerManagerProtocol {
    var isLoggedIn: Bool { get }
    func login(
        withToken token: String,
        secret: String,
        region: String,
        completion: @escaping (Error?) -> Void
    )
    func logout(completion: @escaping () -> Void)

    var activeTask: Task? { get }
    func startTask(with taskId: Int, completion: @escaping (Error?) -> Void)
    func arriveAtWaypoint(completion: @escaping (Error?) -> Void)
    func arriveAtWaypoint(
        customerVehicle: CustomerVehicle,
        completion: @escaping (Error?) -> Void
    )
    func leaveWaypoint(completion: @escaping (Error?) -> Void)
    func updateWaypointETA(eta: Date, completion: @escaping (Error?) -> Void)
    func setUserTransportType(_ transportType: TransportType, completion: ((Error?) -> Void)?)

    func addDelegate(_ delegate: ActiveCustomerManagerDelegate) -> MulticastDelegateSubscription
}
public protocol ActiveCustomerManagerDelegate: AnyObject {
     func activeCustomerManager(_ sender: ActiveCustomerManagerProtocol, taskRemovedWithId taskId: Int)
     func activeCustomerManagerDidLogout()
 }

Authentication related

Get current login state

Allows you to check if the SDK is currently logged in.

var isLoggedIn: Bool { get }
if Bringg.shared.activeCustomerManager.isLoggedIn {
  showLoggedInView()
} else {
  httpService.getBringgLoginParams { params in
    Bringg.shared.activeCustomerManager.login(withToken: params.token, secret: params.secret, region: params.region) { _ in }
  }
}

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.

func login(withToken token: String, secret: String, region: String, completion: @escaping (Error?) -> Void)
if Bringg.shared.activeCustomerManager.isLoggedIn {
  showLoggedInView()
} else {
  httpService.getBringgLoginParams { params in
    Bringg.shared.activeCustomerManager.login(withToken: params.token, secret: params.secret, region: params.region) { _ in }
  }
}

📘

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.

func logout(completion: @escaping () -> Void)
private func userPressedLogoutButton() {
  logoutActivityIndicatorView.startAnimating()
  Bringg.shared.activeCustomerManager.logout {
    self.logoutActivityIndicatorView.stopAnimating()
  }
}

Task related

Active Task

Returns the started task associated with the customer if one exists.

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).

func startTask(with taskId: Int, completion: @escaping (Error?) -> Void)
activityIndicatorView.startAnimating()
Bringg.shared.activeCustomerManager.startTask(with: taskId) { error in
  self.activityIndicatorView.stopAnimating()
  if let error = error {
    self.showStartTaskFailedAlert()
  }
}

Arrive at waypoint

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

func arriveAtWaypoint(completion: @escaping (Error?) -> Void)
func userPressedArriveToPickupPointButton() {
  activityIndicatorView.startAnimating()
  Bringg.shared.activeCustomerManager.arriveAtWaypoint { error in
    self.activityIndicatorView.stopAnimating()
    if let error = error {
        self.presentArriveAtWaypointFailedAlert()    
    }
  }
}

Arrive at waypoint with parking and vehicle details

Changes the active task status to "checked-in" with additional information regarding the customer parking location and vehicle details.
Both parking location and vehicle details attribues are optionals.

func arriveAtWaypoint(
    customerVehicle: CustomerVehicle,
    completion: @escaping (Error?) -> Void
)
func userPressedArriveAtPickupPointWithVehicleDetails() {
    let customerVehicle = CustomerVehicle(licensePlate: "A417823C", model: "Ford Focus", parkingSpot: "B9")
    Bringg.shared.activeCustomerManager.arriveAtWaypoint(
        customerVehicle: customerVehicle) { error in
        if error != nil {
            self.presentArriveAtWaypointFailedAlert()
            return
        }
    }
}

func userPressedArriveAtPickupPointWithParkingDetails() {
    let customerVehicle = CustomerVehicle(color: "Green", parkingSpot: "104")
    Bringg.shared.activeCustomerManager.arriveAtWaypoint(
        customerVehicle: customerVehicle) { error in
        if error != nil {
            self.presentArriveAtWaypointFailedAlert()
            return
        }
    }
}

Leave waypoint

Calling this changes the active task status to "done".
When the task is moved to the done state either by the customer using the SDK or by someone in the store or pickup location, the customer will be moved to the offline state where there is no active location tracking.

func leaveWaypoint(completion: @escaping (Error?) -> Void)
func userPressedLeavePickupPointButton() {
  activityIndicatorView.startAnimating()
  Bringg.shared.activeCustomerManager.leaveWaypoint { error in
    self.activityIndicatorView.stopAnimating()
    if let error = error {
        self.presentLeaveWaypointFailedAlert()
    }
  }
}

Getting events from the SDK

Add delegate

You can register multiple delegates to SDK events by using the addDelegate(_:) method. This will allow you to react to changes in the SDK state.
Events are detailed in ActiveCustomerManagerDelegate

func addDelegate(_ delegate: ActiveCustomerManagerDelegate) -> MulticastDelegateSubscription
class SDKEventListener: ActiveCustomerManagerDelegate {
  init() {
    Bringg.shared.activeCustomerManager.addDelegate(self)
  }
  
    func activeCustomerManager(_ sender: ActiveCustomerManagerProtocol, taskRemovedWithId taskId: Int) {
    // task is done - consider going offline
  }
  
    func activeCustomerManagerDidLogout() {
    // try to re-login if needed
    }
}