UIViewController+Menu.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. import UIKit
  27. extension UIViewController {
  28. fileprivate func handleProfileAction(_ action: NCHovercard.Action, for userId: String) {
  29. switch action.appId {
  30. case "email":
  31. guard
  32. let url = action.hyperlinkUrl,
  33. url.scheme == "mailto",
  34. let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
  35. else {
  36. NCContentPresenter.shared.showError(description: "_cannot_send_mail_error_")
  37. return
  38. }
  39. sendEmail(to: components.path)
  40. case "spreed":
  41. guard
  42. let appDelegate = UIApplication.shared.delegate as? AppDelegate,
  43. let talkUrl = URL(string: "nextcloudtalk://open-conversation?server=\(appDelegate.urlBase)&user=\(appDelegate.userId)&withUser=\(userId)"),
  44. UIApplication.shared.canOpenURL(talkUrl)
  45. else { fallthrough /* default: open web link in browser */ }
  46. UIApplication.shared.open(talkUrl)
  47. default:
  48. guard let url = action.hyperlinkUrl, UIApplication.shared.canOpenURL(url) else {
  49. NCContentPresenter.shared.showError(description: "_open_url_error_")
  50. return
  51. }
  52. UIApplication.shared.open(url, options: [:])
  53. }
  54. }
  55. func showProfileMenu(userId: String) {
  56. guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
  57. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  58. guard serverVersionMajor >= NCGlobal.shared.nextcloudVersion23 else { return }
  59. NCCommunication.shared.getHovercard(for: userId) { card, _, _ in
  60. guard let card = card else { return }
  61. let personHeader = NCMenuAction(
  62. title: card.displayName,
  63. icon: NCUtility.shared.loadUserImage(
  64. for: userId,
  65. displayName: card.displayName,
  66. userBaseUrl: appDelegate),
  67. action: nil)
  68. let actions = card.actions.map { action -> NCMenuAction in
  69. var image = NCUtility.shared.loadImage(named: "user", color: NCBrandColor.shared.label)
  70. if let url = URL(string: action.icon),
  71. let svgSource = SVGKSourceURL.source(from: url),
  72. let svg = SVGKImage(source: svgSource) {
  73. image = svg.uiImage.imageColor(NCBrandColor.shared.label)
  74. }
  75. return NCMenuAction(
  76. title: action.title,
  77. icon: image,
  78. action: { _ in self.handleProfileAction(action, for: userId) })
  79. }
  80. let allActions = [personHeader] + actions
  81. self.presentMenu(with: allActions)
  82. }
  83. }
  84. func sendEmail(to email: String) {
  85. guard MFMailComposeViewController.canSendMail() else {
  86. NCContentPresenter.shared.showError(description: "_cannot_send_mail_error_")
  87. return
  88. }
  89. let mail = MFMailComposeViewController()
  90. mail.mailComposeDelegate = self
  91. mail.setToRecipients([email])
  92. present(mail, animated: true)
  93. }
  94. func presentMenu(with actions: [NCMenuAction]) {
  95. guard !actions.isEmpty else { return }
  96. guard let menuViewController = NCMenu.makeNCMenu(with: actions) else {
  97. NCContentPresenter.shared.showError(description: "_internal_generic_error_")
  98. return
  99. }
  100. let menuPanelController = NCMenuPanelController()
  101. menuPanelController.parentPresenter = self
  102. menuPanelController.delegate = menuViewController
  103. menuPanelController.set(contentViewController: menuViewController)
  104. menuPanelController.track(scrollView: menuViewController.tableView)
  105. present(menuPanelController, animated: true, completion: nil)
  106. }
  107. }
  108. extension UIViewController: MFMailComposeViewControllerDelegate {
  109. public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  110. controller.dismiss(animated: true)
  111. }
  112. }