UIViewController+Menu.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 UIKit
  25. import MessageUI
  26. import SVGKit
  27. import NextcloudKit
  28. extension UIViewController {
  29. fileprivate func handleProfileAction(_ action: NKHovercard.Action, for userId: String, session: NCSession.Session) {
  30. switch action.appId {
  31. case "email":
  32. guard
  33. let url = action.hyperlinkUrl,
  34. url.scheme == "mailto",
  35. let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
  36. else {
  37. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_cannot_send_mail_error_")
  38. NCContentPresenter().showError(error: error)
  39. return
  40. }
  41. sendEmail(to: components.path)
  42. case "spreed":
  43. guard
  44. let talkUrl = URL(string: "nextcloudtalk://open-conversation?server=\(session.urlBase)&user=\(session.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().showError(error: error)
  52. return
  53. }
  54. UIApplication.shared.open(url, options: [:])
  55. }
  56. }
  57. func showProfileMenu(userId: String, session: NCSession.Session) {
  58. guard NCCapabilities.shared.getCapabilities(account: session.account).capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion23 else { return }
  59. NextcloudKit.shared.getHovercard(for: userId, account: session.account) { account, card, _, _ in
  60. guard let card = card, account == session.account else { return }
  61. let personHeader = NCMenuAction(
  62. title: card.displayName,
  63. icon: NCUtility().loadUserImage(for: userId, displayName: card.displayName, urlBase: session.urlBase),
  64. action: nil)
  65. let actions = card.actions.map { action -> NCMenuAction in
  66. var image = NCUtility().loadImage(named: "user", colors: [NCBrandColor.shared.iconImageColor])
  67. if let url = URL(string: action.icon),
  68. let svgSource = SVGKSourceURL.source(from: url),
  69. let svg = SVGKImage(source: svgSource) {
  70. image = svg.uiImage.withTintColor(NCBrandColor.shared.iconImageColor, renderingMode: .alwaysOriginal)
  71. }
  72. return NCMenuAction(
  73. title: action.title,
  74. icon: image,
  75. action: { _ in self.handleProfileAction(action, for: userId, session: session) })
  76. }
  77. let allActions = [personHeader] + actions
  78. self.presentMenu(with: allActions)
  79. }
  80. }
  81. func sendEmail(to email: String) {
  82. guard MFMailComposeViewController.canSendMail() else {
  83. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_cannot_send_mail_error_")
  84. NCContentPresenter().showError(error: error)
  85. return
  86. }
  87. let mail = MFMailComposeViewController()
  88. mail.mailComposeDelegate = self
  89. mail.setToRecipients([email])
  90. present(mail, animated: true)
  91. }
  92. func presentMenu(with actions: [NCMenuAction], menuColor: UIColor = .systemBackground, textColor: UIColor = NCBrandColor.shared.textColor) {
  93. guard !actions.isEmpty else { return }
  94. let actions = actions.sorted(by: { $0.order < $1.order })
  95. guard let menuViewController = NCMenu.makeNCMenu(with: actions, menuColor: menuColor, textColor: textColor) else {
  96. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_internal_generic_error_")
  97. NCContentPresenter().showError(error: 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: @retroactive MFMailComposeViewControllerDelegate {
  109. public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  110. controller.dismiss(animated: true)
  111. }
  112. }