NCMedia+Command.swift 11 KB

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