NCOnDevice.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // NCOnDevice.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. class NCOnDevice: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCOnDeviceListCellDelegate, NCOnDeviceGridCellDelegate, NCOnDeviceHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  25. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var path = ""
  28. var titleCurrentFolder = NSLocalizedString("_on_device_", comment: "")
  29. var datasource = [tableMetadata]()
  30. var datasourceSorted = ""
  31. var datasourceAscending = true
  32. var isEditMode = false
  33. var selectFileID = [String]()
  34. var listLayout: ListLayoutOnDevice!
  35. var gridLayout: GridLayoutOnDevice!
  36. private let highHeader: CGFloat = 50
  37. private let refreshControl = UIRefreshControl()
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. collectionView.register(UINib.init(nibName: "NCOnDeviceListCell", bundle: nil), forCellWithReuseIdentifier: "cell-list")
  41. collectionView.register(UINib.init(nibName: "NCOnDeviceGridCell", bundle: nil), forCellWithReuseIdentifier: "cell-grid")
  42. collectionView.alwaysBounceVertical = true
  43. listLayout = ListLayoutOnDevice()
  44. gridLayout = GridLayoutOnDevice()
  45. if CCUtility.getLayoutTrash() == "list" {
  46. collectionView.collectionViewLayout = listLayout
  47. } else {
  48. collectionView.collectionViewLayout = gridLayout
  49. }
  50. // Add Refresh Control
  51. if #available(iOS 10.0, *) {
  52. collectionView.refreshControl = refreshControl
  53. } else {
  54. collectionView.addSubview(refreshControl)
  55. }
  56. // Configure Refresh Control
  57. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  58. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  59. refreshControl.addTarget(self, action: #selector(loadDatasource(withSynchronized:)), for: .valueChanged)
  60. // empty Data Source
  61. self.collectionView.emptyDataSetDelegate = self;
  62. self.collectionView.emptyDataSetSource = self;
  63. }
  64. override func viewWillAppear(_ animated: Bool) {
  65. super.viewWillAppear(animated)
  66. self.navigationItem.title = titleCurrentFolder
  67. if path == "" {
  68. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  69. path = k_dav + "/trashbin/" + userID! + "/trash/"
  70. }
  71. datasourceSorted = CCUtility.getOrderSettings()
  72. datasourceAscending = CCUtility.getAscendingSettings()
  73. loadDatasource(withSynchronized: false)
  74. }
  75. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  76. super.viewWillTransition(to: size, with: coordinator)
  77. coordinator.animate(alongsideTransition: nil) { _ in
  78. self.collectionView.collectionViewLayout.invalidateLayout()
  79. }
  80. }
  81. // MARK: DZNEmpty
  82. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  83. return NCBrandColor.sharedInstance.backgroundView
  84. }
  85. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  86. return CCGraphics.changeThemingColorImage(UIImage.init(named: "filesNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  87. }
  88. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  89. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  90. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  91. return NSAttributedString.init(string: text, attributes: attributes)
  92. }
  93. /*
  94. func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  95. let text = "\n"+NSLocalizedString("_no_file_pull_down_", comment: "")
  96. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  97. return NSAttributedString.init(string: text, attributes: attributes)
  98. }
  99. */
  100. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  101. return true
  102. }
  103. // MARK: TAP EVENT
  104. func tapSwitchHeaderMenu(sender: Any) {
  105. if collectionView.collectionViewLayout == gridLayout {
  106. // list layout
  107. UIView.animate(withDuration: 0.0, animations: {
  108. self.collectionView.collectionViewLayout.invalidateLayout()
  109. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  110. self.collectionView.reloadData()
  111. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  112. })
  113. })
  114. CCUtility.setLayoutTrash("list")
  115. } else {
  116. // grid layout
  117. UIView.animate(withDuration: 0.0, animations: {
  118. self.collectionView.collectionViewLayout.invalidateLayout()
  119. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  120. self.collectionView.reloadData()
  121. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  122. })
  123. })
  124. CCUtility.setLayoutTrash("grid")
  125. }
  126. }
  127. func tapOrderHeaderMenu(sender: Any) {
  128. var menuView: DropdownMenu?
  129. var selectedRow = 0
  130. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  131. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  132. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  133. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  134. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  135. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  136. switch datasourceSorted {
  137. case "fileName":
  138. if datasourceAscending == true { item1.style = .highlight; selectedRow = 0 }
  139. if datasourceAscending == false { item2.style = .highlight; selectedRow = 1 }
  140. case "date":
  141. if datasourceAscending == false { item3.style = .highlight; selectedRow = 2 }
  142. if datasourceAscending == true { item4.style = .highlight; selectedRow = 3 }
  143. case "size":
  144. if datasourceAscending == true { item5.style = .highlight; selectedRow = 4 }
  145. if datasourceAscending == false { item6.style = .highlight; selectedRow = 5 }
  146. default:
  147. print("")
  148. }
  149. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item1, item2, item3, item4, item5, item6], selectedRow: selectedRow)
  150. menuView?.token = "tapOrderHeaderMenu"
  151. menuView?.delegate = self
  152. menuView?.rowHeight = 50
  153. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  154. menuView?.tableView.alwaysBounceVertical = false
  155. let header = (sender as? UIButton)?.superview as! NCOnDeviceHeaderMenu
  156. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  157. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  158. menuView?.topOffsetY = CGFloat(menuOffsetY)
  159. menuView?.showMenu()
  160. }
  161. func tapMoreHeaderMenu(sender: Any) {
  162. var menuView: DropdownMenu?
  163. if isEditMode {
  164. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_cancel_", comment: ""))
  165. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_selected_", comment: ""))
  166. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_selected_", comment: ""))
  167. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1, item2], selectedRow: -1)
  168. menuView?.token = "tapMoreHeaderMenuSelect"
  169. } else {
  170. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_", comment: ""))
  171. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_all_", comment: ""))
  172. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_all_", comment: ""))
  173. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1, item2], selectedRow: -1)
  174. menuView?.token = "tapMoreHeaderMenu"
  175. }
  176. menuView?.delegate = self
  177. menuView?.rowHeight = 50
  178. menuView?.tableView.alwaysBounceVertical = false
  179. let header = (sender as? UIButton)?.superview as! NCOnDeviceHeaderMenu
  180. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  181. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  182. menuView?.topOffsetY = CGFloat(menuOffsetY)
  183. menuView?.showMenu()
  184. }
  185. func tapRestoreItem(with fileID: String, sender: Any) {
  186. }
  187. func tapMoreItem(with fileID: String, sender: Any) {
  188. }
  189. func tapMoreGridItem(with fileID: String, sender: Any) {
  190. if !isEditMode {
  191. var items = [ActionSheetItem]()
  192. let appearanceDelete = ActionSheetItemAppearance.init()
  193. appearanceDelete.textColor = UIColor.red
  194. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon)))
  195. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  196. itemDelete.customAppearance = appearanceDelete
  197. items.append(itemDelete)
  198. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  199. let actionSheet = ActionSheet(items: items) { sheet, item in
  200. if item.value as? Int == 0 { }
  201. if item.value as? Int == 1 { }
  202. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  203. }
  204. let headerView = actionSheetHeader(with: fileID)
  205. actionSheet.headerView = headerView
  206. actionSheet.headerView?.frame.size.height = 50
  207. actionSheet.present(in: self, from: sender as! UIButton)
  208. } else {
  209. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  210. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  211. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  212. }
  213. }
  214. // MARK: DROP-DOWN-MENU
  215. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  216. if dropdownMenu.token == "tapOrderHeaderMenu" {
  217. switch indexPath.row {
  218. case 0: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(true)
  219. case 1: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(false)
  220. case 2: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(false)
  221. case 3: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(true)
  222. case 4: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(true)
  223. case 5: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(false)
  224. default: print("")
  225. }
  226. datasourceSorted = CCUtility.getOrderSettings()
  227. datasourceAscending = CCUtility.getAscendingSettings()
  228. loadDatasource(withSynchronized: false)
  229. }
  230. if dropdownMenu.token == "tapMoreHeaderMenu" {
  231. }
  232. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  233. }
  234. }
  235. // MARK: NC API
  236. func downloadThumbnail(with tableMetadata: tableMetadata, indexPath: IndexPath) {
  237. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  238. ocNetworking?.downloadPreviewTrash(withFileID: tableMetadata.fileID, fileName: tableMetadata.fileName, completion: { (message, errorCode) in
  239. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(tableMetadata.fileID, fileNameView: tableMetadata.fileName) {
  240. self.collectionView.reloadItems(at: [indexPath])
  241. }
  242. })
  243. }
  244. // MARK: DATASOURCE
  245. @objc func loadDatasource(withSynchronized: Bool = false) {
  246. let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND onDevice == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true)
  247. let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@", appDelegate.activeAccount), sorted: "fileNameView", ascending: true)
  248. collectionView.reloadData()
  249. }
  250. // MARK: COLLECTIONVIEW METHODS
  251. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  252. if kind == UICollectionView.elementKindSectionHeader {
  253. let onDeviceHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerMenu", for: indexPath) as! NCOnDeviceHeaderMenu
  254. if collectionView.collectionViewLayout == gridLayout {
  255. onDeviceHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  256. } else {
  257. onDeviceHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  258. }
  259. onDeviceHeader.delegate = self
  260. if self.datasource.count == 0 {
  261. onDeviceHeader.buttonSwitch.isEnabled = false
  262. onDeviceHeader.buttonOrder.isEnabled = false
  263. onDeviceHeader.buttonMore.isEnabled = false
  264. } else {
  265. onDeviceHeader.buttonSwitch.isEnabled = true
  266. onDeviceHeader.buttonOrder.isEnabled = true
  267. onDeviceHeader.buttonMore.isEnabled = true
  268. }
  269. // Order (∨∧▽△)
  270. var title = ""
  271. switch datasourceSorted {
  272. case "fileName":
  273. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  274. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  275. case "date":
  276. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  277. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  278. case "size":
  279. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  280. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  281. default:
  282. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  283. }
  284. title = title + " ▽"
  285. let size = title.size(withAttributes:[.font: onDeviceHeader.buttonOrder.titleLabel?.font as Any])
  286. onDeviceHeader.buttonOrder.setTitle(title, for: .normal)
  287. onDeviceHeader.buttonOrderWidthConstraint.constant = size.width + 5
  288. return onDeviceHeader
  289. } else {
  290. let onDeviceFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerMenu", for: indexPath) as! NCOnDeviceFooterMenu
  291. onDeviceFooter.labelFooter.textColor = NCBrandColor.sharedInstance.icon
  292. var folders: Int = 0, foldersText = ""
  293. var files: Int = 0, filesText = ""
  294. var size: Double = 0
  295. for record: tableMetadata in self.datasource {
  296. if record.directory {
  297. folders += 1
  298. } else {
  299. files += 1
  300. size = size + record.size
  301. }
  302. }
  303. if folders > 1 {
  304. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  305. } else if folders == 1 {
  306. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  307. }
  308. if files > 1 {
  309. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  310. } else if files == 1 {
  311. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  312. }
  313. if foldersText == "" {
  314. onDeviceFooter.labelFooter.text = filesText
  315. } else if filesText == "" {
  316. onDeviceFooter.labelFooter.text = foldersText
  317. } else {
  318. onDeviceFooter.labelFooter.text = foldersText + ", " + filesText
  319. }
  320. return onDeviceFooter
  321. }
  322. }
  323. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  324. return CGSize(width: collectionView.frame.width, height: highHeader)
  325. }
  326. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  327. return CGSize(width: collectionView.frame.width, height: highHeader)
  328. }
  329. func numberOfSections(in collectionView: UICollectionView) -> Int {
  330. return 1
  331. }
  332. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  333. return datasource.count
  334. }
  335. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  336. let tableMetadata = datasource[indexPath.item]
  337. var image: UIImage?
  338. if tableMetadata.iconName.count > 0 {
  339. image = UIImage.init(named: tableMetadata.iconName)
  340. } else {
  341. image = UIImage.init(named: "file")
  342. }
  343. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableMetadata.fileID, fileNameView: tableMetadata.fileName)) {
  344. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableMetadata.fileID, fileNameView: tableMetadata.fileName))
  345. } else {
  346. if tableMetadata.thumbnailExists && !CCUtility.fileProviderStorageIconExists(tableMetadata.fileID, fileNameView: tableMetadata.fileName) {
  347. downloadThumbnail(with: tableMetadata, indexPath: indexPath)
  348. }
  349. }
  350. if collectionView.collectionViewLayout == listLayout {
  351. // LIST
  352. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCOnDeviceListCell
  353. cell.delegate = self
  354. cell.fileID = tableMetadata.fileID
  355. cell.indexPath = indexPath
  356. cell.labelTitle.text = tableMetadata.fileNameView
  357. if tableMetadata.directory {
  358. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  359. cell.labelInfo.text = CCUtility.dateDiff(tableMetadata.date as Date)
  360. } else {
  361. cell.imageItem.image = image
  362. cell.labelInfo.text = CCUtility.dateDiff(tableMetadata.date as Date) + " " + CCUtility.transformedSize(tableMetadata.size)
  363. }
  364. if isEditMode {
  365. cell.imageItemLeftConstraint.constant = 45
  366. cell.imageSelect.isHidden = false
  367. if selectFileID.contains(tableMetadata.fileID) {
  368. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  369. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  370. } else {
  371. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  372. cell.backgroundView = nil
  373. }
  374. } else {
  375. cell.imageItemLeftConstraint.constant = 10
  376. cell.imageSelect.isHidden = true
  377. cell.backgroundView = nil
  378. }
  379. return cell
  380. } else {
  381. // GRID
  382. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCOnDeviceGridCell
  383. cell.delegate = self
  384. cell.fileID = tableMetadata.fileID
  385. cell.indexPath = indexPath
  386. cell.labelTitle.text = tableMetadata.fileNameView
  387. if tableMetadata.directory {
  388. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  389. } else {
  390. cell.imageItem.image = image
  391. }
  392. if isEditMode {
  393. cell.imageSelect.isHidden = false
  394. if selectFileID.contains(tableMetadata.fileID) {
  395. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  396. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  397. } else {
  398. cell.imageSelect.isHidden = true
  399. cell.backgroundView = nil
  400. }
  401. } else {
  402. cell.imageSelect.isHidden = true
  403. cell.backgroundView = nil
  404. }
  405. return cell
  406. }
  407. }
  408. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  409. let tableMetadata = datasource[indexPath.item]
  410. if isEditMode {
  411. if let index = selectFileID.index(of: tableMetadata.fileID) {
  412. selectFileID.remove(at: index)
  413. } else {
  414. selectFileID.append(tableMetadata.fileID)
  415. }
  416. collectionView.reloadItems(at: [indexPath])
  417. return
  418. }
  419. if tableMetadata.directory {
  420. let ncTrash:NCOnDevice = UIStoryboard(name: "NCOnDevice", bundle: nil).instantiateInitialViewController() as! NCOnDevice
  421. ncTrash.path = tableMetadata.fileName
  422. ncTrash.titleCurrentFolder = tableMetadata.fileNameView
  423. self.navigationController?.pushViewController(ncTrash, animated: true)
  424. }
  425. }
  426. // MARK: UTILITY
  427. private func cellBlurEffect(with frame: CGRect) -> UIView {
  428. let blurEffect = UIBlurEffect(style: .extraLight)
  429. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  430. blurEffectView.frame = frame
  431. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  432. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  433. return blurEffectView
  434. }
  435. private func actionSheetHeader(with fileID: String) -> UIView? {
  436. var image: UIImage?
  437. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  438. return nil
  439. }
  440. // Header
  441. if tableTrash.directory {
  442. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  443. } else if tableTrash.iconName.count > 0 {
  444. image = UIImage.init(named: tableTrash.iconName)
  445. } else {
  446. image = UIImage.init(named: "file")
  447. }
  448. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  449. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  450. }
  451. let headerView = UINib(nibName: "NCActionSheetHeaderView", bundle: nil).instantiate(withOwner: self, options: nil).first as! NCActionSheetHeaderView
  452. headerView.imageItem.image = image
  453. headerView.label.text = tableTrash.trashbinFileName
  454. headerView.label.textColor = NCBrandColor.sharedInstance.icon
  455. return headerView
  456. }
  457. }
  458. class ListLayoutOnDevice: UICollectionViewFlowLayout {
  459. let itemHeight: CGFloat = 60
  460. override init() {
  461. super.init()
  462. minimumInteritemSpacing = 0
  463. minimumLineSpacing = 1
  464. self.scrollDirection = .vertical
  465. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  466. }
  467. required init?(coder aDecoder: NSCoder) {
  468. fatalError("init(coder:) has not been implemented")
  469. }
  470. override var itemSize: CGSize {
  471. get {
  472. if let collectionView = collectionView {
  473. let itemWidth: CGFloat = collectionView.frame.width
  474. return CGSize(width: itemWidth, height: self.itemHeight)
  475. }
  476. // Default fallback
  477. return CGSize(width: 100, height: 100)
  478. }
  479. set {
  480. super.itemSize = newValue
  481. }
  482. }
  483. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  484. return proposedContentOffset
  485. }
  486. }
  487. class GridLayoutOnDevice: UICollectionViewFlowLayout {
  488. let heightLabelPlusButton: CGFloat = 45
  489. let preferenceWidth: CGFloat = 110
  490. let marginLeftRight: CGFloat = 5
  491. override init() {
  492. super.init()
  493. minimumInteritemSpacing = 1
  494. minimumLineSpacing = 1
  495. self.scrollDirection = .vertical
  496. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 10, right: marginLeftRight)
  497. }
  498. required init?(coder aDecoder: NSCoder) {
  499. fatalError("init(coder:) has not been implemented")
  500. }
  501. override var itemSize: CGSize {
  502. get {
  503. if let collectionView = collectionView {
  504. let numItems: Int = Int(collectionView.frame.width / preferenceWidth)
  505. let itemWidth: CGFloat = (collectionView.frame.width - (marginLeftRight * 2) - CGFloat(numItems)) / CGFloat(numItems)
  506. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  507. return CGSize(width: itemWidth, height: itemHeight)
  508. }
  509. // Default fallback
  510. return CGSize(width: 100, height: 100)
  511. }
  512. set {
  513. super.itemSize = newValue
  514. }
  515. }
  516. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  517. return proposedContentOffset
  518. }
  519. }