Manos Chorianopoulos

constructBarcode at CouponBarcodeView

......@@ -14,6 +14,34 @@ import UIKit
// import AVFoundation
extension String {
var length: Int {
return count
}
subscript (i: Int) -> String {
return self[i ..< i + 1]
}
func substring(fromIndex: Int) -> String {
return self[min(fromIndex, length) ..< length]
}
func substring(toIndex: Int) -> String {
return self[0 ..< max(0, toIndex)]
}
subscript (r: Range<Int>) -> String {
let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)),
upper: min(length, max(0, r.upperBound))))
let start = index(startIndex, offsetBy: range.lowerBound)
let end = index(start, offsetBy: range.upperBound - range.lowerBound)
return String(self[start ..< end])
}
}
extension CouponBarcodeView {
struct headerView: View {
var goBack: () -> ()
......@@ -96,6 +124,41 @@ extension CouponBarcodeView {
}
func constructBarcode() -> String? {
// EAN 13 barcode construction
let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""]
let couponString = couponData["coupon"] as? String ?? ""
if (!couponString.isEmpty) {
var checkDigit = 0;
var result = 0;
var fixedCouponCode = couponString;
if (fixedCouponCode.count < 12) {
let loops = 12 - fixedCouponCode.count;
var zerosStr = "";
for i in 0 ..< loops {
zerosStr += "0"
}
fixedCouponCode = zerosStr + fixedCouponCode;
}
var multiplier = 3;
for idx in (0 ... (fixedCouponCode.count - 1)).reversed() {
let curChar = fixedCouponCode[idx];
result += (Int(curChar) ?? 0) * multiplier;
multiplier = multiplier == 3 ? 1 : 3;
}
checkDigit = 10 - (result % 10);
let barcodeStr = fixedCouponCode + String(checkDigit);
return barcodeStr;
}
return ""
}
var body: some View {
let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""]
......@@ -197,7 +260,7 @@ extension CouponBarcodeView {
// .aspectRatio(contentMode: .fit)
// .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1)
Text(couponData["coupon"] as? String ?? "")
Text(constructBarcode() ?? "")
.fontWeight(.regular)
.font(.system(size: 22))
.foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
......