Skip to content

Commit

Permalink
fix openURL (#3266)
Browse files Browse the repository at this point in the history
* fix

Signed-off-by: Marino Faggiana <[email protected]>

* cleaning

Signed-off-by: Marino Faggiana <[email protected]>

* cod

Signed-off-by: Marino Faggiana <[email protected]>

---------

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Jan 11, 2025
1 parent 110ddba commit 66cb58d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import UIKit

protocol NCRecommendationsCellDelegate: AnyObject {
func touchUpInsideButtonMenu(with metadata: tableMetadata, image: UIImage?)
func longPressGestureRecognized(with metadata: tableMetadata, image: UIImage?)
}

class NCRecommendationsCell: UICollectionViewCell {
class NCRecommendationsCell: UICollectionViewCell, UIGestureRecognizerDelegate {
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var labelFilename: UILabel!
@IBOutlet weak var labelInfo: UILabel!
Expand All @@ -38,9 +39,19 @@ class NCRecommendationsCell: UICollectionViewCell {

contentView.layer.borderWidth = 0.5
contentView.layer.borderColor = UIColor.separator.cgColor

let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
longPressedGesture.minimumPressDuration = 0.5
longPressedGesture.delegate = self
longPressedGesture.delaysTouchesBegan = true
self.addGestureRecognizer(longPressedGesture)
}

@IBAction func touchUpInsideButtonMenu(_ sender: Any) {
self.delegate?.touchUpInsideButtonMenu(with: self.metadata, image: image.image)
}

@objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
self.delegate?.longPressGestureRecognized(with: metadata, image: image.image)
}
}
4 changes: 4 additions & 0 deletions iOSClient/Main/Collection Common/NCCollectionViewCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,10 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
didSelectMetadata(metadata)
}

func longPressGestureRecognizedRecommendations(with metadata: tableMetadata, image: UIImage?) {

}

func longPressListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }

func longPressGridItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protocol NCSectionFirstHeaderDelegate: AnyObject {
func tapRichWorkspace(_ sender: Any)
func tapRecommendations(with metadata: tableMetadata)
func tapRecommendationsButtonMenu(with metadata: tableMetadata, image: UIImage?)
func longPressGestureRecognizedRecommendations(with metadata: tableMetadata, image: UIImage?)
}

class NCSectionFirstHeader: UICollectionReusableView, UIGestureRecognizerDelegate {
Expand Down Expand Up @@ -265,4 +266,8 @@ extension NCSectionFirstHeader: NCRecommendationsCellDelegate {
func touchUpInsideButtonMenu(with metadata: tableMetadata, image: UIImage?) {
self.delegate?.tapRecommendationsButtonMenu(with: metadata, image: image)
}

func longPressGestureRecognized(with metadata: tableMetadata, image: UIImage?) {
self.delegate?.longPressGestureRecognizedRecommendations(with: metadata, image: image)
}
}
153 changes: 72 additions & 81 deletions iOSClient/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,134 +205,130 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
let url = URLContexts.first?.url else { return }
let scheme = url.scheme
let action = url.host
let session = SceneManager.shared.getSession(scene: scene)
guard !session.account.isEmpty else { return }

func getMatchedAccount(userId: String, url: String) -> tableAccount? {
func getMatchedAccount(userId: String, url: String, completion: @escaping (_ tableAccount: tableAccount?) -> Void) {
var match: Bool = false

if let activeTableAccount = self.database.getActiveTableAccount() {
let urlBase = URL(string: activeTableAccount.urlBase)
if url.contains(urlBase?.host ?? "") && userId == activeTableAccount.userId {
return activeTableAccount
completion(activeTableAccount)
} else {
for tableAccount in self.database.getAllTableAccount() {
let urlBase = URL(string: tableAccount.urlBase)
if url.contains(urlBase?.host ?? "") && userId == tableAccount.userId {
NCAccount().changeAccount(tableAccount.account, userProfile: nil, controller: controller) { }
return tableAccount
match = true
NCAccount().changeAccount(tableAccount.account, userProfile: nil, controller: controller) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
completion(tableAccount)
}
}
}
}
if !match {
completion(nil)
}
}
}
return nil
}

