/AftermathCompass

Message-driven navigation system built on top of Aftermath and Compass.

Primary LanguageSwiftOtherNOASSERTION

AftermathCompass

CI Status Version Carthage Compatible License Platform

Description

AftermathCompass is a message-driven routing system built on top of Aftermath and Compass.

Usage

Create your first route and error handler.

import Compass

struct UserRoute: Routable {

  func navigate(to location: Location, from currentController: Controller) throws {
    guard let id = location.arguments["id"] else {
      throw RouteError.InvalidArguments(location)
    }

    let controller = UserController(id: id)
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}

struct ErrorRoute: ErrorRoutable {

  func handle(routeError: ErrorType, from currentController: Controller) {
    let controller = ErrorController(error: routeError)
    currentController.navigationController?.pushViewController(controller, animated: true)
  }
}

Optionally, you can create a command route to build new commands based on Location:

class UpdateUserRoute: CommandRoute {

  func buildCommand(from location: Location) throws -> AnyCommand {
    guard let params = location.payload as? User {
      throw UserError
    }

    return UpdateUserCommand(parameters: params)
  }
}

Configure Compass scheme, router and Aftermath in your AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  let router = Router()
  let commandRouter = CommandRouter()
  var compassManager: CompassManager!
  lazy var navigationController = UINavigationController(rootViewController: ViewController())

  lazy var window: UIWindow? = {
    let window = UIWindow(frame: UIScreen.mainScreen().bounds)
    return window
  }()

  func currentController() -> UIViewController {
    return navigationController.topViewController!
  }

  // ...

  func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
      window?.rootViewController = navigationController
      window?.makeKeyAndVisible()

      // ...
      configureCompass()
      return true
  }

  func configureCompass() {
    router.errorRoute = ErrorRoute()
    router.routes = ["users:{id}": UserRoute()]
    commandRouter.routes = ["users:update:{id}": UpdateUserRoute()]

    Compass.scheme = "aftermath"
    Compass.routes = Array(router.routes.keys) + Array(commandRouter.routes.keys)

    compassManager = CompassManager(
      router: { self.router },
      commandRouter: { self.commandRouter },
      currentController: currentController
    )
  }

  // ...
}

Start your journey:

import Aftermath
import AftermathCompass

class ViewController: UIViewController, CommandProducer  {

  // ...

  // Navigate to URN
  func openUser(id: Int) {
    execute(CompassCommand(URN: "users:\(id)"))
  }

  // Execute command from URN
  func updateUser(user: User) {
    execute(CompassCommand(URN: "users:\(user.id)", payload: user))
  }

  // ...
}

Installation

AftermathCompass is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'AftermathCompass'

AftermathCompass is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/AftermathCompass"

AftermathCompass can also be installed manually. Just download and drop Sources folders in your project.

Author

Hyper Interaktiv AS, ios@hyper.no

Contributing

We would love you to contribute to AftermathCompass, check the CONTRIBUTING file for more info.

License

AftermathCompass is available under the MIT license. See the LICENSE file for more info.