NCShareUserMenuView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // NCShareUserMenuView.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 UIKit
  23. import FSCalendar
  24. import NCCommunication
  25. class NCShareUserMenuView: UIView, UIGestureRecognizerDelegate, NCShareNetworkingDelegate, FSCalendarDelegate, FSCalendarDelegateAppearance {
  26. @IBOutlet weak var switchCanReshare: UISwitch!
  27. @IBOutlet weak var labelCanReshare: UILabel!
  28. @IBOutlet weak var switchCanCreate: UISwitch!
  29. @IBOutlet weak var labelCanCreate: UILabel!
  30. @IBOutlet weak var switchCanChange: UISwitch!
  31. @IBOutlet weak var labelCanChange: UILabel!
  32. @IBOutlet weak var switchCanDelete: UISwitch!
  33. @IBOutlet weak var labelCanDelete: UILabel!
  34. @IBOutlet weak var switchSetExpirationDate: UISwitch!
  35. @IBOutlet weak var labelSetExpirationDate: UILabel!
  36. @IBOutlet weak var fieldSetExpirationDate: UITextField!
  37. @IBOutlet weak var imageNoteToRecipient: UIImageView!
  38. @IBOutlet weak var labelNoteToRecipient: UILabel!
  39. @IBOutlet weak var fieldNoteToRecipient: UITextField!
  40. @IBOutlet weak var buttonUnshare: UIButton!
  41. @IBOutlet weak var labelUnshare: UILabel!
  42. @IBOutlet weak var imageUnshare: UIImageView!
  43. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  44. var width: CGFloat = 0
  45. var height: CGFloat = 0
  46. private var tableShare: tableShare?
  47. var metadata: tableMetadata?
  48. var shareViewController: NCShare?
  49. private var networking: NCShareNetworking?
  50. var viewWindow: UIView?
  51. var viewWindowCalendar: UIView?
  52. private var calendar: FSCalendar?
  53. private var selfFrameOriginYDiff: CGFloat = 0
  54. override func awakeFromNib() {
  55. layer.borderColor = UIColor.lightGray.cgColor
  56. layer.borderWidth = 0.5
  57. layer.cornerRadius = 5
  58. layer.masksToBounds = false
  59. layer.shadowOffset = CGSize(width: 2, height: 2)
  60. layer.shadowOpacity = 0.2
  61. switchCanReshare.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  62. switchCanReshare.onTintColor = NCBrandColor.shared.brandElement
  63. switchCanCreate?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  64. switchCanCreate?.onTintColor = NCBrandColor.shared.brandElement
  65. switchCanChange?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  66. switchCanChange?.onTintColor = NCBrandColor.shared.brandElement
  67. switchCanDelete?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  68. switchCanDelete?.onTintColor = NCBrandColor.shared.brandElement
  69. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  70. switchSetExpirationDate.onTintColor = NCBrandColor.shared.brandElement
  71. labelCanReshare?.text = NSLocalizedString("_share_can_reshare_", comment: "")
  72. labelCanReshare?.textColor = NCBrandColor.shared.label
  73. labelCanCreate?.text = NSLocalizedString("_share_can_create_", comment: "")
  74. labelCanCreate?.textColor = NCBrandColor.shared.label
  75. labelCanChange?.text = NSLocalizedString("_share_can_change_", comment: "")
  76. labelCanChange?.textColor = NCBrandColor.shared.label
  77. labelCanDelete?.text = NSLocalizedString("_share_can_delete_", comment: "")
  78. labelCanDelete?.textColor = NCBrandColor.shared.label
  79. labelSetExpirationDate?.text = NSLocalizedString("_share_expiration_date_", comment: "")
  80. labelSetExpirationDate?.textColor = NCBrandColor.shared.label
  81. labelNoteToRecipient?.text = NSLocalizedString("_share_note_recipient_", comment: "")
  82. labelNoteToRecipient?.textColor = NCBrandColor.shared.label
  83. labelUnshare?.text = NSLocalizedString("_share_unshare_", comment: "")
  84. labelUnshare?.textColor = NCBrandColor.shared.label
  85. fieldSetExpirationDate.inputView = UIView()
  86. imageNoteToRecipient.image = UIImage.init(named: "file_txt")!.image(color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  87. imageUnshare.image = NCUtility.shared.loadImage(named: "trash", color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  88. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  89. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  90. }
  91. override func willMove(toWindow newWindow: UIWindow?) {
  92. super.willMove(toWindow: newWindow)
  93. if newWindow == nil {
  94. // UIView disappear
  95. shareViewController?.reloadData()
  96. } else {
  97. // UIView appear
  98. networking = NCShareNetworking.init(metadata: metadata!, urlBase: appDelegate.urlBase, view: self, delegate: self)
  99. }
  100. }
  101. func unLoad() {
  102. viewWindowCalendar?.removeFromSuperview()
  103. viewWindow?.removeFromSuperview()
  104. viewWindowCalendar = nil
  105. viewWindow = nil
  106. }
  107. func reloadData(idShare: Int) {
  108. guard let metadata = self.metadata else { return }
  109. tableShare = NCManageDatabase.shared.getTableShare(account: metadata.account, idShare: idShare)
  110. guard let tableShare = self.tableShare else { return }
  111. // Can reshare (file)
  112. let canReshare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  113. switchCanReshare.setOn(canReshare, animated: false)
  114. if metadata.directory {
  115. // Can create (folder)
  116. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  117. switchCanCreate.setOn(canCreate, animated: false)
  118. // Can change (folder)
  119. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  120. switchCanChange.setOn(canChange, animated: false)
  121. // Can delete (folder)
  122. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  123. switchCanDelete.setOn(canDelete, animated: false)
  124. }
  125. // Set expiration date
  126. if tableShare.expirationDate != nil {
  127. switchSetExpirationDate.setOn(true, animated: false)
  128. fieldSetExpirationDate.isEnabled = true
  129. let dateFormatter = DateFormatter()
  130. dateFormatter.formatterBehavior = .behavior10_4
  131. dateFormatter.dateStyle = .medium
  132. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  133. } else {
  134. switchSetExpirationDate.setOn(false, animated: false)
  135. fieldSetExpirationDate.isEnabled = false
  136. fieldSetExpirationDate.text = ""
  137. }
  138. // Note to recipient
  139. fieldNoteToRecipient.text = tableShare.note
  140. }
  141. // MARK: - Keyboard notification
  142. @objc internal func keyboardWillShow(_ notification : Notification?) {
  143. selfFrameOriginYDiff = 0
  144. if let info = notification?.userInfo, let centerObject = fieldNoteToRecipient.superview?.convert(fieldNoteToRecipient.center, to: nil) {
  145. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  146. // Getting UIKeyboardSize.
  147. if let keyboardFrame = info[frameEndUserInfoKey] as? CGRect {
  148. let diff = keyboardFrame.origin.y - centerObject.y - (fieldNoteToRecipient.frame.height / 2)
  149. if diff < 0 {
  150. selfFrameOriginYDiff = diff
  151. self.frame.origin.y += selfFrameOriginYDiff
  152. }
  153. }
  154. }
  155. }
  156. @objc func keyboardWillHide(_ notification: Notification) {
  157. self.frame.origin.y -= selfFrameOriginYDiff
  158. }
  159. // MARK: - Tap viewWindowCalendar
  160. @objc func tapViewWindowCalendar(gesture: UITapGestureRecognizer) {
  161. calendar?.removeFromSuperview()
  162. viewWindowCalendar?.removeFromSuperview()
  163. calendar = nil
  164. viewWindowCalendar = nil
  165. reloadData(idShare: tableShare?.idShare ?? 0)
  166. }
  167. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  168. return gestureRecognizer.view == touch.view
  169. }
  170. // MARK: - IBAction
  171. // Can reshare
  172. @IBAction func switchCanReshareChanged(sender: UISwitch) {
  173. guard let tableShare = self.tableShare else { return }
  174. guard let metadata = self.metadata else { return }
  175. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  176. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  177. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  178. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  179. var permissions: Int = 0
  180. if metadata.directory {
  181. permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: canCreate, andCanChange: canChange, andCanDelete: canDelete, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  182. } else {
  183. if sender.isOn {
  184. if canEdit {
  185. permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  186. } else {
  187. permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  188. }
  189. } else {
  190. if canEdit {
  191. permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  192. } else {
  193. permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  194. }
  195. }
  196. }
  197. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  198. }
  199. @IBAction func switchCanCreate(sender: UISwitch) {
  200. guard let tableShare = self.tableShare else { return }
  201. guard let metadata = self.metadata else { return }
  202. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  203. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  204. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  205. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  206. let permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: sender.isOn, andCanChange: canChange, andCanDelete: canDelete, andCanShare: canShare, andIsFolder: metadata.directory)
  207. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  208. }
  209. @IBAction func switchCanChange(sender: UISwitch) {
  210. guard let tableShare = self.tableShare else { return }
  211. guard let metadata = self.metadata else { return }
  212. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  213. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  214. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  215. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  216. let permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: canCreate, andCanChange: sender.isOn, andCanDelete: canDelete, andCanShare: canShare, andIsFolder: metadata.directory)
  217. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  218. }
  219. @IBAction func switchCanDelete(sender: UISwitch) {
  220. guard let tableShare = self.tableShare else { return }
  221. guard let metadata = self.metadata else { return }
  222. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  223. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  224. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  225. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  226. let permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: canCreate, andCanChange: canChange, andCanDelete: sender.isOn, andCanShare: canShare, andIsFolder: metadata.directory)
  227. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  228. }
  229. // Set expiration date
  230. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  231. guard let tableShare = self.tableShare else { return }
  232. if sender.isOn {
  233. fieldSetExpirationDate.isEnabled = true
  234. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  235. } else {
  236. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: "", hideDownload: tableShare.hideDownload)
  237. }
  238. }
  239. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  240. let calendar = NCShareCommon.shared.openCalendar(view: self, width: width, height: height)
  241. calendar.calendarView.delegate = self
  242. self.calendar = calendar.calendarView
  243. viewWindowCalendar = calendar.viewWindow
  244. let tap = UITapGestureRecognizer(target: self, action: #selector(tapViewWindowCalendar))
  245. tap.delegate = self
  246. viewWindowCalendar?.addGestureRecognizer(tap)
  247. }
  248. // Note to recipient
  249. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  250. guard let tableShare = self.tableShare else { return }
  251. if fieldNoteToRecipient.text == nil { return }
  252. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: fieldNoteToRecipient.text, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  253. }
  254. // Unshare
  255. @IBAction func buttonUnshare(sender: UIButton) {
  256. guard let tableShare = self.tableShare else { return }
  257. networking?.unShare(idShare: tableShare.idShare)
  258. }
  259. // MARK: - Delegate networking
  260. func readShareCompleted() {
  261. reloadData(idShare: tableShare?.idShare ?? 0)
  262. }
  263. func shareCompleted() {
  264. unLoad()
  265. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  266. }
  267. func unShareCompleted() {
  268. unLoad()
  269. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  270. }
  271. func updateShareWithError(idShare: Int) {
  272. reloadData(idShare: idShare)
  273. }
  274. func getSharees(sharees: [NCCommunicationSharee]?) { }
  275. // MARK: - Delegate calendar
  276. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  277. if monthPosition == .previous || monthPosition == .next {
  278. calendar.setCurrentPage(date, animated: true)
  279. } else {
  280. let dateFormatter = DateFormatter()
  281. dateFormatter.formatterBehavior = .behavior10_4
  282. dateFormatter.dateStyle = .medium
  283. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  284. fieldSetExpirationDate.endEditing(true)
  285. viewWindowCalendar?.removeFromSuperview()
  286. guard let tableShare = self.tableShare else { return }
  287. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  288. let expirationDate = dateFormatter.string(from: date)
  289. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: expirationDate, hideDownload: tableShare.hideDownload)
  290. }
  291. }
  292. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  293. return date > Date()
  294. }
  295. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  296. if date > Date() {
  297. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  298. } else {
  299. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  300. }
  301. }
  302. }