NCDeepLinkHandler.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // DeepLinkHandler.swift
  3. // Nextcloud
  4. //
  5. // Created by Amrut Waghmare on 29/05/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import Foundation
  22. import UIKit
  23. import SwiftUI
  24. import NextcloudKit
  25. enum DeepLink: String {
  26. case openFiles // nextcloud://openFiles
  27. case openFavorites // nextcloud://openFavorites
  28. case openMedia // nextcloud://openMedia
  29. case openShared // nextcloud://openShared
  30. case openOffline // nextcloud://openOffline
  31. case openNotifications // nextcloud://openNotifications
  32. case openDeleted // nextcloud://openDeleted
  33. case openSettings // nextcloud://openSettings
  34. case openAutoUpload // nextcloud://openAutoUpload
  35. case openUrl // nextcloud://openUrl?url=https://nextcloud.com
  36. case createNew // nextcloud://createNew
  37. case checkAppUpdate // nextcloud://checkAppUpdate
  38. }
  39. enum ControllerConstants {
  40. static let filesIndex = 0
  41. static let favouriteIndex = 1
  42. static let mediaIndex = 3
  43. static let moreIndex = 4
  44. static let notification = "NCNotification"
  45. static let shares = "segueShares"
  46. static let offline = "segueOffline"
  47. static let delete = "segueTrash"
  48. }
  49. class NCDeepLinkHandler {
  50. func parseDeepLink(_ url: URL, controller: NCMainTabBarController) {
  51. guard let action = url.host, let deepLink = DeepLink(rawValue: action) else { return }
  52. let params = getQueryParamsFromUrl(url: url)
  53. handleDeepLink(deepLink, controller: controller, params: params)
  54. }
  55. func getQueryParamsFromUrl(url: URL) -> [String: Any]? {
  56. guard
  57. let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
  58. let queryItems = components.queryItems else { return nil }
  59. return queryItems.reduce(into: [String: Any]()) { result, item in
  60. result[item.name] = item.value
  61. }
  62. }
  63. func handleDeepLink(_ deepLink: DeepLink, controller: NCMainTabBarController, params: [String: Any]? = nil) {
  64. switch deepLink {
  65. case .openFiles:
  66. navigateTo(index: ControllerConstants.filesIndex, controller: controller)
  67. case .openFavorites:
  68. navigateTo(index: ControllerConstants.favouriteIndex, controller: controller)
  69. case .openMedia:
  70. navigateTo(index: ControllerConstants.mediaIndex, controller: controller)
  71. case .openShared:
  72. navigateToMore(withSegue: ControllerConstants.shares, controller: controller)
  73. case .openOffline:
  74. navigateToMore(withSegue: ControllerConstants.offline, controller: controller)
  75. case .openNotifications:
  76. navigateToNotification(controller: controller)
  77. case .openDeleted:
  78. navigateToMore(withSegue: ControllerConstants.delete, controller: controller)
  79. case .openSettings:
  80. navigateToSettings(controller: controller)
  81. case .openAutoUpload:
  82. navigateToAutoUpload(controller: controller)
  83. case .openUrl:
  84. openUrl(params: params)
  85. case .createNew:
  86. navigateToCreateNew(controller: controller)
  87. case .checkAppUpdate:
  88. navigateAppUpdate()
  89. }
  90. }
  91. private func navigateTo(index: Int, controller: NCMainTabBarController) {
  92. controller.selectedIndex = index
  93. }
  94. private func navigateToNotification(controller: NCMainTabBarController) {
  95. controller.selectedIndex = ControllerConstants.filesIndex
  96. guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController,
  97. let viewController = UIStoryboard(name: ControllerConstants.notification, bundle: nil).instantiateInitialViewController() as? NCNotification else { return }
  98. viewController.session = NCSession.shared.getSession(controller: controller)
  99. navigationController.pushViewController(viewController, animated: true)
  100. }
  101. private func navigateToCreateNew(controller: NCMainTabBarController) {
  102. guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
  103. controller.selectedIndex = ControllerConstants.filesIndex
  104. DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
  105. let serverUrl = controller.currentServerUrl()
  106. let session = NCSession.shared.getSession(controller: controller)
  107. let fileFolderPath = NCUtilityFileSystem().getFileNamePath("", serverUrl: serverUrl, session: session)
  108. let fileFolderName = (serverUrl as NSString).lastPathComponent
  109. if !FileNameValidator.shared.checkFolderPath(fileFolderPath, account: controller.account) {
  110. controller.present(UIAlertController.warning(message: "\(String(format: NSLocalizedString("_file_name_validator_error_reserved_name_", comment: ""), fileFolderName)) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
  111. return
  112. }
  113. appDelegate.toggleMenu(controller: controller)
  114. }
  115. }
  116. private func navigateToMore(withSegue segue: String, controller: NCMainTabBarController) {
  117. controller.selectedIndex = ControllerConstants.moreIndex
  118. guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController else { return }
  119. navigationController.viewControllers = navigationController.viewControllers.filter({$0.isKind(of: NCMore.self)})
  120. navigationController.performSegue(withIdentifier: segue, sender: self)
  121. }
  122. private func navigateToSettings(controller: NCMainTabBarController) {
  123. controller.selectedIndex = ControllerConstants.moreIndex
  124. guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController else { return }
  125. let settingsView = NCSettingsView(model: NCSettingsModel(controller: controller))
  126. let settingsController = UIHostingController(rootView: settingsView)
  127. navigationController.pushViewController(settingsController, animated: true)
  128. }
  129. private func navigateToAutoUpload(controller: NCMainTabBarController) {
  130. controller.selectedIndex = ControllerConstants.moreIndex
  131. guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController else { return }
  132. let autoUploadView = NCAutoUploadView(model: NCAutoUploadModel(controller: controller))
  133. let autoUploadController = UIHostingController(rootView: autoUploadView)
  134. navigationController.pushViewController(autoUploadController, animated: true)
  135. }
  136. private func navigateAppUpdate() {
  137. guard let url = URL(string: NCBrandOptions.shared.appStoreUrl) else { return }
  138. handleUrl(url: url)
  139. }
  140. private func openUrl(params: [String: Any]?) {
  141. guard let urlString = params?["url"] as? String, let url = URL(string: urlString) else { return }
  142. handleUrl(url: url)
  143. }
  144. private func handleUrl(url: URL) {
  145. if UIApplication.shared.canOpenURL(url) {
  146. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  147. }
  148. }
  149. }