UIViewController+Menu.swift 5.6 KB

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