NCSelect.swift 34 KB

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