Guest User

Untitled

a guest
Jul 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import FirebaseDatabase
  4.  
  5.  
  6. class PledgeTableViewCell: UITableViewCell {
  7.  
  8. var plusBtnAction: ((Any) -> Void)?
  9. var minusBtnAction: ((Any) -> Void)?
  10.  
  11. @IBOutlet weak var rewardAmtLabel: UILabel!
  12. @IBOutlet weak var ticketClasslabel: UILabel!
  13. @IBOutlet weak var ticketDescLabel: UILabel!
  14.  
  15. @IBOutlet weak var ticketCountLabel: UILabel!
  16. @IBOutlet weak var plusBtn: UIButton!
  17. @IBOutlet weak var minusBtn: UIButton!
  18.  
  19. var ref: DatabaseReference!
  20. var currentID = ""
  21. var ticket_count: Int = 0
  22. let userID = Auth.auth().currentUser!.uid
  23.  
  24. @IBAction func minusBtn(_ sender: Any) {
  25.  
  26. if var tickCount = Int(ticketCountLabel.text!) {
  27.  
  28. if(tickCount > 0)
  29. {
  30. tickCount -= 1
  31. ticketCountLabel.text = String(tickCount)
  32. }
  33. }
  34.  
  35. self.minusBtnAction?(sender)
  36. }
  37.  
  38. @IBAction func plusBtn(_ sender: AnyObject) {
  39.  
  40.  
  41. if var tickCount = Int(ticketCountLabel.text!) {
  42.  
  43. //I WANT TO SEND THIS 'TICKCOUNT' TO THE PLEDGEVIEWCONTROLLER
  44. tickCount += 1
  45. ticketCountLabel.text = String(tickCount)
  46. }
  47.  
  48.  
  49. self.plusBtnAction?(sender)
  50.  
  51. }
  52.  
  53.  
  54. }
  55.  
  56. import UIKit
  57. import Foundation
  58. import FirebaseDatabase
  59. import Firebase
  60.  
  61. class PledgeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
  62.  
  63.  
  64. var getID: String!
  65. var rewards = [Rewards]()
  66. var ticket_count: Int = 0
  67. var ref: DatabaseReference!
  68. let userID = Auth.auth().currentUser!.uid
  69. var rewardID: String!
  70.  
  71. @IBOutlet weak var pledgeAmtLabel: UILabel!
  72. @IBOutlet weak var RewardChooseTable: UITableView!
  73. @IBAction func pledgeBtn(_ sender: Any) {
  74. //get the text from the label and run all the checks to see if the tickets are available
  75.  
  76. }
  77.  
  78. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  79. /* myIndex = indexPath.row
  80.  
  81. finalId = idTable[myIndex]
  82. let story = stories[indexPath.row]
  83. ArtcallID = story.id
  84.  
  85. performSegue(withIdentifier: "singleArtcall", sender: self)
  86. */
  87. let reward = rewards[indexPath.row]
  88. let id = reward.rewardID
  89. reward.countUp()
  90.  
  91. print("The reward that was touched is: " + id )
  92. print("One of the buttons were touched")
  93. }
  94.  
  95. let RewardRef = Database.database().reference().child("Rewards")
  96.  
  97.  
  98. func numberOfSections(in tableView: UITableView) -> Int {
  99. return 1
  100. }
  101.  
  102. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  103. return rewards.count
  104. }
  105.  
  106.  
  107. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  108.  
  109. let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell
  110. let reward = rewards[indexPath.row]
  111. cell.reward = reward
  112. cell.currentID = getID
  113. rewardID = reward.rewardID
  114.  
  115. cell.plusBtnAction = { sender in
  116.  
  117. let reward = self.rewards[indexPath.row]
  118. cell.reward = reward
  119. let local_id = reward.rewardID
  120.  
  121. self.ref=Database.database().reference().child("Fund_Project_Request").child(self.getID).child(self.userID).child(local_id).child("Ticket_count")
  122.  
  123.  
  124. self.ref.observeSingleEvent(of: .value, with: { (snapshot) in
  125. // Get user value
  126.  
  127. if snapshot.value is NSNull{
  128. self.ticket_count = 0
  129. self.ticket_count += 1
  130. self.ref.setValue(self.ticket_count)
  131. }
  132.  
  133. else{
  134. self.ticket_count = snapshot.value as! Int
  135. self.ticket_count += 1
  136. self.ref.setValue(self.ticket_count)
  137. }
  138.  
  139.  
  140. }) { (error) in
  141. print(error.localizedDescription)
  142. }
  143.  
  144. Database.database().reference().child("Rewards").child(self.getID).child(local_id).child("reward_ticket_amount").observeSingleEvent(of: .value, with: { (snapshot) in
  145. // Get user value
  146. let reward_amt = snapshot.value as! Int
  147.  
  148. self.pledgeAmtLabel.text = String(reward_amt)
  149.  
  150. }) { (error) in
  151. print(error.localizedDescription)
  152. }
  153.  
  154. // Do whatever you want from your button here.
  155. }
  156.  
  157.  
  158.  
  159. cell.minusBtnAction = { sender in
  160.  
  161. let reward = self.rewards[indexPath.row]
  162. cell.reward = reward
  163. let local_id = reward.rewardID
  164.  
  165. self.ref=Database.database().reference().child("Fund_Project_Request").child(self.getID).child(self.userID).child(local_id).child("Ticket_count")
  166. self.ref.observeSingleEvent(of: .value, with: { (snapshot) in
  167. // Get user value
  168.  
  169. if snapshot.value is NSNull{
  170.  
  171. }
  172.  
  173. else{
  174.  
  175. self.ticket_count = snapshot.value as! Int
  176. if(self.ticket_count != 0)
  177. {
  178. self.ticket_count -= 1
  179. self.ref.setValue(self.ticket_count)
  180. }
  181. }
  182.  
  183.  
  184. }) { (error) in
  185. print(error.localizedDescription)
  186. }
  187.  
  188.  
  189.  
  190. }
  191.  
  192.  
  193.  
  194. return cell
  195.  
  196. }
  197.  
  198.  
  199.  
  200. override func viewDidAppear(_ animated: Bool) {
  201. super.viewDidAppear(animated)
  202.  
  203. RewardRef.child(getID).observe(.value, with: { (snapshot) in
  204. self.rewards.removeAll()
  205.  
  206. for child in snapshot.children {
  207. let childSnapshot = child as! DataSnapshot
  208. let reward = Rewards(snapshot: childSnapshot)
  209. self.rewards.insert(reward, at: 0)
  210. }
  211.  
  212. self.RewardChooseTable.reloadData()
  213. })
  214.  
  215. }
  216.  
  217. override func viewDidLoad() {
  218. super.viewDidLoad()
  219.  
  220. print("The id received from the SingleViewControl is:" + getID)
  221. }
  222.  
  223. }
  224.  
  225. class PledgeTableViewCell: UITableViewCell {
  226.  
  227. var plusBtnAction: ((String) -> Void)?
  228. var minusBtnAction: ((String) -> Void)?
  229.  
  230.  
  231.  
  232. @IBAction func minusBtn(_ sender: Any) {
  233.  
  234. if var tickCount = Int(ticketCountLabel.text!) {
  235.  
  236. if(tickCount > 0)
  237. {
  238. tickCount -= 1
  239. ticketCountLabel.text = String(tickCount)
  240.  
  241. }
  242. self.minusBtnAction?(tickCount)
  243. }
  244. }
  245.  
  246. @IBAction func plusBtn(_ sender: AnyObject) {
  247.  
  248. if var tickCount = Int(ticketCountLabel.text!) {
  249.  
  250. //I WANT TO SEND THIS 'TICKCOUNT' TO THE PLEDGEVIEWCONTROLLER
  251. tickCount += 1
  252. ticketCountLabel.text = String(tickCount)
  253. self.plusBtnAction?(ticketCount)
  254. }
  255.  
  256. }
  257. }
  258.  
  259. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  260.  
  261. let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell
  262. .....
  263. cell.plusBtnAction = { labelText in
  264. //handle labelText here
  265. }
  266. }
Add Comment
Please, Sign In to add comment