shiftManager

Use the shiftManager to integrate user shift-related functionality into your own apps.

Overview

The Bringg Driver SDK for iOS shiftManager allows you to integrate Bringg functionality into your own iOS apps using Bringg user shift-related events and methods, including: starting a shift, force start a shift to ignore any shift errors, and ending a shift. The Bringg Driver SDK for iOS provides the internal functionality and you can customize your apps using that functionality. You can also register your objects for shift-related event notifications and customize your app for these events.

The shiftManager conforms to ShiftManagerProtocol which defines its API, including the methods that the you can use for Bringg shift functionality.

๐Ÿ‘

Objective-C

You can adapt the shiftManager into Objective-C.

The following is the ShiftManagerProtocol :

@objc public protocol ShiftManagerProtocol {
    @objc var currentShift: Shift? { get }

    @objc func addDelegate(_ delegate: ShiftManagerDelegate)
    @objc func removeDelegate(_ delegate: ShiftManagerDelegate)

    @objc func startShift(completion: @escaping (_ networkError: Error?, _ stateError: StartShiftErrorType)->Void)
    @objc func forceStartShift(completion: @escaping (_ error: Error?)->Void)
    @objc func endShift()

ShiftEventsDelegate

The Bringg Driver SDK for iOS uses the multiple delegation design pattern allowing you to register any object for shift events. To register for shift events, the object must conform to the ShiftEventsDelegate protocol.

ShiftEventsDelegate is:

  • The protocol that defines the shift event methods a delegate must implement. A delegate might be any object in the system that requires notification on shift events.
  • The protocol to which a class must conform for notifications of shift events. Objects of that class must be added as delegates of shiftManager.

The following is the ShiftEventsDelegate protocol.

@objc public protocol ShiftManagerDelegate: class {
    @objc func shiftStarted()
    @objc func shiftEnded()
}

๐Ÿ“˜

ShiftEventsDelegate Is Optional

If your objects do not require shift event notifications, your objects do not need to be delegates and you do not need to use the ShiftEventsDelegate methods.

Feature Summary

shiftManager Property

Functionality

currentShift

Optional

If the user is currently on a shift, this property returns a Bringg Shift data type. If the current user is not on shift, the property returns nil.

Note: Bringg data types are immutable.



shiftManager Method

Functionality

addDelegate(_:)

Registers an object as a ShiftEventsDelegate. In most cases, this method is called on an object's init or load.

removeDelegate(_:)

Unregisters an object as a ShiftEventsDelegate.

startShift(completion:)

Starts a shift for the current user.

forceStartShift(completion:)

Forces a start to shift for the current user, ignoring the current shift state. For example, this method starts a shift if the user is already on a shift on another device.

endShift()

Ends a shift for the current user.



Delegate Method

Event Triggering the Delegate Method

shiftStarted()

This delegate method is called after a shift start event.

shiftEnded()

This delegate method is called after a shift end event.

Registering for Shift Events

๐Ÿ“˜

Registering for Shift is Optional

If your objects do not require shift event notifications, they do not need to register for events.

Step 1. Conforming to ShiftEventsDelegate

Your class must conform to the ShiftEventsDelegate protocol.

For example, ShiftViewController uses shiftManager and must conform to ShiftEventsDelegate.

class ShiftViewController: UIViewController, **ShiftEventsDelegate** {

Step 2. Implementing ShiftEventsDelegate methods

Implement the ShiftEventsDelegate methods in your class.

For example, ShiftViewController implements shiftStarted and shiftEnded to update the UI with the new state.

class ShiftViewController: UIViewController, ShiftEventsDelegate {
	
	func **shiftStarted**() {
        setViewTextAndEnabledDependingOnIsOnShiftState()
    }

    func **shiftEnded**() {
        setViewTextAndEnabledDependingOnIsOnShiftState()
        if view.window != nil {
            showMessage(title: "Shift ended", message: "Shift ended from the server")
        }
    }
}

Step 3. Registering as a delegate

Call addDelegate(_:) on shiftManager and pass it the object that needs to be notified on shift events (the object that conforms to ShiftEventsDelegate).

For example, when ShiftViewController loads, viewDidLoad registers self as the ShiftEventsDelegate.

Note: To end notifications for events, call removeDelegate(_:) on shiftManager.

override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

