Skip to content
forked from nberthelot/ToolBox

ToolBox is a toolbox ;) library written in Swift.

License

Notifications You must be signed in to change notification settings

loryhuz/ToolBox

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

ToolBox is a toolbox ;) library written in Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.4%
  • Ruby 1.6%