NCShareCommon.swift 9.2 KB

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