PhotoCollectionViewAdapter.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // PhotoCollectionViewAdapter.swift
  3. // MCPlus
  4. //
  5. // Created by seo ha on 07/02/2019.
  6. // Copyright © 2019 KangSH. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. class PhotoCollectionViewAdapter: NSObject {
  11. var list:[(String,[Photo])]?
  12. //체크 확인용
  13. //var selectedArr = Set<IndexPath>()
  14. var columnCount:CGFloat = UIDevice.current.userInterfaceIdiom == .pad ? 6 : 3
  15. }
  16. extension PhotoCollectionViewAdapter:UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UINavigationControllerDelegate{
  17. func numberOfSections(in collectionView: UICollectionView) -> Int {
  18. return list?.count ?? 0
  19. }
  20. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  21. //리스트에서 키값을 얻어온다
  22. guard let list = list else{ return 0 }
  23. return list[section].1.count
  24. }
  25. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  26. //타이틀 뷰의 생성
  27. let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "PhotoCollectionViewReusableView", for: indexPath) as! PhotoCollectionViewReusableView
  28. guard let list = list else{ return view }
  29. let key = list[indexPath.first ?? 0].0
  30. //키값을 새탕
  31. view.title.text = key
  32. return view
  33. }
  34. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  35. //앨범 이미지 셀을 생성
  36. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! PhotoCollectionViewCell
  37. guard let list = list else{ return cell }
  38. //해당 키값의 파일정보를 얻어 이미지를 불러온다
  39. guard let path = list[indexPath.first ?? 0].1[indexPath.row].file, let image = getImage(path: path) else{
  40. return cell
  41. }
  42. if list[indexPath.first ?? 0].1[indexPath.row].isSended == true{
  43. cell.album.borderWidth = 3.0
  44. }else{
  45. cell.album.borderWidth = 0
  46. }
  47. cell.album.image = image
  48. //체크가 되었을 경우 표시를 다르게 한다
  49. if list[indexPath.first ?? 0].1[indexPath.row].isChecked{
  50. cell.check.isChecked = true
  51. }else{
  52. cell.check.isChecked = false
  53. }
  54. //길게 누를 경우 이벤트
  55. cell.addLongPressGestureRecognizer { [weak self] in
  56. guard let `self` = self else{ return }
  57. let app = UIApplication.shared.delegate as! AppDelegate
  58. guard let nav = app.visibleViewController?.navigationController as? UINavigationController else{ return }
  59. // let alert = UIAlertController(title: "선택", message: "", preferredStyle: .actionSheet)
  60. //개별 앨범 이미지를 삭제한다
  61. // alert.addAction(UIAlertAction(title: "삭제", style: .destructive){ [weak collectionView](action) in
  62. // do {
  63. // let photos = list[indexPath.first ?? 0].1
  64. // let photo = photos[indexPath.row]
  65. // if let file = photo.file{
  66. // self.fileDelete(file: file)
  67. // }
  68. // let _ = photo.delete()
  69. // self.selectedArr = self.selectedArr.filter({$0 != indexPath})
  70. // CustomCameraViewController.photoProcessCommit.value = nil
  71. // }
  72. //
  73. // })
  74. //아이템을 재전송
  75. // alert.addAction(UIAlertAction(title: "재전송", style: .destructive){ [weak collectionView](action) in
  76. // do {
  77. // let photos = list[indexPath.first ?? 0].1
  78. // let photo = photos[indexPath.row]
  79. // PhotoViewController.reUploadItem.value = photo
  80. // CustomCameraViewController.photoProcessCommit.value = nil
  81. // }
  82. //
  83. // })
  84. //디테일 보기 화면으로 이동한다
  85. // alert.addAction(UIAlertAction(title: "크게보기", style: .default){ (action) in
  86. // let storyBoard = UIStoryboard(name: "Main", bundle: nil)
  87. // let VC = storyBoard.instantiateViewController(withIdentifier: "PhotoDetailViewController") as! PhotoDetailViewController
  88. // VC.image = image
  89. // do {
  90. // let photos = list[indexPath.first ?? 0].1
  91. // let photo = photos[indexPath.row]
  92. // VC.photo = photo
  93. // VC.callBack = { [weak collectionView] in
  94. // collectionView?.reloadData()
  95. // }
  96. // }
  97. //
  98. // nav.pushViewController(VC, animated: true)
  99. // })
  100. // show action sheet
  101. // if let popoverController = alert.popoverPresentationController {
  102. // popoverController.sourceView = nav.visibleViewController?.view
  103. // popoverController.sourceRect = CGRect(x: nav.visibleViewController?.view.bounds.midX ?? 0, y: nav.visibleViewController?.view.bounds.midY ?? 0, width: 0, height: 0)
  104. // popoverController.permittedArrowDirections = []
  105. // }
  106. // nav.visibleViewController?.present(alert, animated: true, completion: nil)
  107. let storyBoard = UIStoryboard(name: "Main", bundle: nil)
  108. let VC = storyBoard.instantiateViewController(withIdentifier: "PhotoDetailViewController") as! PhotoDetailViewController
  109. VC.image = image
  110. do {
  111. let photos = list[indexPath.first ?? 0].1
  112. let photo = photos[indexPath.row]
  113. VC.photo = photo
  114. VC.callBack = { [weak collectionView] in
  115. collectionView?.reloadData()
  116. }
  117. }
  118. nav.pushViewController(VC, animated: true)
  119. }
  120. return cell
  121. }
  122. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  123. //체크된 인덱스가 존재하는지 여부
  124. guard let list = list else{ return }
  125. list[indexPath.first ?? 0].1[indexPath.row].isChecked.toggle()
  126. // let isSelected = self.selectedArr.filter({$0 == indexPath}).count > 0
  127. // //존재할경우 해당 아이템을 제거후 반환
  128. // if isSelected{
  129. // self.selectedArr = self.selectedArr.filter({$0 != indexPath})
  130. // }else{
  131. // self.selectedArr.insert(indexPath)
  132. // }
  133. collectionView.reloadItems(at: [indexPath])
  134. }
  135. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  136. //콜렉션뷰의 사이즈를 제어한다
  137. let width = collectionView.frame.width
  138. return CGSize(width: width/columnCount - 1, height: width/columnCount - 1)
  139. }
  140. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  141. return 1.0
  142. }
  143. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  144. return 1.0
  145. }
  146. func getImage(path:String) -> UIImage? {
  147. //도큐먼트의 유저 디렉토리를 찾는다
  148. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  149. //썸네일 이미지 찾기
  150. let thumb = dir.appendingPathComponent("thumb/\(path)")
  151. if let data = try? Data(contentsOf: thumb){
  152. return UIImage(data: data)
  153. }
  154. }
  155. return nil
  156. }
  157. func fileDelete(file:String){
  158. //도큐먼트의 유저 디렉토리를 찾는다
  159. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  160. let path = dir.appendingPathComponent("kunkuk")
  161. //해당 폴더가 없으면 생성
  162. if !FileManager.default.fileExists(atPath: path.path){
  163. try? FileManager.default.createDirectory(at: path, withIntermediateDirectories: true, attributes: nil)
  164. }
  165. let thumb = dir.appendingPathComponent("thumb")
  166. //해당 폴더가 없으면 생성
  167. if !FileManager.default.fileExists(atPath: thumb.path){
  168. try? FileManager.default.createDirectory(at: thumb, withIntermediateDirectories: true, attributes: nil)
  169. }
  170. let fileURL = path.appendingPathComponent(file)
  171. let thumbURL = thumb.appendingPathComponent(file)
  172. try? FileManager.default.removeItem(at: fileURL)
  173. try? FileManager.default.removeItem(at: thumbURL)
  174. }
  175. }
  176. }