Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. //
  2. // AlertsViewController.swift
  3. // MiniChallenge1
  4. //
  5. // Created by Marina Miranda Aranha on 22/04/19.
  6. // Copyright © 2019 Joyce Simão Clímaco. All rights reserved.
  7. //
  8. import UIKit
  9. import Firebase
  10.  
  11. class AlertsViewController: UIViewController, UITableViewDataSource{
  12.  
  13. var productList: [Product] = []
  14. var alertList: [Alert] = []
  15.  
  16. var ref: DatabaseReference!
  17. @IBOutlet weak var alertsTableView: UITableView!
  18.  
  19. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  20. return alertList.count
  21. }
  22.  
  23. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  24.  
  25. var cell = tableView.dequeueReusableCell(withIdentifier: "productAlert", for: indexPath) as! AlertCell
  26.  
  27. let alert = alertList[indexPath.row]
  28.  
  29. if (productList.count > 1)
  30. {
  31. if let productInCell = productList.first(where: {$0.barcode == alert.productBarcode})
  32. {
  33. cell.productName.text = productInCell.name
  34. cell.expirationDateLabel.text = alert.stringExpDate
  35. cell.weightLabel.text = String(productInCell.weight)
  36. }
  37.  
  38. }
  39.  
  40.  
  41. //
  42. // let alertRef = Database.database().reference(withPath: "Alerts")
  43. // alertRef.queryEqual(toValue:product.barcode, childKey: "productBarcode").queryLimited(toFirst: 1)
  44. // alertRef.observeSingleEvent(of: .value, with: { snapshot in
  45. // for child in snapshot.children {
  46. // if let snapshot = child as? DataSnapshot, let alertItem = Alert(snapshot: snapshot) {
  47. // cell.productName.text = product.name
  48. // cell.expirationDateLabel.text = alertItem.stringExpDate
  49. // cell.weightLabel.text = String(product.weight)
  50. // let convertedDate = stringToDate(dateString: alertItem.stringExpDate)
  51. //
  52. //
  53. // }
  54. // }
  55. // })
  56.  
  57. // alertRef.queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in
  58. // for child in snapshot.children {
  59. // if let snapshot = child as? DataSnapshot, let alertItem = Alert(snapshot: snapshot) {
  60. // self.alertList.append(alertItem)
  61. // print(alertItem.key + alertItem.stringExpDate)
  62. // }
  63. // }
  64. //
  65. // })
  66. //
  67. return cell
  68. }
  69.  
  70.  
  71. override func viewDidLoad() {
  72. super.viewDidLoad()
  73. // Do any additional setup after loading the view.
  74.  
  75.  
  76. productList = []
  77.  
  78. ref = Database.database().reference(withPath: "Products")
  79. //Cria um produto
  80. let productToAdd = Product(key: "loreal", name: "Loreal Desodorante", barcode: "1234567890", category: "Cosmetico", subCategory: "Desodorante", weight: 300, alertMeWhen: 30)
  81. //cria um nó para este produto
  82. var productItemRef = self.ref.child(productToAdd.barcode)
  83. //adiciona um filho para este nó
  84. productItemRef.setValue(productToAdd.toAnyObject())
  85.  
  86. //cria novo lote
  87. let lotOne = Lot(lotNumber: "1234", expirationDate: "25-11-1996", originalSalePrice: 20.20, purchasePrice: 30.12, quantity: 10)
  88. //cria um nó para Lotes
  89. let lotItemRef = productItemRef.child("Lots")
  90. //adiciona um filho neste nó em que o child é o lote number
  91. let newLotRef = lotItemRef.child(lotOne.lotNumber)
  92. //adiciona um filho para o nó filho
  93. newLotRef.setValue(lotOne.toAnyObject())
  94.  
  95. //cria novo lote
  96. let lotTwo = Lot(lotNumber: "4567", expirationDate: "12-12-2004", originalSalePrice: 20.30, purchasePrice: 30.25, quantity: 50)
  97. let newLotRefTwo = lotItemRef.child(lotTwo.lotNumber)
  98. newLotRefTwo.setValue(lotTwo.toAnyObject())
  99.  
  100. let secondProductToAdd = Product(key: "macarrao", name: "Nissin Lamen", barcode: "0987654321", category: "Alimento", subCategory: "Cancer", weight: 150, alertMeWhen: 10)
  101. productItemRef = self.ref.child(secondProductToAdd.barcode)
  102. productItemRef.setValue(secondProductToAdd.toAnyObject())
  103.  
  104. //Lógica para adicionar alertas ao banco de dados
  105.  
  106. let alertRef = Database.database().reference(withPath: "Alerts")
  107. let alertToAdd = Alert(productBarcode: "0987654321", lotNumber: "1234", lotStringExpDate: "25/04/2019", productAlertMeWhen: 5)
  108. let alertToAddRef = alertRef.child(alertToAdd.key)
  109. alertToAddRef.setValue(alertToAdd.toAnyObject())
  110.  
  111.  
  112. alertRef.queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in
  113. for child in snapshot.children {
  114. if let snapshot = child as? DataSnapshot, let alertItem = Alert(snapshot: snapshot) {
  115. self.alertList.append(alertItem)
  116. print(alertItem.key + alertItem.stringExpDate)
  117. }
  118. }
  119. self.alertsTableView.reloadData()
  120. })
  121.  
  122. //FAZER QUERIES
  123.  
  124. ref!.queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
  125. var newItems: [Product] = []
  126.  
  127. for child in snapshot.children {
  128. if let snapshot = child as? DataSnapshot, let productItem = Product(snapshot: snapshot) {
  129. newItems.append(productItem)
  130. print(productItem.barcode + "cima")
  131. }
  132. }
  133.  
  134. for items in newItems {
  135. self.productList.append(items)
  136. }
  137.  
  138. DispatchQueue.main.async {
  139. self.alertsTableView.reloadData()
  140. }
  141.  
  142. var lotItems:[Lot] = []
  143.  
  144. for alert in self.productList {
  145. print(alert.barcode)
  146. self.ref!.child("\(alert.barcode)/Lots").observeSingleEvent(of: .value, with: { snapshot in
  147. for child in snapshot.children {
  148. print("antes do if let")
  149. if let snapshot = child as? DataSnapshot, let lotItem = Lot(snapshot: snapshot) {
  150. lotItems.append(lotItem)
  151. print(lotItem)
  152. print("passou aqui")
  153. }
  154. }
  155. })
  156. }
  157. })
  158.  
  159. }
  160.  
  161.  
  162. }
  163.  
  164. class AlertCell: UITableViewCell {
  165.  
  166. @IBOutlet weak var productName: UILabel!
  167. @IBOutlet weak var quantityLabel: UILabel!
  168. @IBOutlet weak var daysLeftLabel: UILabel!
  169. @IBOutlet weak var expirationDateLabel: UILabel!
  170. @IBOutlet weak var weightLabel: UILabel!
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement