Skip to content

Commit

Permalink
Merge pull request #89 from hyperoslo/refactor/documentation-tools
Browse files Browse the repository at this point in the history
Refactoring: update documentation
  • Loading branch information
vadymmarkov authored Jan 25, 2018
2 parents a333fcc + 1b4217c commit 752dbe6
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ final class ViewController: UIViewController {
@IBOutlet var pushScannerButton: UIButton!

@IBAction func handleScannerPresent(_ sender: Any, forEvent event: UIEvent) {
let controller = makeBarcodeScannerViewController()
controller.title = "Barcode Scanner"
present(controller, animated: true, completion: nil)
let viewController = makeBarcodeScannerViewController()
viewController.title = "Barcode Scanner"
present(viewController, animated: true, completion: nil)
}

@IBAction func handleScannerPush(_ sender: Any, forEvent event: UIEvent) {
let controller = makeBarcodeScannerViewController()
controller.title = "Barcode Scanner"
navigationController?.pushViewController(controller, animated: true)
let viewController = makeBarcodeScannerViewController()
viewController.title = "Barcode Scanner"
navigationController?.pushViewController(viewController, animated: true)
}

private func makeBarcodeScannerViewController() -> BarcodeScannerViewController {
Expand All @@ -33,8 +33,7 @@ extension ViewController: BarcodeScannerCodeDelegate {
print("Barcode Data: \(code)")
print("Symbology Type: \(type)")

let delayTime = DispatchTime.now() + Double(Int64(6 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime) {
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
controller.resetWithError()
}
}
Expand Down
142 changes: 75 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ barcode capturing functionality and a great user experience.

### Controller

To start capturing just instantiate `BarcodeScannerController`, set needed
To start capturing just instantiate `BarcodeScannerViewController`, set needed
delegates and present it:

```swift
let controller = BarcodeScannerController()
controller.codeDelegate = self
controller.errorDelegate = self
controller.dismissalDelegate = self
let viewController = BarcodeScannerViewController()
viewController.codeDelegate = self
viewController.errorDelegate = self
viewController.dismissalDelegate = self

present(controller, animated: true, completion: nil)
present(viewController, animated: true, completion: nil)
```

<div align="center">
<img src="https://github.com/hyperoslo/BarcodeScanner/blob/master/Art/ExampleScanning.png" alt="BarcodeScanner scanning" width="270" height="480" />
</div><br/>

You can also push `BarcodeScannerController` to your navigation stack:
You can also push `BarcodeScannerViewController` to your navigation stack:

```swift
let controller = BarcodeScannerController()
controller.codeDelegate = self
let viewController = BarcodeScannerViewController()
viewController.codeDelegate = self

navigationController?.pushViewController(controller, animated: true)
navigationController?.pushViewController(viewController, animated: true)
```

### Delegates
Expand All @@ -72,8 +72,7 @@ Use `BarcodeScannerCodeDelegate` when you want to get the captured code back.

```swift
extension ViewController: BarcodeScannerCodeDelegate {

func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
func barcodeScanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
print(code)
controller.reset()
}
Expand All @@ -85,8 +84,7 @@ extension ViewController: BarcodeScannerCodeDelegate {
Use `BarcodeScannerErrorDelegate` when you want to handle session errors.
```swift
extension ViewController: BarcodeScannerErrorDelegate {

func barcodeScanner(_ controller: BarcodeScannerController, didReceiveError error: Error) {
func barcodeScanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) {
print(error)
}
}
Expand All @@ -95,97 +93,107 @@ extension ViewController: BarcodeScannerErrorDelegate {
**Dismissal delegate**

Use `BarcodeScannerDismissalDelegate` to handle "Close button" tap.
**Please note** that `BarcodeScannerController` doesn't dismiss itself if it was
presented initially.
**Please note** that `BarcodeScannerViewController` doesn't dismiss itself if
it was presented initially.

```swift
extension ViewController: BarcodeScannerDismissalDelegate {

func barcodeScannerDidDismiss(_ controller: BarcodeScannerController) {
func barcodeScannerDidDismiss(_ controller: BarcodeScannerViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
```

### Actions

When the code is captured `BarcodeScannerController` switches to the processing
When the code is captured `BarcodeScannerViewController` switches to the processing
mode:

<div align="center">
<img src="https://github.com/hyperoslo/BarcodeScanner/blob/master/Art/ExampleLoading.png" alt="BarcodeScanner loading" width="270" height="480" />
</div><br/>

While the user see a nice loading animation you can perform some
While the user sees a nice loading animation you can perform some
background task, for example make a network request to fetch product info based
on the code. When the task is done you have 3 options to proceed:

1. Dismiss `BarcodeScannerController` and show your results.
1. Dismiss `BarcodeScannerViewController` and show your results.

```swift
func barcodeScanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
// Code processing
controller.dismiss(animated: true, completion: nil)
}
```

```swift
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
// Code processing
controller.dismiss(animated: true, completion: nil)
}
```
2. Show an error message and switch back to the scanning mode (for example,
when there is no product found with a given barcode in your database):

<div align="center">
<img src="https://github.com/hyperoslo/BarcodeScanner/blob/master/Art/ExampleError.png" alt="BarcodeScanner error" width="270" height="480" />
</div><br/>
<div align="center">
<img src="https://github.com/hyperoslo/BarcodeScanner/blob/master/Art/ExampleError.png" alt="BarcodeScanner error" width="270" height="480" />
</div><br/>

```swift
func barcodeScanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
// Code processing
controller.resetWithError(message: "Error message")
// If message is not provided the default message will be used instead.
}
```

```swift
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
// Code processing
controller.resetWithError(message: "Error message")
// If message is not provided the default message from the config will be used instead.
}
```
3. Reset the controller to the scanning mode (with or without animation):

```swift
func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
func barcodeScanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
// Code processing
controller.reset(animated: true)
}
```

If you want to do continuous barcode scanning just set the `isOneTimeSearch`
property on your `BarcodeScannerController` instance to `false`.
property on your `BarcodeScannerViewController` instance to `false`.

### Customization

We styled **BarcodeScanner** to make it look nice, but feel free to customize
its appearance by changing global configuration variables:
We styled **BarcodeScanner** to make it look nice, but you can always use public
properties or inheritance to customize its appearance.

**Header**

```swift
let viewController = BarcodeScannerViewController()
viewController.headerViewController.titleLabel.text = "Scan barcode"
viewController.headerViewController.closeButton.tintColor = .red
```

**Please note** that `HeaderViewController` is visible only when
`BarcodeScannerViewController` is being presented.

**Footer and messages**

```swift
let viewController = BarcodeScannerViewController()
viewController.messageViewController.regularTintColor = .black
viewController.messageViewController.errorTintColor = .red
viewController.messageViewController.textLabel.textColor = .black
```

**Camera**
```swift
let viewController = BarcodeScannerViewController()
viewController.barCodeFocusViewType = .animated
let title = NSAttributedString(
string: "Settings",
attributes: [.font: UIFont.boldSystemFont(ofSize: 17), .foregroundColor : UIColor.white]
)
viewController.settingButton.setAttributedTitle(title, for: UIControlState())
```

**Metadata**
```swift
// Strings
BarcodeScanner.Title.text = NSLocalizedString("Scan barcode", comment: "")
BarcodeScanner.CloseButton.text = NSLocalizedString("Close", comment: "")
BarcodeScanner.SettingsButton.text = NSLocalizedString("Settings", comment: "")
BarcodeScanner.Info.text = NSLocalizedString(
"Place the barcode within the window to scan. The search will start automatically.", comment: "")
BarcodeScanner.Info.loadingText = NSLocalizedString("Looking for your product...", comment: "")
BarcodeScanner.Info.notFoundText = NSLocalizedString("No product found.", comment: "")
BarcodeScanner.Info.settingsText = NSLocalizedString(
"In order to scan barcodes you have to allow camera under your settings.", comment: "")

// Fonts
BarcodeScanner.Title.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.CloseButton.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.SettingsButton.font = UIFont.boldSystemFont(ofSize: 17)
BarcodeScanner.Info.font = UIFont.boldSystemFont(ofSize: 14)
BarcodeScanner.Info.loadingFont = UIFont.boldSystemFont(ofSize: 16)

// Colors
BarcodeScanner.Title.color = UIColor.black
BarcodeScanner.CloseButton.color = UIColor.black
BarcodeScanner.SettingsButton.color = UIColor.white
BarcodeScanner.Info.textColor = UIColor.black
BarcodeScanner.Info.tint = UIColor.black
BarcodeScanner.Info.loadingTint = UIColor.black
BarcodeScanner.Info.notFoundTint = UIColor.red
// Add extra metadata object type
let viewController = BarcodeScannerViewController()
viewController.metadata.append(AVMetadataObject.ObjectType.qr)
```

## Installation
Expand Down
Loading

0 comments on commit 752dbe6

Please sign in to comment.