NCShareCommon.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // if #available(iOS 11.0, *) {
  49. // constantBottomAnchor = constantBottomAnchor + UIApplication.shared.keyWindow!.safeAreaInsets.bottom
  50. // }
  51. window.addSubview(viewWindow)
  52. viewWindow.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  53. if metadata.directory {
  54. shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkFolderMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  55. } else {
  56. shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  57. }
  58. shareLinkMenuView.width = 250
  59. if metadata.directory {
  60. shareLinkMenuView.height = 540
  61. } else {
  62. shareLinkMenuView.height = 440
  63. }
  64. shareLinkMenuView.metadata = metadata
  65. shareLinkMenuView.viewWindow = viewWindow
  66. shareLinkMenuView.shareViewController = shareViewController
  67. shareLinkMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  68. shareLinkMenuView.translatesAutoresizingMaskIntoConstraints = false
  69. viewWindow.addSubview(shareLinkMenuView)
  70. NSLayoutConstraint.activate([
  71. shareLinkMenuView.widthAnchor.constraint(equalToConstant: shareLinkMenuView.width),
  72. shareLinkMenuView.heightAnchor.constraint(equalToConstant: shareLinkMenuView.height),
  73. shareLinkMenuView.centerXAnchor.constraint(equalTo: viewWindow.centerXAnchor),
  74. shareLinkMenuView.centerYAnchor.constraint(equalTo: viewWindow.centerYAnchor),
  75. ])
  76. return(shareLinkMenuView: shareLinkMenuView, viewWindow: viewWindow)
  77. }
  78. func openViewMenuUser(shareViewController: NCShare, tableShare: tableShare?, metadata: tableMetadata) -> (shareUserMenuView: NCShareUserMenuView, viewWindow: UIView) {
  79. var shareUserMenuView: NCShareUserMenuView
  80. let window = UIApplication.shared.keyWindow!
  81. let viewWindow = UIView(frame: window.bounds)
  82. // let globalPoint = shareViewController.view.superview?.convert(shareViewController.view.frame.origin, to: nil)
  83. // let constantTrailingAnchor = window.bounds.width - shareViewController.view.bounds.width - globalPoint!.x + 40
  84. // var constantBottomAnchor: CGFloat = 10
  85. // if #available(iOS 11.0, *) {
  86. // constantBottomAnchor = constantBottomAnchor + UIApplication.shared.keyWindow!.safeAreaInsets.bottom
  87. // }
  88. window.addSubview(viewWindow)
  89. viewWindow.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  90. if metadata.directory {
  91. shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserFolderMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  92. } else {
  93. shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  94. }
  95. shareUserMenuView.width = 250
  96. if metadata.directory {
  97. shareUserMenuView.height = 410
  98. } else {
  99. shareUserMenuView.height = 260
  100. }
  101. shareUserMenuView.metadata = metadata
  102. shareUserMenuView.viewWindow = viewWindow
  103. shareUserMenuView.shareViewController = shareViewController
  104. shareUserMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  105. shareUserMenuView.translatesAutoresizingMaskIntoConstraints = false
  106. viewWindow.addSubview(shareUserMenuView)
  107. NSLayoutConstraint.activate([
  108. shareUserMenuView.widthAnchor.constraint(equalToConstant: shareUserMenuView.width),
  109. shareUserMenuView.heightAnchor.constraint(equalToConstant: shareUserMenuView.height),
  110. shareUserMenuView.centerXAnchor.constraint(equalTo: viewWindow.centerXAnchor),
  111. shareUserMenuView.centerYAnchor.constraint(equalTo: viewWindow.centerYAnchor),
  112. ])
  113. return(shareUserMenuView: shareUserMenuView, viewWindow: viewWindow)
  114. }
  115. func openCalendar(view: UIView, width: CGFloat, height: CGFloat) -> (calendarView: FSCalendar, viewWindow: UIView) {
  116. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  117. let window = UIApplication.shared.keyWindow!
  118. let viewWindow = UIView(frame: window.bounds)
  119. window.addSubview(viewWindow)
  120. let calendar = FSCalendar(frame: CGRect(x: globalPoint!.x + 10, y: globalPoint!.y + 10, width: width - 20, height: 300))
  121. calendar.backgroundColor = .white
  122. calendar.placeholderType = .none
  123. calendar.appearance.headerMinimumDissolvedAlpha = 0.0
  124. calendar.layer.borderColor = UIColor.lightGray.cgColor
  125. calendar.layer.borderWidth = 0.5
  126. calendar.layer.masksToBounds = false
  127. calendar.layer.cornerRadius = 5
  128. calendar.layer.masksToBounds = false
  129. calendar.layer.shadowOffset = CGSize(width: 2, height: 2)
  130. calendar.layer.shadowOpacity = 0.2
  131. calendar.appearance.headerTitleColor = .black
  132. calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 13)
  133. calendar.appearance.weekdayTextColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
  134. calendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
  135. calendar.appearance.todayColor = NCBrandColor.sharedInstance.brand
  136. calendar.appearance.titleFont = UIFont.systemFont(ofSize: 12)
  137. viewWindow.addSubview(calendar)
  138. return(calendarView: calendar, viewWindow: viewWindow)
  139. }
  140. func copyLink(tableShare: tableShare?, viewController: UIViewController) {
  141. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  142. var url: String = ""
  143. guard let tableShare = tableShare else { return }
  144. if tableShare.token.hasPrefix("http://") || tableShare.token.hasPrefix("https://") {
  145. url = tableShare.token
  146. } else if tableShare.url != "" {
  147. url = tableShare.url
  148. } else {
  149. url = appDelegate.activeUrl + "/" + k_share_link_middle_part_url_after_version_8 + tableShare.token
  150. }
  151. if let name = URL(string: url), !name.absoluteString.isEmpty {
  152. let objectsToShare = [name]
  153. let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
  154. viewController.present(activityVC, animated: true, completion: nil)
  155. }
  156. }
  157. }