KMBatchCollectionView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // KMBatchCollectionView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/1/12.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMBatchCollectionView: BaseXibView {
  10. @IBOutlet weak var collectionView: NSCollectionView!
  11. var delegate: KMBatchCollectionViewDelegate?
  12. var inputType: KMBatchCollectionViewType? {
  13. didSet {
  14. self.didSelectBatchType(type: inputType!)
  15. }
  16. }
  17. // /**
  18. // @abstract 外部传入数据
  19. // @param inputData 文件路劲
  20. // */
  21. // var inputData: [URL]! {
  22. // didSet {
  23. // self.tableView.inputData = inputData
  24. // }
  25. // }
  26. var presenter: KMBatchCollectionViewPrensenter = KMBatchCollectionViewPrensenter()
  27. var data:[KMBatchCollectionViewModel] = []
  28. var isDisable: Bool = false {
  29. didSet {
  30. for item in data {
  31. item.isDisable = isDisable
  32. }
  33. self.collectionView.reloadData()
  34. }
  35. }
  36. override func draw(_ dirtyRect: NSRect) {
  37. super.draw(dirtyRect)
  38. // Drawing code here.
  39. }
  40. deinit {
  41. self.delegate = nil
  42. }
  43. override func setup() {
  44. super.setup()
  45. self.wantsLayer = true
  46. self.border(ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider"), 0.5, 0)
  47. self.contentView.wantsLayer = true
  48. // self.contentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  49. self.collectionView.delegate = self
  50. self.collectionView.dataSource = self
  51. self.collectionView.isSelectable = true
  52. self.collectionView.allowsMultipleSelection = false
  53. //注册cell
  54. self.collectionView.register(KMBatchCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem"))
  55. self.presenter.initPresenter(view: self)
  56. }
  57. func reloadData() {
  58. }
  59. }
  60. extension KMBatchCollectionView: KMBatchCollectionViewPrensenterDelegate {
  61. func showData(presenter: KMBatchCollectionViewPrensenter, data: Array<KMBatchCollectionViewModel>) {
  62. self.data = data
  63. self.collectionView.reloadData()
  64. }
  65. }
  66. extension KMBatchCollectionView: NSCollectionViewDelegate {
  67. //当item被选中
  68. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  69. for item in self.data {
  70. item.isSelect = false
  71. }
  72. let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem
  73. view.model.isSelect = true
  74. self.collectionView.reloadData()
  75. if self.delegate != nil {
  76. self.delegate?.didSelect(index: indexPaths.first!, data: view.model)
  77. }
  78. }
  79. // //当item取消选中
  80. // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  81. // let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem
  82. // view.model.isSelect = false
  83. // self.collectionView.reloadData()
  84. // }
  85. func didSelectBatchType(type: KMBatchCollectionViewType) {
  86. for item in self.data {
  87. if item.type == type {
  88. item.isSelect = true
  89. } else {
  90. item.isSelect = false
  91. }
  92. }
  93. self.collectionView.reloadData()
  94. }
  95. }
  96. extension KMBatchCollectionView: NSCollectionViewDataSource {
  97. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  98. return self.data.count
  99. }
  100. //返回对应的item自定义个体
  101. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  102. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem"), for: indexPath) as! KMBatchCollectionViewItem
  103. view.model = self.data[indexPath.item]
  104. return view
  105. }
  106. }
  107. extension KMBatchCollectionView: NSCollectionViewDelegateFlowLayout {
  108. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  109. return NSSize(width: self.contentView.bounds.width, height: 32)
  110. }
  111. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  112. return 16
  113. }
  114. }
  115. //MARK: KMBatchCollectionViewDelegate
  116. protocol KMBatchCollectionViewDelegate: NSObject {
  117. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel)
  118. }