NCShareCommon.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Foundation
  23. import FSCalendar
  24. import DropDown
  25. class NCShareCommon: NSObject {
  26. @objc static let sharedInstance: NCShareCommon = {
  27. let instance = NCShareCommon()
  28. return instance
  29. }()
  30. func createLinkAvatar() -> UIImage? {
  31. let size: CGFloat = 200
  32. let bottomImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "circle"), width: size, height: size, color: NCBrandColor.sharedInstance.brand)
  33. let topImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: size, height: size, color: UIColor.white)
  34. UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, 0.0)
  35. bottomImage?.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: size, height: size)))
  36. topImage?.draw(in: CGRect(origin: CGPoint(x: size/4, y: size/4), size: CGSize(width: size/2, height: size/2)))
  37. let image = UIGraphicsGetImageFromCurrentImageContext()
  38. UIGraphicsEndImageContext()
  39. return image
  40. }
  41. func openViewMenuShareLink(shareViewController: NCShare, tableShare: tableShare?, metadata: tableMetadata) -> (shareLinkMenuView: NCShareLinkMenuView, viewWindow: UIView) {
  42. var shareLinkMenuView: NCShareLinkMenuView
  43. let window = UIApplication.shared.keyWindow!
  44. let viewWindow = UIView(frame: window.bounds)
  45. // let globalPoint = shareViewController.view.superview?.convert(shareViewController.view.frame.origin, to: nil)
  46. // let constantTrailingAnchor = window.bounds.width - shareViewController.view.bounds.width - globalPoint!.x + 40
  47. // var constantBottomAnchor: CGFloat = 10
  48. // constantBottomAnchor = constantBottomAnchor + UIApplication.shared.keyWindow!.safeAreaInsets.bottom
  49. window.addSubview(viewWindow)
  50. viewWindow.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  51. if metadata.directory {
  52. shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkFolderMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  53. } else {
  54. shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  55. }
  56. shareLinkMenuView.width = 250
  57. if metadata.directory {
  58. shareLinkMenuView.height = 540
  59. } else {
  60. shareLinkMenuView.height = 440
  61. }
  62. shareLinkMenuView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  63. shareLinkMenuView.metadata = metadata
  64. shareLinkMenuView.viewWindow = viewWindow
  65. shareLinkMenuView.shareViewController = shareViewController
  66. shareLinkMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  67. shareLinkMenuView.translatesAutoresizingMaskIntoConstraints = false
  68. viewWindow.addSubview(shareLinkMenuView)
  69. NSLayoutConstraint.activate([
  70. shareLinkMenuView.widthAnchor.constraint(equalToConstant: shareLinkMenuView.width),
  71. shareLinkMenuView.heightAnchor.constraint(equalToConstant: shareLinkMenuView.height),
  72. shareLinkMenuView.centerXAnchor.constraint(equalTo: viewWindow.centerXAnchor),
  73. shareLinkMenuView.centerYAnchor.constraint(equalTo: viewWindow.centerYAnchor),
  74. ])
  75. return(shareLinkMenuView: shareLinkMenuView, viewWindow: viewWindow)
  76. }
  77. func openViewMenuUser(shareViewController: NCShare, tableShare: tableShare?, metadata: tableMetadata) -> (shareUserMenuView: NCShareUserMenuView, viewWindow: UIView) {
  78. var shareUserMenuView: NCShareUserMenuView
  79. let window = UIApplication.shared.keyWindow!
  80. let viewWindow = UIView(frame: window.bounds)
  81. // let globalPoint = shareViewController.view.superview?.convert(shareViewController.view.frame.origin, to: nil)
  82. // let constantTrailingAnchor = window.bounds.width - shareViewController.view.bounds.width - globalPoint!.x + 40
  83. // var constantBottomAnchor: CGFloat = 10
  84. // constantBottomAnchor = constantBottomAnchor + UIApplication.shared.keyWindow!.safeAreaInsets.bottom
  85. window.addSubview(viewWindow)
  86. viewWindow.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  87. if metadata.directory {
  88. shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserFolderMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  89. } else {
  90. shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  91. }
  92. shareUserMenuView.width = 250
  93. if metadata.directory {
  94. shareUserMenuView.height = 410
  95. } else {
  96. shareUserMenuView.height = 260
  97. }
  98. shareUserMenuView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  99. shareUserMenuView.metadata = metadata
  100. shareUserMenuView.viewWindow = viewWindow
  101. shareUserMenuView.shareViewController = shareViewController
  102. shareUserMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  103. shareUserMenuView.translatesAutoresizingMaskIntoConstraints = false
  104. viewWindow.addSubview(shareUserMenuView)
  105. NSLayoutConstraint.activate([
  106. shareUserMenuView.widthAnchor.constraint(equalToConstant: shareUserMenuView.width),
  107. shareUserMenuView.heightAnchor.constraint(equalToConstant: shareUserMenuView.height),
  108. shareUserMenuView.centerXAnchor.constraint(equalTo: viewWindow.centerXAnchor),
  109. shareUserMenuView.centerYAnchor.constraint(equalTo: viewWindow.centerYAnchor),
  110. ])
  111. return(shareUserMenuView: shareUserMenuView, viewWindow: viewWindow)
  112. }
  113. func openCalendar(view: UIView, width: CGFloat, height: CGFloat) -> (calendarView: FSCalendar, viewWindow: UIView) {
  114. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  115. let window = UIApplication.shared.keyWindow!
  116. let viewWindow = UIView(frame: window.bounds)
  117. window.addSubview(viewWindow)
  118. let calendar = FSCalendar(frame: CGRect(x: globalPoint!.x + 10, y: globalPoint!.y + 10, width: width - 20, height: 300))
  119. if #available(iOS 13.0, *) {
  120. calendar.backgroundColor = .systemBackground
  121. calendar.appearance.headerTitleColor = .label
  122. } else {
  123. calendar.backgroundColor = .white
  124. calendar.appearance.headerTitleColor = .black
  125. }
  126. calendar.placeholderType = .none
  127. calendar.appearance.headerMinimumDissolvedAlpha = 0.0
  128. calendar.layer.borderColor = UIColor.lightGray.cgColor
  129. calendar.layer.borderWidth = 0.5
  130. calendar.layer.masksToBounds = false
  131. calendar.layer.cornerRadius = 5
  132. calendar.layer.masksToBounds = false
  133. calendar.layer.shadowOffset = CGSize(width: 2, height: 2)
  134. calendar.layer.shadowOpacity = 0.2
  135. calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 13)
  136. calendar.appearance.weekdayTextColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
  137. calendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
  138. calendar.appearance.todayColor = NCBrandColor.sharedInstance.brand
  139. calendar.appearance.titleFont = UIFont.systemFont(ofSize: 12)
  140. viewWindow.addSubview(calendar)
  141. return(calendarView: calendar, viewWindow: viewWindow)
  142. }
  143. func copyLink(tableShare: tableShare?, viewController: UIViewController, sender: Any) {
  144. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  145. var url: String = ""
  146. guard let tableShare = tableShare else { return }
  147. if tableShare.token.hasPrefix("http://") || tableShare.token.hasPrefix("https://") {
  148. url = tableShare.token
  149. } else if tableShare.url != "" {
  150. url = tableShare.url
  151. } else {
  152. url = appDelegate.activeUrl + "/" + k_share_link_middle_part_url_after_version_8 + tableShare.token
  153. }
  154. if let name = URL(string: url), !name.absoluteString.isEmpty {
  155. let objectsToShare = [name]
  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. }