NCMedia+Command.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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?.date {
  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, UIDevice.current.orientation.isLandscape {
  82. columnCount += 2
  83. }
  84. if CGFloat(columnCount) >= maxImageGrid - 1 {
  85. self.attributesZoomIn = []
  86. self.attributesZoomOut = .disabled
  87. } else if columnCount <= 1 {
  88. self.attributesZoomIn = .disabled
  89. self.attributesZoomOut = []
  90. } else {
  91. self.attributesZoomIn = []
  92. self.attributesZoomOut = []
  93. }
  94. let viewFilterMenu = UIMenu(title: "", options: .displayInline, children: [
  95. UIAction(title: NSLocalizedString("_media_viewimage_show_", comment: ""), image: UIImage(systemName: "photo")) { _ in
  96. self.showOnlyImages = true
  97. self.showOnlyVideos = false
  98. self.reloadDataSource()
  99. },
  100. UIAction(title: NSLocalizedString("_media_viewvideo_show_", comment: ""), image: UIImage(systemName: "video")) { _ in
  101. self.showOnlyImages = false
  102. self.showOnlyVideos = true
  103. self.reloadDataSource()
  104. },
  105. UIAction(title: NSLocalizedString("_media_show_all_", comment: ""), image: UIImage(systemName: "photo.on.rectangle")) { _ in
  106. self.showOnlyImages = false
  107. self.showOnlyVideos = false
  108. self.reloadDataSource()
  109. }
  110. ])
  111. let viewLayoutMenu = UIAction(title: layoutTitle, image: layoutImage) { _ in
  112. if layout == NCGlobal.shared.mediaLayoutRatio {
  113. NCKeychain().mediaTypeLayout = NCGlobal.shared.mediaLayoutSquare
  114. } else {
  115. NCKeychain().mediaTypeLayout = NCGlobal.shared.mediaLayoutRatio
  116. }
  117. self.createMenu()
  118. self.collectionViewReloadData()
  119. }
  120. let zoomViewMediaFolder = UIMenu(title: "", options: .displayInline, children: [
  121. UIMenu(title: NSLocalizedString("_zoom_", comment: ""), children: [
  122. UIAction(title: NSLocalizedString("_zoom_out_", comment: ""), image: UIImage(systemName: "minus.magnifyingglass"), attributes: self.attributesZoomOut) { _ in
  123. UIView.animate(withDuration: 0.0, animations: {
  124. NCKeychain().mediaColumnCount = columnCount + 1
  125. self.createMenu()
  126. self.collectionViewReloadData()
  127. })
  128. },
  129. UIAction(title: NSLocalizedString("_zoom_in_", comment: ""), image: UIImage(systemName: "plus.magnifyingglass"), attributes: self.attributesZoomIn) { _ in
  130. UIView.animate(withDuration: 0.0, animations: {
  131. NCKeychain().mediaColumnCount = columnCount - 1
  132. self.createMenu()
  133. self.collectionViewReloadData()
  134. })
  135. }
  136. ]),
  137. UIMenu(title: NSLocalizedString("_media_view_options_", comment: ""), children: [viewFilterMenu, viewLayoutMenu]),
  138. UIAction(title: NSLocalizedString("_select_media_folder_", comment: ""), image: UIImage(systemName: "folder"), handler: { _ in
  139. guard let navigationController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateInitialViewController() as? UINavigationController,
  140. let viewController = navigationController.topViewController as? NCSelect else { return }
  141. viewController.delegate = self
  142. viewController.typeOfCommandView = .select
  143. viewController.type = "mediaFolder"
  144. self.present(navigationController, animated: true)
  145. })
  146. ])
  147. let playFile = UIAction(title: NSLocalizedString("_play_from_files_", comment: ""), image: UIImage(systemName: "play.circle")) { _ in
  148. guard let tabBarController = self.appDelegate.window?.rootViewController as? UITabBarController else { return }
  149. self.documentPickerViewController = NCDocumentPickerViewController(tabBarController: tabBarController, isViewerMedia: true, allowsMultipleSelection: false, viewController: self)
  150. }
  151. let playURL = UIAction(title: NSLocalizedString("_play_from_url_", comment: ""), image: UIImage(systemName: "link")) { _ in
  152. let alert = UIAlertController(title: NSLocalizedString("_valid_video_url_", comment: ""), message: nil, preferredStyle: .alert)
  153. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  154. alert.addTextField(configurationHandler: { textField in
  155. textField.placeholder = "http://myserver.com/movie.mkv"
  156. })
  157. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
  158. guard let stringUrl = alert.textFields?.first?.text, !stringUrl.isEmpty, let url = URL(string: stringUrl) else { return }
  159. let fileName = url.lastPathComponent
  160. 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: "")
  161. NCManageDatabase.shared.addMetadata(metadata)
  162. NCViewer().view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  163. }))
  164. self.present(alert, animated: true)
  165. }
  166. menuButton.menu = UIMenu(title: "", children: [zoomViewMediaFolder, playFile, playURL])
  167. }
  168. }
  169. extension NCMedia: NCMediaSelectTabBarDelegate {
  170. func delete() {
  171. let selectOcId = self.selectOcId.map { $0 }
  172. if !selectOcId.isEmpty {
  173. let alertController = UIAlertController(
  174. title: NSLocalizedString("_delete_selected_photos_", comment: ""),
  175. message: "",
  176. preferredStyle: .alert)
  177. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (_: UIAlertAction) in
  178. Task {
  179. var error = NKError()
  180. var ocIds: [String] = []
  181. for ocId in selectOcId where error == .success {
  182. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  183. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  184. if error == .success {
  185. ocIds.append(metadata.ocId)
  186. }
  187. }
  188. }
  189. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocIds, "onlyLocalCache": false, "error": error])
  190. }
  191. self.isEditMode = false
  192. self.setSelectcancelButton()
  193. })
  194. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (_: UIAlertAction) in })
  195. present(alertController, animated: true, completion: { })
  196. }
  197. }
  198. }