NCShareCommon.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 && UIImage(data: data!) != nil {
  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. } else {
  59. cell.imageItem.image = UIImage(named: "avatar")
  60. }
  61. })
  62. }
  63. }
  64. }
  65. func downloadAvatar(user: String, cell: NCShareUserDropDownCell) {
  66. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  67. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + user + ".png"
  68. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  69. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  70. cell.imageItem.image = image
  71. }
  72. } else {
  73. DispatchQueue.global().async {
  74. let url = appDelegate.activeUrl + k_avatar + user + "/128"
  75. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  76. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  77. if errorCode == 0 && UIImage(data: data!) != nil {
  78. do {
  79. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  80. } catch { return }
  81. cell.imageItem.image = UIImage(data: data!)
  82. } else {
  83. cell.imageItem.image = UIImage(named: "avatar")
  84. }
  85. })
  86. }
  87. }
  88. }
  89. func openViewMenuShareLink(view: UIView, tableShare: tableShare?, metadata: tableMetadata) -> (shareLinkMenuView: NCShareLinkMenuView, viewWindow: UIView) {
  90. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  91. let window = UIApplication.shared.keyWindow!
  92. let viewWindow = UIView(frame: window.bounds)
  93. window.addSubview(viewWindow)
  94. let shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  95. shareLinkMenuView.metadata = metadata
  96. shareLinkMenuView.viewWindow = viewWindow
  97. shareLinkMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  98. let shareLinkMenuViewX = view.bounds.width/2 - shareLinkMenuView.frame.width/2 + globalPoint!.x
  99. let shareLinkMenuViewY = globalPoint!.y + 10
  100. shareLinkMenuView.frame = CGRect(x: shareLinkMenuViewX, y: shareLinkMenuViewY, width: shareLinkMenuView.width, height: shareLinkMenuView.height)
  101. viewWindow.addSubview(shareLinkMenuView)
  102. return(shareLinkMenuView: shareLinkMenuView, viewWindow: viewWindow)
  103. }
  104. func openViewMenuUser(view: UIView, tableShare: tableShare?, metadata: tableMetadata) -> (shareUserMenuView: NCShareUserMenuView, viewWindow: UIView) {
  105. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  106. let window = UIApplication.shared.keyWindow!
  107. let viewWindow = UIView(frame: window.bounds)
  108. window.addSubview(viewWindow)
  109. let shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  110. shareUserMenuView.metadata = metadata
  111. shareUserMenuView.viewWindow = viewWindow
  112. shareUserMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  113. let shareUserMenuViewX = view.bounds.width/2 - shareUserMenuView.frame.width/2 + globalPoint!.x
  114. let shareUserMenuViewY = globalPoint!.y + 100
  115. shareUserMenuView.frame = CGRect(x: shareUserMenuViewX, y: shareUserMenuViewY, width: shareUserMenuView.width, height: shareUserMenuView.height)
  116. viewWindow.addSubview(shareUserMenuView)
  117. return(shareUserMenuView: shareUserMenuView, viewWindow: viewWindow)
  118. }
  119. func openCalendar(view: UIView, width: CGFloat, height: CGFloat) -> (calendarView: FSCalendar, viewWindow: UIView) {
  120. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  121. let window = UIApplication.shared.keyWindow!
  122. let viewWindow = UIView(frame: window.bounds)
  123. window.addSubview(viewWindow)
  124. let calendar = FSCalendar(frame: CGRect(x: globalPoint!.x + 10, y: globalPoint!.y + 100, width: width - 20, height: 300))
  125. calendar.backgroundColor = .white
  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.headerTitleColor = .black
  136. calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 13)
  137. calendar.appearance.weekdayTextColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
  138. calendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
  139. calendar.appearance.todayColor = NCBrandColor.sharedInstance.brand
  140. calendar.appearance.titleFont = UIFont.systemFont(ofSize: 12)
  141. viewWindow.addSubview(calendar)
  142. return(calendarView: calendar, viewWindow: viewWindow)
  143. }
  144. func copyLink(tableShare: tableShare?, viewController: UIViewController) {
  145. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  146. var url: String = ""
  147. guard let tableShare = tableShare else { return }
  148. if tableShare.token.hasPrefix("http://") || tableShare.token.hasPrefix("https://") {
  149. url = tableShare.token
  150. } else if tableShare.url != "" {
  151. url = tableShare.url
  152. } else {
  153. url = appDelegate.activeUrl + "/" + k_share_link_middle_part_url_after_version_8 + tableShare.token
  154. }
  155. if let name = URL(string: url), !name.absoluteString.isEmpty {
  156. let objectsToShare = [name]
  157. let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
  158. viewController.present(activityVC, animated: true, completion: nil)
  159. }
  160. }
  161. }