TaskFlowCategoryViewController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // TaskFlowCategoryViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/7/28.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import CocoaLumberjack
  15. private let reuseHeaderIdentifier = "AppCategoryHeaderView"
  16. private let reuseCellIdentifier = "AppCategoryCell"
  17. class TaskFlowCategoryViewController: UICollectionViewController {
  18. var apps:[Application] = []
  19. fileprivate static var ItemLineCount:Int {
  20. if UIDevice.deviceModelReadable() == "iPhone 5S" || UIDevice.deviceModelReadable() == "iPhone 5" || UIDevice.deviceModelReadable() == "iPhone SE" {
  21. return 3
  22. }else{
  23. return 4
  24. }
  25. }
  26. fileprivate let ItemSize = Double(SCREEN_WIDTH)/Double(ItemLineCount)
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. self.loadAppList()
  30. }
  31. func loadAppList(){
  32. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(ApplicationContext.applicationContextKey, query: ApplicationContext.applicationListQuery, parameter: nil)
  33. self.showMessage(title: "应用加载中...")
  34. self.apps.removeAll()
  35. Alamofire.request(url!).responseArray(queue: nil, keyPath: "data", context: nil, completionHandler: { (response:DataResponse<[Application]>) in
  36. switch response.result {
  37. case .success(let apps):
  38. self.apps.append(contentsOf: apps)
  39. self.collectionView?.reloadData()
  40. self.showSuccess(title: "加载完成")
  41. case .failure(let err):
  42. DDLogError(err.localizedDescription)
  43. self.showError(title: "加载失败")
  44. }
  45. })
  46. }
  47. // MARK: UICollectionViewDataSource
  48. override func numberOfSections(in collectionView: UICollectionView) -> Int {
  49. return apps.count
  50. }
  51. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  52. return apps[section].processList!.count
  53. }
  54. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  55. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseCellIdentifier, for: indexPath) as! AppCategoryCell
  56. let process = apps[(indexPath as NSIndexPath).section].processList![(indexPath as NSIndexPath).row]
  57. cell.appProcess = process
  58. return cell
  59. }
  60. override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  61. if kind == UICollectionView.elementKindSectionHeader {
  62. let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reuseHeaderIdentifier, for: indexPath) as! AppCategoryHeaderView
  63. let app = apps[(indexPath as NSIndexPath).section]
  64. headerView.app = app
  65. headerView.backgroundColor = UIColor.lightGray
  66. headerView.alpha = 0.7
  67. return headerView
  68. }else{
  69. return UICollectionReusableView(frame: CGRect(x: 0,y: 0,width: SCREEN_WIDTH,height: 1))
  70. }
  71. }
  72. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  73. let process = apps[(indexPath as NSIndexPath).section].processList![(indexPath as NSIndexPath).row]
  74. DDLogDebug("\(String(describing: process.toJSONString()))")
  75. self.performSegue(withIdentifier: "showStartFlowSegue", sender: process)
  76. }
  77. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  78. if segue.identifier == "showStartFlowSegue" {
  79. let destVc = segue.destination as! TaskCreateViewController
  80. destVc.process = sender as? AppProcess
  81. }
  82. }
  83. }
  84. extension TaskFlowCategoryViewController:UICollectionViewDelegateFlowLayout{
  85. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  86. return CGSize(width: CGFloat(ItemSize-1),height: CGFloat(ItemSize-1))
  87. }
  88. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  89. return UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
  90. }
  91. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  92. return CGSize(width: SCREEN_WIDTH,height: 40)
  93. }
  94. func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,referenceSizeForFooterInSection section: Int) -> CGSize {
  95. return CGSize(width: 0,height: 0)
  96. }
  97. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  98. return 0.0
  99. }
  100. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  101. return 0.0
  102. }
  103. }