NCOffline.swift 33 KB

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