NCCreateFormUploadRichdocuments.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // CCCreateCloud.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/11/18.
  6. // Copyright © 2017 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. // MARK: -
  25. class NCCreateFormUploadRichdocuments: XLFormViewController, NCSelectDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
  26. var typeTemplate = ""
  27. var serverUrl = ""
  28. var fileNameFolder = ""
  29. var fileName = ""
  30. var listOfTemplate = [NCRichDocumentTemplate]()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. @IBOutlet weak var collectionView: UICollectionView!
  33. // MARK: - View Life Cycle
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  37. fileNameFolder = "/"
  38. } else {
  39. fileNameFolder = (serverUrl as NSString).lastPathComponent
  40. }
  41. self.initializeForm()
  42. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  43. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  44. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  45. self.navigationItem.leftBarButtonItem = cancelButton
  46. self.navigationItem.rightBarButtonItem = saveButton
  47. self.navigationItem.rightBarButtonItem?.isEnabled = false
  48. self.navigationController?.navigationBar.isTranslucent = false
  49. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  50. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  51. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  52. // load the templates available
  53. getTemplate()
  54. }
  55. // MARK: - Tableview (XLForm)
  56. func initializeForm() {
  57. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_upload_photos_videos_", comment: "")) as XLFormDescriptor
  58. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  59. var section : XLFormSectionDescriptor
  60. var row : XLFormRowDescriptor
  61. // Section: Destination Folder
  62. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: "").uppercased())
  63. form.addFormSection(section)
  64. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: fileNameFolder)
  65. row.action.formSelector = #selector(changeDestinationFolder(_:))
  66. row.value = fileNameFolder
  67. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:1, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  68. row.cellConfig["imageView.image"] = imageFolder
  69. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  70. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  71. section.addFormRow(row)
  72. // Section: File Name
  73. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: "").uppercased())
  74. form.addFormSection(section)
  75. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  76. row.value = fileName
  77. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  78. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  79. section.addFormRow(row)
  80. self.form = form
  81. }
  82. override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  83. let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
  84. header.textLabel?.font = UIFont.systemFont(ofSize: 13.0)
  85. header.textLabel?.textColor = NCBrandColor.sharedInstance.icon //UIColor.lightGray
  86. }
  87. // MARK: - CollectionView
  88. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  89. return listOfTemplate.count
  90. }
  91. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  92. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
  93. let template = listOfTemplate[indexPath.row]
  94. let imageView = cell.viewWithTag(100) as! UIImageView
  95. let name = cell.viewWithTag(200) as! UILabel
  96. if template.preview != "" {
  97. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + template.name + ".png"
  98. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  99. let imageURL = URL(fileURLWithPath: fileNameLocalPath)
  100. if let image = UIImage(contentsOfFile: imageURL.path) {
  101. imageView.image = image
  102. }
  103. } else {
  104. getImage(template: template, indexPath: indexPath)
  105. }
  106. }
  107. name.text = template.name
  108. return cell
  109. }
  110. // MARK: - Action
  111. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  112. guard let serverUrl = serverUrl else {
  113. return
  114. }
  115. self.serverUrl = serverUrl
  116. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  117. fileNameFolder = "/"
  118. } else {
  119. fileNameFolder = (serverUrl as NSString).lastPathComponent
  120. }
  121. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  122. buttonDestinationFolder.title = fileNameFolder
  123. self.tableView.reloadData()
  124. }
  125. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  126. self.deselectFormRow(sender)
  127. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  128. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  129. let viewController = navigationController.topViewController as! NCSelect
  130. viewController.delegate = self
  131. viewController.hideButtonCreateFolder = false
  132. viewController.includeDirectoryE2EEncryption = false
  133. viewController.includeImages = false
  134. viewController.layoutViewSelect = k_layout_view_move
  135. viewController.selectFile = false
  136. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  137. viewController.type = ""
  138. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  139. self.present(navigationController, animated: true, completion: nil)
  140. }
  141. @objc func save() {
  142. self.dismiss(animated: true, completion: {
  143. //self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  144. })
  145. }
  146. @objc func cancel() {
  147. self.dismiss(animated: true, completion: nil)
  148. }
  149. // MARK: NC API
  150. func getTemplate() {
  151. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  152. ocNetworking?.createTemplateRichdocuments(withTemplate: typeTemplate, success: { (listOfTemplate) in
  153. self.listOfTemplate = listOfTemplate as! [NCRichDocumentTemplate]
  154. self.collectionView.reloadData()
  155. }, failure: { (message, errorCode) in
  156. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  157. })
  158. }
  159. func getImage(template: NCRichDocumentTemplate, indexPath: IndexPath) {
  160. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  161. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + template.name + ".png"
  162. ocNetworking?.downloadFile(template.preview, fileNameLocalPath: fileNameLocalPath, success: {
  163. self.collectionView.reloadItems(at: [indexPath])
  164. }, failure: { (message, errorCode) in
  165. print("\(errorCode)")
  166. })
  167. }
  168. }