ScanCollectionView.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //
  2. // ScanCollectionView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/08/18.
  6. // Copyright (c) 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. @available(iOS 11, *)
  25. class DragDropViewController: UIViewController {
  26. //Data Source for collectionViewSource
  27. private var itemsSource = [String]()
  28. //Data Source for collectionViewDestination
  29. private var imagesDestination = [UIImage]()
  30. private var itemsDestination = [String]()
  31. //AppDelegate
  32. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. //MARK: Outlets
  34. @IBOutlet weak var collectionViewSource: UICollectionView!
  35. @IBOutlet weak var collectionViewDestination: UICollectionView!
  36. @IBOutlet weak var cancel: UIBarButtonItem!
  37. @IBOutlet weak var save: UIBarButtonItem!
  38. @IBOutlet weak var add: UIButton!
  39. @IBOutlet weak var transferDown: UIButton!
  40. @IBOutlet weak var labelTitlePDFzone: UILabel!
  41. @IBOutlet weak var segmentControlFilter: UISegmentedControl!
  42. // filter
  43. enum typeFilter {
  44. case original
  45. case grayScale
  46. case bn
  47. }
  48. private var filter: typeFilter = typeFilter.original
  49. override var canBecomeFirstResponder: Bool { return true }
  50. //MARK: View Lifecycle Methods
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. collectionViewSource.dragInteractionEnabled = true
  54. collectionViewSource.dragDelegate = self
  55. collectionViewSource.dropDelegate = self
  56. collectionViewDestination.dragInteractionEnabled = true
  57. collectionViewDestination.dropDelegate = self
  58. collectionViewDestination.dragDelegate = self
  59. collectionViewDestination.reorderingCadence = .fast //default value - .immediate
  60. self.navigationItem.title = NSLocalizedString("_scanned_images_", comment: "")
  61. cancel.title = NSLocalizedString("_cancel_", comment: "")
  62. save.title = NSLocalizedString("_save_", comment: "")
  63. labelTitlePDFzone.text = NSLocalizedString("_scan_label_document_zone_", comment: "")
  64. segmentControlFilter.setTitle(NSLocalizedString("_filter_original_", comment: ""), forSegmentAt: 0)
  65. segmentControlFilter.setTitle(NSLocalizedString("_filter_grayscale_", comment: ""), forSegmentAt: 1)
  66. segmentControlFilter.setTitle(NSLocalizedString("_filter_bn_", comment: ""), forSegmentAt: 2)
  67. add.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "add"), multiplier:2, color: NCBrandColor.sharedInstance.brand), for: .normal)
  68. transferDown.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "transferDown"), multiplier:2, color: NCBrandColor.sharedInstance.brand), for: .normal)
  69. let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(recognizer:)))
  70. add.addGestureRecognizer(longPressRecognizer)
  71. // changeTheming
  72. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  73. changeTheming()
  74. }
  75. override func viewWillAppear(_ animated: Bool) {
  76. super.viewWillAppear(animated)
  77. labelTitlePDFzone.textColor = NCBrandColor.sharedInstance.brandText
  78. labelTitlePDFzone.backgroundColor = NCBrandColor.sharedInstance.brand
  79. segmentControlFilter.tintColor = NCBrandColor.sharedInstance.brand
  80. // Save button
  81. if imagesDestination.count == 0 {
  82. save.isEnabled = false
  83. } else {
  84. save.isEnabled = true
  85. }
  86. loadImage(atPath: CCUtility.getDirectoryScan(), items: &itemsSource)
  87. }
  88. @objc func changeTheming() {
  89. appDelegate.changeTheming(self, tableView: nil, collectionView: nil, form: false)
  90. collectionViewSource.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  91. collectionViewDestination.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  92. }
  93. //MARK: Button Action
  94. @IBAction func cancelAction(sender: UIBarButtonItem) {
  95. self.dismiss(animated: true, completion: nil)
  96. }
  97. @IBAction func saveAction(sender: UIBarButtonItem) {
  98. if imagesDestination.count > 0 {
  99. var images = [UIImage]()
  100. for image in imagesDestination {
  101. images.append(filter(image: image)!)
  102. }
  103. let formViewController = NCCreateFormUploadScanDocument.init(serverUrl: appDelegate.activeMain.serverUrl, arrayImages: images)
  104. self.navigationController?.pushViewController(formViewController, animated: true)
  105. }
  106. }
  107. @IBAction func add(sender: UIButton) {
  108. NCCreateScanDocument.sharedInstance.openScannerDocument(viewController: self, openScan: false)
  109. }
  110. @IBAction func transferDown(sender: UIButton) {
  111. for fileName in itemsSource {
  112. if !itemsDestination.contains(fileName) {
  113. let fileNamePathAt = CCUtility.getDirectoryScan() + "/" + fileName
  114. guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePathAt)) else { return }
  115. guard let image = UIImage(data: data) else { return }
  116. imagesDestination.append(image)
  117. itemsDestination.append(fileName)
  118. }
  119. }
  120. // Save button
  121. if imagesDestination.count == 0 {
  122. save.isEnabled = false
  123. } else {
  124. save.isEnabled = true
  125. }
  126. collectionViewDestination.reloadData()
  127. }
  128. @IBAction func indexChanged(_ sender: AnyObject) {
  129. switch segmentControlFilter.selectedSegmentIndex
  130. {
  131. case 0:
  132. filter = typeFilter.original
  133. case 1:
  134. filter = typeFilter.grayScale
  135. case 2:
  136. filter = typeFilter.bn
  137. default:
  138. break
  139. }
  140. collectionViewDestination.reloadData()
  141. }
  142. //MARK: Private Methods
  143. private func loadImage(atPath: String, items: inout [String]) {
  144. items.removeAll()
  145. do {
  146. let directoryContents = try FileManager.default.contentsOfDirectory(atPath: atPath)
  147. for fileName in directoryContents {
  148. if fileName.first != "." {
  149. items.append(fileName)
  150. }
  151. }
  152. } catch {
  153. print(error.localizedDescription)
  154. }
  155. items = items.sorted()
  156. collectionViewSource.reloadData()
  157. }
  158. func filter(image: UIImage) -> UIImage? {
  159. var inputContrast: Double = 0
  160. if filter == typeFilter.original {
  161. return image
  162. }
  163. if filter == typeFilter.grayScale {
  164. inputContrast = 1
  165. }
  166. if filter == typeFilter.bn {
  167. inputContrast = 4
  168. }
  169. let ciImage = CIImage(image: image)!
  170. let imageFilter = ciImage.applyingFilter("CIColorControls", parameters: ["inputSaturation": 0, "inputContrast": inputContrast])
  171. let context:CIContext = CIContext.init(options: nil)
  172. let cgImage:CGImage = context.createCGImage(imageFilter, from: imageFilter.extent)!
  173. let image:UIImage = UIImage.init(cgImage: cgImage)
  174. return image
  175. }
  176. /// This method moves a cell from source indexPath to destination indexPath within the same collection view. It works for only 1 item. If multiple items selected, no reordering happens.
  177. ///
  178. /// - Parameters:
  179. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  180. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  181. /// - collectionView: collectionView in which reordering needs to be done.
  182. private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
  183. let items = coordinator.items
  184. if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath {
  185. var dIndexPath = destinationIndexPath
  186. if dIndexPath.row >= collectionView.numberOfItems(inSection: 0) {
  187. dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  188. }
  189. collectionView.performBatchUpdates({
  190. if collectionView === collectionViewDestination {
  191. imagesDestination.remove(at: sourceIndexPath.row)
  192. imagesDestination.insert(item.dragItem.localObject as! UIImage, at: dIndexPath.row)
  193. let fileName = itemsDestination[sourceIndexPath.row]
  194. itemsDestination.remove(at: sourceIndexPath.row)
  195. itemsDestination.insert(fileName, at: dIndexPath.row)
  196. } else {
  197. itemsSource.remove(at: sourceIndexPath.row)
  198. itemsSource.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  199. }
  200. collectionView.deleteItems(at: [sourceIndexPath])
  201. collectionView.insertItems(at: [dIndexPath])
  202. })
  203. coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
  204. }
  205. }
  206. /// This method copies a cell from source indexPath in 1st collection view to destination indexPath in 2nd collection view. It works for multiple items.
  207. ///
  208. /// - Parameters:
  209. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  210. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  211. /// - collectionView: collectionView in which reordering needs to be done.
  212. private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  213. {
  214. collectionView.performBatchUpdates({
  215. var indexPaths = [IndexPath]()
  216. for (index, item) in coordinator.items.enumerated() {
  217. let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
  218. if collectionView === collectionViewDestination {
  219. let fileName = item.dragItem.localObject as! String
  220. let fileNamePathAt = CCUtility.getDirectoryScan() + "/" + fileName
  221. guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePathAt)) else {
  222. return
  223. }
  224. guard let image = UIImage(data: data) else {
  225. return
  226. }
  227. imagesDestination.insert(image, at: indexPath.row)
  228. itemsDestination.insert(fileName, at: indexPath.row)
  229. } else {
  230. // NOT PERMITTED
  231. return
  232. }
  233. indexPaths.append(indexPath)
  234. }
  235. collectionView.insertItems(at: indexPaths)
  236. })
  237. }
  238. // MARK: - UIGestureRecognizerv - Paste
  239. @objc func handleLongPressGesture(recognizer: UIGestureRecognizer) {
  240. if recognizer.state == UIGestureRecognizer.State.began {
  241. self.becomeFirstResponder()
  242. let pasteboard = UIPasteboard.general
  243. if let recognizerView = recognizer.view, let recognizerSuperView = recognizerView.superview, pasteboard.hasImages {
  244. UIMenuController.shared.menuItems = [UIMenuItem(title: "Paste", action: #selector(pasteImage))]
  245. UIMenuController.shared.setTargetRect(recognizerView.frame, in: recognizerSuperView)
  246. UIMenuController.shared.setMenuVisible(true, animated:true)
  247. }
  248. }
  249. }
  250. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  251. if action == #selector(pasteImage) {
  252. return true
  253. }
  254. return false
  255. }
  256. @objc func pasteImage() {
  257. let pasteboard = UIPasteboard.general
  258. if pasteboard.hasImages {
  259. let fileName = CCUtility.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)!
  260. let fileNamePath = CCUtility.getDirectoryScan() + "/" + fileName
  261. guard let image = pasteboard.image else {
  262. return
  263. }
  264. do {
  265. try image.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath), options: .atomic)
  266. } catch {
  267. return
  268. }
  269. loadImage(atPath: CCUtility.getDirectoryScan(), items: &itemsSource)
  270. }
  271. }
  272. }
  273. // MARK: - UICollectionViewDataSource Methods
  274. @available(iOS 11, *)
  275. extension DragDropViewController : UICollectionViewDataSource {
  276. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  277. return collectionView == collectionViewSource ? itemsSource.count : imagesDestination.count
  278. }
  279. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  280. if collectionView == collectionViewSource {
  281. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! ScanCell
  282. let fileNamePath = CCUtility.getDirectoryScan() + "/" + itemsSource[indexPath.row]
  283. guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePath)) else {
  284. return cell
  285. }
  286. guard var image = UIImage(data: data) else {
  287. return cell
  288. }
  289. let imageWidthInPixels = image.size.width * image.scale
  290. let imageHeightInPixels = image.size.height * image.scale
  291. // 72 DPI
  292. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  293. image = CCGraphics.scale(image, to: CGSize(width: 595, height: 842), isAspectRation: true)
  294. }
  295. cell.customImageView?.image = image
  296. cell.delete.addTarget(self, action: #selector(deleteSource(_:)), for: .touchUpInside)
  297. return cell
  298. } else {
  299. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! ScanCell
  300. var image = imagesDestination[indexPath.row]
  301. let imageWidthInPixels = image.size.width * image.scale
  302. let imageHeightInPixels = image.size.height * image.scale
  303. // 72 DPI
  304. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  305. image = CCGraphics.scale(image, to: CGSize(width: 595, height: 842), isAspectRation: true)
  306. }
  307. cell.customImageView?.image = self.filter(image: image)
  308. cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row+1)"
  309. cell.delete.addTarget(self, action: #selector(deleteDestination(_:)), for: .touchUpInside)
  310. cell.rotate.addTarget(self, action: #selector(rotateDestination(_:)), for: .touchUpInside)
  311. return cell
  312. }
  313. }
  314. @objc func deleteSource(_ sender: UIButton) {
  315. let buttonPosition:CGPoint = sender.convert(.zero, to: collectionViewSource)
  316. let indexPath:IndexPath = collectionViewSource.indexPathForItem(at: buttonPosition)!
  317. let fileNameAtPath = CCUtility.getDirectoryScan() + "/" + itemsSource[indexPath.row]
  318. CCUtility.removeFile(atPath: fileNameAtPath)
  319. itemsSource.remove(at: indexPath.row)
  320. collectionViewSource.reloadData()
  321. }
  322. @objc func deleteDestination(_ sender:UIButton) {
  323. let buttonPosition:CGPoint = sender.convert(.zero, to: collectionViewDestination)
  324. let indexPath:IndexPath = collectionViewDestination.indexPathForItem(at: buttonPosition)!
  325. imagesDestination.remove(at: indexPath.row)
  326. itemsDestination.remove(at: indexPath.row)
  327. collectionViewDestination.reloadData()
  328. // Save button
  329. if imagesDestination.count == 0 {
  330. save.isEnabled = false
  331. } else {
  332. save.isEnabled = true
  333. }
  334. }
  335. @objc func rotateDestination(_ sender:UIButton) {
  336. let buttonPosition:CGPoint = sender.convert(.zero, to: collectionViewDestination)
  337. let indexPath:IndexPath = collectionViewDestination.indexPathForItem(at: buttonPosition)!
  338. let image = imagesDestination[indexPath.row]
  339. imagesDestination[indexPath.row] = image.rotate(radians: .pi/2)!
  340. collectionViewDestination.reloadData()
  341. }
  342. }
  343. extension UIImage {
  344. func rotate(radians: Float) -> UIImage? {
  345. var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).size
  346. // Trim off the extremely small float value to prevent core graphics from rounding it up
  347. newSize.width = floor(newSize.width)
  348. newSize.height = floor(newSize.height)
  349. UIGraphicsBeginImageContextWithOptions(newSize, true, self.scale)
  350. let context = UIGraphicsGetCurrentContext()!
  351. // Move origin to middle
  352. context.translateBy(x: newSize.width/2, y: newSize.height/2)
  353. // Rotate around middle
  354. context.rotate(by: CGFloat(radians))
  355. // Draw the image at its center
  356. self.draw(in: CGRect(x: -self.size.width/2, y: -self.size.height/2, width: self.size.width, height: self.size.height))
  357. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  358. UIGraphicsEndImageContext()
  359. return newImage
  360. }
  361. }
  362. // MARK: - UICollectionViewDragDelegate Methods
  363. @available(iOS 11, *)
  364. extension DragDropViewController : UICollectionViewDragDelegate
  365. {
  366. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  367. if collectionView == collectionViewSource {
  368. let item = itemsSource[indexPath.row]
  369. let itemProvider = NSItemProvider(object: item as NSString)
  370. let dragItem = UIDragItem(itemProvider: itemProvider)
  371. dragItem.localObject = item
  372. return [dragItem]
  373. } else {
  374. let item = imagesDestination[indexPath.row]
  375. let itemProvider = NSItemProvider(object: item as UIImage)
  376. let dragItem = UIDragItem(itemProvider: itemProvider)
  377. dragItem.localObject = item
  378. return [dragItem]
  379. }
  380. }
  381. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
  382. if collectionView == collectionViewSource {
  383. let item = itemsSource[indexPath.row]
  384. let itemProvider = NSItemProvider(object: item as NSString)
  385. let dragItem = UIDragItem(itemProvider: itemProvider)
  386. dragItem.localObject = item
  387. return [dragItem]
  388. } else {
  389. let item = imagesDestination[indexPath.row]
  390. let itemProvider = NSItemProvider(object: item as UIImage)
  391. let dragItem = UIDragItem(itemProvider: itemProvider)
  392. dragItem.localObject = item
  393. return [dragItem]
  394. }
  395. }
  396. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
  397. let previewParameters = UIDragPreviewParameters()
  398. if collectionView == collectionViewSource {
  399. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 100, height: 100))
  400. } else {
  401. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 80, height: 80))
  402. }
  403. return previewParameters
  404. }
  405. }
  406. // MARK: - UICollectionViewDropDelegate Methods
  407. @available(iOS 11, *)
  408. extension DragDropViewController : UICollectionViewDropDelegate {
  409. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  410. return true //session.canLoadObjects(ofClass: NSString.self)
  411. }
  412. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  413. if collectionView == collectionViewSource {
  414. if collectionView.hasActiveDrag {
  415. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  416. } else {
  417. return UICollectionViewDropProposal(operation: .forbidden)
  418. }
  419. } else {
  420. if collectionView.hasActiveDrag {
  421. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  422. } else {
  423. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  424. }
  425. }
  426. }
  427. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  428. let destinationIndexPath: IndexPath
  429. switch coordinator.proposal.operation {
  430. case .move:
  431. if let indexPath = coordinator.destinationIndexPath {
  432. destinationIndexPath = indexPath
  433. } else {
  434. // Get last index path of table view.
  435. let section = collectionView.numberOfSections - 1
  436. let row = collectionView.numberOfItems(inSection: section)
  437. destinationIndexPath = IndexPath(row: row, section: section)
  438. }
  439. self.reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  440. break
  441. case .copy:
  442. // Get last index path of table view.
  443. let section = collectionView.numberOfSections - 1
  444. let row = collectionView.numberOfItems(inSection: section)
  445. destinationIndexPath = IndexPath(row: row, section: section)
  446. self.copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  447. break
  448. default:
  449. return
  450. }
  451. }
  452. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  453. collectionViewDestination.reloadData()
  454. // Save button
  455. if imagesDestination.count == 0 {
  456. save.isEnabled = false
  457. } else {
  458. save.isEnabled = true
  459. }
  460. }
  461. }