Giter Site home page Giter Site logo

toolbox's Introduction

ToolBox

Build Status Version License Platform

ToolBox is a toolbox ;) library written in Swift.

Installation

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

pod 'ToolBox'

Router

Router will help you to manage routes and navigation through the application.

A route is a class (in most cases a UIViewController) which implements TBRoutable protocol:

public protocol TBRoutable: class {
  static func loadController(with data: Any?, for route: String) -> UIViewController?
}

The TBRoutable protocol has a default implementation that just returns the designated initializer. If you need more control on initialization you can override it.

class HomeViewController: UIViewController, TBRoutable {
  public static func loadController(with data: Any?, for route: String) -> UIViewController? {
    //You can add some logic like data validation
    return self.init()
  }
}

Routes can be added manually:

extension TBRouter.Route {
  static let home = TBRouter.Route("home")
}

TBRouter.addRoute(.home, routableClass: HomeViewController.self)

Or from a local or a remote routes file (JSON): Routes.json

{
  "home": {
    "class" : "HomeViewController"
  },
}
if let url = Bundle.main.url(forResource: "Routes", withExtension: "json") {
  TBRouter.loadRoutes(from: url)
}

To perform a route just call x_performRoute from UIViewController:

x_performRoute(.home, presentationType: .push)
//or
x_performRoute(.home, presentationType: .modal, data: someData, animated: true)

Or just get the route's controller and perform a presentation programmatically:

let homeController = TBRouter.route(.home, with: someData)

Container

TBContainer is a key/value RAM storage

//Set
TBContainer.add(1, for: "key")
//Get
let val: Int? = TBContainer.getValue(for: "key")
//Remove
val = TBContainer.removeValue(for: "key")

Keys are associated with value type so same key can be reused

TBContainer.add(1, for: "key")
TBContainer.add("Hello", for: "key")

let val: Int? = TBContainer.getValue(for: "key") // 1
let val: String? = TBContainer.getValue(for: "key") // "Hello"

Be careful: TBContainer doesn't work with inheritance, set and get need to use the same type

TBContainer.add(myCollectionView, for: "collectionView")
let view: UIView? = TBContainer.getValue(for: "collectionView") // nil
let collectionView: UICollectionView? = TBContainer.getValue(for: "collectionView") // myCollectionView

Services

TBServices is a service container. To create a service and make it available:

  • Create MyServiceProtocol protocol, witch inherits TBServiceProtocol
  • Create your service which implements the previous protocol and TBServiceProtocol
  • Add the service to TBServices
protocol MyServiceProtocol: TBServiceProtocol {
  // Add methods
}

final class MyService: MyServiceProtocol {
  // Add protocol implementation
  static func loadService() -> MyService {
    return MyService() // Service can be a singleton or not
  }
}

// Add service
TBServices.add(MyService.self, for: MyServiceProtocol.self)

// Retrieve service
let service: MyServiceProtocol = TBServices.retrieve()
// or
TBServices.retrieve(type: MyServiceProtocol.self)

Be careful: the service needs to be added before being retrieved

Feature flipping

You can create a new feature definition

extension TBFeature.Name {
  public static let foo = TBFeature.Name("foo")
  public static let bar = TBFeature.Name("bar")
}

And set/get feature status.

// Set
TBFeature.set(.foo, enabled: true)
TBFeature.set(.foo, enabled: false)
// Get
TBFeature.isEnabled(.foo) // false

Notification is sent when feature status did change (tbFeaturesDidChange):

NotificationCenter.default.addObserver(forName: .tbFeaturesDidChange, object: nil, queue: .main) { (notif) in
  if TBFeature.feature(.foo, matchNotification: notif) {
    // Foo feature status did change
  }
}

Printer

TBPrint allow you to print informations and toggle logging. Logger is operational only if DEBUG flag is enabled.

tbPrint("message", category: .ui)
tbDebugPrint("message", category: .ui)

To create a new category, just add:

extension TBPrint.Category {
  static let foo = TBPrint.Category("foo")
}

tbPrint("message", category: .foo)

Default Categories are :

  • network
  • database
  • data
  • file
  • service
  • ui
  • none
  • all

You can select which categories are logged:

TBPrint.categories = [PrintCategory.ui, PrintCategory.file]
//or
TBPrint.categories = [PrintCategory.all]

TBReusable

TBReusable is a protocol for cells (UICollectionView and UITableViewCell) which simplify cell registration and cell dequeue.

public class MyTableViewCell: UITableViewCell, TBReusable {

}

// Register
tableView.x_registerReusableCell(MyTableViewCell.self)

// Dequeue
let cell: MyTableViewCell = tableView.x_dequeueReusableCell(indexPath: indexPath)

Other

ToolBox contains other undocumented features like:

  • Tabbar with custom tabbar items
  • Pager with custom transitions
  • Some extensions (Date, Array, Float, String, AVURLAsset, UIViewController...)
  • UIObject

Requirements

  • iOS 10.0+ / tvOS 10.0+
  • Xcode 9.4+
  • Swift 4.2+

Example

To run the example project, clone the repository, and run pod install from the Example directory first.

Author

Nicolas Berthelot

License

ToolBox is available under the MIT license. See the LICENSE file for more informations.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.