Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 KB | None | 0 0
  1. import UIKit
  2. import Parse
  3.  
  4. private let reuseIdentifier = "Cell"
  5. /// This class will act as both the data source and delegate since UICollectionViewController conforms to both of our desired protocols: UICollectionViewDataSource and UICollectionViewDelegate. The data source will act as a vendor providing views and information, while the delegate handles managing selections and performing actions within the collection.
  6.  
  7. class PaymentStatusCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  8. //var users = Set<String>()
  9. var users:[String] = ["Name", "Andrew", "Peter", "Paul","Mark"]
  10. var payments:[String] = ["Payment", "100", "500", "200", "50"]
  11.  
  12. fileprivate let concurrentPhotoQueue = DispatchQueue( label: "com.raywenderlich.GooglyPuff.photoQueue", attributes: .concurrent)
  13. let queue = DispatchQueue(label: "com.appcoda.myqueue")
  14. let queue2 = DispatchQueue.global()
  15.  
  16. let button = UIButton()
  17.  
  18. var usersPayments:[String:[String]] = ["Name":["Amount"]]
  19. //this object expects one entry per user.
  20. var usersTotalPayments:[String:String] = ["Name":"Total"]
  21.  
  22.  
  23. //var usersTotalPayments = [String:[String]]()
  24. //var usersTotalPayments = Dictionary<String, Array<String>>()
  25. @IBOutlet var paymentStatusCollectionView: UICollectionView!
  26.  
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29.  
  30. // Uncomment the following line to preserve selection between presentations
  31. // self.clearsSelectionOnViewWillAppear = false
  32.  
  33. // Register cell classes
  34.  
  35. // Do any additional setup after loading the view.
  36. usersTotalPayments["BB"] = "10000"
  37. usersTotalPayments["AA"] = "1000"
  38. //self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  39. //self.collectionView?.register(PaymentStatusCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  40.  
  41. _ = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(PaymentStatusCollectionViewController.reloadData), userInfo: nil, repeats: false)
  42.  
  43. //button.setTitle("Shoudl This Force..", for: [])
  44. //self.view.addSubview(button)
  45. }
  46.  
  47. func reloadData() {
  48. DispatchQueue.main.async { [unowned self] in
  49. print(#function, "========================================")
  50. self.collectionView?.reloadData()
  51. }
  52. }
  53.  
  54. override func viewDidAppear(_ animated: Bool) {
  55. super.viewDidAppear(true)
  56.  
  57. //print(#function, " :", usersTotalPayments.keys.count)
  58. //self.collectionView?.reloadData()
  59. //self.collectionView!.reloadItems(at: <#[IndexPath]#>)
  60. self.collectionView?.reloadData()
  61. //self.collectionView?.reloadInputViews()
  62. //self.collectionViewLayout.invalidateLayout()
  63. print(#function, " :", self.usersTotalPayments.keys.count)
  64. self.collectionView?.reloadItems(at:(self.collectionView?.indexPathsForVisibleItems)!)
  65.  
  66. }
  67.  
  68. // override func viewDidLayoutSubviews() {
  69. // super.viewDidLayoutSubviews()
  70. // //print(#function, " :", usersTotalPayments.keys.count)
  71. // self.collectionViewLayout.invalidateLayout()
  72. // }
  73.  
  74. // override func viewWillAppear(_ animated: Bool) {
  75. // super.viewWillAppear(true)
  76. // getPaymentDetails()
  77. // }
  78.  
  79. func getPaymentDetails() {
  80.  
  81.  
  82. //self.paymentStatusCollectionView.reloadData()
  83.  
  84. // Do any additional setup after loading the view.
  85. let paymentsQuery = Payments.query()
  86. paymentsQuery?.findObjectsInBackground(block: { (objects, error) in
  87. if let objects = objects {
  88. // print(objects.count)
  89. if objects.count == 0 {
  90. // Utils.stopIndicator(activityIndicator: activityIndicator)
  91. Utils.createAlert(self, title: "No Payment has been made yet", message: "Please Encourage USers to make some Payments", buttonTitle: "Ok")
  92. // TODO:- i think here, we should go back to our homeScreen or to a list of possible friends...
  93. }else {
  94. //print(#function, " no. of objects :", objects.count)
  95. for paymentsObject in objects {
  96. let user = paymentsObject[Utils.name] as! String
  97. let amount = paymentsObject[Utils.amount] as! String
  98. self.usersTotalPayments[user] = amount
  99. self.users.append(user)
  100. self.payments.append(amount)
  101.  
  102. //print(self.usersTotalPayments[user]!)
  103. //print(#function, " A ---- USER :", user)
  104. // if self.usersTotalPayments[user] == nil {
  105. // //print(#function, " B ---- 0")
  106. // self.usersTotalPayments[user] = [amount]
  107. // print(self.usersTotalPayments[user]!)
  108. // }else {
  109. // //print(#function, " C ---- Other")
  110. // self.usersTotalPayments[user]?.append(amount)
  111. // print(self.usersTotalPayments[user]!)
  112. // }
  113. }
  114. // DispatchQueue.main.async(execute: {
  115. // //Thread.sleep(forTimeInterval: 10)
  116. // //self.paymentStatusCollectionView.reloadData()
  117. // self.collectionView?.reloadData()
  118. // print("A - Done Reloading with COUNT: ", self.usersTotalPayments.keys.count)
  119. // })
  120.  
  121. // DispatchQueue.main.async(execute: {
  122. // //Thread.sleep(forTimeInterval: 10)
  123. // //self.paymentStatusCollectionView.reloadData()
  124. // self.collectionView?.reloadData()
  125. // print("B - Done Reloading with COUNT: ", self.usersTotalPayments.keys.count)
  126. // })
  127. // OperationQueue.main.addOperation({
  128. // self.collectionView?.reloadData()
  129. // //self.collectionView?.reloadInputViews()
  130. // self.collectionViewLayout.invalidateLayout()
  131. // print("B - Done Reloading with COUNT: ", self.usersTotalPayments.keys.count)
  132. // })
  133.  
  134. DispatchQueue.main.async { [unowned self] in
  135. self.collectionView?.reloadData()
  136. }
  137. }
  138. }else if error != nil {
  139. print(error!)
  140. }else {
  141. print("Unknown Error")
  142. }
  143. })
  144.  
  145. }
  146.  
  147. override func didReceiveMemoryWarning() {
  148. super.didReceiveMemoryWarning()
  149. // Dispose of any resources that can be recreated.
  150. }
  151.  
  152. // MARK: UICollectionViewDataSource
  153.  
  154. override func numberOfSections(in collectionView: UICollectionView) -> Int {
  155. // #warning Incomplete implementation, return the number of sections
  156. print(#function, " :", usersTotalPayments.keys.count)
  157. // guard let coumt = usersTotalPayments.keys!.count else {
  158. // return 0
  159. // }
  160. //if usersTotalPayments.keys.count == 1 {
  161. // return 1
  162. //} else {
  163. return users.count
  164. //}
  165. }
  166.  
  167. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  168. return CGSize(width: 300, height:30)
  169. }
  170.  
  171. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  172. // #warning Incomplete implementation, return the number of items
  173. return 2
  174. }
  175. // the function belo mde things worse
  176. // override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  177. // print(#function, " :", usersTotalPayments.keys.count)
  178. // collectionView.reloadData()
  179. // }
  180.  
  181.  
  182. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  183. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PaymentStatusCollectionViewCell
  184. print(#function, " :", indexPath)
  185. // Configure the cell
  186. cell.label.text = "Sec " + indexPath.section.description + "/Item " + indexPath.item.description
  187. cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.yellow : UIColor.lightGray
  188.  
  189. //let key = usersTotalPayments.
  190. if indexPath.item == 0 { //item0 represents the object in column 0. the section is the index for row
  191. cell.label.text = users[indexPath.section]
  192. }else if indexPath.item == 1{
  193. cell.label.text = payments[indexPath.section]
  194. }
  195. return cell
  196. }
  197.  
  198. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  199. //print("selected (indexPath)")
  200. }
  201.  
  202. func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
  203. // if isLandscape {
  204. // return CGSize(width: 200, height:30)
  205. // }
  206. // else {
  207. // return CGSize(width: 200, height:30)
  208. // }
  209. return CGSize(width: 200, height:30)
  210. }
  211.  
  212.  
  213.  
  214.  
  215. //
  216. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  217. // print(#function)
  218. // return self.view.frame.size
  219. // }
  220. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
  221. // //return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
  222. // return CGSize(width: 200, height:30)
  223. // }
  224. //
  225. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
  226. // return 2
  227. // }
  228. //
  229. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
  230. // return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  231. // }
  232. //
  233. //
  234. // // MARK: UICollectionViewDelegateFlowLayout
  235. // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  236. // return 4.0
  237. // }
  238. //
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement