UIViewController+Menu.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // NCHovercard+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by admin on 10.11.21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import SVGKit
  10. extension UIViewController {
  11. func handleProfileAction(_ action: NCHovercard.Action, for userId: String) {
  12. switch action.appId {
  13. case "email":
  14. guard let url = action.hyperlinkUrl,
  15. url.scheme == "mailto",
  16. let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
  17. NCContentPresenter.shared.showGenericError(description: "_cannot_send_mail_error_")
  18. return
  19. }
  20. sendEmail(to: components.path)
  21. case "spreed":
  22. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  23. if let talkUrl = URL(string: "nextcloudtalk://open-conversation?server=\(appDelegate.urlBase)&user=\(userId)&withUser=\(appDelegate.userId)"),
  24. UIApplication.shared.canOpenURL(talkUrl) {
  25. UIApplication.shared.open(talkUrl, options: [.universalLinksOnly: true])
  26. } else if let url = action.hyperlinkUrl {
  27. UIApplication.shared.open(url, options: [:])
  28. }
  29. default:
  30. guard let url = action.hyperlinkUrl, UIApplication.shared.canOpenURL(url) else {
  31. NCContentPresenter.shared.showGenericError(description: "_open_url_error")
  32. return
  33. }
  34. UIApplication.shared.open(url, options: [:])
  35. }
  36. }
  37. func showProfileMenu(userId: String) {
  38. NCCommunication.shared.getHovercard(for: userId) { (card, errCode, err) in
  39. guard let card = card else {
  40. return
  41. }
  42. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  43. let personHeader = NCMenuAction(
  44. title: card.displayName,
  45. icon: NCUtility.shared.loadUserImage(for: userId, displayName: card.displayName, urlBase: appDelegate.urlBase),
  46. action: nil)
  47. let actions = card.actions.map { action -> NCMenuAction in
  48. var image = UIImage()
  49. if let url = URL(string: action.icon), let svg = SVGKImage(contentsOf: url) {
  50. image = svg.uiImage
  51. }
  52. return NCMenuAction(
  53. title: action.title,
  54. icon: image,
  55. action: { _ in self.handleProfileAction(action, for: userId) })
  56. }
  57. let allActions = [personHeader] + actions
  58. self.presentMenu(with: allActions)
  59. }
  60. }
  61. func sendEmail(to email: String) {
  62. guard MFMailComposeViewController.canSendMail() else {
  63. NCContentPresenter.shared.showGenericError(description: "_cannot_send_mail_error_")
  64. return
  65. }
  66. let mail = MFMailComposeViewController()
  67. mail.mailComposeDelegate = self
  68. mail.setToRecipients([email])
  69. present(mail, animated: true)
  70. }
  71. func presentMenu(with actions: [NCMenuAction]) {
  72. let menuViewController = NCMenu.makeNCMenu(with: actions)
  73. let menuPanelController = NCMenuPanelController()
  74. menuPanelController.parentPresenter = self
  75. menuPanelController.delegate = menuViewController
  76. menuPanelController.set(contentViewController: menuViewController)
  77. menuPanelController.track(scrollView: menuViewController.tableView)
  78. present(menuPanelController, animated: true, completion: nil)
  79. }
  80. }
  81. extension UIViewController: MFMailComposeViewControllerDelegate {
  82. public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  83. controller.dismiss(animated: true)
  84. }
  85. }