UIViewController+Menu.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.showGenericError(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.showGenericError(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, errCode, err) in
  60. guard let card = card else { return }
  61. let personHeader = NCMenuAction(
  62. title: card.displayName,
  63. icon: NCUtility.shared.loadUserImage(for: userId, displayName: card.displayName, urlBase: appDelegate.urlBase),
  64. action: nil)
  65. let actions = card.actions.map { action -> NCMenuAction in
  66. var image = NCUtility.shared.loadImage(named: "user", color: NCBrandColor.shared.label)
  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
  71. }
  72. return NCMenuAction(
  73. title: action.title,
  74. icon: image,
  75. action: { _ in self.handleProfileAction(action, for: userId) })
  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. NCContentPresenter.shared.showGenericError(description: "_cannot_send_mail_error_")
  84. return
  85. }
  86. let mail = MFMailComposeViewController()
  87. mail.mailComposeDelegate = self
  88. mail.setToRecipients([email])
  89. present(mail, animated: true)
  90. }
  91. func presentMenu(with actions: [NCMenuAction]) {
  92. let menuViewController = NCMenu.makeNCMenu(with: actions)
  93. let menuPanelController = NCMenuPanelController()
  94. menuPanelController.parentPresenter = self
  95. menuPanelController.delegate = menuViewController
  96. menuPanelController.set(contentViewController: menuViewController)
  97. menuPanelController.track(scrollView: menuViewController.tableView)
  98. present(menuPanelController, animated: true, completion: nil)
  99. }
  100. }
  101. extension UIViewController: MFMailComposeViewControllerDelegate {
  102. public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  103. controller.dismiss(animated: true)
  104. }
  105. }