NCCreateFormUploadRichdocuments.swift 11 KB

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