<commit_before># NavKit

[![Build Status](https://travis-ci.org/wilbertliu/NavKit.svg?branch=master)](https://travis-ci.org/wilbertliu/NavKit)
[![codecov](https://codecov.io/gh/wilbertliu/NavKit/branch/master/graph/badge.svg)](https://codecov.io/gh/wilbertliu/NavKit)

Simple and integrated way to customize navigation bar experience on our app.
It should save our time that we usually use to make abstraction of navigation bar,
back button, and so on.

## Installation

### CocoaPods

Add the following line into the `Podfile` :

```
pod 'NavKit'
```

After that, run the following command :

```
$ pod install
```

## Usage

### Navigation Bar Appearance

In order to define the navigation bar appearance,
we should make a class that conforms to `NavigationConfig` protocol.
Here is an example of defining the navigation bar background color :

```swift
import Foundation
import NavKit

class MyNavigationConfig: NavigationConfig {

    var barBackgroundColor: UIColor {
        return .blue
    }

}
```

There are other properties that we could define. Make sure to check
`NavigationConfig` protocol for further usage.

### Back Button Appearance

To customize the back button appearance, we should make a class that conforms
to `BackConfig` protocol. Here is an example of defining the back button
image and text on it's right :

```swift
import Foundation
import NavKit

class MyBackConfig: BackConfig {

    var backImage: UIImage {
        return UIImage(named: "Back")!
    }

    var backText: String {
        return "Back"
    }

}
```

### Back Button Action

If we define the back button experience, by default the user would be directed to the
previous screen when tapping into the button. But sometimes we need to define the custom
behavior on some specific screen. In order to do that, we should make a class that conforms to `BackActionConfig` protocol, or possibly just conform the protocol from the respecting controller's class. Let's say we want to direct user to the "root screen", here is an example :

```swift
import UIKit
import NavKit

class ViewController: UIViewController, BackActionConfig {

    // MARK: - Back Action Config

    func backTappedAction(sender: Any) {
        _ = navigationController?.popToRootViewController(animated: true)
    }

}
```

### Gluing Altogether

To make it worked, we have to instantiate an object of `NavigationKit` class in *every view controller*, and glue all the things that we have defined above.

```swift
import UIKit
import NavKit

class ViewController: UIViewController, BackActionConfig {

    // MARK: - Properties

    var navigationKit: NavigationKit!

    // MARK: - Life Cycles

    override func viewDidLoad() {
        super.viewDidLoad()

        if let navigationController = navigationController {
            navigationKit = NavigationKit(
                navigationController: navigationController,
                navigationItem: navigationItem
            )

            navigationKit.navigationConfig = MyNavigationConfig()
            navigationKit.backConfig = MyBackConfig()
            navigationKit.backActionConfig = self

            navigationKit.doSetup()
        }
    }

    // MARK: - Back Action Config

    func backTappedAction(sender: Any) {
        _ = navigationController?.popToRootViewController(animated: true)
    }

}
```

Note : If you are sure that your project would use a single configuration for your navigation bar, you should consider to put the instantiation above into an *extension*. Thus, you just have to call the method on your *extension* in every controller. Hence, makes your code simpler.

## Support

If you have any feedbacks, feel free to submit a PR! And I'm more than happy to answer your
questions, or maybe just some hi?! To do that, shoot me a DM or tweet [@wilbertliu](https://twitter.com/wilbertliu)

## License

MIT © <NAME><commit_msg>Add new readme<commit_after># NavKit

[![Build Status](https://travis-ci.org/wilbertliu/NavKit.svg?branch=master)](https://travis-ci.org/wilbertliu/NavKit)
[![codecov](https://codecov.io/gh/wilbertliu/NavKit/branch/master/graph/badge.svg)](https://codecov.io/gh/wilbertliu/NavKit)

Simple and integrated way to customize navigation bar experience on iOS app.
It should save our time that we usually use to make abstraction of navigation bar,
back button, and so on.

## Installation

### CocoaPods

Add the following line into the `Podfile` :

```
pod 'NavKit'
```

After that, run the following command :

```
$ pod install
```

## Usage

### Defining Global Configuration

We usually want a global configuration for our navigation bar experience to maintain
design consistency throughout the app. In order to do that, we should make a class
that conforms to `NavigationConfig` protocol. Without further ado, here is an example :

```swift
import Foundation
import NavKit

class MyNavigationConfig: NavigationConfig {

    var barBackgroundColor: UIColor = .red
    var isBarTranslucent: Bool = false
    var backImage: UIImage = UIImage(named: "Back")!
    var backTappedAction: ((Any) -> Void)?

}
```

From example above, we just defined a configuration for navigation bar background color,
it's translucency, the back button image, and also whether or not we would have a custom
action when the back button is tapped. When the value of `backTappedAction` property is nil,
the action that would be performed is the standard pop animation. There are other properties that we could define, just make sure to check `NavigationConfig` protocol for further usage.

### Consuming Global Configuration

To make it worked, we have to instantiate an object of `NavigationKit` class in *every view controller*, and glue all the things that we have defined above.

```swift
import UIKit
import NavKit

class ViewController: UIViewController {

    // MARK: - Properties

    var navigationKit: NavigationKit!

    // MARK: - Life Cycles

    override func viewDidLoad() {
        super.viewDidLoad()

        if let navigationController = navigationController {
            navigationKit = NavigationKit(
                navigationConfig: MyNavigationConfig(),
                navigationController: navigationController,
                navigationItem: navigationItem
            )

            navigationKit.doSetup()
        }
    }

}
```

### Customizing Experience on Specific View Controller

In order to customize the navigation bar experience on some specific view controller,
we just have to set a new value to any property that we have defined in a class
that conforms to `NavigationConfig` protocol. Here is an example to change
the navigation bar background color and it's behavior when the back button is tapped :

```swift
import UIKit
import NavKit

class ViewController: UIViewController {

    // MARK: - Properties

    var navigationKit: NavigationKit!

    // MARK: - Life Cycles

    override func viewDidLoad() {
        super.viewDidLoad()

        if let navigationController = navigationController {
            let navigationConfig = MyNavigationConfig()
            navigationConfig.barBackgroundColor = .blue

            navigationConfig.backTappedAction = { [unowned self] sender in
                _ = self.navigationController.popToRootViewController(animated: true)
            }

            navigationKit = NavigationKit(
                navigationConfig: navigationConfig,
                navigationController: navigationController,
                navigationItem: navigationItem
            )

            navigationKit.doSetup()
        }
    }

}
```

## Support

If you have any feedbacks, feel free to submit a PR! And I'm more than happy to answer your
questions, or maybe just some hi?! To do that, shoot me a DM or tweet [@wilbertliu](https://twitter.com/wilbertliu)

## License

MIT © <NAME>
{'fraction_non_alphanumeric': 0.07467657230690405, 'fraction_numerical': 0.0, 'mean_word_length': 3.4288145207033467, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4566298', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1964, 'n_words': 842}