NCShareCommon.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // NCShareCommon.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 DropDown
  25. class NCShareCommon: NSObject {
  26. @objc static let shared: NCShareCommon = {
  27. let instance = NCShareCommon()
  28. return instance
  29. }()
  30. let SHARE_TYPE_USER = 0
  31. let SHARE_TYPE_GROUP = 1
  32. let SHARE_TYPE_LINK = 3
  33. let SHARE_TYPE_EMAIL = 4
  34. let SHARE_TYPE_CONTACT = 5
  35. let SHARE_TYPE_REMOTE = 6
  36. let SHARE_TYPE_CIRCLE = 7
  37. let SHARE_TYPE_GUEST = 8
  38. let SHARE_TYPE_REMOTE_GROUP = 9
  39. let SHARE_TYPE_ROOM = 10
  40. func createLinkAvatar(imageName: String, colorCircle: UIColor) -> UIImage? {
  41. let size: CGFloat = 200
  42. let bottomImage = UIImage.init(named: "circle.fill")!.image(color: colorCircle, size: size/2)
  43. let topImage = UIImage.init(named: imageName)!.image(color: .white, size: size/2)
  44. UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, UIScreen.main.scale)
  45. bottomImage.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: size, height: size)))
  46. topImage.draw(in: CGRect(origin: CGPoint(x: size/4, y: size/4), size: CGSize(width: size/2, height: size/2)))
  47. let image = UIGraphicsGetImageFromCurrentImageContext()
  48. UIGraphicsEndImageContext()
  49. return image
  50. }
  51. func openViewMenuShareLink(shareViewController: NCShare, tableShare: tableShare?, metadata: tableMetadata) -> (shareLinkMenuView: NCShareLinkMenuView, viewWindow: UIView) {
  52. var shareLinkMenuView: NCShareLinkMenuView
  53. let window = UIApplication.shared.keyWindow!
  54. let viewWindow = UIView(frame: window.bounds)
  55. // let globalPoint = shareViewController.view.superview?.convert(shareViewController.view.frame.origin, to: nil)
  56. // let constantTrailingAnchor = window.bounds.width - shareViewController.view.bounds.width - globalPoint!.x + 40
  57. // var constantBottomAnchor: CGFloat = 10
  58. // constantBottomAnchor = constantBottomAnchor + UIApplication.shared.keyWindow!.safeAreaInsets.bottom
  59. window.addSubview(viewWindow)
  60. viewWindow.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  61. if metadata.directory {
  62. shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkFolderMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  63. } else {
  64. shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  65. }
  66. shareLinkMenuView.width = 250
  67. if metadata.directory {
  68. shareLinkMenuView.height = 540
  69. } else {
  70. shareLinkMenuView.height = 440
  71. }
  72. shareLinkMenuView.backgroundColor = NCBrandColor.shared.systemBackground
  73. shareLinkMenuView.metadata = metadata
  74. shareLinkMenuView.viewWindow = viewWindow
  75. shareLinkMenuView.shareViewController = shareViewController
  76. shareLinkMenuView.reloadData(idShare: tableShare?.idShare ?? 0)
  77. shareLinkMenuView.translatesAutoresizingMaskIntoConstraints = false
  78. viewWindow.addSubview(shareLinkMenuView)
  79. NSLayoutConstraint.activate([
  80. shareLinkMenuView.widthAnchor.constraint(equalToConstant: shareLinkMenuView.width),
  81. shareLinkMenuView.heightAnchor.constraint(equalToConstant: shareLinkMenuView.height),
  82. shareLinkMenuView.centerXAnchor.constraint(equalTo: viewWindow.centerXAnchor),
  83. shareLinkMenuView.centerYAnchor.constraint(equalTo: viewWindow.centerYAnchor),
  84. ])
  85. return(shareLinkMenuView: shareLinkMenuView, viewWindow: viewWindow)
  86. }
  87. func openViewMenuUser(shareViewController: NCShare, tableShare: tableShare?, metadata: tableMetadata) -> (shareUserMenuView: NCShareUserMenuView, viewWindow: UIView) {
  88. var shareUserMenuView: NCShareUserMenuView
  89. let window = UIApplication.shared.keyWindow!
  90. let viewWindow = UIView(frame: window.bounds)
  91. // let globalPoint = shareViewController.view.superview?.convert(shareViewController.view.frame.origin, to: nil)
  92. // let constantTrailingAnchor = window.bounds.width - shareViewController.view.bounds.width - globalPoint!.x + 40
  93. // var constantBottomAnchor: CGFloat = 10
  94. // constantBottomAnchor = constantBottomAnchor + UIApplication.shared.keyWindow!.safeAreaInsets.bottom
  95. window.addSubview(viewWindow)
  96. viewWindow.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  97. if metadata.directory {
  98. shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserFolderMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  99. } else {
  100. shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  101. }
  102. shareUserMenuView.width = 250
  103. if metadata.directory {
  104. shareUserMenuView.height = 410
  105. } else {
  106. shareUserMenuView.height = 260
  107. }
  108. shareUserMenuView.backgroundColor = NCBrandColor.shared.systemBackground
  109. shareUserMenuView.metadata = metadata
  110. shareUserMenuView.viewWindow = viewWindow
  111. shareUserMenuView.shareViewController = shareViewController
  112. shareUserMenuView.reloadData(idShare: tableShare?.idShare ?? 0)
  113. shareUserMenuView.translatesAutoresizingMaskIntoConstraints = false
  114. viewWindow.addSubview(shareUserMenuView)
  115. NSLayoutConstraint.activate([
  116. shareUserMenuView.widthAnchor.constraint(equalToConstant: shareUserMenuView.width),
  117. shareUserMenuView.heightAnchor.constraint(equalToConstant: shareUserMenuView.height),
  118. shareUserMenuView.centerXAnchor.constraint(equalTo: viewWindow.centerXAnchor),
  119. shareUserMenuView.centerYAnchor.constraint(equalTo: viewWindow.centerYAnchor),
  120. ])
  121. return(shareUserMenuView: shareUserMenuView, viewWindow: viewWindow)
  122. }
  123. func openCalendar(view: UIView, width: CGFloat, height: CGFloat) -> (calendarView: FSCalendar, viewWindow: UIView) {
  124. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  125. let window = UIApplication.shared.keyWindow!
  126. let viewWindow = UIView(frame: window.bounds)
  127. window.addSubview(viewWindow)
  128. let calendar = FSCalendar(frame: CGRect(x: globalPoint!.x + 10, y: globalPoint!.y + 10, width: width - 20, height: 300))
  129. if #available(iOS 13.0, *) {
  130. calendar.appearance.headerTitleColor = .label
  131. } else {
  132. calendar.appearance.headerTitleColor = .black
  133. }
  134. calendar.backgroundColor = NCBrandColor.shared.systemBackground
  135. calendar.placeholderType = .none
  136. calendar.appearance.headerMinimumDissolvedAlpha = 0.0
  137. calendar.layer.borderColor = UIColor.lightGray.cgColor
  138. calendar.layer.borderWidth = 0.5
  139. calendar.layer.masksToBounds = false
  140. calendar.layer.cornerRadius = 5
  141. calendar.layer.masksToBounds = false
  142. calendar.layer.shadowOffset = CGSize(width: 2, height: 2)
  143. calendar.layer.shadowOpacity = 0.2
  144. calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 13)
  145. calendar.appearance.weekdayTextColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
  146. calendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
  147. calendar.appearance.todayColor = NCBrandColor.shared.brandElement
  148. calendar.appearance.titleFont = UIFont.systemFont(ofSize: 12)
  149. viewWindow.addSubview(calendar)
  150. return(calendarView: calendar, viewWindow: viewWindow)
  151. }
  152. func copyLink(link: String, viewController: UIViewController, sender: Any) {
  153. //guard let tableShare = tableShare else { return }
  154. //if let name = URL(string: tableShare.url), !name.absoluteString.isEmpty {
  155. let objectsToShare = [link]
  156. let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
  157. if UIDevice.current.userInterfaceIdiom == .pad {
  158. if activityViewController.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
  159. activityViewController.popoverPresentationController?.sourceView = sender as? UIView
  160. activityViewController.popoverPresentationController?.sourceRect = (sender as AnyObject).bounds
  161. }
  162. }
  163. viewController.present(activityViewController, animated: true, completion: nil)
  164. //}
  165. }
  166. func getImageShareType(shareType: Int) -> UIImage? {
  167. switch shareType {
  168. case SHARE_TYPE_USER:
  169. return UIImage(named: "shareTypeUser")?.imageColor(NCBrandColor.shared.label)
  170. case self.SHARE_TYPE_GROUP:
  171. return UIImage(named: "shareTypeGroup")?.imageColor(NCBrandColor.shared.label)
  172. case self.SHARE_TYPE_LINK:
  173. return UIImage(named: "shareTypeLink")?.imageColor(NCBrandColor.shared.label)
  174. case self.SHARE_TYPE_EMAIL:
  175. return UIImage(named: "shareTypeEmail")?.imageColor(NCBrandColor.shared.label)
  176. case self.SHARE_TYPE_CONTACT:
  177. return UIImage(named: "shareTypeUser")?.imageColor(NCBrandColor.shared.label)
  178. case self.SHARE_TYPE_REMOTE:
  179. return UIImage(named: "shareTypeUser")?.imageColor(NCBrandColor.shared.label)
  180. case self.SHARE_TYPE_CIRCLE:
  181. return UIImage(named: "shareTypeCircles")?.imageColor(NCBrandColor.shared.label)
  182. case self.SHARE_TYPE_GUEST:
  183. return UIImage(named: "shareTypeUser")?.imageColor(NCBrandColor.shared.label)
  184. case self.SHARE_TYPE_REMOTE_GROUP:
  185. return UIImage(named: "shareTypeGroup")?.imageColor(NCBrandColor.shared.label)
  186. case self.SHARE_TYPE_ROOM:
  187. return UIImage(named: "shareTypeRoom")?.imageColor(NCBrandColor.shared.label)
  188. default:
  189. return UIImage(named: "shareTypeUser")?.imageColor(NCBrandColor.shared.label)
  190. }
  191. }
  192. }