NCShareExtension.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. //
  2. // NCShareExtension.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 20/04/2021.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. import NCCommunication
  25. class NCShareExtension: UIViewController, UIGestureRecognizerDelegate, UIAdaptivePresentationControllerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, NCEmptyDataSetDelegate {
  26. @IBOutlet weak var collectionView: UICollectionView!
  27. @IBOutlet weak var tableView: UITableView!
  28. @IBOutlet fileprivate weak var buttonCancel: UIBarButtonItem!
  29. // -------------------------------------------------------------
  30. var titleCurrentFolder = NCBrandOptions.shared.brand
  31. var serverUrl = ""
  32. // -------------------------------------------------------------
  33. private var emptyDataSet: NCEmptyDataSet?
  34. private let keyLayout = NCGlobal.shared.layoutViewMove
  35. private var serverUrlPush = ""
  36. private var metadataTouch: tableMetadata?
  37. private var metadataFolder = tableMetadata()
  38. private var networkInProgress = false
  39. private var dataSource = NCDataSource()
  40. internal var richWorkspaceText: String?
  41. private var sort: String = ""
  42. private var ascending: Bool = true
  43. private var directoryOnTop: Bool = true
  44. private var layout = ""
  45. private var groupBy = ""
  46. private var titleButton = ""
  47. private var itemForLine = 0
  48. private var autoUploadFileName = ""
  49. private var autoUploadDirectory = ""
  50. private var listLayout: NCListLayout!
  51. private var gridLayout: NCGridLayout!
  52. private let headerHeight: CGFloat = 50
  53. private var headerRichWorkspaceHeight: CGFloat = 0
  54. private let footerHeight: CGFloat = 100
  55. private var shares: [tableShare]?
  56. private let refreshControl = UIRefreshControl()
  57. private var activeAccount: tableAccount!
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. self.navigationController?.navigationBar.prefersLargeTitles = true
  61. self.navigationController?.presentationController?.delegate = self
  62. activeAccount = NCManageDatabase.shared.getAccountActive()
  63. // Cell
  64. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  65. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  66. // Header
  67. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  68. // Footer
  69. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  70. collectionView.alwaysBounceVertical = true
  71. listLayout = NCListLayout()
  72. gridLayout = NCGridLayout()
  73. // Add Refresh Control
  74. collectionView.addSubview(refreshControl)
  75. refreshControl.tintColor = NCBrandColor.shared.brandText
  76. refreshControl.backgroundColor = NCBrandColor.shared.brandElement
  77. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  78. // Empty
  79. emptyDataSet = NCEmptyDataSet.init(view: collectionView, offset: 0, delegate: self)
  80. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  81. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  82. changeTheming()
  83. }
  84. override func viewWillAppear(_ animated: Bool) {
  85. super.viewWillAppear(animated)
  86. self.navigationItem.title = titleCurrentFolder
  87. // set the serverUrl
  88. if serverUrl == "" {
  89. serverUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: activeAccount.urlBase, account: activeAccount.account)
  90. }
  91. // get auto upload folder
  92. autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
  93. autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: activeAccount.urlBase, account: activeAccount.account)
  94. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: keyLayout,serverUrl: serverUrl)
  95. gridLayout.itemForLine = CGFloat(itemForLine)
  96. if layout == NCGlobal.shared.layoutList {
  97. collectionView.collectionViewLayout = listLayout
  98. } else {
  99. collectionView.collectionViewLayout = gridLayout
  100. }
  101. loadDatasource(withLoadFolder: true)
  102. shares = NCManageDatabase.shared.getTableShares(account: activeAccount.account, serverUrl: serverUrl)
  103. }
  104. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  105. super.viewWillTransition(to: size, with: coordinator)
  106. coordinator.animate(alongsideTransition: nil) { _ in
  107. self.collectionView?.collectionViewLayout.invalidateLayout()
  108. }
  109. }
  110. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  111. super.traitCollectionDidChange(previousTraitCollection)
  112. if traitCollection.userInterfaceStyle == .dark {
  113. } else {
  114. }
  115. }
  116. @objc func changeTheming() {
  117. view.backgroundColor = NCBrandColor.shared.backgroundView
  118. collectionView.backgroundColor = NCBrandColor.shared.backgroundView
  119. collectionView.reloadData()
  120. refreshControl.backgroundColor = NCBrandColor.shared.backgroundView
  121. }
  122. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  123. // Dismission
  124. }
  125. // MARK: - Empty
  126. func emptyDataSetView(_ view: NCEmptyView) {
  127. if networkInProgress {
  128. view.emptyImage.image = UIImage.init(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
  129. view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  130. view.emptyDescription.text = ""
  131. } else {
  132. view.emptyImage.image = UIImage.init(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  133. view.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  134. view.emptyDescription.text = ""
  135. }
  136. }
  137. // MARK: ACTION
  138. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  139. extensionContext?.completeRequest(returningItems: extensionContext?.inputItems, completionHandler: nil)
  140. }
  141. func createFolderButtonPressed(_ sender: UIButton) {
  142. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_", comment: ""), message:"", preferredStyle: .alert)
  143. alertController.addTextField { (textField) in
  144. textField.autocapitalizationType = UITextAutocapitalizationType.words
  145. }
  146. let actionSave = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default) { (action:UIAlertAction) in
  147. if let fileName = alertController.textFields?.first?.text {
  148. self.createFolder(with: fileName)
  149. }
  150. }
  151. let actionCancel = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  152. print("You've pressed cancel button")
  153. }
  154. alertController.addAction(actionSave)
  155. alertController.addAction(actionCancel)
  156. self.present(alertController, animated: true, completion:nil)
  157. }
  158. // MARK: TAP EVENT
  159. func tapSwitchHeader(sender: Any) {
  160. if collectionView.collectionViewLayout == gridLayout {
  161. // list layout
  162. UIView.animate(withDuration: 0.0, animations: {
  163. self.collectionView.collectionViewLayout.invalidateLayout()
  164. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  165. self.collectionView.reloadData()
  166. })
  167. })
  168. layout = NCGlobal.shared.layoutList
  169. } else {
  170. // grid layout
  171. UIView.animate(withDuration: 0.0, animations: {
  172. self.collectionView.collectionViewLayout.invalidateLayout()
  173. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  174. self.collectionView.reloadData()
  175. })
  176. })
  177. layout = NCGlobal.shared.layoutGrid
  178. }
  179. }
  180. func tapOrderHeader(sender: Any) {
  181. let sortMenu = NCSortMenu()
  182. sortMenu.toggleMenu(viewController: self, key: keyLayout, sortButton: sender as? UIButton, serverUrl: serverUrl)
  183. }
  184. func tapMoreHeader(sender: Any) {
  185. }
  186. func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {
  187. }
  188. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {
  189. }
  190. func tapShareListItem(with objectId: String, sender: Any) {
  191. }
  192. func tapRichWorkspace(sender: Any) {
  193. }
  194. func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  195. }
  196. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  197. }
  198. func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  199. }
  200. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  201. }
  202. }
  203. // MARK: - Collection View
  204. extension NCShareExtension: UICollectionViewDelegate {
  205. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  206. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  207. if metadata.directory {
  208. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
  209. guard let visualController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
  210. self.serverUrlPush = serverUrlPush
  211. self.metadataTouch = metadata
  212. visualController.titleCurrentFolder = metadataTouch!.fileNameView
  213. visualController.serverUrl = serverUrlPush
  214. self.navigationController?.pushViewController(visualController, animated: true)
  215. }
  216. }
  217. }
  218. extension NCShareExtension: UICollectionViewDataSource {
  219. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  220. if kind == UICollectionView.elementKindSectionHeader {
  221. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  222. if collectionView.collectionViewLayout == gridLayout {
  223. header.buttonSwitch.setImage(UIImage.init(named: "switchList")?.image(color: NCBrandColor.shared.icon, size: 25), for: .normal)
  224. } else {
  225. header.buttonSwitch.setImage(UIImage.init(named: "switchGrid")?.image(color: NCBrandColor.shared.icon, size: 25), for: .normal)
  226. }
  227. header.delegate = self
  228. header.setStatusButton(count: dataSource.metadatas.count)
  229. header.setTitleSorted(datasourceTitleButton: titleButton)
  230. header.viewRichWorkspaceHeightConstraint.constant = headerRichWorkspaceHeight
  231. header.setRichWorkspaceText(richWorkspaceText: richWorkspaceText)
  232. return header
  233. } else {
  234. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  235. let info = dataSource.getFilesInformation()
  236. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
  237. return footer
  238. }
  239. }
  240. func numberOfSections(in collectionView: UICollectionView) -> Int {
  241. return 1
  242. }
  243. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  244. let numberOfItems = dataSource.numberOfItems()
  245. emptyDataSet?.numberOfItemsInSection(numberOfItems, section:section)
  246. return numberOfItems
  247. }
  248. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  249. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  250. if layout == NCGlobal.shared.layoutList {
  251. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  252. } else {
  253. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  254. }
  255. }
  256. var tableShare: tableShare?
  257. var isShare = false
  258. var isMounted = false
  259. // Download preview
  260. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: activeAccount.urlBase, view: collectionView, indexPath: indexPath)
  261. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  262. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  263. if dataSource.metadataShare[metadata.ocId] != nil {
  264. tableShare = dataSource.metadataShare[metadata.ocId]
  265. }
  266. // LAYOUT LIST
  267. if layout == NCGlobal.shared.layoutList {
  268. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  269. cell.delegate = self
  270. cell.objectId = metadata.ocId
  271. cell.indexPath = indexPath
  272. cell.labelTitle.text = metadata.fileNameView
  273. cell.labelTitle.textColor = NCBrandColor.shared.textView
  274. cell.separator.backgroundColor = NCBrandColor.shared.separator
  275. cell.imageSelect.image = nil
  276. cell.imageStatus.image = nil
  277. cell.imageLocal.image = nil
  278. cell.imageFavorite.image = nil
  279. cell.imageShared.image = nil
  280. cell.imageMore.image = nil
  281. cell.imageItem.image = nil
  282. cell.imageItem.backgroundColor = nil
  283. cell.progressView.progress = 0.0
  284. if metadata.directory {
  285. if metadata.e2eEncrypted {
  286. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  287. } else if isShare {
  288. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  289. } else if (tableShare != nil && tableShare?.shareType != 3) {
  290. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  291. } else if (tableShare != nil && tableShare?.shareType == 3) {
  292. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  293. } else if metadata.mountType == "group" {
  294. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  295. } else if isMounted {
  296. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  297. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  298. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  299. } else {
  300. cell.imageItem.image = NCBrandColor.cacheImages.folder
  301. }
  302. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  303. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  304. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, lockServerUrl))
  305. // Local image: offline
  306. if tableDirectory != nil && tableDirectory!.offline {
  307. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  308. }
  309. } else {
  310. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  311. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  312. } else {
  313. if metadata.hasPreview {
  314. cell.imageItem.backgroundColor = .lightGray
  315. } else {
  316. if metadata.iconName.count > 0 {
  317. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  318. } else {
  319. cell.imageItem.image = NCBrandColor.cacheImages.file
  320. }
  321. }
  322. }
  323. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  324. // image local
  325. if dataSource.metadataOffLine.contains(metadata.ocId) {
  326. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  327. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  328. cell.imageLocal.image = NCBrandColor.cacheImages.local
  329. }
  330. }
  331. // image Favorite
  332. if metadata.favorite {
  333. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  334. }
  335. // Share image
  336. if (isShare) {
  337. cell.imageShared.image = NCBrandColor.cacheImages.shared
  338. } else if (tableShare != nil && tableShare?.shareType == 3) {
  339. cell.imageShared.image = NCBrandColor.cacheImages.shareByLink
  340. } else if (tableShare != nil && tableShare?.shareType != 3) {
  341. cell.imageShared.image = NCBrandColor.cacheImages.shared
  342. } else {
  343. cell.imageShared.image = NCBrandColor.cacheImages.canShare
  344. }
  345. if metadata.ownerId.count > 0 && metadata.ownerId != activeAccount.userId {
  346. let fileNameUser = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(activeAccount.user, urlBase: activeAccount.urlBase)) + "-" + metadata.ownerId + ".png"
  347. if FileManager.default.fileExists(atPath: fileNameUser) {
  348. cell.imageShared.image = UIImage(contentsOfFile: fileNameUser)
  349. } else {
  350. NCCommunication.shared.downloadAvatar(userId: metadata.ownerId, fileNameLocalPath: fileNameUser, size: NCGlobal.shared.avatarSize) { (account, data, errorCode, errorMessage) in
  351. if errorCode == 0 && account == self.activeAccount.account {
  352. cell.imageShared.image = UIImage(contentsOfFile: fileNameUser)
  353. }
  354. }
  355. }
  356. }
  357. cell.imageSelect.isHidden = true
  358. cell.backgroundView = nil
  359. cell.hideButtonMore(true)
  360. cell.hideButtonShare(true)
  361. cell.selectMode(false)
  362. // Live Photo
  363. if metadata.livePhoto {
  364. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  365. }
  366. // Remove last separator
  367. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  368. cell.separator.isHidden = true
  369. } else {
  370. cell.separator.isHidden = false
  371. }
  372. return cell
  373. }
  374. // LAYOUT GRID
  375. if layout == NCGlobal.shared.layoutGrid {
  376. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  377. cell.delegate = self
  378. cell.objectId = metadata.ocId
  379. cell.indexPath = indexPath
  380. cell.labelTitle.text = metadata.fileNameView
  381. cell.labelTitle.textColor = NCBrandColor.shared.textView
  382. cell.imageSelect.image = nil
  383. cell.imageStatus.image = nil
  384. cell.imageLocal.image = nil
  385. cell.imageFavorite.image = nil
  386. cell.imageItem.image = nil
  387. cell.imageItem.backgroundColor = nil
  388. cell.progressView.progress = 0.0
  389. if metadata.directory {
  390. if metadata.e2eEncrypted {
  391. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  392. } else if isShare {
  393. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  394. } else if (tableShare != nil && tableShare!.shareType != 3) {
  395. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  396. } else if (tableShare != nil && tableShare!.shareType == 3) {
  397. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  398. } else if metadata.mountType == "group" {
  399. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  400. } else if isMounted {
  401. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  402. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  403. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  404. } else {
  405. cell.imageItem.image = NCBrandColor.cacheImages.folder
  406. }
  407. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  408. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, lockServerUrl))
  409. // Local image: offline
  410. if tableDirectory != nil && tableDirectory!.offline {
  411. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  412. }
  413. } else {
  414. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  415. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  416. } else {
  417. if metadata.hasPreview {
  418. cell.imageItem.backgroundColor = .lightGray
  419. } else {
  420. if metadata.iconName.count > 0 {
  421. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  422. } else {
  423. cell.imageItem.image = NCBrandColor.cacheImages.file
  424. }
  425. }
  426. }
  427. // image Local
  428. if dataSource.metadataOffLine.contains(metadata.ocId) {
  429. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  430. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  431. cell.imageLocal.image = NCBrandColor.cacheImages.local
  432. }
  433. }
  434. // image Favorite
  435. if metadata.favorite {
  436. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  437. }
  438. cell.imageSelect.isHidden = true
  439. cell.backgroundView = nil
  440. cell.hideButtonMore(true)
  441. // Live Photo
  442. if metadata.livePhoto {
  443. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  444. }
  445. return cell
  446. }
  447. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  448. }
  449. }
  450. extension NCShareExtension: UICollectionViewDelegateFlowLayout {
  451. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  452. headerRichWorkspaceHeight = 0
  453. if let richWorkspaceText = richWorkspaceText {
  454. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  455. if trimmed.count > 0 {
  456. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  457. }
  458. }
  459. return CGSize(width: collectionView.frame.width, height: headerHeight + headerRichWorkspaceHeight)
  460. }
  461. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  462. return CGSize(width: collectionView.frame.width, height: footerHeight)
  463. }
  464. }
  465. // MARK: - NC API & Algorithm
  466. extension NCShareExtension {
  467. @objc func reloadDataSource() {
  468. loadDatasource(withLoadFolder: false)
  469. }
  470. @objc func loadDatasource(withLoadFolder: Bool) {
  471. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: keyLayout, serverUrl: serverUrl)
  472. let metadatasSource = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account, serverUrl))
  473. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, favoriteOnTop: true, filterLivePhoto: true)
  474. if withLoadFolder {
  475. loadFolder()
  476. } else {
  477. self.refreshControl.endRefreshing()
  478. }
  479. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", activeAccount.account,serverUrl))
  480. richWorkspaceText = directory?.richWorkspace
  481. collectionView.reloadData()
  482. }
  483. func createFolder(with fileName: String) {
  484. NCNetworking.shared.createFolder(fileName: fileName, serverUrl: serverUrl, account: activeAccount.account, urlBase: activeAccount.urlBase) { (errorCode, errorDescription) in
  485. if errorCode == 0 {
  486. self.loadDatasource(withLoadFolder: true)
  487. } else {
  488. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  489. }
  490. }
  491. }
  492. func loadFolder() {
  493. networkInProgress = true
  494. collectionView.reloadData()
  495. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: activeAccount.account) { (_, _, _, _, _, errorCode, errorDescription) in
  496. if errorCode != 0 {
  497. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  498. }
  499. self.networkInProgress = false
  500. self.loadDatasource(withLoadFolder: false)
  501. }
  502. }
  503. }