Skip to content

Instantly share code, notes, and snippets.

@jakunico
Last active November 15, 2018 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakunico/905b825c7e746ebdd4985b09640541d2 to your computer and use it in GitHub Desktop.
Save jakunico/905b825c7e746ebdd4985b09640541d2 to your computer and use it in GitHub Desktop.
Mozio Mobile SDK public interface proposal
///
/// USAGE
/// Shows how the SDK would be integrated into an application, in this case Despegar's mobile app.
///
// DespegarAppDelegate.swift
// This is the entry point for an iOS app and where initialization should happen
import Mozio
let configuration = Configuration(apiKey: "some-api-key")
Mozio.shared.initialize(configuration: configuration)
// DespegarHomeViewController.swift
// This is a sample view controller where Despegar shows a button to get ground transporatation
import Mozio
func actionTapGroundTransportation() {
let query = BookQuery(startAddress: startField.text, endAddress: endField.text)
Mozio.shared.startBooking(query: query, presenter: self)
}
///
/// INTERFACE
///
public class Configuration {
public let apiKey: String
}
public protocol BookDelegate {
/// Called before presenting the booking interface
public func willPresentBooking(mozio: Mozio)
/// Called after presenting the booking interface
public func didPresentBooking(mozio: Mozio)
/// Called when the booking flow was cancelled and the booking interface was dismissed
public func didCancelBooking(mozio: Mozio)
/// Called when the booking flow has succeeded and the given `book` instance was done by the user
public func mozio(mozio: Mozio, didBookTransporatation book: Book)
}
public class Mozio {
// MARK: Public
/// The shared instance through which you interact with the Mozio SDK
public static let shared = Mozio()
/// The delegate to get informed about events from the booking flow
weak public var bookDelegate: BookDelegate?
/// Initializes the SDK with the given configuration
public func initialize(configuration: Configuration) {
guard shared.configuration == nil else {
print("Attempted to initialize more than once, ignoring call")
return
}
self.configuration = configuration
}
/// Presents the booking interface for ground transporatation
public func startBooking(query: BookQuery, presenter: UIViewController) {
guard initialized else {
print("Ignoring call to Mozio.search() because the SDK was not initialized, please call Mozio.shared.initialize() first")
return nil
}
let viewController = SearchViewController(query: query)
presenter.present(viewController, animated: true, completion: nil)
}
// MARK: Private
/// This holds the configuration of the SDK.
/// It is null if the SDK was not initialized yet.
private var configuration: Configuration?
private var initialized: Bool { return configuration != nil }
}
public class BookQuery {
public let startAddress: String?
public let endAddress: String?
static let empty = BookQuery(startAddress: nil, endAddress: nil)
}
public class Book {
// contains information about the booking made by the user
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment