NCShareLinkMenuView.swift 13 KB

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