Showing
3 changed files
with
64 additions
and
1 deletions
No preview for this file type
No preview for this file type
... | @@ -14,6 +14,34 @@ import UIKit | ... | @@ -14,6 +14,34 @@ import UIKit |
14 | // import AVFoundation | 14 | // import AVFoundation |
15 | 15 | ||
16 | 16 | ||
17 | + | ||
18 | +extension String { | ||
19 | + | ||
20 | + var length: Int { | ||
21 | + return count | ||
22 | + } | ||
23 | + | ||
24 | + subscript (i: Int) -> String { | ||
25 | + return self[i ..< i + 1] | ||
26 | + } | ||
27 | + | ||
28 | + func substring(fromIndex: Int) -> String { | ||
29 | + return self[min(fromIndex, length) ..< length] | ||
30 | + } | ||
31 | + | ||
32 | + func substring(toIndex: Int) -> String { | ||
33 | + return self[0 ..< max(0, toIndex)] | ||
34 | + } | ||
35 | + | ||
36 | + subscript (r: Range<Int>) -> String { | ||
37 | + let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)), | ||
38 | + upper: min(length, max(0, r.upperBound)))) | ||
39 | + let start = index(startIndex, offsetBy: range.lowerBound) | ||
40 | + let end = index(start, offsetBy: range.upperBound - range.lowerBound) | ||
41 | + return String(self[start ..< end]) | ||
42 | + } | ||
43 | +} | ||
44 | + | ||
17 | extension CouponBarcodeView { | 45 | extension CouponBarcodeView { |
18 | struct headerView: View { | 46 | struct headerView: View { |
19 | var goBack: () -> () | 47 | var goBack: () -> () |
... | @@ -96,6 +124,41 @@ extension CouponBarcodeView { | ... | @@ -96,6 +124,41 @@ extension CouponBarcodeView { |
96 | } | 124 | } |
97 | 125 | ||
98 | 126 | ||
127 | + func constructBarcode() -> String? { | ||
128 | + // EAN 13 barcode construction | ||
129 | + let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""] | ||
130 | + let couponString = couponData["coupon"] as? String ?? "" | ||
131 | + | ||
132 | + if (!couponString.isEmpty) { | ||
133 | + var checkDigit = 0; | ||
134 | + var result = 0; | ||
135 | + | ||
136 | + var fixedCouponCode = couponString; | ||
137 | + if (fixedCouponCode.count < 12) { | ||
138 | + let loops = 12 - fixedCouponCode.count; | ||
139 | + var zerosStr = ""; | ||
140 | + for i in 0 ..< loops { | ||
141 | + zerosStr += "0" | ||
142 | + } | ||
143 | + fixedCouponCode = zerosStr + fixedCouponCode; | ||
144 | + } | ||
145 | + | ||
146 | + var multiplier = 3; | ||
147 | + for idx in (0 ... (fixedCouponCode.count - 1)).reversed() { | ||
148 | + let curChar = fixedCouponCode[idx]; | ||
149 | + result += (Int(curChar) ?? 0) * multiplier; | ||
150 | + multiplier = multiplier == 3 ? 1 : 3; | ||
151 | + } | ||
152 | + checkDigit = 10 - (result % 10); | ||
153 | + | ||
154 | + let barcodeStr = fixedCouponCode + String(checkDigit); | ||
155 | + return barcodeStr; | ||
156 | + } | ||
157 | + | ||
158 | + return "" | ||
159 | + } | ||
160 | + | ||
161 | + | ||
99 | var body: some View { | 162 | var body: some View { |
100 | let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""] | 163 | let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""] |
101 | 164 | ||
... | @@ -197,7 +260,7 @@ extension CouponBarcodeView { | ... | @@ -197,7 +260,7 @@ extension CouponBarcodeView { |
197 | // .aspectRatio(contentMode: .fit) | 260 | // .aspectRatio(contentMode: .fit) |
198 | // .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1) | 261 | // .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1) |
199 | 262 | ||
200 | - Text(couponData["coupon"] as? String ?? "") | 263 | + Text(constructBarcode() ?? "") |
201 | .fontWeight(.regular) | 264 | .fontWeight(.regular) |
202 | .font(.system(size: 22)) | 265 | .font(.system(size: 22)) |
203 | .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803)) | 266 | .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803)) | ... | ... |
-
Please register or login to post a comment