-
-
Notifications
You must be signed in to change notification settings - Fork 890
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deep link support for screens navigation (#2963)
* added DeepLink code by Amrut Waghmare Signed-off-by: Marino Faggiana <[email protected]> * fix Signed-off-by: Marino Faggiana <[email protected]> * fix Signed-off-by: Marino Faggiana <[email protected]> * fix Signed-off-by: Marino Faggiana <[email protected]> * cleaning Signed-off-by: Marino Faggiana <[email protected]> * fix Signed-off-by: Marino Faggiana <[email protected]> * fix Signed-off-by: Marino Faggiana <[email protected]> * change classname Signed-off-by: Marino Faggiana <[email protected]> * cleaning code Signed-off-by: Marino Faggiana <[email protected]> * cleaning Signed-off-by: Marino Faggiana <[email protected]> --------- Signed-off-by: Marino Faggiana <[email protected]>
- Loading branch information
1 parent
6849606
commit aae2a7d
Showing
6 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// | ||
// DeepLinkHandler.swift | ||
// Nextcloud | ||
// | ||
// Created by Amrut Waghmare on 29/05/24. | ||
// Copyright © 2024 Marino Faggiana. All rights reserved. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import SwiftUI | ||
|
||
enum DeepLink: String { | ||
case openFiles // nextcloud://openFiles | ||
case openFavorites // nextcloud://openFavorites | ||
case openMedia // nextcloud://openMedia | ||
case openShared // nextcloud://openShared | ||
case openOffline // nextcloud://openOffline | ||
case openNotifications // nextcloud://openNotifications | ||
case openDeleted // nextcloud://openDeleted | ||
case openSettings // nextcloud://openSettings | ||
case openAutoUpload // nextcloud://openAutoUpload | ||
case openUrl // nextcloud://openUrl?url=https://nextcloud.com | ||
case createNew // nextcloud://createNew | ||
case checkAppUpdate // nextcloud://checkAppUpdate | ||
} | ||
|
||
enum ControllerConstants { | ||
static let filesIndex = 0 | ||
static let favouriteIndex = 1 | ||
static let mediaIndex = 3 | ||
static let moreIndex = 4 | ||
static let notification = "NCNotification" | ||
static let shares = "segueShares" | ||
static let offline = "segueOffline" | ||
static let delete = "segueTrash" | ||
} | ||
|
||
class NCDeepLinkHandler { | ||
func parseDeepLink(_ url: URL, controller: NCMainTabBarController) { | ||
guard let action = url.host, let deepLink = DeepLink(rawValue: action) else { return } | ||
let params = getQueryParamsFromUrl(url: url) | ||
handleDeepLink(deepLink, controller: controller, params: params) | ||
} | ||
|
||
func getQueryParamsFromUrl(url: URL) -> [String: Any]? { | ||
guard | ||
let components = URLComponents(url: url, resolvingAgainstBaseURL: true), | ||
let queryItems = components.queryItems else { return nil } | ||
return queryItems.reduce(into: [String: Any]()) { result, item in | ||
result[item.name] = item.value | ||
} | ||
} | ||
|
||
func handleDeepLink(_ deepLink: DeepLink, controller: NCMainTabBarController, params: [String: Any]? = nil) { | ||
switch deepLink { | ||
case .openFiles: | ||
navigateTo(index: ControllerConstants.filesIndex, controller: controller) | ||
case .openFavorites: | ||
navigateTo(index: ControllerConstants.favouriteIndex, controller: controller) | ||
case .openMedia: | ||
navigateTo(index: ControllerConstants.mediaIndex, controller: controller) | ||
case .openShared: | ||
navigateToMore(withSegue: ControllerConstants.shares, controller: controller) | ||
case .openOffline: | ||
navigateToMore(withSegue: ControllerConstants.offline, controller: controller) | ||
case .openNotifications: | ||
navigateToNotification(controller: controller) | ||
case .openDeleted: | ||
navigateToMore(withSegue: ControllerConstants.delete, controller: controller) | ||
case .openSettings: | ||
navigateToSettings(controller: controller) | ||
case .openAutoUpload: | ||
navigateToAutoUpload(controller: controller) | ||
case .openUrl: | ||
openUrl(params: params) | ||
case .createNew: | ||
navigateToCreateNew(controller: controller) | ||
case .checkAppUpdate: | ||
navigateAppUpdate() | ||
} | ||
} | ||
|
||
private func navigateTo(index: Int, controller: NCMainTabBarController) { | ||
controller.selectedIndex = index | ||
} | ||
|
||
private func navigateToNotification(controller: NCMainTabBarController) { | ||
controller.selectedIndex = ControllerConstants.filesIndex | ||
guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController, | ||
let viewController = UIStoryboard(name: ControllerConstants.notification, bundle: nil).instantiateInitialViewController() as? NCNotification else { return } | ||
navigationController.pushViewController(viewController, animated: true) | ||
} | ||
|
||
private func navigateToCreateNew(controller: NCMainTabBarController) { | ||
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return } | ||
controller.selectedIndex = ControllerConstants.filesIndex | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 4) { | ||
appDelegate.toggleMenu(controller: controller) | ||
} | ||
} | ||
|
||
private func navigateToMore(withSegue segue: String, controller: NCMainTabBarController) { | ||
controller.selectedIndex = ControllerConstants.moreIndex | ||
guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController else { return } | ||
navigationController.viewControllers = navigationController.viewControllers.filter({$0.isKind(of: NCMore.self)}) | ||
navigationController.performSegue(withIdentifier: segue, sender: self) | ||
} | ||
|
||
private func navigateToSettings(controller: NCMainTabBarController) { | ||
controller.selectedIndex = ControllerConstants.moreIndex | ||
guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController else { return } | ||
|
||
let settingsView = NCSettingsView(model: NCSettingsModel(controller: controller)) | ||
let settingsController = UIHostingController(rootView: settingsView) | ||
navigationController.pushViewController(settingsController, animated: true) | ||
} | ||
|
||
private func navigateToAutoUpload(controller: NCMainTabBarController) { | ||
controller.selectedIndex = ControllerConstants.moreIndex | ||
guard let navigationController = controller.viewControllers?[controller.selectedIndex] as? UINavigationController else { return } | ||
|
||
let autoUploadView = NCAutoUploadView(model: NCAutoUploadModel(controller: controller)) | ||
let autoUploadController = UIHostingController(rootView: autoUploadView) | ||
navigationController.pushViewController(autoUploadController, animated: true) | ||
} | ||
|
||
private func navigateAppUpdate() { | ||
guard let url = URL(string: NCBrandOptions.shared.appStoreUrl) else { return } | ||
handleUrl(url: url) | ||
} | ||
|
||
private func openUrl(params: [String: Any]?) { | ||
guard let urlString = params?["url"] as? String, let url = URL(string: urlString) else { return } | ||
handleUrl(url: url) | ||
} | ||
|
||
private func handleUrl(url: URL) { | ||
if UIApplication.shared.canOpenURL(url) { | ||
UIApplication.shared.open(url, options: [:], completionHandler: nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters