PhotoCollectionViewAdapter.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 self.selectedArr.contains(indexPath){
  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. alert.addAction(UIAlertAction(title: "크게보기", style: .default){ (action) in
  75. let storyBoard = UIStoryboard(name: "Main", bundle: nil)
  76. let VC = storyBoard.instantiateViewController(withIdentifier: "PhotoDetailViewController") as! PhotoDetailViewController
  77. VC.image = image
  78. do {
  79. let photos = list[indexPath.first ?? 0].1
  80. let photo = photos[indexPath.row]
  81. VC.photo = photo
  82. VC.callBack = { [weak collectionView] in
  83. collectionView?.reloadData()
  84. }
  85. }
  86. nav.pushViewController(VC, animated: true)
  87. })
  88. // show action sheet
  89. if let popoverController = alert.popoverPresentationController {
  90. popoverController.sourceView = nav.visibleViewController?.view
  91. popoverController.sourceRect = CGRect(x: nav.visibleViewController?.view.bounds.midX ?? 0, y: nav.visibleViewController?.view.bounds.midY ?? 0, width: 0, height: 0)
  92. popoverController.permittedArrowDirections = []
  93. }
  94. nav.visibleViewController?.present(alert, animated: true, completion: nil)
  95. }
  96. return cell
  97. }
  98. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  99. //체크된 인덱스가 존재하는지 여부
  100. let isSelected = self.selectedArr.filter({$0 == indexPath}).count > 0
  101. //존재할경우 해당 아이템을 제거후 반환
  102. if isSelected{
  103. self.selectedArr = self.selectedArr.filter({$0 != indexPath})
  104. }else{
  105. self.selectedArr.insert(indexPath)
  106. }
  107. collectionView.reloadItems(at: [indexPath])
  108. }
  109. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  110. //콜렉션뷰의 사이즈를 제어한다
  111. let width = collectionView.frame.width
  112. return CGSize(width: width/columnCount - 1, height: width/columnCount - 1)
  113. }
  114. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  115. return 1.0
  116. }
  117. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  118. return 1.0
  119. }
  120. func getImage(path:String) -> UIImage? {
  121. //도큐먼트의 유저 디렉토리를 찾는다
  122. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  123. //썸네일 이미지 찾기
  124. let thumb = dir.appendingPathComponent("thumb/\(path)")
  125. if let data = try? Data(contentsOf: thumb){
  126. return UIImage(data: data)
  127. }
  128. }
  129. return nil
  130. }
  131. func fileDelete(file:String){
  132. //도큐먼트의 유저 디렉토리를 찾는다
  133. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  134. let path = dir.appendingPathComponent("kunkuk")
  135. //해당 폴더가 없으면 생성
  136. if !FileManager.default.fileExists(atPath: path.path){
  137. try? FileManager.default.createDirectory(at: path, withIntermediateDirectories: true, attributes: nil)
  138. }
  139. let thumb = dir.appendingPathComponent("thumb")
  140. //해당 폴더가 없으면 생성
  141. if !FileManager.default.fileExists(atPath: thumb.path){
  142. try? FileManager.default.createDirectory(at: thumb, withIntermediateDirectories: true, attributes: nil)
  143. }
  144. let fileURL = path.appendingPathComponent(file)
  145. let thumbURL = thumb.appendingPathComponent(file)
  146. try? FileManager.default.removeItem(at: fileURL)
  147. try? FileManager.default.removeItem(at: thumbURL)
  148. }
  149. }
  150. }