       ** Bringg.shared.shiftManager.addDelegate(self)**

        setViewTextAndEnabledDependingOnIsOnShiftState()
    }

An Example Using shiftManager

The following example shows startShiftButtonPressed using the shiftManager method startShift (:completion:).

@objc private func startShiftButtonPressed(_ sender: UIButton) {
        activityIndicatorView.startAnimating()
        **Bringg.shared.shiftManager.startShift { (error, shiftStateError)** in
            self.activityIndicatorView.stopAnimating()

            if let error = error {
                self.showError("Error starting shift. (error)")
                return
            }
            switch shiftStateError {
            case .none:
                print("Started shift")
                self.setViewTextAndEnabledDependingOnIsOnShiftState()
            case .alreadyExists:
                self.handleShiftStartFailedDueToErrorWithForceStartOption(message: "Shift already exists")
            case .alreadyExistsOnDifferentDevice:
                self.handleShiftStartFailedDueToErrorWithForceStartOption(message: "Shift already exists on different device")
            case .notAllowedDueToDistanceFromHome:
                self.handleShiftStartFailedDueToErrorWithForceStartOption(message: "Shift start not allowed due to distance from home")
            case .notAllowedDueToScheduleTimeOfDay:
                self.handleShiftStartFailedDueToErrorWithForceStartOption(message: "Shift start not allowed due to schedule time of day")
            case .notAllowedDueToDistanceFromScheduleHomeAndTimeOfDay:
                self.handleShiftStartFailedDueToErrorWithForceStartOption(message: "Shift start not allowed due to schedule time of day and distance from home")
            }
        }

๐Ÿ“˜

Additional Examples

For a complete example, please refer to the Example folder in the Bringg-iOS-DriverSDK GitHub repo

shiftManager Property

currentShift

Use to get the currently logged in user's shift information, if a user is currently logged in.

currentShift

Shift

OPTIONAL

A Bringg Shift data type. If the current user is not on shift, the value is nil.

Note: Bringg data types are immutable.

shiftManager Methods

addDelegate(_:)

Use to register an object as a ShiftEventsDelegate. In most cases, this method is called on an object's init or load.

@objc func addDelegate(_ delegate: ShiftManagerDelegate)

Input Parameters

ShiftEventsDelegate

delegate

REQUIRED

An object that needs to be notified on shift events. The value should conform to ShiftEventsDelegate protocol (see Registering for Shift Events, Step 1. Conforming to ShiftEventsDelegate).

removeDelegate(_:)

Use to unregister an object as a ShiftEventsDelegate.

@objc func removeDelegate(_ delegate: ShiftManagerDelegate)

Input Parameters

ShiftEventsDelegate

delegate

REQUIRED

An object that no longer needs to be notified on user events. The value should conform to ShiftEventsDelegate protocol (see Registering for Shift Events, Step 1. Conforming to ShiftEventsDelegate).

startShift(completion:)

Use to start a user shift. If successful, this method returns nothing. If not successful, one of two error messages is returned. The error message is either a network error or start shift error.

@objc func startShift(completion: @escaping (_ networkError: Error?, _ stateError: StartShiftErrorType)->Void)

Input Parameters

None

Completion

Error

String

If a network error occurs, the error message indicating the reason for failure.

StartShiftErrorType

String

If a start shift occurs, the error message indicating the reason the shift could not start.

forceStartShift(completion:)

Use to a start to shift for the current user, ignoring the current shift state. For example, this method starts a shift if the user is already on a shift on another device. If not successful, an error message is returned.

: A forced start shift will start a shift regardless of the current shift state.

@objc func forceStartShift(completion: @escaping (_ error: Error?)->Void)

Input Parameters

None

Completion

Error

String

If an error occurs, the error message indicating the reason for failure.

endShift()

Use to end a user shift. If successful or not successful, this method returns nothing.

@objc func endShift()

Input Parameters

None

Completion

None

ShiftEventsDelegate Methods

shiftStarted()

This delegate method is called after a shift start event. After this method is called, the logged-in user data is available using the currentShift property. You must register for shift events to use this method.

@objc func shiftStarted()

shiftEnded()

This delegate method is called after a shift end event. A user logout event can be the result of either calling logout method, or the SDK's internal logic triggering the logout event. You must register for shift events to use this method.

@objc func shiftEnded()