NCShareExtension.swift 28 KB

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