NCMedia+Command.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // NCMedia+Command.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/02/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import NextcloudKit
  10. extension NCMedia {
  11. @IBAction func selectOrCancelButtonPressed(_ sender: UIButton) {
  12. isEditMode = !isEditMode
  13. setSelectcancelButton()
  14. }
  15. func setSelectcancelButton() {
  16. selectOcId.removeAll()
  17. tabBarSelect.selectCount = selectOcId.count
  18. if let visibleCells = self.collectionView?.indexPathsForVisibleItems.compactMap({ self.collectionView?.cellForItem(at: $0) }) {
  19. for case let cell as NCGridMediaCell in visibleCells {
  20. cell.selected(false)
  21. }
  22. }
  23. if isEditMode {
  24. activityIndicatorTrailing.constant = 150
  25. selectOrCancelButton.setTitle( NSLocalizedString("_cancel_", comment: ""), for: .normal)
  26. selectOrCancelButtonTrailing.constant = 10
  27. selectOrCancelButton.isHidden = false
  28. menuButton.isHidden = true
  29. tabBarSelect.show()
  30. } else {
  31. activityIndicatorTrailing.constant = 150
  32. selectOrCancelButton.setTitle( NSLocalizedString("_select_", comment: ""), for: .normal)
  33. selectOrCancelButtonTrailing.constant = 50
  34. selectOrCancelButton.isHidden = false
  35. menuButton.isHidden = false
  36. tabBarSelect.hide()
  37. }
  38. }
  39. func setTitleDate(_ offset: CGFloat = 10) {
  40. titleDate?.text = ""
  41. if let metadata = metadatas?.first {
  42. let contentOffsetY = collectionView.contentOffset.y
  43. let top = insetsTop + view.safeAreaInsets.top + offset
  44. if insetsTop + view.safeAreaInsets.top + contentOffsetY < 10 {
  45. titleDate?.text = utility.getTitleFromDate(metadata.date as Date)
  46. return
  47. }
  48. let point = CGPoint(x: offset, y: top + contentOffsetY)
  49. if let indexPath = collectionView.indexPathForItem(at: point) {
  50. let cell = self.collectionView(collectionView, cellForItemAt: indexPath) as? NCGridMediaCell
  51. if let date = cell?.fileDate {
  52. self.titleDate?.text = utility.getTitleFromDate(date)
  53. }
  54. } else {
  55. if offset < 20 {
  56. self.setTitleDate(20)
  57. }
  58. }
  59. }
  60. }
  61. func setColor() {
  62. if isTop {
  63. titleDate?.textColor = .label
  64. activityIndicator.color = .label
  65. selectOrCancelButton.setTitleColor(.label, for: .normal)
  66. menuButton.setImage(UIImage(systemName: "ellipsis")?.withTintColor(.label, renderingMode: .alwaysOriginal), for: .normal)
  67. gradientView.isHidden = true
  68. } else {
  69. titleDate?.textColor = .white
  70. activityIndicator.color = .white
  71. selectOrCancelButton.setTitleColor(.white, for: .normal)
  72. menuButton.setImage(UIImage(systemName: "ellipsis")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)
  73. gradientView.isHidden = false
  74. }
  75. }
  76. func createMenu() {
  77. var columnCount = NCKeychain().mediaColumnCount
  78. let layout = NCKeychain().mediaTypeLayout
  79. let layoutTitle = (layout == NCGlobal.shared.mediaLayoutRatio) ? NSLocalizedString("_media_square_", comment: "") : NSLocalizedString("_media_ratio_", comment: "")
  80. let layoutImage = (layout == NCGlobal.shared.mediaLayoutRatio) ? UIImage(systemName: "square.grid.3x3") : UIImage(systemName: "rectangle.grid.3x2")
  81. if UIDevice.current.userInterfaceIdiom == .phone,
  82. (UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight) {
  83. columnCount += 2
  84. }
  85. if CGFloat(columnCount) >= maxImageGrid - 1 {
  86. self.attributesZoomIn = []
  87. self.attributesZoomOut = .disabled
  88. } else if columnCount <= 1 {
  89. self.attributesZoomIn = .disabled
  90. self.attributesZoomOut = []
  91. } else {
  92. self.attributesZoomIn = []
  93. self.attributesZoomOut = []
  94. }
  95. let viewFilterMenu = UIMenu(title: "", options: .displayInline, children: [
  96. UIAction(title: NSLocalizedString("_media_viewimage_show_", comment: ""), image: UIImage(systemName: "photo")) { _ in
  97. self.showOnlyImages = true
  98. self.showOnlyVideos = false
  99. self.reloadDataSource()
  100. },
  101. UIAction(title: NSLocalizedString("_media_viewvideo_show_", comment: ""), image: UIImage(systemName: "video")) { _ in
  102. self.showOnlyImages = false
  103. self.showOnlyVideos = true
  104. self.reloadDataSource()
  105. },
  106. UIAction(title: NSLocalizedString("_media_show_all_", comment: ""), image: UIImage(systemName: "photo.on.rectangle")) { _ in
  107. self.showOnlyImages = false
  108. self.showOnlyVideos = false
  109. self.reloadDataSource()
  110. }
  111. ])
  112. let viewLayoutMenu = UIAction(title: layoutTitle, image: layoutImage) { _ in
  113. if layout == NCGlobal.shared.mediaLayoutRatio {
  114. NCKeychain().mediaTypeLayout = NCGlobal.shared.mediaLayoutSquare
  115. } else {
  116. NCKeychain().mediaTypeLayout = NCGlobal.shared.mediaLayoutRatio
  117. }
  118. self.createMenu()
  119. self.collectionViewReloadData()
  120. }
  121. let zoomViewMediaFolder = UIMenu(title: "", options: .displayInline, children: [
  122. UIMenu(title: NSLocalizedString("_zoom_", comment: ""), children: [
  123. UIAction(title: NSLocalizedString("_zoom_out_", comment: ""), image: UIImage(systemName: "minus.magnifyingglass"), attributes: self.attributesZoomOut) { _ in
  124. UIView.animate(withDuration: 0.0, animations: {
  125. NCKeychain().mediaColumnCount = columnCount + 1
  126. self.createMenu()
  127. self.collectionViewReloadData()
  128. })
  129. },
  130. UIAction(title: NSLocalizedString("_zoom_in_", comment: ""), image: UIImage(systemName: "plus.magnifyingglass"), attributes: self.attributesZoomIn) { _ in
  131. UIView.animate(withDuration: 0.0, animations: {
  132. NCKeychain().mediaColumnCount = columnCount - 1
  133. self.createMenu()
  134. self.collectionViewReloadData()
  135. })
  136. }
  137. ]),
  138. UIMenu(title: NSLocalizedString("_media_view_options_", comment: ""), children: [viewFilterMenu, viewLayoutMenu]),
  139. UIAction(title: NSLocalizedString("_select_media_folder_", comment: ""), image: UIImage(systemName: "folder"), handler: { _ in
  140. guard let navigationController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateInitialViewController() as? UINavigationController,
  141. let viewController = navigationController.topViewController as? NCSelect else { return }
  142. viewController.delegate = self
  143. viewController.typeOfCommandView = .select
  144. viewController.type = "mediaFolder"
  145. self.present(navigationController, animated: true)
  146. })
  147. ])
  148. let playFile = UIAction(title: NSLocalizedString("_play_from_files_", comment: ""), image: UIImage(systemName: "play.circle")) { _ in
  149. guard let tabBarController = self.appDelegate.window?.rootViewController as? UITabBarController else { return }
  150. self.documentPickerViewController = NCDocumentPickerViewController(tabBarController: tabBarController, isViewerMedia: true, allowsMultipleSelection: false, viewController: self)
  151. }
  152. let playURL = UIAction(title: NSLocalizedString("_play_from_url_", comment: ""), image: UIImage(systemName: "link")) { _ in
  153. let alert = UIAlertController(title: NSLocalizedString("_valid_video_url_", comment: ""), message: nil, preferredStyle: .alert)
  154. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  155. alert.addTextField(configurationHandler: { textField in
  156. textField.placeholder = "http://myserver.com/movie.mkv"
  157. })
  158. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
  159. guard let stringUrl = alert.textFields?.first?.text, !stringUrl.isEmpty, let url = URL(string: stringUrl) else { return }
  160. let fileName = url.lastPathComponent
  161. let metadata = NCManageDatabase.shared.createMetadata(account: self.activeAccount.account, user: self.activeAccount.user, userId: self.activeAccount.userId, fileName: fileName, fileNameView: fileName, ocId: NSUUID().uuidString, serverUrl: "", urlBase: self.activeAccount.urlBase, url: stringUrl, contentType: "")
  162. NCManageDatabase.shared.addMetadata(metadata)
  163. NCViewer().view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  164. }))
  165. self.present(alert, animated: true)
  166. }
  167. menuButton.menu = UIMenu(title: "", children: [zoomViewMediaFolder, playFile, playURL])
  168. }
  169. }
  170. extension NCMedia: NCMediaSelectTabBarDelegate {
  171. func delete() {
  172. let selectOcId = self.selectOcId.map { $0 }
  173. if !selectOcId.isEmpty {
  174. let alertController = UIAlertController(
  175. title: NSLocalizedString("_delete_selected_photos_", comment: ""),
  176. message: "",
  177. preferredStyle: .alert)
  178. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (_: UIAlertAction) in
  179. Task {
  180. var error = NKError()
  181. var ocIds: [String] = []
  182. for ocId in selectOcId where error == .success {
  183. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  184. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  185. if error == .success {
  186. ocIds.append(metadata.ocId)
  187. }
  188. }
  189. }
  190. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocIds, "onlyLocalCache": false, "error": error])
  191. }
  192. self.isEditMode = false
  193. self.setSelectcancelButton()
  194. })
  195. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (_: UIAlertAction) in })
  196. present(alertController, animated: true, completion: { })
  197. }
  198. }
  199. }