UIViewController+Menu.swift 5.5 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. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  60. guard serverVersionMajor >= NCGlobal.shared.nextcloudVersion23 else { return }
  61. NextcloudKit.shared.getHovercard(for: userId) { account, card, data, _ in
  62. guard let card = card, account == appDelegate.account else { return }
  63. let personHeader = NCMenuAction(
  64. title: card.displayName,
  65. icon: NCUtility.shared.loadUserImage(
  66. for: userId,
  67. displayName: card.displayName,
  68. userBaseUrl: appDelegate),
  69. action: nil)
  70. let actions = card.actions.map { action -> NCMenuAction in
  71. var image = NCUtility.shared.loadImage(named: "user", color: .label)
  72. if let url = URL(string: action.icon),
  73. let svgSource = SVGKSourceURL.source(from: url),
  74. let svg = SVGKImage(source: svgSource) {
  75. image = svg.uiImage.imageColor(.label)
  76. }
  77. return NCMenuAction(
  78. title: action.title,
  79. icon: image,
  80. action: { _ in self.handleProfileAction(action, for: userId) })
  81. }
  82. let allActions = [personHeader] + actions
  83. self.presentMenu(with: allActions)
  84. }
  85. }
  86. func sendEmail(to email: String) {
  87. guard MFMailComposeViewController.canSendMail() else {
  88. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_cannot_send_mail_error_")
  89. NCContentPresenter.shared.showError(error: error)
  90. return
  91. }
  92. let mail = MFMailComposeViewController()
  93. mail.mailComposeDelegate = self
  94. mail.setToRecipients([email])
  95. present(mail, animated: true)
  96. }
  97. func presentMenu(with actions: [NCMenuAction]) {
  98. guard !actions.isEmpty else { return }
  99. guard let menuViewController = NCMenu.makeNCMenu(with: actions) 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. }