NCShareCommon.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 downloadAvatar(user: String, cell: NCShareUserCell) {
  42. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  43. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + user + ".png"
  44. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  45. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  46. cell.imageItem.image = image
  47. }
  48. } else {
  49. DispatchQueue.global().async {
  50. let url = appDelegate.activeUrl + k_avatar + user + "/128"
  51. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  52. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  53. if errorCode == 0 {
  54. do {
  55. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  56. } catch { return }
  57. cell.imageItem.image = UIImage(data: data!)
  58. }
  59. })
  60. }
  61. }
  62. }
  63. func downloadAvatar(user: String, cell: NCShareUserDropDownCell) {
  64. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  65. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + user + ".png"
  66. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  67. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  68. cell.imageItem.image = image
  69. }
  70. } else {
  71. DispatchQueue.global().async {
  72. let url = appDelegate.activeUrl + k_avatar + user + "/128"
  73. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  74. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  75. if errorCode == 0 {
  76. do {
  77. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  78. } catch { return }
  79. cell.imageItem.image = UIImage(data: data!)
  80. }
  81. })
  82. }
  83. }
  84. }
  85. func openViewMenuShareLink(view: UIView, tableShare: tableShare?, metadata: tableMetadata) -> (shareLinkMenuView: NCShareLinkMenuView, viewWindow: UIView) {
  86. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  87. let window = UIApplication.shared.keyWindow!
  88. let viewWindow = UIView(frame: window.bounds)
  89. window.addSubview(viewWindow)
  90. let shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  91. shareLinkMenuView.metadata = metadata
  92. shareLinkMenuView.viewWindow = viewWindow
  93. shareLinkMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  94. let shareLinkMenuViewX = view.bounds.width/2 - shareLinkMenuView.frame.width/2 + globalPoint!.x
  95. let shareLinkMenuViewY = globalPoint!.y + 10
  96. shareLinkMenuView.frame = CGRect(x: shareLinkMenuViewX, y: shareLinkMenuViewY, width: shareLinkMenuView.width, height: shareLinkMenuView.height)
  97. viewWindow.addSubview(shareLinkMenuView)
  98. return(shareLinkMenuView: shareLinkMenuView, viewWindow: viewWindow)
  99. }
  100. func openViewMenuUser(view: UIView, tableShare: tableShare?, metadata: tableMetadata) -> (shareUserMenuView: NCShareUserMenuView, viewWindow: UIView) {
  101. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  102. let window = UIApplication.shared.keyWindow!
  103. let viewWindow = UIView(frame: window.bounds)
  104. window.addSubview(viewWindow)
  105. let shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  106. shareUserMenuView.metadata = metadata
  107. shareUserMenuView.viewWindow = viewWindow
  108. shareUserMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  109. let shareUserMenuViewX = view.bounds.width/2 - shareUserMenuView.frame.width/2 + globalPoint!.x
  110. let shareUserMenuViewY = globalPoint!.y + 100
  111. shareUserMenuView.frame = CGRect(x: shareUserMenuViewX, y: shareUserMenuViewY, width: shareUserMenuView.width, height: shareUserMenuView.height)
  112. viewWindow.addSubview(shareUserMenuView)
  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 + 100, 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. }