Skip to content

Commit

Permalink
feat: Add SweetCardScanner
Browse files Browse the repository at this point in the history
SweetCardScanner for UIViewControllerRepresentable was added.
  • Loading branch information
aaronlab committed Nov 14, 2020
1 parent 8fe3cc6 commit 11c7dda
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions Sources/SweetCardScanner/SweetCardScanner.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,79 @@
struct SweetCardScanner {
var text = "Hello, World!"
//
// SweetCardScanner.swift
// SweetCardScanner
//
// Created by Aaron Lee on 2020-11-14.
//

import SwiftUI

public struct SweetCardScanner: UIViewControllerRepresentable {

private var onDismiss: (() -> Void)?
private var onError: ((CreditCardScannerError) -> Void)?
private var onSuccess: ((CreditCard) -> Void)?

public init() {}

public func makeUIViewController(context: Context) -> some UIViewController {
let viewController = CreditCardScannerViewController(delegate: context.coordinator)
return viewController
}

public func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }

public func onDismiss(perform callback: @escaping () -> ()) -> Self {
var copy = self
copy.onDismiss = callback
return copy
}

public func onError(perform callback: @escaping (CreditCardScannerError) -> ()) -> Self {
var copy = self
copy.onError = callback
return copy
}

public func onSuccess(perform callback: @escaping (CreditCard) -> ()) -> Self {
var copy = self
copy.onSuccess = callback
return copy
}

}

// MARK: - COORDINATOR

extension SweetCardScanner {

public func makeCoordinator() -> Coordinator {
return Coordinator(onDismiss: self.onDismiss, onError: self.onError, onSuccess: self.onSuccess)
}

public class Coordinator: NSObject, CreditCardScannerViewControllerDelegate {

private var onDismiss: (() -> Void)?
private var onError: ((CreditCardScannerError) -> Void)?
private var onSuccess: ((CreditCard) -> Void)?

public init(onDismiss: (() -> Void)?, onError: ((CreditCardScannerError) -> Void)?, onSuccess: ((CreditCard) -> Void)?) {
self.onDismiss = onDismiss
self.onError = onError
self.onSuccess = onSuccess
}

public func creditCardScannerViewControllerDidCancel(_ viewController: CreditCardScannerViewController) {
self.onDismiss?()
}

public func creditCardScannerViewController(_ viewController: CreditCardScannerViewController, didErrorWith error: CreditCardScannerError) {
self.onError?(error)
}

public func creditCardScannerViewController(_ viewController: CreditCardScannerViewController, didFinishWith card: CreditCard) {
self.onSuccess?(card)
}

}

}

0 comments on commit 11c7dda

Please sign in to comment.