NCShareCommon.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. class NCShareCommon: NSObject {
  25. @objc static let sharedInstance: NCShareCommon = {
  26. let instance = NCShareCommon()
  27. return instance
  28. }()
  29. func createLinkAvatar() -> UIImage? {
  30. let size: CGFloat = 200
  31. let bottomImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "circle"), width: size, height: size, color: NCBrandColor.sharedInstance.brand)
  32. let topImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: size, height: size, color: UIColor.white)
  33. UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, 0.0)
  34. bottomImage?.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: size, height: size)))
  35. topImage?.draw(in: CGRect(origin: CGPoint(x: size/4, y: size/4), size: CGSize(width: size/2, height: size/2)))
  36. let image = UIGraphicsGetImageFromCurrentImageContext()
  37. UIGraphicsEndImageContext()
  38. return image
  39. }
  40. func downloadAvatar(user: String, cell: NCShareUserCell) {
  41. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  42. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + user + ".png"
  43. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  44. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  45. cell.imageItem.image = image
  46. }
  47. } else {
  48. DispatchQueue.global().async {
  49. let url = appDelegate.activeUrl + k_avatar + user + "/128"
  50. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  51. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  52. if errorCode == 0 {
  53. do {
  54. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  55. } catch { return }
  56. cell.imageItem.image = UIImage(data: data!)
  57. }
  58. })
  59. }
  60. }
  61. }
  62. func openViewMenuShareLink(view: UIView, tableShare: tableShare?, metadata: tableMetadata) -> (shareLinkMenuView: NCShareLinkMenuView, viewWindow: UIView) {
  63. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  64. let window = UIApplication.shared.keyWindow!
  65. let viewWindow = UIView(frame: window.bounds)
  66. window.addSubview(viewWindow)
  67. let shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as! NCShareLinkMenuView
  68. shareLinkMenuView.metadata = metadata
  69. shareLinkMenuView.viewWindow = viewWindow
  70. shareLinkMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  71. let shareLinkMenuViewX = view.bounds.width/2 - shareLinkMenuView.frame.width/2 + globalPoint!.x
  72. let shareLinkMenuViewY = globalPoint!.y + 10
  73. shareLinkMenuView.frame = CGRect(x: shareLinkMenuViewX, y: shareLinkMenuViewY, width: shareLinkMenuView.width, height: shareLinkMenuView.height)
  74. viewWindow.addSubview(shareLinkMenuView)
  75. return(shareLinkMenuView: shareLinkMenuView, viewWindow: viewWindow)
  76. }
  77. func openViewMenuUser(view: UIView, tableShare: tableShare?, metadata: tableMetadata) -> (shareUserMenuView: NCShareUserMenuView, viewWindow: UIView) {
  78. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  79. let window = UIApplication.shared.keyWindow!
  80. let viewWindow = UIView(frame: window.bounds)
  81. window.addSubview(viewWindow)
  82. let shareUserMenuView = Bundle.main.loadNibNamed("NCShareUserMenuView", owner: self, options: nil)?.first as! NCShareUserMenuView
  83. shareUserMenuView.metadata = metadata
  84. shareUserMenuView.viewWindow = viewWindow
  85. shareUserMenuView.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  86. let shareUserMenuViewX = view.bounds.width/2 - shareUserMenuView.frame.width/2 + globalPoint!.x
  87. let shareUserMenuViewY = globalPoint!.y + 100
  88. shareUserMenuView.frame = CGRect(x: shareUserMenuViewX, y: shareUserMenuViewY, width: shareUserMenuView.width, height: shareUserMenuView.height)
  89. viewWindow.addSubview(shareUserMenuView)
  90. return(shareUserMenuView: shareUserMenuView, viewWindow: viewWindow)
  91. }
  92. func openCalendar(view: UIView, width: CGFloat, height: CGFloat) -> (calendarView: FSCalendar, viewWindow: UIView) {
  93. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  94. let window = UIApplication.shared.keyWindow!
  95. let viewWindow = UIView(frame: window.bounds)
  96. window.addSubview(viewWindow)
  97. let calendar = FSCalendar(frame: CGRect(x: globalPoint!.x + 10, y: globalPoint!.y + 100, width: width - 20, height: 300))
  98. calendar.backgroundColor = .white
  99. calendar.placeholderType = .none
  100. calendar.appearance.headerMinimumDissolvedAlpha = 0.0
  101. calendar.layer.borderColor = UIColor.lightGray.cgColor
  102. calendar.layer.borderWidth = 0.5
  103. calendar.layer.masksToBounds = false
  104. calendar.layer.cornerRadius = 5
  105. calendar.layer.masksToBounds = false
  106. calendar.layer.shadowOffset = CGSize(width: 2, height: 2)
  107. calendar.layer.shadowOpacity = 0.2
  108. calendar.appearance.headerTitleColor = .black
  109. calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 13)
  110. calendar.appearance.weekdayTextColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
  111. calendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
  112. calendar.appearance.todayColor = NCBrandColor.sharedInstance.brand
  113. calendar.appearance.titleFont = UIFont.systemFont(ofSize: 12)
  114. viewWindow.addSubview(calendar)
  115. return(calendarView: calendar, viewWindow: viewWindow)
  116. }
  117. func copyLink(tableShare: tableShare?, viewController: UIViewController) {
  118. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  119. var url: String = ""
  120. guard let tableShare = tableShare else { return }
  121. if tableShare.token.hasPrefix("http://") || tableShare.token.hasPrefix("https://") {
  122. url = tableShare.token
  123. } else if tableShare.url != "" {
  124. url = tableShare.url
  125. } else {
  126. url = appDelegate.activeUrl + "/" + k_share_link_middle_part_url_after_version_8 + tableShare.token
  127. }
  128. if let name = URL(string: url), !name.absoluteString.isEmpty {
  129. let objectsToShare = [name]
  130. let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
  131. viewController.present(activityVC, animated: true, completion: nil)
  132. }
  133. }
  134. }