NCOffline.swift 32 KB

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