NCShareLinkMenuView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. public let width: CGFloat = 250
  52. public let height: CGFloat = 440
  53. private var tableShare: tableShare?
  54. public var metadata: tableMetadata?
  55. public var viewWindow: UIView?
  56. public var viewWindowCalendar: UIView?
  57. override func awakeFromNib() {
  58. self.frame.size.width = width
  59. self.frame.size.height = height
  60. layer.borderColor = UIColor.lightGray.cgColor
  61. layer.borderWidth = 0.5
  62. layer.cornerRadius = 5
  63. layer.masksToBounds = false
  64. layer.shadowOffset = CGSize(width: 2, height: 2)
  65. layer.shadowOpacity = 0.2
  66. switchAllowEditing.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  67. switchAllowEditing.onTintColor = NCBrandColor.sharedInstance.brand
  68. switchHideDownload.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  69. switchHideDownload.onTintColor = NCBrandColor.sharedInstance.brand
  70. switchPasswordProtect.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  71. switchPasswordProtect.onTintColor = NCBrandColor.sharedInstance.brand
  72. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  73. switchSetExpirationDate.onTintColor = NCBrandColor.sharedInstance.brand
  74. fieldSetExpirationDate.inputView = UIView()
  75. 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))
  76. 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))
  77. 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))
  78. }
  79. func unLoad() {
  80. viewWindowCalendar?.removeFromSuperview()
  81. viewWindow?.removeFromSuperview()
  82. viewWindowCalendar = nil
  83. viewWindow = nil
  84. }
  85. func reloadData(idRemoteShared: Int) {
  86. guard let metadata = self.metadata else { return }
  87. tableShare = NCManageDatabase.sharedInstance.getTableShare(account: metadata.account, idRemoteShared: idRemoteShared)
  88. guard let tableShare = self.tableShare else { return }
  89. if metadata.directory {
  90. } else {
  91. // Allow editing
  92. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  93. switchAllowEditing.setOn(true, animated: false)
  94. } else {
  95. switchAllowEditing.setOn(false, animated: false)
  96. }
  97. }
  98. // Hide download
  99. if tableShare.hideDownload {
  100. switchHideDownload.setOn(true, animated: false)
  101. } else {
  102. switchHideDownload.setOn(false, animated: false)
  103. }
  104. // Password protect
  105. if tableShare.shareWith.count > 0 {
  106. switchPasswordProtect.setOn(true, animated: false)
  107. fieldPasswordProtect.isEnabled = true
  108. fieldPasswordProtect.text = tableShare.shareWith
  109. } else {
  110. switchPasswordProtect.setOn(false, animated: false)
  111. fieldPasswordProtect.isEnabled = false
  112. fieldPasswordProtect.text = ""
  113. }
  114. // Set expiration date
  115. if tableShare.expirationDate != nil {
  116. switchSetExpirationDate.setOn(true, animated: false)
  117. fieldSetExpirationDate.isEnabled = true
  118. let dateFormatter = DateFormatter()
  119. dateFormatter.formatterBehavior = .behavior10_4
  120. dateFormatter.dateStyle = .medium
  121. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  122. } else {
  123. switchSetExpirationDate.setOn(false, animated: false)
  124. fieldSetExpirationDate.isEnabled = false
  125. fieldSetExpirationDate.text = ""
  126. }
  127. // Note to recipient
  128. fieldNoteToRecipient.text = tableShare.note
  129. }
  130. // MARK: - IBAction
  131. // Allow editing
  132. @IBAction func switchAllowEditingChanged(sender: UISwitch) {
  133. guard let tableShare = self.tableShare else { return }
  134. guard let metadata = self.metadata else { return }
  135. var permission: Int = 0
  136. if sender.isOn {
  137. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  138. } else {
  139. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  140. }
  141. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  142. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  143. }
  144. // Read Only
  145. @IBAction func switchReadOnly(sender: UISwitch) {
  146. }
  147. // Allow Upload And Editing
  148. @IBAction func switchAllowUploadAndEditing(sender: UISwitch) {
  149. }
  150. // File Drop
  151. @IBAction func switchFileDrop(sender: UISwitch) {
  152. }
  153. // Hide download
  154. @IBAction func switchHideDownloadChanged(sender: UISwitch) {
  155. guard let tableShare = self.tableShare else { return }
  156. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  157. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: nil, hideDownload: sender.isOn)
  158. }
  159. // Password protect
  160. @IBAction func switchPasswordProtectChanged(sender: UISwitch) {
  161. guard let tableShare = self.tableShare else { return }
  162. guard let metadata = self.metadata else { return }
  163. if sender.isOn {
  164. fieldPasswordProtect.isEnabled = true
  165. fieldPasswordProtect.text = ""
  166. fieldPasswordProtect.becomeFirstResponder()
  167. } else {
  168. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  169. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: "", permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  170. }
  171. }
  172. @IBAction func fieldPasswordProtectDidEndOnExit(textField: UITextField) {
  173. guard let tableShare = self.tableShare else { return }
  174. guard let metadata = self.metadata else { return }
  175. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  176. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: fieldPasswordProtect.text, permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  177. }
  178. // Set expiration date
  179. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  180. guard let tableShare = self.tableShare else { return }
  181. guard let metadata = self.metadata else { return }
  182. if sender.isOn {
  183. fieldSetExpirationDate.isEnabled = true
  184. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  185. } else {
  186. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  187. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: "", hideDownload: tableShare.hideDownload)
  188. }
  189. }
  190. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  191. let calendar = NCShareCommon.sharedInstance.openCalendar(view: self, width: width, height: height)
  192. calendar.calendarView.delegate = self
  193. viewWindowCalendar = calendar.viewWindow
  194. }
  195. // Note to recipient
  196. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  197. guard let tableShare = self.tableShare else { return }
  198. guard let metadata = self.metadata else { return }
  199. if fieldNoteToRecipient.text == nil { return }
  200. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  201. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: fieldNoteToRecipient.text, expirationTime: nil, hideDownload: tableShare.hideDownload)
  202. }
  203. // Delete share link
  204. @IBAction func buttonDeleteShareLink(sender: UIButton) {
  205. guard let tableShare = self.tableShare else { return }
  206. guard let metadata = self.metadata else { return }
  207. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  208. networking.unShare(idRemoteShared: tableShare.idRemoteShared)
  209. }
  210. // Add another link
  211. @IBAction func buttonAddAnotherLink(sender: UIButton) {
  212. guard let metadata = self.metadata else { return }
  213. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  214. networking.share(metadata: metadata, password: "", permission: 1, hideDownload: false)
  215. }
  216. // MARK: - Delegate networking
  217. func readShareCompleted() {
  218. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  219. }
  220. func shareCompleted() {
  221. unLoad()
  222. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  223. }
  224. func unShareCompleted() {
  225. unLoad()
  226. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  227. }
  228. func updateShareWithError(idRemoteShared: Int) {
  229. reloadData(idRemoteShared: idRemoteShared)
  230. }
  231. func getUserAndGroup(items: [OCShareUser]?) { }
  232. // MARK: - Delegate calendar
  233. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  234. if monthPosition == .previous || monthPosition == .next {
  235. calendar.setCurrentPage(date, animated: true)
  236. } else {
  237. let dateFormatter = DateFormatter()
  238. dateFormatter.formatterBehavior = .behavior10_4
  239. dateFormatter.dateStyle = .medium
  240. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  241. fieldSetExpirationDate.endEditing(true)
  242. viewWindowCalendar?.removeFromSuperview()
  243. guard let tableShare = self.tableShare else { return }
  244. guard let metadata = self.metadata else { return }
  245. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  246. dateFormatter.dateFormat = "YYYY-MM-dd"
  247. let expirationTime = dateFormatter.string(from: date)
  248. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: expirationTime, hideDownload: tableShare.hideDownload)
  249. }
  250. }
  251. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  252. return date > Date()
  253. }
  254. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  255. if date > Date() {
  256. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  257. } else {
  258. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  259. }
  260. }
  261. }