/*
Example: nextcloud://open-action?action=create-voice-memo&&user=marinofaggiana&url=https://cloud.nextcloud.com
*/

if scheme == NCGlobal.shared.appScheme && action == "open-action" {

if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
let queryItems = urlComponents.queryItems
guard let actionScheme = queryItems?.filter({ $0.name == "action" }).first?.value,
let userScheme = queryItems?.filter({ $0.name == "user" }).first?.value,
let urlScheme = queryItems?.filter({ $0.name == "url" }).first?.value else { return }
if getMatchedAccount(userId: userScheme, url: urlScheme) == nil {
let message = NSLocalizedString("_the_account_", comment: "") + " " + userScheme + NSLocalizedString("_of_", comment: "") + " " + urlScheme + " " + NSLocalizedString("_does_not_exist_", comment: "")
let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))

controller.present(alertController, animated: true, completion: { })
return
}

switch actionScheme {
case NCGlobal.shared.actionUploadAsset:

NCAskAuthorization().askAuthorizationPhotoLibrary(controller: controller) { hasPermission in
if hasPermission {
NCPhotosPickerViewController(controller: controller, maxSelectedAssets: 0, singleSelectedMode: false)
}
}

case NCGlobal.shared.actionScanDocument:

NCDocumentCamera.shared.openScannerDocument(viewController: controller)

case NCGlobal.shared.actionTextDocument:
getMatchedAccount(userId: userScheme, url: urlScheme) { tableAccount in
if tableAccount == nil {
let message = NSLocalizedString("_the_account_", comment: "") + " " + userScheme + NSLocalizedString("_of_", comment: "") + " " + urlScheme + " " + NSLocalizedString("_does_not_exist_", comment: "")
let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))

let directEditingCreators = self.database.getDirectEditingCreators(account: session.account)
let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorText})!
let serverUrl = controller.currentServerUrl()

Task {
let fileName = await NCNetworking.shared.createFileName(fileNameBase: NSLocalizedString("_untitled_", comment: "") + ".md", account: session.account, serverUrl: serverUrl)
let fileNamePath = NCUtilityFileSystem().getFileNamePath(String(describing: fileName), serverUrl: serverUrl, session: session)

NCCreateDocument().createDocument(controller: controller, fileNamePath: fileNamePath, fileName: String(describing: fileName), editorId: NCGlobal.shared.editorText, creatorId: directEditingCreator.identifier, templateId: NCGlobal.shared.templateDocument, account: session.account)
controller.present(alertController, animated: true, completion: { })
return
}
let session = SceneManager.shared.getSession(scene: scene)

case NCGlobal.shared.actionVoiceMemo:

NCAskAuthorization().askAuthorizationAudioRecord(viewController: controller) { hasPermission in
if hasPermission {
if let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as? NCAudioRecorderViewController {
viewController.controller = controller
viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
controller.present(viewController, animated: true, completion: nil)
switch actionScheme {
case NCGlobal.shared.actionUploadAsset:
NCAskAuthorization().askAuthorizationPhotoLibrary(controller: controller) { hasPermission in
if hasPermission {
NCPhotosPickerViewController(controller: controller, maxSelectedAssets: 0, singleSelectedMode: false)
}
}
case NCGlobal.shared.actionScanDocument:
NCDocumentCamera.shared.openScannerDocument(viewController: controller)
case NCGlobal.shared.actionTextDocument:
let directEditingCreators = self.database.getDirectEditingCreators(account: session.account)
let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorText})!
let serverUrl = controller.currentServerUrl()

Task {
let fileName = await NCNetworking.shared.createFileName(fileNameBase: NSLocalizedString("_untitled_", comment: "") + ".md", account: session.account, serverUrl: serverUrl)
let fileNamePath = NCUtilityFileSystem().getFileNamePath(String(describing: fileName), serverUrl: serverUrl, session: session)

NCCreateDocument().createDocument(controller: controller, fileNamePath: fileNamePath, fileName: String(describing: fileName), editorId: NCGlobal.shared.editorText, creatorId: directEditingCreator.identifier, templateId: NCGlobal.shared.templateDocument, account: session.account)
}
case NCGlobal.shared.actionVoiceMemo:
NCAskAuthorization().askAuthorizationAudioRecord(viewController: controller) { hasPermission in
if hasPermission {
if let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as? NCAudioRecorderViewController {
viewController.controller = controller
viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
controller.present(viewController, animated: true, completion: nil)
}
}
}
default:
print("No action")
}

default:
print("No action")
}
}
return
}

