Manos Chorianopoulos

MarketPassViewController dynamic data

...@@ -21,6 +21,10 @@ import AVFoundation ...@@ -21,6 +21,10 @@ import AVFoundation
21 @IBOutlet weak var smLogosView: UIView! 21 @IBOutlet weak var smLogosView: UIView!
22 @IBOutlet weak var mapButton: UIButton! 22 @IBOutlet weak var mapButton: UIButton!
23 23
24 + var marketPassDetails:swiftApi.MarketPassDetailsModel?
25 +
26 + let spinner = SpinnerViewController()
27 +
24 let uiscreen: CGRect = UIScreen.main.bounds 28 let uiscreen: CGRect = UIScreen.main.bounds
25 29
26 public override func viewDidLoad() { 30 public override func viewDidLoad() {
...@@ -28,6 +32,9 @@ import AVFoundation ...@@ -28,6 +32,9 @@ import AVFoundation
28 32
29 self.hidesBottomBarWhenPushed = true 33 self.hidesBottomBarWhenPushed = true
30 34
35 + passContainerView.isHidden = true
36 + getMarketPassDetailsRequest()
37 +
31 setBackButton() 38 setBackButton()
32 setNavigationTitle("SUPERMARKET DEALS") 39 setNavigationTitle("SUPERMARKET DEALS")
33 // Setup the info button with an action 40 // Setup the info button with an action
...@@ -74,38 +81,35 @@ import AVFoundation ...@@ -74,38 +81,35 @@ import AVFoundation
74 barcodeView.layer.borderWidth = 2 81 barcodeView.layer.borderWidth = 2
75 barcodeView.layer.borderColor = UIColor(rgb: 0xDADDE4).cgColor 82 barcodeView.layer.borderColor = UIColor(rgb: 0xDADDE4).cgColor
76 83
77 - //TODO: FIX 84 + let barcodeString = ""
78 -// let barcodeString = constructBarcode(coupon: coupon) ?? ""
79 -// let barcodeString = coupon?._barcode ?? ""
80 - let barcodeString = "4006381333931"
81 barcodeLabel.text = barcodeString 85 barcodeLabel.text = barcodeString
82 barcodeLabel.font = UIFont(name: "PeridotPE-Regular", size: 19.0) 86 barcodeLabel.font = UIFont(name: "PeridotPE-Regular", size: 19.0)
83 barcodeLabel.textColor = UIColor(rgb: 0x212121) 87 barcodeLabel.textColor = UIColor(rgb: 0x212121)
84 88
85 - if let barcodeUIImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.12)) { 89 +// if let barcodeUIImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.12)) {
86 -// if let barcodeUIImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 110, height: 83)) { 90 +// // if let barcodeUIImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 110, height: 83)) {
87 - barcodeImage.image = barcodeUIImage 91 +// barcodeImage.image = barcodeUIImage
88 92
89 - barcodeImageHeight.constant = self.uiscreen.height * 0.12 93 +// barcodeImageHeight.constant = self.uiscreen.height * 0.12
90 -// barcodeImageHeight.constant = 83 94 +// // barcodeImageHeight.constant = 83
91 95
92 - } else { 96 +// } else {
93 - barcodeImageHeight.constant = 0 97 +// barcodeImageHeight.constant = 0
94 - } 98 +// }
95 99
96 // TODO: FIX 100 // TODO: FIX
97 - let couponsValueString = "30" 101 + // let couponsValueString = "30"
98 - let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 17) ?? UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.white] 102 + // let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 17) ?? UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.white]
99 - let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Bold", size: 17) ?? UIFont.boldSystemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.white] 103 + // let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Bold", size: 17) ?? UIFont.boldSystemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.white]
100 104
101 - let couponsValueNormalText1 = "Έχεις κουπόνια συνολικής αξίας " 105 + // let couponsValueNormalText1 = "Έχεις κουπόνια συνολικής αξίας "
102 - let couponsValueBoldText = couponsValueString + "€" 106 + // let couponsValueBoldText = couponsValueString + "€"
103 107
104 - let couponsValueAttributedString = NSMutableAttributedString(string:couponsValueNormalText1, attributes:attrRegular) 108 + // let couponsValueAttributedString = NSMutableAttributedString(string:couponsValueNormalText1, attributes:attrRegular)
105 - let couponsValueBoldString = NSMutableAttributedString(string: couponsValueBoldText, attributes:attrBold) 109 + // let couponsValueBoldString = NSMutableAttributedString(string: couponsValueBoldText, attributes:attrBold)
106 110
107 - couponsValueAttributedString.append(couponsValueBoldString) 111 + // couponsValueAttributedString.append(couponsValueBoldString)
108 - couponsValueLabel.attributedText = couponsValueAttributedString 112 + // couponsValueLabel.attributedText = couponsValueAttributedString
109 113
110 mapButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 16) 114 mapButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 16)
111 mapButton.setTitle("Δες στο χάρτη", for: .normal) 115 mapButton.setTitle("Δες στο χάρτη", for: .normal)
...@@ -122,9 +126,69 @@ import AVFoundation ...@@ -122,9 +126,69 @@ import AVFoundation
122 smLogosView.layer.maskedCorners = [ .layerMinXMaxYCorner, .layerMaxXMaxYCorner] // Bottom left, right corner radius 126 smLogosView.layer.maskedCorners = [ .layerMinXMaxYCorner, .layerMaxXMaxYCorner] // Bottom left, right corner radius
123 127
124 // TODO: FIX - Replace with dynamic images 128 // TODO: FIX - Replace with dynamic images
125 - let imageNames = ["active_code_logo_2", "active_code_logo_2", "active_code_logo_2"] 129 +// let imageNames = ["active_code_logo_2", "active_code_logo_2", "active_code_logo_2"]
126 -// let images = imageNames.compactMap { UIImage(named: $0) } 130 +// // let images = imageNames.compactMap { UIImage(named: $0) }
127 - let images = imageNames.compactMap { UIImage(named: $0, in: MyEmptyClass.resourceBundle(), compatibleWith: nil) } 131 +// let images = imageNames.compactMap { UIImage(named: $0, in: MyEmptyClass.resourceBundle(), compatibleWith: nil) }
132 +
133 +// // Create a horizontal stack view
134 +// let stackView = UIStackView()
135 +// stackView.axis = .horizontal
136 +// stackView.spacing = 15 // Spacing between images
137 +// stackView.alignment = .center
138 +// stackView.distribution = .equalSpacing
139 +
140 +// // Add UIImageViews to the stack view
141 +// for image in images {
142 +// let imageView = UIImageView(image: image)
143 +// imageView.contentMode = .scaleAspectFit
144 +// imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true // Set a width
145 +// imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true // Set a height
146 +// stackView.addArrangedSubview(imageView)
147 +// }
148 +
149 +// // Add the stack view to your main view
150 +// smLogosView.addSubview(stackView)
151 +
152 +// // Set up constraints for the stack view
153 +// stackView.translatesAutoresizingMaskIntoConstraints = false
154 +// NSLayoutConstraint.activate([
155 +// stackView.leadingAnchor.constraint(equalTo: smLogosView.leadingAnchor, constant: 25),
156 +// stackView.centerYAnchor.constraint(equalTo: smLogosView.centerYAnchor),
157 +// ])
158 + }
159 +
160 + func updateUI() {
161 +// let barcodeString = constructBarcode(coupon: marketPassDetails?._barcode) ?? ""
162 + let barcodeString = marketPassDetails?._barcode ?? ""
163 + barcodeLabel.text = barcodeString
164 + barcodeLabel.frame.size.width = barcodeLabel.intrinsicContentSize.width
165 + barcodeLabel.frame.size.height = barcodeLabel.intrinsicContentSize.height
166 +
167 + if let barcodeUIImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.12)) {
168 +// if let barcodeUIImage = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObject.ObjectType.ean13.rawValue, targetSize: CGSize(width: 110, height: 83)) {
169 + barcodeImage.image = barcodeUIImage
170 +
171 + barcodeImageHeight.constant = self.uiscreen.height * 0.12
172 +// barcodeImageHeight.constant = 83
173 +
174 + } else {
175 + barcodeImageHeight.constant = 0
176 + }
177 +
178 + let couponsValueString = String(marketPassDetails?._total_available_discount ?? 0.0)
179 + let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 17) ?? UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.white]
180 + let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Bold", size: 17) ?? UIFont.boldSystemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.white]
181 +
182 + let couponsValueNormalText1 = "Έχεις κουπόνια συνολικής αξίας "
183 + let couponsValueBoldText = couponsValueString + "€"
184 +
185 + let couponsValueAttributedString = NSMutableAttributedString(string:couponsValueNormalText1, attributes:attrRegular)
186 + let couponsValueBoldString = NSMutableAttributedString(string: couponsValueBoldText, attributes:attrBold)
187 +
188 + couponsValueAttributedString.append(couponsValueBoldString)
189 + couponsValueLabel.attributedText = couponsValueAttributedString
190 +
191 + let supermarkets: Array<swiftApi.SupermarketModel> = marketPassDetails?._supermarkets ?? []
128 192
129 // Create a horizontal stack view 193 // Create a horizontal stack view
130 let stackView = UIStackView() 194 let stackView = UIStackView()
...@@ -133,12 +197,16 @@ import AVFoundation ...@@ -133,12 +197,16 @@ import AVFoundation
133 stackView.alignment = .center 197 stackView.alignment = .center
134 stackView.distribution = .equalSpacing 198 stackView.distribution = .equalSpacing
135 199
136 - // Add UIImageViews to the stack view 200 + // Load images dynamically
137 - for image in images { 201 + for sm in supermarkets {
138 - let imageView = UIImageView(image: image) 202 + let imageView = UIImageView()
139 imageView.contentMode = .scaleAspectFit 203 imageView.contentMode = .scaleAspectFit
140 - imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true // Set a width 204 + imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
141 - imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true // Set a height 205 + imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
206 +
207 + imageView.load(link: sm._logo, placeholder: UIImage(), cache: URLCache())
208 +
209 + // Add image view to the stack view
142 stackView.addArrangedSubview(imageView) 210 stackView.addArrangedSubview(imageView)
143 } 211 }
144 212
...@@ -151,6 +219,29 @@ import AVFoundation ...@@ -151,6 +219,29 @@ import AVFoundation
151 stackView.leadingAnchor.constraint(equalTo: smLogosView.leadingAnchor, constant: 25), 219 stackView.leadingAnchor.constraint(equalTo: smLogosView.leadingAnchor, constant: 25),
152 stackView.centerYAnchor.constraint(equalTo: smLogosView.centerYAnchor), 220 stackView.centerYAnchor.constraint(equalTo: smLogosView.centerYAnchor),
153 ]) 221 ])
222 +
223 + passContainerView.isHidden = false
224 + self.view.layoutIfNeeded()
225 + }
226 +
227 + func showSpinner() {
228 + // add the spinner view controller
229 + // addChild(spinner)
230 + // spinner.view.frame = view.frame
231 + // view.addSubview(spinner.view)
232 + // spinner.didMove(toParent: self)
233 +
234 + addChild(spinner)
235 + spinner.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
236 + self.view.addSubview(spinner.view)
237 + spinner.didMove(toParent: self)
238 + }
239 +
240 + func hideSpinner() {
241 + // remove the spinner view controller
242 + spinner.willMove(toParent: nil)
243 + spinner.view.removeFromSuperview()
244 + spinner.removeFromParent()
154 } 245 }
155 246
156 func constructBarcode() -> String? { 247 func constructBarcode() -> String? {
...@@ -189,6 +280,31 @@ import AVFoundation ...@@ -189,6 +280,31 @@ import AVFoundation
189 return "" 280 return ""
190 } 281 }
191 282
283 + // MARK: - API Functions
284 + func getMarketPassDetailsRequest() {
285 + showSpinner()
286 +
287 + swiftApi().getMarketPassDetailsAsync(getMarketPassDetailsCallback, failureCallback: {errorCode in
288 + print("========= getMarketPassDetailsRequest ERROR CODE =========", errorCode)
289 + self.hideSpinner()
290 + })
291 + }
292 +
293 + func getMarketPassDetailsCallback (_ marketPassDetailsData: swiftApi.MarketPassDetailsModel?) -> Void {
294 + self.hideSpinner()
295 +
296 + if (marketPassDetailsData != nil) {
297 + DispatchQueue.main.async {
298 + if let tempMarketPassDetailsData = marketPassDetailsData {
299 + self.marketPassDetails = tempMarketPassDetailsData
300 + self.updateUI()
301 + }
302 + }
303 + } else {
304 + }
305 + }
306 + // MARK: - API Functions
307 +
192 // MARK: - Actions 308 // MARK: - Actions
193 @IBAction func mapButtonAction(_ sender: Any) { 309 @IBAction func mapButtonAction(_ sender: Any) {
194 swiftApi().openSupermarketMap(self) 310 swiftApi().openSupermarketMap(self)
......