Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import FirebaseStorage
  4.  
  5. class StoreViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  6.  
  7. @IBOutlet weak var tableList: UITableView!
  8.  
  9. var listProduct = [StoreModel]()
  10.  
  11. var ref: DatabaseReference!
  12. let storage = Storage.storage()
  13. let storageRef = Storage.storage().reference()
  14. let userID = Auth.auth().currentUser!.uid
  15. var storeID = [String]()
  16. var shopID : String?
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20.  
  21.  
  22. Database.database().reference().child("Users/Sellers").observe(DataEventType.value, with: { (snapshot) in
  23.  
  24. if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
  25. print("SNAPSHOT: (snapshot)")
  26. for snap in snapshot {
  27. if let storeDict = snap.value as? Dictionary<String, AnyObject> {
  28.  
  29. if storeDict["storename"] as? String != "" {
  30. let key = snap.key
  31. print(key)
  32. }
  33. }
  34. }
  35. }
  36. })
  37.  
  38.  
  39. let refList = Database.database().reference().child("Users/Sellers")
  40. refList.observe(DataEventType.value, with:{(snapshot) in
  41. if snapshot.childrenCount>0{
  42. self.listProduct.removeAll()
  43.  
  44. for lists in snapshot.children.allObjects as! [DataSnapshot]{
  45. let productList = lists.childSnapshot(forPath: "user_info")
  46. let userList = productList.value as? [String: AnyObject]
  47. let listName = userList?["storename"]
  48.  
  49. let list = StoreModel(name: listName as! String?)
  50.  
  51. self.listProduct.append(list)
  52. }
  53. self.tableList.reloadData()
  54. }
  55.  
  56. })
  57. }
  58.  
  59. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  60. return UITableView.automaticDimension
  61. }
  62.  
  63. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  64. return listProduct.count
  65.  
  66. }
  67.  
  68. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  69. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StoreTableViewCell
  70.  
  71. let list: StoreModel
  72. list = listProduct[indexPath.row]
  73.  
  74. // let url = URL(string: list.product_image_url!)
  75. // let data = try? Data(contentsOf: url!)
  76. // if let imageData = data {
  77. // let image = UIImage(data: imageData)
  78. // cell.imgProduct.image = image
  79. // }else {
  80. // cell.imgProduct.image = UIImage(named: "avatar")
  81. // }
  82.  
  83. cell.lblName.text = list.name
  84.  
  85. return cell
  86. }
  87.  
  88. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  89. self.performSegue(withIdentifier: "storetoproduct", sender: self)
  90. }
  91.  
  92.  
  93. }
  94.  
  95. let refList = Database.database().reference().child("Users/Sellers")
  96.  
  97. refList.observe(DataEventType.value, with:{(snapshot) in
  98. if snapshot.childrenCount>0{
  99. self.listProduct.removeAll()
  100.  
  101. for user in snapshot.children.allObjects as! [DataSnapshot]{
  102. let productList = user.childSnapshot(forPath: "product_list")
  103. for product in productList.children.allObjects as! [DataSnapshot]{
  104. let userList = product.value as? [String: AnyObject]
  105. let listName = userList?["name"]
  106. let listDetail = userList?["details"]
  107. let listPrice = userList?["price"]
  108. let productImage = userList?["product_image_url"]
  109.  
  110. let list = ListModel(name: listName as! String?, details: listDetail as! String?, price: listPrice as! String?, product_image_url: productImage as! String?)
  111.  
  112. self.listProduct.append(list)
  113.  
  114. }
  115. }
  116. self.tableList.reloadData()
  117. }
  118.  
  119. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement