Manos Chorianopoulos

barcode lib v17

......@@ -14,6 +14,33 @@ import RSBarcodes_Swift
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: () -> ()
......@@ -95,8 +122,44 @@ extension CouponBarcodeView {
return nil
}
func constructBarcode() -> String? {
// EAN 13 barcode construction
// let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""]
let couponString = coupon["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 barcodeString = constructBarcode() ?? ""
GeometryReader { geometry in
ScrollView(showsIndicators: false) {
VStack(alignment: .leading) {
......@@ -131,7 +194,7 @@ extension CouponBarcodeView {
VStack(alignment: .center) {
VStack {
Text("1A2C378")
Text(coupon["coupon"] as? String ?? "")
.fontWeight(.bold)
.font(.system(size: 27))
.foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
......@@ -166,7 +229,7 @@ extension CouponBarcodeView {
// gen.fillColor = UIColor.white
// gen.strokeColor = UIColor.black
if let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode("1234567891234", machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 300, height: 300)) {
if let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 300, height: 300)) {
// VStack(alignment: .center) {
// Image(uiImage: image)
......@@ -200,7 +263,7 @@ extension CouponBarcodeView {
// .aspectRatio(contentMode: .fit)
// .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1)
Text("1234567891234")
Text(barcodeString)
.fontWeight(.regular)
.font(.system(size: 22))
.foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803))
......