NCMedia+Command.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // 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 Foundation
  24. import UIKit
  25. import NextcloudKit
  26. extension NCMedia {
  27. @IBAction func selectOrCancelButtonPressed(_ sender: UIButton) {
  28. isEditMode = !isEditMode
  29. setSelectcancelButton()
  30. }
  31. func setEditMode(_ editMode: Bool) {
  32. isEditMode = editMode
  33. setSelectcancelButton()
  34. }
  35. func setSelectcancelButton() {
  36. fileSelect.removeAll()
  37. fileDeleted.removeAll()
  38. tabBarSelect.selectCount = fileSelect.count
  39. if let visibleCells = self.collectionView?.indexPathsForVisibleItems.compactMap({ self.collectionView?.cellForItem(at: $0) }) {
  40. for case let cell as NCMediaCell in visibleCells {
  41. cell.selected(false)
  42. }
  43. }
  44. if isEditMode {
  45. activityIndicatorTrailing.constant = 150
  46. selectOrCancelButton.setTitle( NSLocalizedString("_cancel_", comment: ""), for: .normal)
  47. selectOrCancelButtonTrailing.constant = 10
  48. selectOrCancelButton.isHidden = false
  49. menuButton.isHidden = true
  50. tabBarSelect.show()
  51. } else {
  52. activityIndicatorTrailing.constant = 150
  53. selectOrCancelButton.setTitle( NSLocalizedString("_select_", comment: ""), for: .normal)
  54. selectOrCancelButtonTrailing.constant = 50
  55. selectOrCancelButton.isHidden = false
  56. menuButton.isHidden = false
  57. tabBarSelect.hide()
  58. }
  59. }
  60. func setTitleDate() {
  61. if let layoutAttributes = collectionView.collectionViewLayout.layoutAttributesForElements(in: collectionView.bounds) {
  62. let sortedAttributes = layoutAttributes.sorted { $0.frame.minY < $1.frame.minY || ($0.frame.minY == $1.frame.minY && $0.frame.minX < $1.frame.minX) }
  63. if let firstAttribute = sortedAttributes.first, let metadata = dataSource.getMetadata(indexPath: firstAttribute.indexPath) {
  64. titleDate?.text = utility.getTitleFromDate(metadata.date as Date)
  65. return
  66. }
  67. }
  68. titleDate?.text = ""
  69. }
  70. func setColor() {
  71. if isTop {
  72. UIView.animate(withDuration: 0.3) { [self] in
  73. gradientView.alpha = 0
  74. titleDate?.textColor = NCBrandColor.shared.textColor
  75. activityIndicator.color = NCBrandColor.shared.textColor
  76. selectOrCancelButton.setTitleColor(NCBrandColor.shared.textColor, for: .normal)
  77. menuButton.setImage(NCUtility().loadImage(named: "ellipsis", colors: [NCBrandColor.shared.textColor]), for: .normal)
  78. }
  79. } else {
  80. UIView.animate(withDuration: 0.3) { [self] in
  81. gradientView.alpha = 1
  82. titleDate?.textColor = .white
  83. activityIndicator.color = .white
  84. selectOrCancelButton.setTitleColor(.white, for: .normal)
  85. menuButton.setImage(NCUtility().loadImage(named: "ellipsis", colors: [.white]), for: .normal)
  86. }
  87. }
  88. }
  89. func createMenu() {
  90. let layoutForView = database.getLayoutForView(account: session.account, key: global.layoutViewMedia, serverUrl: "")
  91. var layout = layoutForView?.layout ?? global.mediaLayoutRatio
  92. /// Overwrite default value
  93. if layout == global.layoutList { layout = global.mediaLayoutRatio }
  94. ///
  95. let layoutTitle = (layout == global.mediaLayoutRatio) ? NSLocalizedString("_media_square_", comment: "") : NSLocalizedString("_media_ratio_", comment: "")
  96. let layoutImage = (layout == global.mediaLayoutRatio) ? utility.loadImage(named: "square.grid.3x3") : utility.loadImage(named: "rectangle.grid.3x2")
  97. let viewFilterMenu = UIMenu(title: "", options: .displayInline, children: [
  98. UIAction(title: NSLocalizedString("_media_viewimage_show_", comment: ""), image: utility.loadImage(named: "photo")) { _ in
  99. self.showOnlyImages = true
  100. self.showOnlyVideos = false
  101. self.loadDataSource()
  102. self.networkRemoveAll()
  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.loadDataSource()
  108. self.networkRemoveAll()
  109. },
  110. UIAction(title: NSLocalizedString("_media_show_all_", comment: ""), image: utility.loadImage(named: "photo.on.rectangle")) { _ in
  111. self.showOnlyImages = false
  112. self.showOnlyVideos = false
  113. self.loadDataSource()
  114. self.searchMediaUI()
  115. }
  116. ])
  117. let viewLayoutMenu = UIMenu(title: "", options: .displayInline, children: [
  118. UIAction(title: layoutTitle, image: layoutImage) { _ in
  119. if layout == self.global.mediaLayoutRatio {
  120. self.database.setLayoutForView(account: self.session.account, key: self.global.layoutViewMedia, serverUrl: "", layout: self.global.mediaLayoutSquare)
  121. self.layoutType = self.global.mediaLayoutSquare
  122. } else {
  123. self.database.setLayoutForView(account: self.session.account, key: self.global.layoutViewMedia, serverUrl: "", layout: self.global.mediaLayoutRatio)
  124. self.layoutType = self.global.mediaLayoutRatio
  125. }
  126. self.createMenu()
  127. self.collectionViewReloadData()
  128. }
  129. ])
  130. let viewFolderMedia = UIMenu(title: "", options: .displayInline, children: [
  131. UIAction(title: NSLocalizedString("_select_media_folder_", comment: ""), image: utility.loadImage(named: "folder"), handler: { _ in
  132. guard let navigationController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateInitialViewController() as? UINavigationController,
  133. let viewController = navigationController.topViewController as? NCSelect else { return }
  134. viewController.delegate = self
  135. viewController.typeOfCommandView = .select
  136. viewController.type = "mediaFolder"
  137. viewController.session = self.session
  138. self.present(navigationController, animated: true)
  139. })
  140. ])
  141. let playFile = UIAction(title: NSLocalizedString("_play_from_files_", comment: ""), image: utility.loadImage(named: "play.circle")) { _ in
  142. guard let controller = self.controller else { return }
  143. self.documentPickerViewController = NCDocumentPickerViewController(controller: controller, isViewerMedia: true, allowsMultipleSelection: false, viewController: self)
  144. }
  145. let playURL = UIAction(title: NSLocalizedString("_play_from_url_", comment: ""), image: utility.loadImage(named: "link")) { _ in
  146. let alert = UIAlertController(title: NSLocalizedString("_valid_video_url_", comment: ""), message: nil, preferredStyle: .alert)
  147. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  148. alert.addTextField(configurationHandler: { textField in
  149. textField.placeholder = "http://myserver.com/movie.mkv"
  150. })
  151. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
  152. guard let stringUrl = alert.textFields?.first?.text, !stringUrl.isEmpty, let url = URL(string: stringUrl) else { return }
  153. let fileName = url.lastPathComponent
  154. let metadata = self.database.createMetadata(fileName: fileName,
  155. fileNameView: fileName,
  156. ocId: NSUUID().uuidString,
  157. serverUrl: "",
  158. url: stringUrl,
  159. contentType: "",
  160. session: self.session,
  161. sceneIdentifier: self.controller?.sceneIdentifier)
  162. self.database.addMetadata(metadata)
  163. NCViewer().view(viewController: self, metadata: metadata)
  164. }))
  165. self.present(alert, animated: true)
  166. }
  167. menuButton.menu = UIMenu(title: "", children: [viewFilterMenu, viewLayoutMenu, viewFolderMedia, playFile, playURL])
  168. }
  169. }
  170. extension NCMedia: NCMediaSelectTabBarDelegate {
  171. func delete() {
  172. let ocIds = self.fileSelect.map { $0 }
  173. var alertStyle = UIAlertController.Style.actionSheet
  174. var indexPaths: [IndexPath] = []
  175. var metadatas: [tableMetadata] = []
  176. if UIDevice.current.userInterfaceIdiom == .pad { alertStyle = .alert }
  177. if !ocIds.isEmpty {
  178. let indices = dataSource.metadatas.enumerated().filter { ocIds.contains($0.element.ocId) }.map { $0.offset }
  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. self.isEditMode = false
  182. self.setSelectcancelButton()
  183. for ocId in ocIds {
  184. if let metadata = self.database.getMetadataFromOcId(ocId) {
  185. metadatas.append(metadata)
  186. }
  187. }
  188. NCNetworking.shared.deleteMetadatas(metadatas, sceneIdentifier: self.controller?.sceneIdentifier)
  189. for index in indices {
  190. let indexPath = IndexPath(row: index, section: 0)
  191. if let cell = self.collectionView.cellForItem(at: indexPath) as? NCMediaCell,
  192. self.dataSource.metadatas[index].ocId == cell.ocId {
  193. indexPaths.append(indexPath)
  194. }
  195. }
  196. self.dataSource.removeMetadata(ocIds)
  197. if indexPaths.count == ocIds.count {
  198. self.collectionView.deleteItems(at: indexPaths)
  199. } else {
  200. self.collectionViewReloadData()
  201. }
  202. })
  203. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in })
  204. present(alertController, animated: true, completion: { })
  205. }
  206. }
  207. }