NCOnDevice.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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), 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()
  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.graySoft)
  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()
  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() {
  246. let directoryes = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND onDevice == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true)
  247. collectionView.reloadData()
  248. }
  249. // MARK: COLLECTIONVIEW METHODS
  250. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  251. if kind == UICollectionView.elementKindSectionHeader {
  252. let onDeviceHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerMenu", for: indexPath) as! NCOnDeviceHeaderMenu
  253. if collectionView.collectionViewLayout == gridLayout {
  254. onDeviceHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  255. } else {
  256. onDeviceHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  257. }
  258. onDeviceHeader.delegate = self
  259. if self.datasource.count == 0 {
  260. onDeviceHeader.buttonSwitch.isEnabled = false
  261. onDeviceHeader.buttonOrder.isEnabled = false
  262. onDeviceHeader.buttonMore.isEnabled = false
  263. } else {
  264. onDeviceHeader.buttonSwitch.isEnabled = true
  265. onDeviceHeader.buttonOrder.isEnabled = true
  266. onDeviceHeader.buttonMore.isEnabled = true
  267. }
  268. // Order (∨∧▽△)
  269. var title = ""
  270. switch datasourceSorted {
  271. case "fileName":
  272. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  273. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  274. case "date":
  275. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  276. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  277. case "size":
  278. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  279. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  280. default:
  281. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  282. }
  283. title = title + " ▽"
  284. let size = title.size(withAttributes:[.font: onDeviceHeader.buttonOrder.titleLabel?.font as Any])
  285. onDeviceHeader.buttonOrder.setTitle(title, for: .normal)
  286. onDeviceHeader.buttonOrderWidthConstraint.constant = size.width + 5
  287. return onDeviceHeader
  288. } else {
  289. let onDeviceFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerMenu", for: indexPath) as! NCOnDeviceFooterMenu
  290. onDeviceFooter.labelFooter.textColor = NCBrandColor.sharedInstance.icon
  291. var folders: Int = 0, foldersText = ""
  292. var files: Int = 0, filesText = ""
  293. var size: Double = 0
  294. for record: tableMetadata in self.datasource {
  295. if record.directory {
  296. folders += 1
  297. } else {
  298. files += 1
  299. size = size + record.size
  300. }
  301. }
  302. if folders > 1 {
  303. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  304. } else if folders == 1 {
  305. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  306. }
  307. if files > 1 {
  308. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  309. } else if files == 1 {
  310. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  311. }
  312. if foldersText == "" {
  313. onDeviceFooter.labelFooter.text = filesText
  314. } else if filesText == "" {
  315. onDeviceFooter.labelFooter.text = foldersText
  316. } else {
  317. onDeviceFooter.labelFooter.text = foldersText + ", " + filesText
  318. }
  319. return onDeviceFooter
  320. }
  321. }
  322. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  323. return CGSize(width: collectionView.frame.width, height: highHeader)
  324. }
  325. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  326. return CGSize(width: collectionView.frame.width, height: highHeader)
  327. }
  328. func numberOfSections(in collectionView: UICollectionView) -> Int {
  329. return 1
  330. }
  331. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  332. return datasource.count
  333. }
  334. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  335. let tableMetadata = datasource[indexPath.item]
  336. var image: UIImage?
  337. if tableMetadata.iconName.count > 0 {
  338. image = UIImage.init(named: tableMetadata.iconName)
  339. } else {
  340. image = UIImage.init(named: "file")
  341. }
  342. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableMetadata.fileID, fileNameView: tableMetadata.fileName)) {
  343. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableMetadata.fileID, fileNameView: tableMetadata.fileName))
  344. } else {
  345. if tableMetadata.thumbnailExists && !CCUtility.fileProviderStorageIconExists(tableMetadata.fileID, fileNameView: tableMetadata.fileName) {
  346. downloadThumbnail(with: tableMetadata, indexPath: indexPath)
  347. }
  348. }
  349. if collectionView.collectionViewLayout == listLayout {
  350. // LIST
  351. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCOnDeviceListCell
  352. cell.delegate = self
  353. cell.fileID = tableMetadata.fileID
  354. cell.indexPath = indexPath
  355. cell.labelTitle.text = tableMetadata.fileNameView
  356. if tableMetadata.directory {
  357. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  358. cell.labelInfo.text = CCUtility.dateDiff(tableMetadata.date as Date)
  359. } else {
  360. cell.imageItem.image = image
  361. cell.labelInfo.text = CCUtility.dateDiff(tableMetadata.date as Date) + " " + CCUtility.transformedSize(tableMetadata.size)
  362. }
  363. if isEditMode {
  364. cell.imageItemLeftConstraint.constant = 45
  365. cell.imageSelect.isHidden = false
  366. if selectFileID.contains(tableMetadata.fileID) {
  367. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  368. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  369. } else {
  370. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  371. cell.backgroundView = nil
  372. }
  373. } else {
  374. cell.imageItemLeftConstraint.constant = 10
  375. cell.imageSelect.isHidden = true
  376. cell.backgroundView = nil
  377. }
  378. return cell
  379. } else {
  380. // GRID
  381. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCOnDeviceGridCell
  382. cell.delegate = self
  383. cell.fileID = tableMetadata.fileID
  384. cell.indexPath = indexPath
  385. cell.labelTitle.text = tableMetadata.fileNameView
  386. if tableMetadata.directory {
  387. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  388. } else {
  389. cell.imageItem.image = image
  390. }
  391. if isEditMode {
  392. cell.imageSelect.isHidden = false
  393. if selectFileID.contains(tableMetadata.fileID) {
  394. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  395. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  396. } else {
  397. cell.imageSelect.isHidden = true
  398. cell.backgroundView = nil
  399. }
  400. } else {
  401. cell.imageSelect.isHidden = true
  402. cell.backgroundView = nil
  403. }
  404. return cell
  405. }
  406. }
  407. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  408. let tableMetadata = datasource[indexPath.item]
  409. if isEditMode {
  410. if let index = selectFileID.index(of: tableMetadata.fileID) {
  411. selectFileID.remove(at: index)
  412. } else {
  413. selectFileID.append(tableMetadata.fileID)
  414. }
  415. collectionView.reloadItems(at: [indexPath])
  416. return
  417. }
  418. if tableMetadata.directory {
  419. let ncTrash:NCOnDevice = UIStoryboard(name: "NCOnDevice", bundle: nil).instantiateInitialViewController() as! NCOnDevice
  420. ncTrash.path = tableMetadata.fileName
  421. ncTrash.titleCurrentFolder = tableMetadata.fileNameView
  422. self.navigationController?.pushViewController(ncTrash, animated: true)
  423. }
  424. }
  425. // MARK: UTILITY
  426. private func cellBlurEffect(with frame: CGRect) -> UIView {
  427. let blurEffect = UIBlurEffect(style: .extraLight)
  428. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  429. blurEffectView.frame = frame
  430. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  431. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  432. return blurEffectView
  433. }
  434. private func actionSheetHeader(with fileID: String) -> UIView? {
  435. var image: UIImage?
  436. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  437. return nil
  438. }
  439. // Header
  440. if tableTrash.directory {
  441. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  442. } else if tableTrash.iconName.count > 0 {
  443. image = UIImage.init(named: tableTrash.iconName)
  444. } else {
  445. image = UIImage.init(named: "file")
  446. }
  447. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  448. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  449. }
  450. let headerView = UINib(nibName: "NCActionSheetHeaderView", bundle: nil).instantiate(withOwner: self, options: nil).first as! NCActionSheetHeaderView
  451. headerView.imageItem.image = image
  452. headerView.label.text = tableTrash.trashbinFileName
  453. headerView.label.textColor = NCBrandColor.sharedInstance.icon
  454. return headerView
  455. }
  456. }
  457. class ListLayoutOnDevice: UICollectionViewFlowLayout {
  458. let itemHeight: CGFloat = 60
  459. override init() {
  460. super.init()
  461. minimumInteritemSpacing = 0
  462. minimumLineSpacing = 1
  463. self.scrollDirection = .vertical
  464. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  465. }
  466. required init?(coder aDecoder: NSCoder) {
  467. fatalError("init(coder:) has not been implemented")
  468. }
  469. override var itemSize: CGSize {
  470. get {
  471. if let collectionView = collectionView {
  472. let itemWidth: CGFloat = collectionView.frame.width
  473. return CGSize(width: itemWidth, height: self.itemHeight)
  474. }
  475. // Default fallback
  476. return CGSize(width: 100, height: 100)
  477. }
  478. set {
  479. super.itemSize = newValue
  480. }
  481. }
  482. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  483. return proposedContentOffset
  484. }
  485. }
  486. class GridLayoutOnDevice: UICollectionViewFlowLayout {
  487. let heightLabelPlusButton: CGFloat = 45
  488. let preferenceWidth: CGFloat = 110
  489. let marginLeftRight: CGFloat = 5
  490. override init() {
  491. super.init()
  492. minimumInteritemSpacing = 1
  493. minimumLineSpacing = 1
  494. self.scrollDirection = .vertical
  495. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 10, right: marginLeftRight)
  496. }
  497. required init?(coder aDecoder: NSCoder) {
  498. fatalError("init(coder:) has not been implemented")
  499. }
  500. override var itemSize: CGSize {
  501. get {
  502. if let collectionView = collectionView {
  503. let numItems: Int = Int(collectionView.frame.width / preferenceWidth)
  504. let itemWidth: CGFloat = (collectionView.frame.width - (marginLeftRight * 2) - CGFloat(numItems)) / CGFloat(numItems)
  505. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  506. return CGSize(width: itemWidth, height: itemHeight)
  507. }
  508. // Default fallback
  509. return CGSize(width: 100, height: 100)
  510. }
  511. set {
  512. super.itemSize = newValue
  513. }
  514. }
  515. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  516. return proposedContentOffset
  517. }
  518. }