NCShareLinkMenuView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // NCShareLinkMenuView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/07/2019.
  6. // Copyright © 2019 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. import Foundation
  23. import FSCalendar
  24. class NCShareLinkMenuView: UIView, UIGestureRecognizerDelegate, NCShareNetworkingDelegate, FSCalendarDelegate, FSCalendarDelegateAppearance {
  25. @IBOutlet weak var switchAllowEditing: UISwitch!
  26. @IBOutlet weak var labelAllowEditing: UILabel!
  27. @IBOutlet weak var switchReadOnly: UISwitch!
  28. @IBOutlet weak var labelReadOnly: UILabel!
  29. @IBOutlet weak var switchAllowUploadAndEditing: UISwitch!
  30. @IBOutlet weak var labelAllowUploadAndEditing: UILabel!
  31. @IBOutlet weak var switchFileDrop: UISwitch!
  32. @IBOutlet weak var labelFileDrop: UILabel!
  33. @IBOutlet weak var switchHideDownload: UISwitch!
  34. @IBOutlet weak var labelHideDownload: UILabel!
  35. @IBOutlet weak var switchPasswordProtect: UISwitch!
  36. @IBOutlet weak var labelPasswordProtect: UILabel!
  37. @IBOutlet weak var fieldPasswordProtect: UITextField!
  38. @IBOutlet weak var switchSetExpirationDate: UISwitch!
  39. @IBOutlet weak var labelSetExpirationDate: UILabel!
  40. @IBOutlet weak var fieldSetExpirationDate: UITextField!
  41. @IBOutlet weak var imageNoteToRecipient: UIImageView!
  42. @IBOutlet weak var labelNoteToRecipient: UILabel!
  43. @IBOutlet weak var fieldNoteToRecipient: UITextField!
  44. @IBOutlet weak var buttonDeleteShareLink: UIButton!
  45. @IBOutlet weak var labelDeleteShareLink: UILabel!
  46. @IBOutlet weak var imageDeleteShareLink: UIImageView!
  47. @IBOutlet weak var buttonAddAnotherLink: UIButton!
  48. @IBOutlet weak var labelAddAnotherLink: UILabel!
  49. @IBOutlet weak var imageAddAnotherLink: UIImageView!
  50. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  51. var width: CGFloat = 0
  52. var height: CGFloat = 0
  53. private var tableShare: tableShare?
  54. var metadata: tableMetadata?
  55. var viewWindow: UIView?
  56. var viewWindowCalendar: UIView?
  57. override func awakeFromNib() {
  58. layer.borderColor = UIColor.lightGray.cgColor
  59. layer.borderWidth = 0.5
  60. layer.cornerRadius = 5
  61. layer.masksToBounds = false
  62. layer.shadowOffset = CGSize(width: 2, height: 2)
  63. layer.shadowOpacity = 0.2
  64. switchAllowEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  65. switchAllowEditing?.onTintColor = NCBrandColor.sharedInstance.brand
  66. switchReadOnly?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  67. switchReadOnly?.onTintColor = NCBrandColor.sharedInstance.brand
  68. switchAllowUploadAndEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  69. switchAllowUploadAndEditing?.onTintColor = NCBrandColor.sharedInstance.brand
  70. switchFileDrop?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  71. switchFileDrop?.onTintColor = NCBrandColor.sharedInstance.brand
  72. switchHideDownload.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  73. switchHideDownload.onTintColor = NCBrandColor.sharedInstance.brand
  74. switchPasswordProtect.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  75. switchPasswordProtect.onTintColor = NCBrandColor.sharedInstance.brand
  76. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  77. switchSetExpirationDate.onTintColor = NCBrandColor.sharedInstance.brand
  78. fieldSetExpirationDate.inputView = UIView()
  79. imageNoteToRecipient.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "file_txt"), width: 100, height: 100, color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1))
  80. imageDeleteShareLink.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 100, height: 100, color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1))
  81. imageAddAnotherLink.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "add"), width: 100, height: 100, color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1))
  82. }
  83. override func willMove(toWindow newWindow: UIWindow?) {
  84. super.willMove(toWindow: newWindow)
  85. }
  86. func unLoad() {
  87. viewWindowCalendar?.removeFromSuperview()
  88. viewWindow?.removeFromSuperview()
  89. viewWindowCalendar = nil
  90. viewWindow = nil
  91. }
  92. func reloadData(idRemoteShared: Int) {
  93. guard let metadata = self.metadata else { return }
  94. tableShare = NCManageDatabase.sharedInstance.getTableShare(account: metadata.account, idRemoteShared: idRemoteShared)
  95. guard let tableShare = self.tableShare else { return }
  96. if metadata.directory {
  97. // Read Only
  98. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  99. switchReadOnly.setOn(false, animated: false)
  100. switchAllowUploadAndEditing.setOn(true, animated: false)
  101. } else {
  102. switchReadOnly.setOn(true, animated: false)
  103. switchAllowUploadAndEditing.setOn(false, animated: false)
  104. }
  105. // File Drop
  106. if UtilsFramework.isPermission(toCanCreate: tableShare.permissions) {
  107. switchReadOnly.setOn(false, animated: false)
  108. switchAllowUploadAndEditing.setOn(false, animated: false)
  109. switchFileDrop.setOn(true, animated: false)
  110. } else {
  111. switchFileDrop.setOn(false, animated: false)
  112. }
  113. } else {
  114. // Allow editing
  115. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  116. switchAllowEditing.setOn(true, animated: false)
  117. } else {
  118. switchAllowEditing.setOn(false, animated: false)
  119. }
  120. }
  121. // Hide download
  122. if tableShare.hideDownload {
  123. switchHideDownload.setOn(true, animated: false)
  124. } else {
  125. switchHideDownload.setOn(false, animated: false)
  126. }
  127. // Password protect
  128. if tableShare.shareWith.count > 0 {
  129. switchPasswordProtect.setOn(true, animated: false)
  130. fieldPasswordProtect.isEnabled = true
  131. fieldPasswordProtect.text = tableShare.shareWith
  132. } else {
  133. switchPasswordProtect.setOn(false, animated: false)
  134. fieldPasswordProtect.isEnabled = false
  135. fieldPasswordProtect.text = ""
  136. }
  137. // Set expiration date
  138. if tableShare.expirationDate != nil {
  139. switchSetExpirationDate.setOn(true, animated: false)
  140. fieldSetExpirationDate.isEnabled = true
  141. let dateFormatter = DateFormatter()
  142. dateFormatter.formatterBehavior = .behavior10_4
  143. dateFormatter.dateStyle = .medium
  144. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  145. } else {
  146. switchSetExpirationDate.setOn(false, animated: false)
  147. fieldSetExpirationDate.isEnabled = false
  148. fieldSetExpirationDate.text = ""
  149. }
  150. // Note to recipient
  151. fieldNoteToRecipient.text = tableShare.note
  152. }
  153. // MARK: - IBAction
  154. // Allow editing
  155. @IBAction func switchAllowEditingChanged(sender: UISwitch) {
  156. guard let tableShare = self.tableShare else { return }
  157. guard let metadata = self.metadata else { return }
  158. var permission: Int = 0
  159. if sender.isOn {
  160. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  161. } else {
  162. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  163. }
  164. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  165. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  166. }
  167. // Read Only
  168. @IBAction func switchReadOnly(sender: UISwitch) {
  169. }
  170. // Allow Upload And Editing
  171. @IBAction func switchAllowUploadAndEditing(sender: UISwitch) {
  172. }
  173. // File Drop
  174. @IBAction func switchFileDrop(sender: UISwitch) {
  175. }
  176. // Hide download
  177. @IBAction func switchHideDownloadChanged(sender: UISwitch) {
  178. guard let tableShare = self.tableShare else { return }
  179. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  180. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: nil, hideDownload: sender.isOn)
  181. }
  182. // Password protect
  183. @IBAction func switchPasswordProtectChanged(sender: UISwitch) {
  184. guard let tableShare = self.tableShare else { return }
  185. guard let metadata = self.metadata else { return }
  186. if sender.isOn {
  187. fieldPasswordProtect.isEnabled = true
  188. fieldPasswordProtect.text = ""
  189. fieldPasswordProtect.becomeFirstResponder()
  190. } else {
  191. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  192. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: "", permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  193. }
  194. }
  195. @IBAction func fieldPasswordProtectDidEndOnExit(textField: UITextField) {
  196. guard let tableShare = self.tableShare else { return }
  197. guard let metadata = self.metadata else { return }
  198. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  199. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: fieldPasswordProtect.text, permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  200. }
  201. // Set expiration date
  202. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  203. guard let tableShare = self.tableShare else { return }
  204. guard let metadata = self.metadata else { return }
  205. if sender.isOn {
  206. fieldSetExpirationDate.isEnabled = true
  207. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  208. } else {
  209. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  210. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: "", hideDownload: tableShare.hideDownload)
  211. }
  212. }
  213. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  214. let calendar = NCShareCommon.sharedInstance.openCalendar(view: self, width: width, height: height)
  215. calendar.calendarView.delegate = self
  216. viewWindowCalendar = calendar.viewWindow
  217. }
  218. // Note to recipient
  219. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  220. guard let tableShare = self.tableShare else { return }
  221. guard let metadata = self.metadata else { return }
  222. if fieldNoteToRecipient.text == nil { return }
  223. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  224. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: fieldNoteToRecipient.text, expirationTime: nil, hideDownload: tableShare.hideDownload)
  225. }
  226. // Delete share link
  227. @IBAction func buttonDeleteShareLink(sender: UIButton) {
  228. guard let tableShare = self.tableShare else { return }
  229. guard let metadata = self.metadata else { return }
  230. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  231. networking.unShare(idRemoteShared: tableShare.idRemoteShared)
  232. }
  233. // Add another link
  234. @IBAction func buttonAddAnotherLink(sender: UIButton) {
  235. guard let metadata = self.metadata else { return }
  236. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  237. networking.share(metadata: metadata, password: "", permission: 1, hideDownload: false)
  238. }
  239. // MARK: - Delegate networking
  240. func readShareCompleted() {
  241. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  242. }
  243. func shareCompleted() {
  244. unLoad()
  245. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  246. }
  247. func unShareCompleted() {
  248. unLoad()
  249. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  250. }
  251. func updateShareWithError(idRemoteShared: Int) {
  252. reloadData(idRemoteShared: idRemoteShared)
  253. }
  254. func getUserAndGroup(items: [OCShareUser]?) { }
  255. // MARK: - Delegate calendar
  256. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  257. if monthPosition == .previous || monthPosition == .next {
  258. calendar.setCurrentPage(date, animated: true)
  259. } else {
  260. let dateFormatter = DateFormatter()
  261. dateFormatter.formatterBehavior = .behavior10_4
  262. dateFormatter.dateStyle = .medium
  263. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  264. fieldSetExpirationDate.endEditing(true)
  265. viewWindowCalendar?.removeFromSuperview()
  266. guard let tableShare = self.tableShare else { return }
  267. guard let metadata = self.metadata else { return }
  268. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  269. dateFormatter.dateFormat = "YYYY-MM-dd"
  270. let expirationTime = dateFormatter.string(from: date)
  271. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: expirationTime, hideDownload: tableShare.hideDownload)
  272. }
  273. }
  274. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  275. return date > Date()
  276. }
  277. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  278. if date > Date() {
  279. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  280. } else {
  281. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  282. }
  283. }
  284. }