NCOffline.swift 33 KB

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