Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AlertsViewController.swift
- // MiniChallenge1
- //
- // Created by Marina Miranda Aranha on 22/04/19.
- // Copyright © 2019 Joyce Simão Clímaco. All rights reserved.
- //
- import UIKit
- import Firebase
- class AlertsViewController: UIViewController, UITableViewDataSource{
- var productList: [Product] = []
- var alertList: [Alert] = []
- var ref: DatabaseReference!
- @IBOutlet weak var alertsTableView: UITableView!
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return alertList.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- var cell = tableView.dequeueReusableCell(withIdentifier: "productAlert", for: indexPath) as! AlertCell
- let alert = alertList[indexPath.row]
- if (productList.count > 1)
- {
- if let productInCell = productList.first(where: {$0.barcode == alert.productBarcode})
- {
- cell.productName.text = productInCell.name
- cell.expirationDateLabel.text = alert.stringExpDate
- cell.weightLabel.text = String(productInCell.weight)
- }
- }
- //
- // let alertRef = Database.database().reference(withPath: "Alerts")
- // alertRef.queryEqual(toValue:product.barcode, childKey: "productBarcode").queryLimited(toFirst: 1)
- // alertRef.observeSingleEvent(of: .value, with: { snapshot in
- // for child in snapshot.children {
- // if let snapshot = child as? DataSnapshot, let alertItem = Alert(snapshot: snapshot) {
- // cell.productName.text = product.name
- // cell.expirationDateLabel.text = alertItem.stringExpDate
- // cell.weightLabel.text = String(product.weight)
- // let convertedDate = stringToDate(dateString: alertItem.stringExpDate)
- //
- //
- // }
- // }
- // })
- // alertRef.queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in
- // for child in snapshot.children {
- // if let snapshot = child as? DataSnapshot, let alertItem = Alert(snapshot: snapshot) {
- // self.alertList.append(alertItem)
- // print(alertItem.key + alertItem.stringExpDate)
- // }
- // }
- //
- // })
- //
- return cell
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- productList = []
- ref = Database.database().reference(withPath: "Products")
- //Cria um produto
- let productToAdd = Product(key: "loreal", name: "Loreal Desodorante", barcode: "1234567890", category: "Cosmetico", subCategory: "Desodorante", weight: 300, alertMeWhen: 30)
- //cria um nó para este produto
- var productItemRef = self.ref.child(productToAdd.barcode)
- //adiciona um filho para este nó
- productItemRef.setValue(productToAdd.toAnyObject())
- //cria novo lote
- let lotOne = Lot(lotNumber: "1234", expirationDate: "25-11-1996", originalSalePrice: 20.20, purchasePrice: 30.12, quantity: 10)
- //cria um nó para Lotes
- let lotItemRef = productItemRef.child("Lots")
- //adiciona um filho neste nó em que o child é o lote number
- let newLotRef = lotItemRef.child(lotOne.lotNumber)
- //adiciona um filho para o nó filho
- newLotRef.setValue(lotOne.toAnyObject())
- //cria novo lote
- let lotTwo = Lot(lotNumber: "4567", expirationDate: "12-12-2004", originalSalePrice: 20.30, purchasePrice: 30.25, quantity: 50)
- let newLotRefTwo = lotItemRef.child(lotTwo.lotNumber)
- newLotRefTwo.setValue(lotTwo.toAnyObject())
- let secondProductToAdd = Product(key: "macarrao", name: "Nissin Lamen", barcode: "0987654321", category: "Alimento", subCategory: "Cancer", weight: 150, alertMeWhen: 10)
- productItemRef = self.ref.child(secondProductToAdd.barcode)
- productItemRef.setValue(secondProductToAdd.toAnyObject())
- //Lógica para adicionar alertas ao banco de dados
- let alertRef = Database.database().reference(withPath: "Alerts")
- let alertToAdd = Alert(productBarcode: "0987654321", lotNumber: "1234", lotStringExpDate: "25/04/2019", productAlertMeWhen: 5)
- let alertToAddRef = alertRef.child(alertToAdd.key)
- alertToAddRef.setValue(alertToAdd.toAnyObject())
- alertRef.queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in
- for child in snapshot.children {
- if let snapshot = child as? DataSnapshot, let alertItem = Alert(snapshot: snapshot) {
- self.alertList.append(alertItem)
- print(alertItem.key + alertItem.stringExpDate)
- }
- }
- self.alertsTableView.reloadData()
- })
- //FAZER QUERIES
- ref!.queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
- var newItems: [Product] = []
- for child in snapshot.children {
- if let snapshot = child as? DataSnapshot, let productItem = Product(snapshot: snapshot) {
- newItems.append(productItem)
- print(productItem.barcode + "cima")
- }
- }
- for items in newItems {
- self.productList.append(items)
- }
- DispatchQueue.main.async {
- self.alertsTableView.reloadData()
- }
- var lotItems:[Lot] = []
- for alert in self.productList {
- print(alert.barcode)
- self.ref!.child("\(alert.barcode)/Lots").observeSingleEvent(of: .value, with: { snapshot in
- for child in snapshot.children {
- print("antes do if let")
- if let snapshot = child as? DataSnapshot, let lotItem = Lot(snapshot: snapshot) {
- lotItems.append(lotItem)
- print(lotItem)
- print("passou aqui")
- }
- }
- })
- }
- })
- }
- }
- class AlertCell: UITableViewCell {
- @IBOutlet weak var productName: UILabel!
- @IBOutlet weak var quantityLabel: UILabel!
- @IBOutlet weak var daysLeftLabel: UILabel!
- @IBOutlet weak var expirationDateLabel: UILabel!
- @IBOutlet weak var weightLabel: UILabel!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement