NCOffline.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  25. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  26. var titleCurrentFolder = NSLocalizedString("_manage_file_offline_", comment: "")
  27. var directoryID = ""
  28. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. private var isEditMode = false
  30. private var selectFileID = [String]()
  31. private var sectionDatasource = CCSectionDataSourceMetadata()
  32. private var typeLayout = ""
  33. private var datasourceSorted = ""
  34. private var datasourceAscending = true
  35. private var datasourceGroupBy = ""
  36. private var datasourceDirectoryOnTop = false
  37. private var listLayout: NCListLayout!
  38. private var gridLayout: NCGridLayout!
  39. private var actionSheet: ActionSheet?
  40. private let headerMenuHeight: CGFloat = 50
  41. private let sectionHeaderHeight: CGFloat = 20
  42. private let footerHeight: CGFloat = 50
  43. private let refreshControl = UIRefreshControl()
  44. private var metadataSelect = tableMetadata()
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. // Cell
  48. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  49. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  50. // Header
  51. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  52. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  53. // Footer
  54. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  55. collectionView.alwaysBounceVertical = true
  56. listLayout = NCListLayout()
  57. gridLayout = NCGridLayout()
  58. // Add Refresh Control
  59. if #available(iOS 10.0, *) {
  60. collectionView.refreshControl = refreshControl
  61. } else {
  62. collectionView.addSubview(refreshControl)
  63. }
  64. // Configure Refresh Control
  65. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  66. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  67. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  68. // empty Data Source
  69. self.collectionView.emptyDataSetDelegate = self;
  70. self.collectionView.emptyDataSetSource = self;
  71. }
  72. override func viewWillAppear(_ animated: Bool) {
  73. super.viewWillAppear(animated)
  74. // Color
  75. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  76. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  77. self.navigationItem.title = titleCurrentFolder
  78. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_offline)
  79. if typeLayout == "list" {
  80. collectionView.collectionViewLayout = listLayout
  81. } else {
  82. collectionView.collectionViewLayout = gridLayout
  83. }
  84. loadDatasource()
  85. }
  86. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  87. super.viewWillTransition(to: size, with: coordinator)
  88. coordinator.animate(alongsideTransition: nil) { _ in
  89. self.collectionView.collectionViewLayout.invalidateLayout()
  90. self.actionSheet?.viewDidLayoutSubviews()
  91. }
  92. }
  93. // MARK: DZNEmpty
  94. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  95. return NCBrandColor.sharedInstance.backgroundView
  96. }
  97. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  98. return CCGraphics.changeThemingColorImage(UIImage.init(named: "filesNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  99. }
  100. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  101. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  102. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  103. return NSAttributedString.init(string: text, attributes: attributes)
  104. }
  105. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  106. return true
  107. }
  108. // MARK: TAP EVENT
  109. func tapSwitchHeader(sender: Any) {
  110. if collectionView.collectionViewLayout == gridLayout {
  111. // list layout
  112. UIView.animate(withDuration: 0.0, animations: {
  113. self.collectionView.collectionViewLayout.invalidateLayout()
  114. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  115. self.collectionView.reloadData()
  116. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  117. })
  118. })
  119. typeLayout = "list"
  120. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  121. } else {
  122. // grid layout
  123. UIView.animate(withDuration: 0.0, animations: {
  124. self.collectionView.collectionViewLayout.invalidateLayout()
  125. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  126. self.collectionView.reloadData()
  127. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  128. })
  129. })
  130. typeLayout = "grid"
  131. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  132. }
  133. }
  134. func tapOrderHeader(sender: Any) {
  135. var menuView: DropdownMenu?
  136. var selectedIndexPath = [IndexPath()]
  137. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  138. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  139. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  140. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  141. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  142. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  143. switch datasourceSorted {
  144. case "fileName":
  145. if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
  146. if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
  147. case "date":
  148. if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
  149. if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
  150. case "size":
  151. if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
  152. if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
  153. default:
  154. ()
  155. }
  156. let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
  157. let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
  158. let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
  159. switch datasourceGroupBy {
  160. case "alphabetic":
  161. item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
  162. case "typefile":
  163. item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
  164. case "date":
  165. item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
  166. default:
  167. ()
  168. }
  169. let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
  170. if datasourceDirectoryOnTop {
  171. item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
  172. }
  173. let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
  174. let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
  175. let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
  176. menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
  177. menuView?.token = "tapOrderHeaderMenu"
  178. menuView?.delegate = self
  179. menuView?.rowHeight = 45
  180. menuView?.sectionHeaderHeight = 8
  181. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  182. menuView?.tableView.alwaysBounceVertical = false
  183. menuView?.tableViewBackgroundColor = UIColor.white
  184. let header = (sender as? UIButton)?.superview
  185. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  186. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  187. menuView?.topOffsetY = CGFloat(menuOffsetY)
  188. menuView?.showMenu()
  189. }
  190. func tapMoreHeader(sender: Any) {
  191. }
  192. func tapMoreListItem(with fileID: String, sender: Any) {
  193. tapMoreGridItem(with: fileID, sender: sender)
  194. }
  195. func tapMoreGridItem(with fileID: String, sender: Any) {
  196. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
  197. return
  198. }
  199. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  200. return
  201. }
  202. if !isEditMode {
  203. var items = [ActionSheetItem]()
  204. let appearanceDelete = ActionSheetItemAppearance.init()
  205. appearanceDelete.textColor = UIColor.red
  206. if (metadata.directory == false || serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl)) {
  207. items.append(ActionSheetItem(title: NSLocalizedString("_remove_available_offline_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "offline"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  208. }
  209. items.append(ActionSheetItem(title: NSLocalizedString("_share_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  210. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 2, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  211. itemDelete.customAppearance = appearanceDelete
  212. items.append(itemDelete)
  213. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  214. actionSheet = ActionSheet(items: items) { sheet, item in
  215. if item.value as? Int == 0 {
  216. if metadata.directory {
  217. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!, offline: false)
  218. } else {
  219. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, offline: false)
  220. }
  221. self.loadDatasource()
  222. }
  223. if item.value as? Int == 1 { self.appDelegate.activeMain.openWindowShare(metadata) }
  224. if item.value as? Int == 2 { self.deleteItem(with: metadata, sender: sender) }
  225. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  226. }
  227. let headerView = actionSheetHeader(with: metadata)
  228. actionSheet?.headerView = headerView
  229. actionSheet?.headerView?.frame.size.height = 50
  230. actionSheet?.present(in: self, from: sender as! UIButton)
  231. } else {
  232. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  233. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  234. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  235. }
  236. }
  237. // MARK: DROP-DOWN-MENU
  238. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  239. if dropdownMenu.token == "tapOrderHeaderMenu" {
  240. switch indexPath.section {
  241. case 0: switch indexPath.row {
  242. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  243. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  244. case 2: datasourceSorted = "date"; datasourceAscending = false
  245. case 3: datasourceSorted = "date"; datasourceAscending = true
  246. case 4: datasourceSorted = "size"; datasourceAscending = true
  247. case 5: datasourceSorted = "size"; datasourceAscending = false
  248. default: ()
  249. }
  250. case 1: switch indexPath.row {
  251. case 0:
  252. if datasourceGroupBy == "alphabetic" {
  253. datasourceGroupBy = "none"
  254. } else {
  255. datasourceGroupBy = "alphabetic"
  256. }
  257. case 1:
  258. if datasourceGroupBy == "typefile" {
  259. datasourceGroupBy = "none"
  260. } else {
  261. datasourceGroupBy = "typefile"
  262. }
  263. case 2:
  264. if datasourceGroupBy == "date" {
  265. datasourceGroupBy = "none"
  266. } else {
  267. datasourceGroupBy = "date"
  268. }
  269. default: ()
  270. }
  271. case 2:
  272. if datasourceDirectoryOnTop {
  273. datasourceDirectoryOnTop = false
  274. } else {
  275. datasourceDirectoryOnTop = true
  276. }
  277. default: ()
  278. }
  279. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  280. loadDatasource()
  281. }
  282. if dropdownMenu.token == "tapMoreHeaderMenu" {
  283. }
  284. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  285. }
  286. }
  287. // MARK: NC API
  288. func downloadThumbnail(with tableMetadata: tableMetadata, indexPath: IndexPath) {
  289. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  290. ocNetworking?.downloadPreviewTrash(withFileID: tableMetadata.fileID, fileName: tableMetadata.fileName, completion: { (message, errorCode) in
  291. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(tableMetadata.fileID, fileNameView: tableMetadata.fileName) {
  292. self.collectionView.reloadItems(at: [indexPath])
  293. }
  294. })
  295. }
  296. func deleteItem(with metadata: tableMetadata, sender: Any) {
  297. var items = [ActionSheetItem]()
  298. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  299. return
  300. }
  301. guard let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == serverUrl", appDelegate.activeAccount, serverUrl)) else {
  302. return
  303. }
  304. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  305. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  306. actionSheet = ActionSheet(items: items) { sheet, item in
  307. if item is ActionSheetDangerButton {
  308. NCMainCommon.sharedInstance.deleteFile(metadatas: [metadata], e2ee: tableDirectory.e2eEncrypted, serverUrl: serverUrl, folderFileID: tableDirectory.fileID) { (errorCode, message) in
  309. self.loadDatasource()
  310. }
  311. }
  312. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  313. }
  314. let headerView = actionSheetHeader(with: metadata)
  315. actionSheet?.headerView = headerView
  316. actionSheet?.headerView?.frame.size.height = 50
  317. actionSheet?.present(in: self, from: sender as! UIButton)
  318. }
  319. // MARK: DATASOURCE
  320. @objc func loadDatasource() {
  321. var fileIDs = [String]()
  322. sectionDatasource = CCSectionDataSourceMetadata()
  323. if directoryID == "" {
  324. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true) {
  325. for directory: tableDirectory in directories {
  326. fileIDs.append(directory.fileID)
  327. }
  328. }
  329. if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true) {
  330. for file: tableLocalFile in files {
  331. fileIDs.append(file.fileID)
  332. }
  333. }
  334. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND fileID IN %@", appDelegate.activeAccount, fileIDs), sorted: datasourceSorted, ascending: datasourceAscending) {
  335. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  336. }
  337. } else {
  338. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND directoryID == %@", appDelegate.activeAccount, directoryID), sorted: datasourceSorted, ascending: datasourceAscending) {
  339. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  340. }
  341. }
  342. self.refreshControl.endRefreshing()
  343. collectionView.reloadData()
  344. }
  345. // MARK: COLLECTIONVIEW METHODS
  346. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  347. if (indexPath.section == 0) {
  348. if kind == UICollectionView.elementKindSectionHeader {
  349. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  350. if collectionView.collectionViewLayout == gridLayout {
  351. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  352. } else {
  353. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  354. }
  355. header.delegate = self
  356. header.setStatusButton(count: sectionDatasource.allFileID.count)
  357. header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  358. if datasourceGroupBy == "none" {
  359. header.labelSection.isHidden = true
  360. header.labelSectionHeightConstraint.constant = 0
  361. } else {
  362. header.labelSection.isHidden = false
  363. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  364. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  365. }
  366. return header
  367. } else {
  368. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  369. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  370. return footer
  371. }
  372. } else {
  373. if kind == UICollectionView.elementKindSectionHeader {
  374. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  375. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  376. return header
  377. } else {
  378. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  379. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  380. return footer
  381. }
  382. }
  383. }
  384. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  385. if section == 0 {
  386. if datasourceGroupBy == "none" {
  387. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  388. } else {
  389. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  390. }
  391. } else {
  392. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  393. }
  394. }
  395. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  396. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  397. if (section == sections - 1) {
  398. return CGSize(width: collectionView.frame.width, height: footerHeight)
  399. } else {
  400. return CGSize(width: collectionView.frame.width, height: 0)
  401. }
  402. }
  403. func numberOfSections(in collectionView: UICollectionView) -> Int {
  404. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  405. return sections
  406. }
  407. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  408. let key = sectionDatasource.sections.object(at: section)
  409. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  410. return datasource.count
  411. }
  412. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  413. var image: UIImage?
  414. var imagePreview = false
  415. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  416. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  417. }
  418. if metadata.iconName.count > 0 {
  419. image = UIImage.init(named: metadata.iconName)
  420. } else {
  421. image = UIImage.init(named: "file")
  422. }
  423. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName)) {
  424. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName))
  425. imagePreview = true
  426. } else {
  427. if metadata.hasPreview == 1 && !CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) {
  428. downloadThumbnail(with: metadata, indexPath: indexPath)
  429. }
  430. }
  431. if collectionView.collectionViewLayout == listLayout {
  432. // LIST
  433. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  434. cell.delegate = self
  435. cell.fileID = metadata.fileID
  436. cell.indexPath = indexPath
  437. cell.labelTitle.text = metadata.fileNameView
  438. if metadata.directory {
  439. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  440. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  441. } else {
  442. cell.imageItem.image = image
  443. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " " + CCUtility.transformedSize(metadata.size)
  444. }
  445. if isEditMode {
  446. cell.imageItemLeftConstraint.constant = 45
  447. cell.imageSelect.isHidden = false
  448. if selectFileID.contains(metadata.fileID) {
  449. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  450. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  451. } else {
  452. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  453. cell.backgroundView = nil
  454. }
  455. } else {
  456. cell.imageItemLeftConstraint.constant = 10
  457. cell.imageSelect.isHidden = true
  458. cell.backgroundView = nil
  459. }
  460. // Remove last separator
  461. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  462. cell.separator.isHidden = true
  463. } else {
  464. cell.separator.isHidden = false
  465. }
  466. return cell
  467. } else {
  468. // GRID
  469. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  470. cell.delegate = self
  471. cell.fileID = metadata.fileID
  472. cell.indexPath = indexPath
  473. cell.labelTitle.text = metadata.fileNameView
  474. if metadata.directory {
  475. image = UIImage.init(named: "folder")
  476. cell.imageItem.image = CCGraphics.changeThemingColorImage(image, width: image!.size.width*6, height: image!.size.height*6, scale: 3.0, color: NCBrandColor.sharedInstance.brandElement)
  477. cell.imageItem.contentMode = .center
  478. } else {
  479. cell.imageItem.image = image
  480. if imagePreview == false {
  481. let width = cell.imageItem.image!.size.width * 2
  482. //let scale = UIScreen.main.scale
  483. cell.imageItem.image = NCUtility.sharedInstance.resizeImage(image: image!, newWidth: width)
  484. cell.imageItem.contentMode = .center
  485. }
  486. }
  487. if isEditMode {
  488. cell.imageSelect.isHidden = false
  489. if selectFileID.contains(metadata.fileID) {
  490. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  491. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  492. } else {
  493. cell.imageSelect.isHidden = true
  494. cell.backgroundView = nil
  495. }
  496. } else {
  497. cell.imageSelect.isHidden = true
  498. cell.backgroundView = nil
  499. }
  500. return cell
  501. }
  502. }
  503. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  504. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  505. return
  506. }
  507. if isEditMode {
  508. if let index = selectFileID.index(of: metadata.fileID) {
  509. selectFileID.remove(at: index)
  510. } else {
  511. selectFileID.append(metadata.fileID)
  512. }
  513. collectionView.reloadItems(at: [indexPath])
  514. return
  515. }
  516. if metadata.directory {
  517. let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  518. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  519. return
  520. }
  521. let serverUrlPush = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)
  522. guard let directoryIDPush = NCManageDatabase.sharedInstance.getDirectoryID(serverUrlPush) else {
  523. return
  524. }
  525. ncOffline.directoryID = directoryIDPush
  526. ncOffline.titleCurrentFolder = metadata.fileNameView
  527. self.navigationController?.pushViewController(ncOffline, animated: true)
  528. } else {
  529. metadataSelect = metadata
  530. performSegue(withIdentifier: "segueDetail", sender: self)
  531. }
  532. }
  533. // MARK: SEGUE
  534. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  535. let photoDataSource: NSMutableArray = []
  536. for fileID: String in sectionDatasource.allFileID as! [String] {
  537. let metadata = sectionDatasource.allRecordsDataSource.object(forKey: fileID) as! tableMetadata
  538. if metadata.typeFile == k_metadataTypeFile_image {
  539. photoDataSource.add(metadata)
  540. }
  541. }
  542. if let segueNavigationController = segue.destination as? UINavigationController {
  543. if let segueViewController = segueNavigationController.topViewController as? CCDetail {
  544. segueViewController.metadataDetail = metadataSelect
  545. segueViewController.dateFilterQuery = nil
  546. segueViewController.photoDataSource = photoDataSource
  547. segueViewController.title = metadataSelect.fileNameView
  548. }
  549. }
  550. }
  551. // MARK: UTILITY
  552. private func cellBlurEffect(with frame: CGRect) -> UIView {
  553. let blurEffect = UIBlurEffect(style: .extraLight)
  554. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  555. blurEffectView.frame = frame
  556. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  557. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  558. return blurEffectView
  559. }
  560. private func actionSheetHeader(with metadata: tableMetadata) -> UIView? {
  561. var image: UIImage?
  562. // Header
  563. if metadata.directory {
  564. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  565. } else if metadata.iconName.count > 0 {
  566. image = UIImage.init(named: metadata.iconName)
  567. } else {
  568. image = UIImage.init(named: "file")
  569. }
  570. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView)) {
  571. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  572. }
  573. let headerView = UINib(nibName: "NCActionSheetHeaderView", bundle: nil).instantiate(withOwner: self, options: nil).first as! NCActionSheetHeaderView
  574. headerView.imageItem.image = image
  575. headerView.label.text = metadata.fileNameView
  576. headerView.label.textColor = NCBrandColor.sharedInstance.icon
  577. return headerView
  578. }
  579. }