NCSelect.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. //
  2. // NCSelect.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/11/2018.
  6. // Copyright © 2018 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 NCCommunication
  25. @objc protocol NCSelectDelegate {
  26. @objc func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, buttonType: String, overwrite: Bool)
  27. }
  28. class NCSelect: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  29. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  30. @IBOutlet fileprivate weak var toolbar: UIView!
  31. @IBOutlet fileprivate weak var overwriteView: UIView!
  32. @IBOutlet fileprivate weak var buttonCancel: UIBarButtonItem!
  33. @IBOutlet fileprivate weak var buttonCreateFolder: UIButton!
  34. @IBOutlet fileprivate weak var buttonDone: UIButton!
  35. @IBOutlet fileprivate weak var buttonDone1: UIButton!
  36. @IBOutlet fileprivate weak var overwriteSwitch: UISwitch!
  37. @IBOutlet fileprivate weak var overwriteLabel: UILabel!
  38. // ------ external settings ------------------------------------
  39. @objc var delegate: NCSelectDelegate?
  40. @objc var hideButtonCreateFolder = false
  41. @objc var selectFile = false
  42. @objc var includeDirectoryE2EEncryption = false
  43. @objc var includeImages = false
  44. @objc var type = ""
  45. @objc var titleButtonDone = NSLocalizedString("_move_", comment: "")
  46. @objc var titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  47. @objc var isButtonDone1Hide = true
  48. @objc var isOverwriteHide = true
  49. @objc var layoutViewSelect = k_layout_view_move
  50. var titleCurrentFolder = NCBrandOptions.sharedInstance.brand
  51. var serverUrl = ""
  52. // -------------------------------------------------------------
  53. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  54. private var serverUrlPush = ""
  55. private var metadataPush: tableMetadata?
  56. private var metadataFolder = tableMetadata()
  57. private var isEditMode = false
  58. private var networkInProgress = false
  59. private var selectocId = [String]()
  60. private var overwrite = false
  61. private var sectionDatasource = CCSectionDataSourceMetadata()
  62. private var typeLayout = ""
  63. private var datasourceSorted = ""
  64. private var datasourceAscending = true
  65. private var datasourceGroupBy = ""
  66. private var datasourceDirectoryOnTop = false
  67. private var autoUploadFileName = ""
  68. private var autoUploadDirectory = ""
  69. private var listLayout: NCListLayout!
  70. private var gridLayout: NCGridLayout!
  71. private let headerMenuHeight: CGFloat = 50
  72. private let sectionHeaderHeight: CGFloat = 20
  73. private let footerHeight: CGFloat = 50
  74. private var shares: [tableShare]?
  75. private let refreshControl = UIRefreshControl()
  76. override func viewDidLoad() {
  77. super.viewDidLoad()
  78. // Cell
  79. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  80. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  81. // Header
  82. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  83. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  84. // Footer
  85. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  86. collectionView.alwaysBounceVertical = true
  87. collectionView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  88. listLayout = NCListLayout()
  89. gridLayout = NCGridLayout()
  90. // Add Refresh Control
  91. collectionView.addSubview(refreshControl)
  92. // Configure Refresh Control
  93. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  94. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  95. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  96. // empty Data Source
  97. self.collectionView.emptyDataSetDelegate = self;
  98. self.collectionView.emptyDataSetSource = self;
  99. // title button
  100. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  101. buttonCreateFolder.setTitle(NSLocalizedString("_create_folder_", comment: ""), for: .normal)
  102. overwriteLabel.text = NSLocalizedString("_overwrite_", comment: "")
  103. // button
  104. buttonCreateFolder.layer.cornerRadius = 15
  105. buttonCreateFolder.layer.masksToBounds = true
  106. buttonCreateFolder.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  107. buttonCreateFolder.setTitleColor(.black, for: .normal)
  108. buttonDone.layer.cornerRadius = 15
  109. buttonDone.layer.masksToBounds = true
  110. buttonDone.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  111. buttonDone.setTitleColor(.black, for: .normal)
  112. buttonDone1.layer.cornerRadius = 15
  113. buttonDone1.layer.masksToBounds = true
  114. buttonDone1.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  115. buttonDone1.setTitleColor(.black, for: .normal)
  116. // changeTheming
  117. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  118. changeTheming()
  119. }
  120. override func viewWillAppear(_ animated: Bool) {
  121. super.viewWillAppear(animated)
  122. self.navigationItem.title = titleCurrentFolder
  123. buttonDone.setTitle(titleButtonDone, for: .normal)
  124. buttonDone1.setTitle(titleButtonDone1, for: .normal)
  125. buttonDone1.isHidden = isButtonDone1Hide
  126. overwriteSwitch.isOn = overwrite
  127. overwriteView.isHidden = isOverwriteHide
  128. if selectFile {
  129. buttonDone.isEnabled = false
  130. buttonDone.tintColor = UIColor.clear
  131. }
  132. if hideButtonCreateFolder {
  133. buttonCreateFolder.isEnabled = false
  134. buttonCreateFolder.tintColor = UIColor.clear
  135. }
  136. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: layoutViewSelect)
  137. // get auto upload folder
  138. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  139. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(appDelegate.activeUrl)
  140. if typeLayout == k_layout_list {
  141. collectionView.collectionViewLayout = listLayout
  142. } else {
  143. collectionView.collectionViewLayout = gridLayout
  144. }
  145. loadDatasource(withLoadFolder: true)
  146. shares = NCManageDatabase.sharedInstance.getTableShares(account: appDelegate.activeAccount, serverUrl: serverUrl)
  147. }
  148. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  149. super.viewWillTransition(to: size, with: coordinator)
  150. coordinator.animate(alongsideTransition: nil) { _ in
  151. self.collectionView.collectionViewLayout.invalidateLayout()
  152. }
  153. }
  154. @objc func changeTheming() {
  155. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  156. toolbar.backgroundColor = NCBrandColor.sharedInstance.tabBar
  157. //toolbar.tintColor = .gray
  158. }
  159. // MARK: DZNEmpty
  160. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  161. return NCBrandColor.sharedInstance.backgroundView
  162. }
  163. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  164. if networkInProgress {
  165. return CCGraphics.changeThemingColorImage(UIImage.init(named: "networkInProgress"), width: 300, height: 300, color: UIColor.lightGray)
  166. } else {
  167. return CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
  168. }
  169. }
  170. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  171. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  172. if networkInProgress {
  173. return NSAttributedString.init(string: "\n"+NSLocalizedString("_request_in_progress_", comment: ""), attributes: attributes)
  174. } else if includeImages {
  175. return NSAttributedString.init(string: "\n"+NSLocalizedString("_files_no_files_", comment: ""), attributes: attributes)
  176. } else {
  177. return NSAttributedString.init(string: "\n"+NSLocalizedString("_files_no_folders_", comment: ""), attributes: attributes)
  178. }
  179. }
  180. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  181. return true
  182. }
  183. // MARK: ACTION
  184. @IBAction func actionCancel(_ sender: Any) {
  185. delegate?.dismissSelect(serverUrl: nil, metadata: nil, type: type, buttonType: "cancel", overwrite: overwrite)
  186. self.dismiss(animated: true, completion: nil)
  187. }
  188. @IBAction func actionDone(_ sender: Any) {
  189. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, buttonType: "done", overwrite: overwrite)
  190. self.dismiss(animated: true, completion: nil)
  191. }
  192. @IBAction func actionDone1(_ sender: Any) {
  193. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadataFolder, type: type, buttonType: "done1", overwrite: overwrite)
  194. self.dismiss(animated: true, completion: nil)
  195. }
  196. @IBAction func actionCreateFolder(_ sender: Any) {
  197. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message:"", preferredStyle: .alert)
  198. alertController.addTextField { (textField) in
  199. textField.autocapitalizationType = UITextAutocapitalizationType.words
  200. }
  201. let actionSave = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default) { (action:UIAlertAction) in
  202. if let fileName = alertController.textFields?.first?.text {
  203. self.createFolder(with: fileName)
  204. }
  205. }
  206. let actionCancel = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  207. print("You've pressed cancel button")
  208. }
  209. alertController.addAction(actionSave)
  210. alertController.addAction(actionCancel)
  211. self.present(alertController, animated: true, completion:nil)
  212. }
  213. @IBAction func valueChangedSwitchOverwrite(_ sender: Any) {
  214. if let viewControllers = self.navigationController?.viewControllers {
  215. for viewController in viewControllers {
  216. if viewController is NCSelect {
  217. (viewController as! NCSelect).overwrite = overwriteSwitch.isOn
  218. }
  219. }
  220. }
  221. }
  222. // MARK: TAP EVENT
  223. func tapSwitchHeader(sender: Any) {
  224. if collectionView.collectionViewLayout == gridLayout {
  225. // list layout
  226. UIView.animate(withDuration: 0.0, animations: {
  227. self.collectionView.collectionViewLayout.invalidateLayout()
  228. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  229. self.collectionView.reloadData()
  230. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  231. })
  232. })
  233. typeLayout = k_layout_list
  234. NCUtility.sharedInstance.setLayoutForView(key: layoutViewSelect, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  235. } else {
  236. // grid layout
  237. UIView.animate(withDuration: 0.0, animations: {
  238. self.collectionView.collectionViewLayout.invalidateLayout()
  239. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  240. self.collectionView.reloadData()
  241. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  242. })
  243. })
  244. typeLayout = k_layout_grid
  245. NCUtility.sharedInstance.setLayoutForView(key: layoutViewSelect, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  246. }
  247. }
  248. func tapOrderHeader(sender: Any) {
  249. var menuView: DropdownMenu?
  250. var selectedIndexPath = [IndexPath()]
  251. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  252. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  253. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  254. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  255. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  256. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  257. switch datasourceSorted {
  258. case "fileName":
  259. if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
  260. if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
  261. case "date":
  262. if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
  263. if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
  264. case "size":
  265. if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
  266. if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
  267. default:
  268. ()
  269. }
  270. let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
  271. let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
  272. let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
  273. switch datasourceGroupBy {
  274. case "alphabetic":
  275. item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
  276. case "typefile":
  277. item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
  278. case "date":
  279. item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
  280. default:
  281. ()
  282. }
  283. let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
  284. if datasourceDirectoryOnTop {
  285. item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
  286. }
  287. let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
  288. let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
  289. let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
  290. menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
  291. menuView?.token = "tapOrderHeaderMenu"
  292. menuView?.delegate = self
  293. menuView?.rowHeight = 45
  294. menuView?.sectionHeaderHeight = 8
  295. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  296. menuView?.tableView.alwaysBounceVertical = false
  297. menuView?.tableViewSeperatorColor = NCBrandColor.sharedInstance.separator
  298. menuView?.tableViewBackgroundColor = NCBrandColor.sharedInstance.backgroundForm
  299. menuView?.cellBackgroundColor = NCBrandColor.sharedInstance.backgroundForm
  300. menuView?.textColor = NCBrandColor.sharedInstance.textView
  301. let header = (sender as? UIButton)?.superview
  302. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  303. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  304. menuView?.topOffsetY = CGFloat(menuOffsetY)
  305. menuView?.showMenu()
  306. }
  307. func tapMoreHeader(sender: Any) {
  308. }
  309. func tapMoreListItem(with objectId: String, sender: Any) {
  310. }
  311. func tapMoreGridItem(with objectId: String, sender: Any) {
  312. }
  313. func tapShareListItem(with objectId: String, sender: Any) {
  314. }
  315. // MARK: DROP-DOWN-MENU
  316. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  317. if dropdownMenu.token == "tapOrderHeaderMenu" {
  318. switch indexPath.section {
  319. case 0: switch indexPath.row {
  320. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  321. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  322. case 2: datasourceSorted = "date"; datasourceAscending = false
  323. case 3: datasourceSorted = "date"; datasourceAscending = true
  324. case 4: datasourceSorted = "size"; datasourceAscending = true
  325. case 5: datasourceSorted = "size"; datasourceAscending = false
  326. default: ()
  327. }
  328. case 1: switch indexPath.row {
  329. case 0:
  330. if datasourceGroupBy == "alphabetic" {
  331. datasourceGroupBy = "none"
  332. } else {
  333. datasourceGroupBy = "alphabetic"
  334. }
  335. case 1:
  336. if datasourceGroupBy == "typefile" {
  337. datasourceGroupBy = "none"
  338. } else {
  339. datasourceGroupBy = "typefile"
  340. }
  341. case 2:
  342. if datasourceGroupBy == "date" {
  343. datasourceGroupBy = "none"
  344. } else {
  345. datasourceGroupBy = "date"
  346. }
  347. default: ()
  348. }
  349. case 2:
  350. if datasourceDirectoryOnTop {
  351. datasourceDirectoryOnTop = false
  352. } else {
  353. datasourceDirectoryOnTop = true
  354. }
  355. default: ()
  356. }
  357. NCUtility.sharedInstance.setLayoutForView(key: layoutViewSelect, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  358. loadDatasource(withLoadFolder: false)
  359. }
  360. if dropdownMenu.token == "tapMoreHeaderMenu" {
  361. }
  362. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  363. }
  364. }
  365. }
  366. // MARK: - Collection View
  367. extension NCSelect: UICollectionViewDelegate {
  368. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  369. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  370. return
  371. }
  372. if isEditMode {
  373. if let index = selectocId.firstIndex(of: metadata.ocId) {
  374. selectocId.remove(at: index)
  375. } else {
  376. selectocId.append(metadata.ocId)
  377. }
  378. collectionView.reloadItems(at: [indexPath])
  379. return
  380. }
  381. if metadata.directory {
  382. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
  383. guard let visualController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  384. self.serverUrlPush = serverUrlPush
  385. self.metadataPush = metadata
  386. visualController.delegate = delegate
  387. visualController.hideButtonCreateFolder = hideButtonCreateFolder
  388. visualController.selectFile = selectFile
  389. visualController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
  390. visualController.includeImages = includeImages
  391. visualController.type = type
  392. visualController.titleButtonDone = titleButtonDone
  393. visualController.titleButtonDone1 = titleButtonDone1
  394. visualController.layoutViewSelect = layoutViewSelect
  395. visualController.isButtonDone1Hide = isButtonDone1Hide
  396. visualController.isOverwriteHide = isOverwriteHide
  397. visualController.overwrite = overwrite
  398. visualController.titleCurrentFolder = metadataPush!.fileNameView
  399. visualController.serverUrl = serverUrlPush
  400. self.navigationController?.pushViewController(visualController, animated: true)
  401. } else {
  402. delegate?.dismissSelect(serverUrl: serverUrl, metadata: metadata, type: type, buttonType: "select", overwrite: overwrite)
  403. self.dismiss(animated: true, completion: nil)
  404. }
  405. }
  406. }
  407. extension NCSelect: UICollectionViewDataSource {
  408. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  409. if (indexPath.section == 0) {
  410. if kind == UICollectionView.elementKindSectionHeader {
  411. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  412. if collectionView.collectionViewLayout == gridLayout {
  413. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  414. } else {
  415. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  416. }
  417. header.delegate = self
  418. header.setStatusButton(count: sectionDatasource.allOcId.count)
  419. header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  420. if datasourceGroupBy == "none" {
  421. header.labelSection.isHidden = true
  422. header.labelSectionHeightConstraint.constant = 0
  423. } else {
  424. header.labelSection.isHidden = false
  425. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  426. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  427. }
  428. return header
  429. } else {
  430. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  431. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  432. return footer
  433. }
  434. } else {
  435. if kind == UICollectionView.elementKindSectionHeader {
  436. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  437. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  438. return header
  439. } else {
  440. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  441. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  442. return footer
  443. }
  444. }
  445. }
  446. func numberOfSections(in collectionView: UICollectionView) -> Int {
  447. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  448. return sections
  449. }
  450. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  451. let key = sectionDatasource.sections.object(at: section)
  452. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  453. return datasource.count
  454. }
  455. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  456. let cell: UICollectionViewCell
  457. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  458. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  459. }
  460. if typeLayout == k_layout_grid {
  461. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  462. } else {
  463. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  464. }
  465. NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: metadataFolder, serverUrl: serverUrl, isEditMode: isEditMode, selectocId: selectocId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory ,hideButtonMore: true, downloadThumbnail: true, shares: shares, source: self)
  466. if typeLayout == k_layout_grid {
  467. let cell = cell as! NCGridCell
  468. cell.buttonMore.isHidden = true
  469. return cell
  470. } else {
  471. let cell = cell as! NCListCell
  472. cell.imageMore.isHidden = true
  473. cell.sharedLeftConstraint.constant = 15
  474. return cell
  475. }
  476. }
  477. }
  478. extension NCSelect: UICollectionViewDelegateFlowLayout {
  479. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  480. if section == 0 {
  481. if datasourceGroupBy == "none" {
  482. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  483. } else {
  484. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  485. }
  486. } else {
  487. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  488. }
  489. }
  490. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  491. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  492. if (section == sections - 1) {
  493. return CGSize(width: collectionView.frame.width, height: footerHeight)
  494. } else {
  495. return CGSize(width: collectionView.frame.width, height: 0)
  496. }
  497. }
  498. }
  499. // MARK: - NC API & Algorithm
  500. extension NCSelect {
  501. @objc func loadDatasource(withLoadFolder: Bool) {
  502. sectionDatasource = CCSectionDataSourceMetadata()
  503. var predicate: NSPredicate?
  504. if serverUrl == "" {
  505. serverUrl = CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl)
  506. }
  507. if includeDirectoryE2EEncryption {
  508. if includeImages {
  509. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (directory == true OR typeFile == 'image')", appDelegate.activeAccount, serverUrl)
  510. } else {
  511. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", appDelegate.activeAccount, serverUrl)
  512. }
  513. } else {
  514. if includeImages {
  515. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND (directory == true OR typeFile == 'image')", appDelegate.activeAccount, serverUrl)
  516. } else {
  517. predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND e2eEncrypted == false AND directory == true", appDelegate.activeAccount, serverUrl)
  518. }
  519. }
  520. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: predicate!, sorted: nil, ascending: false) {
  521. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterTypeFileImage: false, filterTypeFileVideo: false, filterLivePhoto: false, sorted: datasourceSorted, ascending: datasourceAscending, activeAccount: appDelegate.activeAccount)
  522. }
  523. if withLoadFolder {
  524. loadFolder()
  525. } else {
  526. self.refreshControl.endRefreshing()
  527. }
  528. collectionView.reloadData()
  529. }
  530. func createFolder(with fileName: String) {
  531. NCNetworking.shared.createFolder(fileName: fileName, serverUrl: serverUrl, account: appDelegate.activeAccount, url: appDelegate.activeUrl) { (errorCode, errorDescription) in
  532. if errorCode == 0 {
  533. self.loadDatasource(withLoadFolder: true)
  534. } else {
  535. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  536. }
  537. }
  538. }
  539. func loadFolder() {
  540. networkInProgress = true
  541. collectionView.reloadData()
  542. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: appDelegate.activeAccount) { (account, metadataFolder, metadatas, errorCode, errorDescription) in
  543. self.networkInProgress = false
  544. self.loadDatasource(withLoadFolder: false)
  545. }
  546. }
  547. }