NCOffline.swift 31 KB

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