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