Skip to content

Commit

Permalink
Merge pull request #96 from hyperoslo/improve/info-messages
Browse files Browse the repository at this point in the history
Improve: add support for customisable info messages
  • Loading branch information
vadymmarkov authored Jan 31, 2018
2 parents 51acd30 + 184fd3e commit 2f1a597
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 40 deletions.
2 changes: 1 addition & 1 deletion BarcodeScanner.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "BarcodeScanner"
s.summary = "Simple and beautiful barcode scanner."
s.version = "4.0.1"
s.version = "4.1.0"
s.homepage = "https://github.com/hyperoslo/BarcodeScanner"
s.license = 'MIT'
s.author = { "Hyper Interaktiv AS" => "[email protected]" }
Expand Down
4 changes: 2 additions & 2 deletions Example/BarcodeScannerExample/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- BarcodeScanner (4.0.0)
- BarcodeScanner (4.1.0)

DEPENDENCIES:
- BarcodeScanner (from `../../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: ../../

SPEC CHECKSUMS:
BarcodeScanner: 706068c838405a0b6b624d284c4db3b240919e29
BarcodeScanner: 1d811612a55549741cc9dcfecdd6e562d9982edb

PODFILE CHECKSUM: ea40d735f047f0ae7ae319d7a320a82facf3361f

Expand Down
4 changes: 2 additions & 2 deletions Sources/Controllers/BarcodeScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ open class BarcodeScannerViewController: UIViewController {

private func changeStatus(from oldValue: Status, to newValue: Status) {
guard newValue.state != .notFound else {
messageViewController.state = newValue.state
messageViewController.status = newValue
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0) {
self.status = Status(state: .scanning)
}
Expand All @@ -153,7 +153,7 @@ open class BarcodeScannerViewController: UIViewController {
expandedConstraints.activate()
}

messageViewController.state = newValue.state
messageViewController.status = newValue

UIView.animate(
withDuration: duration,
Expand Down
20 changes: 11 additions & 9 deletions Sources/Controllers/MessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public final class MessageViewController: UIViewController {
public var regularTintColor: UIColor = .black
// Image tint color for `.notFound` state.
public var errorTintColor: UIColor = .red
// Customizable state messages.
public var messages = StateMessageProvider()

// MARK: - UI properties

Expand All @@ -23,9 +25,9 @@ public final class MessageViewController: UIViewController {
// Constraints that are activated when the view is used for loading animation and error messages.
private lazy var expandedConstraints: [NSLayoutConstraint] = self.makeExpandedConstraints()

var state: State = .scanning {
var status = Status(state: .scanning) {
didSet {
handleStateUpdate()
handleStatusUpdate()
}
}

Expand All @@ -36,7 +38,7 @@ public final class MessageViewController: UIViewController {
view.addSubview(blurView)
blurView.contentView.addSubviews(textLabel, imageView, borderView)
setupSubviews()
handleStateUpdate()
handleStatusUpdate()
}

public override func viewDidLayoutSubviews() {
Expand All @@ -58,7 +60,7 @@ public final class MessageViewController: UIViewController {
- Parameter style: The current blur style.
*/
private func animate(blurStyle: UIBlurEffectStyle) {
guard state == .processing else { return }
guard status.state == .processing else { return }

UIView.animate(
withDuration: 2.0,
Expand All @@ -78,7 +80,7 @@ public final class MessageViewController: UIViewController {
- Parameter angle: Rotation angle.
*/
private func animate(borderViewAngle: CGFloat) {
guard state == .processing else {
guard status.state == .processing else {
borderView.transform = .identity
return
}
Expand Down Expand Up @@ -117,12 +119,12 @@ public final class MessageViewController: UIViewController {

// MARK: - State handling

private func handleStateUpdate() {
private func handleStatusUpdate() {
borderView.isHidden = true
borderView.layer.removeAllAnimations()
textLabel.text = state.text
textLabel.text = status.text ?? messages.makeText(for: status.state)

switch state {
switch status.state {
case .scanning, .unauthorized:
textLabel.font = UIFont.boldSystemFont(ofSize: 14)
textLabel.numberOfLines = 3
Expand All @@ -141,7 +143,7 @@ public final class MessageViewController: UIViewController {
imageView.tintColor = errorTintColor
}

if state == .scanning || state == .unauthorized {
if status.state == .scanning || status.state == .unauthorized {
expandedConstraints.deactivate()
collapsedConstraints.activate()
} else {
Expand Down
59 changes: 33 additions & 26 deletions Sources/DataStructures/State.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import UIKit

// MARK: - State

/// Barcode scanner state.
enum State {
case scanning
case processing
case unauthorized
case notFound
}

/// State message provider.
public struct StateMessageProvider {
public var scanningText = localizedString("INFO_DESCRIPTION_TEXT")
public var processingText = localizedString("INFO_LOADING_TITLE")
public var unathorizedText = localizedString("ASK_FOR_PERMISSION_TEXT")
public var notFoundText = localizedString("NO_PRODUCT_ERROR_TITLE")

func makeText(for state: State) -> String {
switch state {
case .scanning:
return scanningText
case .processing:
return processingText
case .unauthorized:
return unathorizedText
case .notFound:
return notFoundText
}
}
}

// MARK: - Status

/// Status is a holder of the current state with a few additional configuration properties.
Expand All @@ -9,7 +40,7 @@ struct Status {
/// Flag to enable/disable animation.
let animated: Bool
/// Text that overrides a text from the state.
let text: String
let text: String?

/**
Creates a new instance of `Status`.
Expand All @@ -20,30 +51,6 @@ struct Status {
init(state: State, animated: Bool = true, text: String? = nil) {
self.state = state
self.animated = animated
self.text = text ?? state.text
}
}

// MARK: - State

/// Barcode scanner state.
enum State {
case scanning
case processing
case unauthorized
case notFound

/// State message.
var text: String {
switch self {
case .scanning:
return localizedString("INFO_DESCRIPTION_TEXT")
case .processing:
return localizedString("INFO_LOADING_TITLE")
case .unauthorized:
return localizedString("ASK_FOR_PERMISSION_TEXT")
case .notFound:
return localizedString("NO_PRODUCT_ERROR_TITLE")
}
self.text = text
}
}

0 comments on commit 2f1a597

Please sign in to comment.