Skip to content

Cobra is a lightweight application routing framework written in Swift

License

Notifications You must be signed in to change notification settings

locationlabs/Cobra

Repository files navigation

Cobra

Build Status

Cobra is a lightweight application routing framework written in Swift that provides modular abstractions to your code base. Cobra is built on top of Swinject, a lightweight dependency injection framework.

Features

  • Type safe application routing via features
  • Multiple flavor (e.g. environments) support for different components
  • Proxy powered routing for easy A/B testing of features
  • Foundation for modular code architecture

Cobra works best when used with Gorgon, an application event distribution framework written in Swift, and Moccasin, Xcode templates that provides VIPER-based scaffolding.

See Boa, a sample app written in Swift, for details.

Requirements

  • iOS 8+
  • Swift 3
  • Xcode 8.0+

Installation

Cobra is available through CocoaPods or Carthage.

CocoaPods

To install Cobra with CocoaPods, add the following lines to your Podfile.

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Cobra', '~> 3.0'

Then run pod install command. For details of the installation and usage of CocoaPods, visit its official website.

Carthage

To install Cobra with Carthage, add the following line to your Cartfile.

github "locationlabs/Cobra" ~> 3.0

Then run carthage update. For details of the installation and usage of Carthage, visit its project page.

Documentation

Cobra is a VIPER-based application routing framework for building modular iOS applications. Using Cobra, an application is broken up into VIPER modules, each module consisting of the following types: View, Interactor, Presenter, Router, Data Manager, Styler, Assembler, Storyboard and Feature. Each of these types own a single responsibility, i.e. the View is responsible for view logic, the Interactor is responsible for business logic, etc. Dividing application logic into distinct layers of responsibility this way enables developers to isolate dependencies, test interactions, and generally write clear and consistent code.

Creating a Module

Cobra works best when used with Moccasin, which leverages Xcode templates to generate VIPER-based scaffolding.

Bootstrapping

Component frameworks and VIPER modules are bootstrapped like so:

    // Create a Cobra configuration for assembling the components and properties for the application
    let config = Config(components: [
        Component<DaemonAssembly>(),
        Component<ServiceAssembly>()
    ], properties: [
        JsonProperty(name: "properties")
    ])
    
    // Provide the configuration to the Cobra application
    try! App.sharedInstance.config(config)

    // Register Feature to Module proxies for the Cobra application routes
    App.sharedInstance.registerProxies([
        Proxy<AddCityFeatureType>(modules: Module<AddCityAssembly>()),
        Proxy<WeatherFeatureType>(modules: Module<WeatherAssembly>()),
        Proxy<WeatherDetailFeatureType>(modules: Module<WeatherDetailAssembly>())
    ])
    
    // Route to our first feature in our application window
    try! App.sharedInstance.feature(WeatherFeatureType.self).showInWindow(window!)
    return true
}