NCShareUserMenuView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 Foundation
  23. import FSCalendar
  24. class NCShareUserMenuView: UIView, UIGestureRecognizerDelegate, NCShareNetworkingDelegate, FSCalendarDelegate, FSCalendarDelegateAppearance {
  25. @IBOutlet weak var switchCanReshare: UISwitch!
  26. @IBOutlet weak var labelCanReshare: UILabel!
  27. @IBOutlet weak var switchSetExpirationDate: UISwitch!
  28. @IBOutlet weak var labelSetExpirationDate: UILabel!
  29. @IBOutlet weak var fieldSetExpirationDate: UITextField!
  30. @IBOutlet weak var imageNoteToRecipient: UIImageView!
  31. @IBOutlet weak var labelNoteToRecipient: UILabel!
  32. @IBOutlet weak var fieldNoteToRecipient: UITextField!
  33. @IBOutlet weak var buttonUnshare: UIButton!
  34. @IBOutlet weak var labelUnshare: UILabel!
  35. @IBOutlet weak var imageUnshare: UIImageView!
  36. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37. public let width: CGFloat = 250
  38. public let height: CGFloat = 260
  39. private var tableShare: tableShare?
  40. public var metadata: tableMetadata?
  41. public var viewWindow: UIView?
  42. public var viewWindowCalendar: UIView?
  43. override func awakeFromNib() {
  44. self.frame.size.width = width
  45. self.frame.size.height = height
  46. layer.borderColor = UIColor.lightGray.cgColor
  47. layer.borderWidth = 0.5
  48. layer.cornerRadius = 5
  49. layer.masksToBounds = false
  50. layer.shadowOffset = CGSize(width: 2, height: 2)
  51. layer.shadowOpacity = 0.2
  52. switchCanReshare.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  53. switchCanReshare.onTintColor = NCBrandColor.sharedInstance.brand
  54. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  55. switchSetExpirationDate.onTintColor = NCBrandColor.sharedInstance.brand
  56. fieldSetExpirationDate.inputView = UIView()
  57. 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))
  58. imageUnshare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 100, height: 100, color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1))
  59. }
  60. func unLoad() {
  61. viewWindowCalendar?.removeFromSuperview()
  62. viewWindow?.removeFromSuperview()
  63. viewWindowCalendar = nil
  64. viewWindow = nil
  65. }
  66. func reloadData(idRemoteShared: Int) {
  67. guard let metadata = self.metadata else { return }
  68. tableShare = NCManageDatabase.sharedInstance.getTableShare(account: metadata.account, idRemoteShared: idRemoteShared)
  69. guard let tableShare = self.tableShare else { return }
  70. // Can reshare
  71. let canReshare = UtilsFramework.isPermission(toCanShare: tableShare.permissions)
  72. if canReshare {
  73. switchCanReshare.setOn(true, animated: false)
  74. } else {
  75. switchCanReshare.setOn(false, animated: false)
  76. }
  77. // Set expiration date
  78. if tableShare.expirationDate != nil {
  79. switchSetExpirationDate.setOn(true, animated: false)
  80. fieldSetExpirationDate.isEnabled = true
  81. let dateFormatter = DateFormatter()
  82. dateFormatter.formatterBehavior = .behavior10_4
  83. dateFormatter.dateStyle = .medium
  84. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  85. } else {
  86. switchSetExpirationDate.setOn(false, animated: false)
  87. fieldSetExpirationDate.isEnabled = false
  88. fieldSetExpirationDate.text = ""
  89. }
  90. // Note to recipient
  91. fieldNoteToRecipient.text = tableShare.note
  92. }
  93. // MARK: - IBAction
  94. // Can reshare
  95. @IBAction func switchCanReshareChanged(sender: UISwitch) {
  96. guard let tableShare = self.tableShare else { return }
  97. guard let metadata = self.metadata else { return }
  98. let canEdit = UtilsFramework.isAnyPermission(toEdit: tableShare.permissions)
  99. var permission: Int = 0
  100. if sender.isOn {
  101. if canEdit {
  102. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: true, andIsFolder: metadata.directory)
  103. } else {
  104. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: true, andIsFolder: metadata.directory)
  105. }
  106. } else {
  107. if canEdit {
  108. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  109. } else {
  110. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  111. }
  112. }
  113. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  114. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  115. }
  116. // Set expiration date
  117. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  118. guard let tableShare = self.tableShare else { return }
  119. guard let metadata = self.metadata else { return }
  120. if sender.isOn {
  121. fieldSetExpirationDate.isEnabled = true
  122. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  123. } else {
  124. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  125. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: "", hideDownload: tableShare.hideDownload)
  126. }
  127. }
  128. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  129. let calendar = NCShareCommon.sharedInstance.openCalendar(view: self, width: width, height: height)
  130. calendar.calendarView.delegate = self
  131. viewWindowCalendar = calendar.viewWindow
  132. }
  133. // Note to recipient
  134. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  135. guard let tableShare = self.tableShare else { return }
  136. guard let metadata = self.metadata else { return }
  137. if fieldNoteToRecipient.text == nil { return }
  138. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  139. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: fieldNoteToRecipient.text, expirationTime: nil, hideDownload: tableShare.hideDownload)
  140. }
  141. // Unshare
  142. @IBAction func buttonUnshare(sender: UIButton) {
  143. guard let tableShare = self.tableShare else { return }
  144. guard let metadata = self.metadata else { return }
  145. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  146. networking.unShare(idRemoteShared: tableShare.idRemoteShared)
  147. }
  148. // MARK: - Delegate networking
  149. func readShareCompleted() {
  150. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  151. }
  152. func shareCompleted() {
  153. unLoad()
  154. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  155. }
  156. func unShareCompleted() {
  157. unLoad()
  158. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  159. }
  160. func updateShareWithError(idRemoteShared: Int) {
  161. reloadData(idRemoteShared: idRemoteShared)
  162. }
  163. func getUserAndGroup(items: [OCShareUser]?) { }
  164. // MARK: - Delegate calendar
  165. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  166. if monthPosition == .previous || monthPosition == .next {
  167. calendar.setCurrentPage(date, animated: true)
  168. } else {
  169. let dateFormatter = DateFormatter()
  170. dateFormatter.formatterBehavior = .behavior10_4
  171. dateFormatter.dateStyle = .medium
  172. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  173. fieldSetExpirationDate.endEditing(true)
  174. viewWindowCalendar?.removeFromSuperview()
  175. guard let tableShare = self.tableShare else { return }
  176. guard let metadata = self.metadata else { return }
  177. let networking = NCShareNetworking.init(account: metadata.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  178. dateFormatter.dateFormat = "YYYY-MM-dd"
  179. let expirationTime = dateFormatter.string(from: date)
  180. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: expirationTime, hideDownload: tableShare.hideDownload)
  181. }
  182. }
  183. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  184. return date > Date()
  185. }
  186. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  187. if date > Date() {
  188. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  189. } else {
  190. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  191. }
  192. }
  193. }