NCShareLinkMenuView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 shareViewController: NCShare?
  56. private var networking: NCShareNetworking?
  57. var viewWindow: UIView?
  58. var viewWindowCalendar: UIView?
  59. private var calendar: FSCalendar?
  60. override func awakeFromNib() {
  61. layer.borderColor = UIColor.lightGray.cgColor
  62. layer.borderWidth = 0.5
  63. layer.cornerRadius = 5
  64. layer.masksToBounds = false
  65. layer.shadowOffset = CGSize(width: 2, height: 2)
  66. layer.shadowOpacity = 0.2
  67. switchAllowEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  68. switchAllowEditing?.onTintColor = NCBrandColor.sharedInstance.brand
  69. switchReadOnly?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  70. switchReadOnly?.onTintColor = NCBrandColor.sharedInstance.brand
  71. switchAllowUploadAndEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  72. switchAllowUploadAndEditing?.onTintColor = NCBrandColor.sharedInstance.brand
  73. switchFileDrop?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  74. switchFileDrop?.onTintColor = NCBrandColor.sharedInstance.brand
  75. switchHideDownload.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  76. switchHideDownload.onTintColor = NCBrandColor.sharedInstance.brand
  77. switchPasswordProtect.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  78. switchPasswordProtect.onTintColor = NCBrandColor.sharedInstance.brand
  79. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  80. switchSetExpirationDate.onTintColor = NCBrandColor.sharedInstance.brand
  81. labelAllowEditing?.text = NSLocalizedString("_share_allow_editing_", comment: "")
  82. labelReadOnly?.text = NSLocalizedString("_share_read_only_", comment: "")
  83. labelAllowUploadAndEditing?.text = NSLocalizedString("_share_allow_upload_", comment: "")
  84. labelFileDrop?.text = NSLocalizedString("_share_file_drop_", comment: "")
  85. labelHideDownload?.text = NSLocalizedString("_share_hide_download_", comment: "")
  86. labelPasswordProtect?.text = NSLocalizedString("_share_password_protect_", comment: "")
  87. labelSetExpirationDate?.text = NSLocalizedString("_share_expiration_date_", comment: "")
  88. labelNoteToRecipient?.text = NSLocalizedString("_share_note_recipient_", comment: "")
  89. labelDeleteShareLink?.text = NSLocalizedString("_share_delete_sharelink_", comment: "")
  90. labelAddAnotherLink?.text = NSLocalizedString("_share_add_sharelink_", comment: "")
  91. fieldSetExpirationDate.inputView = UIView()
  92. 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))
  93. 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))
  94. 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))
  95. }
  96. override func willMove(toWindow newWindow: UIWindow?) {
  97. super.willMove(toWindow: newWindow)
  98. if newWindow == nil {
  99. // UIView disappear
  100. shareViewController?.reloadData()
  101. } else {
  102. // UIView appear
  103. networking = NCShareNetworking.init(metadata: metadata!, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  104. }
  105. }
  106. func unLoad() {
  107. viewWindowCalendar?.removeFromSuperview()
  108. viewWindow?.removeFromSuperview()
  109. viewWindowCalendar = nil
  110. viewWindow = nil
  111. }
  112. func reloadData(idRemoteShared: Int) {
  113. guard let metadata = self.metadata else { return }
  114. tableShare = NCManageDatabase.sharedInstance.getTableShare(account: metadata.account, idRemoteShared: idRemoteShared)
  115. guard let tableShare = self.tableShare else { return }
  116. if metadata.directory {
  117. // File Drop
  118. if tableShare.permissions == k_create_share_permission {
  119. switchReadOnly.setOn(false, animated: false)
  120. switchAllowUploadAndEditing.setOn(false, animated: false)
  121. switchFileDrop.setOn(true, animated: false)
  122. } else {
  123. // Read Only
  124. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  125. switchReadOnly.setOn(false, animated: false)
  126. switchAllowUploadAndEditing.setOn(true, animated: false)
  127. } else {
  128. switchReadOnly.setOn(true, animated: false)
  129. switchAllowUploadAndEditing.setOn(false, animated: false)
  130. }
  131. switchFileDrop.setOn(false, animated: false)
  132. }
  133. } else {
  134. // Allow editing
  135. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  136. switchAllowEditing.setOn(true, animated: false)
  137. } else {
  138. switchAllowEditing.setOn(false, animated: false)
  139. }
  140. }
  141. // Hide download
  142. if tableShare.hideDownload {
  143. switchHideDownload.setOn(true, animated: false)
  144. } else {
  145. switchHideDownload.setOn(false, animated: false)
  146. }
  147. // Password protect
  148. if tableShare.shareWith.count > 0 {
  149. switchPasswordProtect.setOn(true, animated: false)
  150. fieldPasswordProtect.isEnabled = true
  151. fieldPasswordProtect.text = tableShare.shareWith
  152. } else {
  153. switchPasswordProtect.setOn(false, animated: false)
  154. fieldPasswordProtect.isEnabled = false
  155. fieldPasswordProtect.text = ""
  156. }
  157. // Set expiration date
  158. if tableShare.expirationDate != nil {
  159. switchSetExpirationDate.setOn(true, animated: false)
  160. fieldSetExpirationDate.isEnabled = true
  161. let dateFormatter = DateFormatter()
  162. dateFormatter.formatterBehavior = .behavior10_4
  163. dateFormatter.dateStyle = .medium
  164. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  165. } else {
  166. switchSetExpirationDate.setOn(false, animated: false)
  167. fieldSetExpirationDate.isEnabled = false
  168. fieldSetExpirationDate.text = ""
  169. }
  170. // Note to recipient
  171. fieldNoteToRecipient.text = tableShare.note
  172. }
  173. // MARK: - Tap viewWindowCalendar
  174. @objc func tapViewWindowCalendar(gesture: UITapGestureRecognizer) {
  175. calendar?.removeFromSuperview()
  176. viewWindowCalendar?.removeFromSuperview()
  177. calendar = nil
  178. viewWindowCalendar = nil
  179. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  180. }
  181. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  182. return gestureRecognizer.view == touch.view
  183. }
  184. // MARK: - IBAction
  185. // Allow editing (file)
  186. @IBAction func switchAllowEditingChanged(sender: UISwitch) {
  187. guard let tableShare = self.tableShare else { return }
  188. guard let metadata = self.metadata else { return }
  189. var permission: Int = 0
  190. if sender.isOn {
  191. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  192. } else {
  193. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  194. }
  195. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  196. }
  197. // Read Only (directory)
  198. @IBAction func switchReadOnly(sender: UISwitch) {
  199. guard let tableShare = self.tableShare else { return }
  200. guard let metadata = self.metadata else { return }
  201. let permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  202. if sender.isOn && permission != tableShare.permissions {
  203. switchAllowUploadAndEditing.setOn(false, animated: false)
  204. switchFileDrop.setOn(false, animated: false)
  205. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  206. } else {
  207. sender.setOn(true, animated: false)
  208. }
  209. }
  210. // Allow Upload And Editing (directory)
  211. @IBAction func switchAllowUploadAndEditing(sender: UISwitch) {
  212. guard let tableShare = self.tableShare else { return }
  213. guard let metadata = self.metadata else { return }
  214. let permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  215. if sender.isOn && permission != tableShare.permissions {
  216. switchReadOnly.setOn(false, animated: false)
  217. switchFileDrop.setOn(false, animated: false)
  218. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  219. } else {
  220. sender.setOn(true, animated: false)
  221. }
  222. }
  223. // File Drop (directory)
  224. @IBAction func switchFileDrop(sender: UISwitch) {
  225. guard let tableShare = self.tableShare else { return }
  226. let permission = Int(k_create_share_permission)
  227. if sender.isOn && permission != tableShare.permissions {
  228. switchReadOnly.setOn(false, animated: false)
  229. switchAllowUploadAndEditing.setOn(false, animated: false)
  230. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  231. } else {
  232. sender.setOn(true, animated: false)
  233. }
  234. }
  235. // Hide download
  236. @IBAction func switchHideDownloadChanged(sender: UISwitch) {
  237. guard let tableShare = self.tableShare else { return }
  238. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: nil, hideDownload: sender.isOn)
  239. }
  240. // Password protect
  241. @IBAction func switchPasswordProtectChanged(sender: UISwitch) {
  242. guard let tableShare = self.tableShare else { return }
  243. if sender.isOn {
  244. fieldPasswordProtect.isEnabled = true
  245. fieldPasswordProtect.text = ""
  246. fieldPasswordProtect.becomeFirstResponder()
  247. } else {
  248. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: "", permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  249. }
  250. }
  251. @IBAction func fieldPasswordProtectDidEndOnExit(textField: UITextField) {
  252. guard let tableShare = self.tableShare else { return }
  253. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: fieldPasswordProtect.text, permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  254. }
  255. // Set expiration date
  256. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  257. guard let tableShare = self.tableShare else { return }
  258. if sender.isOn {
  259. fieldSetExpirationDate.isEnabled = true
  260. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  261. } else {
  262. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: "", hideDownload: tableShare.hideDownload)
  263. }
  264. }
  265. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  266. let calendar = NCShareCommon.sharedInstance.openCalendar(view: self, width: width, height: height)
  267. calendar.calendarView.delegate = self
  268. self.calendar = calendar.calendarView
  269. viewWindowCalendar = calendar.viewWindow
  270. let tap = UITapGestureRecognizer(target: self, action: #selector(tapViewWindowCalendar))
  271. tap.delegate = self
  272. viewWindowCalendar?.addGestureRecognizer(tap)
  273. }
  274. // Note to recipient
  275. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  276. guard let tableShare = self.tableShare else { return }
  277. if fieldNoteToRecipient.text == nil { return }
  278. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: fieldNoteToRecipient.text, expirationTime: nil, hideDownload: tableShare.hideDownload)
  279. }
  280. // Delete share link
  281. @IBAction func buttonDeleteShareLink(sender: UIButton) {
  282. guard let tableShare = self.tableShare else { return }
  283. networking?.unShare(idRemoteShared: tableShare.idRemoteShared)
  284. }
  285. // Add another link
  286. @IBAction func buttonAddAnotherLink(sender: UIButton) {
  287. networking?.share(password: "", permission: 1, hideDownload: false)
  288. }
  289. // MARK: - Delegate networking
  290. func readShareCompleted() {
  291. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  292. }
  293. func shareCompleted() {
  294. unLoad()
  295. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  296. }
  297. func unShareCompleted() {
  298. unLoad()
  299. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  300. }
  301. func updateShareWithError(idRemoteShared: Int) {
  302. reloadData(idRemoteShared: idRemoteShared)
  303. }
  304. func getUserAndGroup(items: [OCShareUser]?) { }
  305. // MARK: - Delegate calendar
  306. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  307. if monthPosition == .previous || monthPosition == .next {
  308. calendar.setCurrentPage(date, animated: true)
  309. } else {
  310. let dateFormatter = DateFormatter()
  311. dateFormatter.formatterBehavior = .behavior10_4
  312. dateFormatter.dateStyle = .medium
  313. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  314. fieldSetExpirationDate.endEditing(true)
  315. viewWindowCalendar?.removeFromSuperview()
  316. guard let tableShare = self.tableShare else { return }
  317. dateFormatter.dateFormat = "YYYY-MM-dd"
  318. let expirationTime = dateFormatter.string(from: date)
  319. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: expirationTime, hideDownload: tableShare.hideDownload)
  320. }
  321. }
  322. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  323. return date > Date()
  324. }
  325. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  326. if date > Date() {
  327. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  328. } else {
  329. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  330. }
  331. }
  332. }