Showing
2 changed files
with
66 additions
and
3 deletions
No preview for this file type
... | @@ -14,6 +14,33 @@ import RSBarcodes_Swift | ... | @@ -14,6 +14,33 @@ import RSBarcodes_Swift |
14 | import AVFoundation | 14 | import AVFoundation |
15 | 15 | ||
16 | 16 | ||
17 | + extension String { | ||
18 | + | ||
19 | + var length: Int { | ||
20 | + return count | ||
21 | + } | ||
22 | + | ||
23 | + subscript (i: Int) -> String { | ||
24 | + return self[i ..< i + 1] | ||
25 | + } | ||
26 | + | ||
27 | + func substring(fromIndex: Int) -> String { | ||
28 | + return self[min(fromIndex, length) ..< length] | ||
29 | + } | ||
30 | + | ||
31 | + func substring(toIndex: Int) -> String { | ||
32 | + return self[0 ..< max(0, toIndex)] | ||
33 | + } | ||
34 | + | ||
35 | + subscript (r: Range<Int>) -> String { | ||
36 | + let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)), | ||
37 | + upper: min(length, max(0, r.upperBound)))) | ||
38 | + let start = index(startIndex, offsetBy: range.lowerBound) | ||
39 | + let end = index(start, offsetBy: range.upperBound - range.lowerBound) | ||
40 | + return String(self[start ..< end]) | ||
41 | + } | ||
42 | + } | ||
43 | + | ||
17 | extension CouponBarcodeView { | 44 | extension CouponBarcodeView { |
18 | struct headerView: View { | 45 | struct headerView: View { |
19 | var goBack: () -> () | 46 | var goBack: () -> () |
... | @@ -95,8 +122,44 @@ extension CouponBarcodeView { | ... | @@ -95,8 +122,44 @@ extension CouponBarcodeView { |
95 | return nil | 122 | return nil |
96 | } | 123 | } |
97 | 124 | ||
125 | + func constructBarcode() -> String? { | ||
126 | + // EAN 13 barcode construction | ||
127 | + // let couponData = coupon["coupon_data"] as? [String: Any] ?? ["":""] | ||
128 | + let couponString = coupon["coupon"] as? String ?? "" | ||
129 | + | ||
130 | + if (!couponString.isEmpty) { | ||
131 | + var checkDigit = 0; | ||
132 | + var result = 0; | ||
133 | + | ||
134 | + var fixedCouponCode = couponString; | ||
135 | + if (fixedCouponCode.count < 12) { | ||
136 | + let loops = 12 - fixedCouponCode.count; | ||
137 | + var zerosStr = ""; | ||
138 | + for i in 0 ..< loops { | ||
139 | + zerosStr += "0" | ||
140 | + } | ||
141 | + fixedCouponCode = zerosStr + fixedCouponCode; | ||
142 | + } | ||
143 | + | ||
144 | + var multiplier = 3; | ||
145 | + for idx in (0 ... (fixedCouponCode.count - 1)).reversed() { | ||
146 | + let curChar = fixedCouponCode[idx]; | ||
147 | + result += (Int(curChar) ?? 0) * multiplier; | ||
148 | + multiplier = multiplier == 3 ? 1 : 3; | ||
149 | + } | ||
150 | + checkDigit = 10 - (result % 10); | ||
151 | + | ||
152 | + let barcodeStr = fixedCouponCode + String(checkDigit); | ||
153 | + return barcodeStr; | ||
154 | + } | ||
155 | + | ||
156 | + return "" | ||
157 | + } | ||
158 | + | ||
98 | 159 | ||
99 | var body: some View { | 160 | var body: some View { |
161 | + let barcodeString = constructBarcode() ?? "" | ||
162 | + | ||
100 | GeometryReader { geometry in | 163 | GeometryReader { geometry in |
101 | ScrollView(showsIndicators: false) { | 164 | ScrollView(showsIndicators: false) { |
102 | VStack(alignment: .leading) { | 165 | VStack(alignment: .leading) { |
... | @@ -131,7 +194,7 @@ extension CouponBarcodeView { | ... | @@ -131,7 +194,7 @@ extension CouponBarcodeView { |
131 | 194 | ||
132 | VStack(alignment: .center) { | 195 | VStack(alignment: .center) { |
133 | VStack { | 196 | VStack { |
134 | - Text("1A2C378") | 197 | + Text(coupon["coupon"] as? String ?? "") |
135 | .fontWeight(.bold) | 198 | .fontWeight(.bold) |
136 | .font(.system(size: 27)) | 199 | .font(.system(size: 27)) |
137 | .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803)) | 200 | .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803)) |
... | @@ -166,7 +229,7 @@ extension CouponBarcodeView { | ... | @@ -166,7 +229,7 @@ extension CouponBarcodeView { |
166 | // gen.fillColor = UIColor.white | 229 | // gen.fillColor = UIColor.white |
167 | // gen.strokeColor = UIColor.black | 230 | // gen.strokeColor = UIColor.black |
168 | 231 | ||
169 | - if let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode("1234567891234", machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 300, height: 300)) { | 232 | + if let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 300, height: 300)) { |
170 | 233 | ||
171 | // VStack(alignment: .center) { | 234 | // VStack(alignment: .center) { |
172 | // Image(uiImage: image) | 235 | // Image(uiImage: image) |
... | @@ -200,7 +263,7 @@ extension CouponBarcodeView { | ... | @@ -200,7 +263,7 @@ extension CouponBarcodeView { |
200 | // .aspectRatio(contentMode: .fit) | 263 | // .aspectRatio(contentMode: .fit) |
201 | // .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1) | 264 | // .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1) |
202 | 265 | ||
203 | - Text("1234567891234") | 266 | + Text(barcodeString) |
204 | .fontWeight(.regular) | 267 | .fontWeight(.regular) |
205 | .font(.system(size: 22)) | 268 | .font(.system(size: 22)) |
206 | .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803)) | 269 | .foregroundColor(Color(red: 0.2549019607843137, green: 0.3333333333333333, blue: 0.39215686274509803)) | ... | ... |
-
Please register or login to post a comment