iOS Trip feature

Our SDK can manage for you the notion of trip, giving you useful data like traveled distance or the start time of the user’s last ongoing stop.

Get the SDK instance

The trip feature is available through 2 protocols, you must chose the one that you prefer.

LRSafeRiderDelegationProtocol

protocol LRSafeRiderDelegationProtocol: LRSafeRiderProtocol {

    var tripStatusDelegate: LRTripStatusDelegate?

    func startTrip(for vehicleType: LRVehicleType) -> LRTripStatus?
    func stopTrip()
}

You can get this protocol as explained below

var safeRiderInstance = LRSafeRider.getShared(LRSafeRiderDelegationProtocol.self)!

The delegation protocol comes with one delegate that helps you to know what happens.

protocol LRTripStatusDelegate {

    func onTripStatusChanged(_ tripStatus: LRTripStatus)
    func onTripStatusError(_ error: LRSafeRiderError?)
}

The LRTripStatusDelegate allows you to be notified of changes during a trip for a given vehicle type or the possible SDK errors.

LRSafeRiderReactiveProtocol

protocol LRSafeRiderReactiveProtocol: LRSafeRiderProtocol {

    var tripStatusPublisher: AnyPublisher<LRTripStatus, Never> { get }
    var tripStatusErrorPublisher: AnyPublisher<LRSafeRiderError, Never> { get }

    func startTrip(for vehicleType: LRVehicleType) -> LRTripStatus?
    func stopTrip()
}

You can get this protocol as explained below

var safeRiderInstance = LRSafeRider.getShared(LRSafeRiderReactiveProtocol.self)!

The reactive protocol provides basically the same services than the delegation one but exposing Combine publishers.

Manage a trip

Trip status

struct LRTripStatus {

    var tripStartTime: Date
    var distanceMeter: CLLocationDistance
    var dwellStartTime: Date?
}

The LRTripStatus gives you useful data about an ongoing trip:

  • tripStartTime gives you the start time ot the trip

  • distanceMeter gives you the total traveled distance in meter from tripStartTime

  • dwellStartTime gives you the start time of the user’s last ongoing stop. If nil, user is currently moving.

Start a trip

After having initialized the SDK, you can start a trip for a given vehicle type.

self.safeRiderInstance.startTrip(for: .bicycle)

This will start a continuous service to monitor a trip for the given vehicle type.

Stop a trip

After having initialized the SDK and start a trip for a given vehicle type, you can stop it.

self.safeRiderInstance.stopTrip()

Listen to the trip status

In order to be notified of a data change during a trip for a given vehicle type, you have to do, considering the relative SafeRider protocol you are using.

Using the LRSaferiderDelegationProtocol

self.safeRiderInstance.tripStatusDelegate = self

extension MyClass: LRTripStatusDelegate {

    func onTripStatusChanged(_ tripStatus: LRTripStatus) {
        log.debug("new trip status: \(tripStatus)")
    }

    func onTripStatusError(_ error: LRSafeRiderError?) {
        log.error("trip status error: \(error.localizedDescription)")
    }
}

Using the LRSaferiderReactiveProtocol

var cancellableBag = Set<AnyCancellable>()

self.safeRiderInstance.tripStatusPublisher
.sink { tripStatus in
    log.debug("new trip status: \(tripStatus)")
}
.store(in: &self.cancellableBag)

self.safeRiderInstance.tripStatusErrorPublisher
    .sink { error in
        guard let error else { return }
            log.error("trip status error: \(error.localizedDescription)")
        }
    .store(in: &self.cancellableBag)