/*
Example: nextcloud://open-file?path=Talk/IMG_0000123.jpg&user=marinofaggiana&link=https://cloud.nextcloud.com/f/123
*/

else if scheme == NCGlobal.shared.appScheme && action == "open-file" {

if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {

var serverUrl: String = ""
var fileName: String = ""
let queryItems = urlComponents.queryItems
guard let userScheme = queryItems?.filter({ $0.name == "user" }).first?.value,
let pathScheme = queryItems?.filter({ $0.name == "path" }).first?.value,
let linkScheme = queryItems?.filter({ $0.name == "link" }).first?.value else { return}

guard let matchedAccount = getMatchedAccount(userId: userScheme, url: linkScheme) else {
guard let domain = URL(string: linkScheme)?.host else { return }
fileName = (pathScheme as NSString).lastPathComponent
let message = String(format: NSLocalizedString("_account_not_available_", comment: ""), userScheme, domain, fileName)
let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))

controller.present(alertController, animated: true, completion: { })
return
}

let davFiles = "remote.php/dav/files/" + session.userId
getMatchedAccount(userId: userScheme, url: linkScheme) { tableAccount in
guard let tableAccount else {
guard let domain = URL(string: linkScheme)?.host else { return }
fileName = (pathScheme as NSString).lastPathComponent
let message = String(format: NSLocalizedString("_account_not_available_", comment: ""), userScheme, domain, fileName)
let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))

if pathScheme.contains("/") {
fileName = (pathScheme as NSString).lastPathComponent
serverUrl = matchedAccount.urlBase + "/" + davFiles + "/" + (pathScheme as NSString).deletingLastPathComponent
} else {
fileName = pathScheme
serverUrl = matchedAccount.urlBase + "/" + davFiles
}
controller.present(alertController, animated: true, completion: { })
return
}
let davFiles = "remote.php/dav/files/" + tableAccount.userId

if pathScheme.contains("/") {
fileName = (pathScheme as NSString).lastPathComponent
serverUrl = tableAccount.urlBase + "/" + davFiles + "/" + (pathScheme as NSString).deletingLastPathComponent
} else {
fileName = pathScheme
serverUrl = tableAccount.urlBase + "/" + davFiles
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
NCActionCenter.shared.openFileViewInFolder(serverUrl: serverUrl, fileNameBlink: nil, fileNameOpen: fileName, sceneIdentifier: controller.sceneIdentifier)
}
}
return

/*
Example: nextcloud://open-and-switch-account?user=marinofaggiana&url=https://cloud.nextcloud.com
Expand All @@ -344,16 +340,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
guard let userScheme = queryItems?.filter({ $0.name == "user" }).first?.value,
let urlScheme = queryItems?.filter({ $0.name == "url" }).first?.value else { return }
// If the account doesn't exist, return false which will open the app without switching
if getMatchedAccount(userId: userScheme, url: urlScheme) == nil {
return
}
// Otherwise open the app and switch accounts
return
getMatchedAccount(userId: userScheme, url: urlScheme) { _ in }
} else if let action {
if DeepLink(rawValue: action) != nil {
NCDeepLinkHandler().parseDeepLink(url, controller: controller)
}
return
} else {
let applicationHandle = NCApplicationHandle()
let isHandled = applicationHandle.applicationOpenURL(url)
Expand Down
2 changes: 2 additions & 0 deletions iOSClient/Select/NCSelect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ class NCSelect: UIViewController, UIGestureRecognizerDelegate, UIAdaptivePresent

func tapRecommendations(with metadata: tableMetadata) { }

func longPressGestureRecognizedRecommendations(with metadata: tableMetadata, image: UIImage?) { }

// MARK: - Push metadata

func pushMetadata(_ metadata: tableMetadata) {
Expand Down

0 comments on commit 66cb58d

Please sign in to comment.