UIViewController+Menu.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // UIViewController+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 10.11.21.
  6. // Copyright © 2021 Henrik Storch All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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. //
  23. import Foundation
  24. import SVGKit
  25. import NCCommunication
  26. extension UIViewController {
  27. fileprivate func handleProfileAction(_ action: NCHovercard.Action, for userId: String) {
  28. switch action.appId {
  29. case "email":
  30. guard
  31. let url = action.hyperlinkUrl,
  32. url.scheme == "mailto",
  33. let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
  34. else {
  35. NCContentPresenter.shared.showGenericError(description: "_cannot_send_mail_error_")
  36. return
  37. }
  38. sendEmail(to: components.path)
  39. case "spreed":
  40. guard
  41. let appDelegate = UIApplication.shared.delegate as? AppDelegate,
  42. let talkUrl = URL(string: "nextcloudtalk://open-conversation?server=\(appDelegate.urlBase)&user=\(appDelegate.userId)&withUser=\(userId)"),
  43. UIApplication.shared.canOpenURL(talkUrl)
  44. else { fallthrough /* default: open web link in browser */ }
  45. UIApplication.shared.open(talkUrl)
  46. default:
  47. guard let url = action.hyperlinkUrl, UIApplication.shared.canOpenURL(url) else {
  48. NCContentPresenter.shared.showGenericError(description: "_open_url_error_")
  49. return
  50. }
  51. UIApplication.shared.open(url, options: [:])
  52. }
  53. }
  54. func showProfileMenu(userId: String) {
  55. guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
  56. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  57. guard serverVersionMajor >= NCGlobal.shared.nextcloudVersion23 else { return }
  58. NCCommunication.shared.getHovercard(for: userId) { (card, errCode, err) in
  59. guard let card = card else { return }
  60. let personHeader = NCMenuAction(
  61. title: card.displayName,
  62. icon: NCUtility.shared.loadUserImage(for: userId, displayName: card.displayName, urlBase: appDelegate.urlBase),
  63. action: nil)
  64. let actions = card.actions.map { action -> NCMenuAction in
  65. var image = UIImage()
  66. if let url = URL(string: action.icon),
  67. let svgSource = SVGKSourceURL.source(from: url),
  68. let svg = SVGKImage(source: svgSource) {
  69. image = svg.uiImage
  70. }
  71. return NCMenuAction(
  72. title: action.title,
  73. icon: image,
  74. action: { _ in self.handleProfileAction(action, for: userId) })
  75. }
  76. let allActions = [personHeader] + actions
  77. self.presentMenu(with: allActions)
  78. }
  79. }
  80. func sendEmail(to email: String) {
  81. guard MFMailComposeViewController.canSendMail() else {
  82. NCContentPresenter.shared.showGenericError(description: "_cannot_send_mail_error_")
  83. return
  84. }
  85. let mail = MFMailComposeViewController()
  86. mail.mailComposeDelegate = self
  87. mail.setToRecipients([email])
  88. present(mail, animated: true)
  89. }
  90. func presentMenu(with actions: [NCMenuAction]) {
  91. let menuViewController = NCMenu.makeNCMenu(with: actions)
  92. let menuPanelController = NCMenuPanelController()
  93. menuPanelController.parentPresenter = self
  94. menuPanelController.delegate = menuViewController
  95. menuPanelController.set(contentViewController: menuViewController)
  96. menuPanelController.track(scrollView: menuViewController.tableView)
  97. present(menuPanelController, animated: true, completion: nil)
  98. }
  99. }
  100. extension UIViewController: MFMailComposeViewControllerDelegate {
  101. public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  102. controller.dismiss(animated: true)
  103. }
  104. }