NCCreateFormUploadRichdocuments.swift 13 KB

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