Skip to content

Commit

Permalink
feat(DeeplinkService): If app started with a public share link before…
Browse files Browse the repository at this point in the history
… authentication it is replayed after
  • Loading branch information
adrien-coye committed Dec 19, 2024
1 parent 711de84 commit 909c5b7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
3 changes: 3 additions & 0 deletions kDrive/UI/Controller/LoginDelegateHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kDriveResources
public final class LoginDelegateHandler: InfomaniakLoginDelegate {
@LazyInjectService var accountManager: AccountManageable
@LazyInjectService var router: AppNavigable
@LazyInjectService var deeplinkService: DeeplinkServiceable

var didStartLoginCallback: (() -> Void)?
var didCompleteLoginCallback: (() -> Void)?
Expand Down Expand Up @@ -57,6 +58,8 @@ public final class LoginDelegateHandler: InfomaniakLoginDelegate {
}

didCompleteLoginCallback?()

deeplinkService.processDeeplinksPostAuthentication()
}
}

Expand Down
43 changes: 12 additions & 31 deletions kDriveCore/Utils/Deeplinks/DeeplinkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,26 @@
*/

import Foundation
import SwiftRegex

public struct PublicShareLink {
public static let parsingRegex = Regex(pattern: #"^/app/share/([0-9]+)/([a-z0-9-]+)$"#)

public let publicShareURL: URL
public let shareLinkUid: String
public let driveId: Int

public init?(publicShareURL: URL) async {
guard let components = URLComponents(url: publicShareURL, resolvingAgainstBaseURL: true) else {
return nil
}

let path = components.path
guard let matches = Self.parsingRegex?.matches(in: path) else {
return nil
}

guard let firstMatch = matches.first,
let driveId = firstMatch[safe: 1],
let driveIdInt = Int(driveId),
let shareLinkUid = firstMatch[safe: 2] else {
return nil
}

self.driveId = driveIdInt
self.shareLinkUid = shareLinkUid
self.publicShareURL = publicShareURL
}
}

public protocol DeeplinkServiceable: AnyObject {
func setLastPublicShare(_ link: PublicShareLink)
func processDeeplinksPostAuthentication()
}

public class DeeplinkService: DeeplinkServiceable {
var lastPublicShareLink: PublicShareLink?
public func setLastPublicShare(_ link: PublicShareLink) {
lastPublicShareLink = link
}

public func processDeeplinksPostAuthentication() {
guard let lastPublicShareLink else {
return
}

Task {
await UniversalLinksHelper.processPublicShareLink(lastPublicShareLink)
self.lastPublicShareLink = nil
}
}
}
32 changes: 31 additions & 1 deletion kDriveCore/Utils/Deeplinks/PublicShareLink.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
/*
Infomaniak kDrive - iOS App
Copyright (C) 2024 Infomaniak Network SA
Expand All @@ -18,3 +17,34 @@
*/

import Foundation
import SwiftRegex

public struct PublicShareLink: Sendable {
public static let parsingRegex = Regex(pattern: #"^/app/share/([0-9]+)/([a-z0-9-]+)$"#)

public let publicShareURL: URL
public let shareLinkUid: String
public let driveId: Int

public init?(publicShareURL: URL) async {
guard let components = URLComponents(url: publicShareURL, resolvingAgainstBaseURL: true) else {
return nil
}

let path = components.path
guard let matches = Self.parsingRegex?.matches(in: path) else {
return nil
}

guard let firstMatch = matches.first,
let driveId = firstMatch[safe: 1],
let driveIdInt = Int(driveId),
let shareLinkUid = firstMatch[safe: 2] else {
return nil
}

self.driveId = driveIdInt
self.shareLinkUid = shareLinkUid
self.publicShareURL = publicShareURL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import CocoaLumberjackSwift
import Foundation
import InfomaniakCore
import InfomaniakDI
import kDriveCore
import kDriveResources
import SwiftRegex
import UIKit

#if !ISEXTENSION
enum UniversalLinksHelper {
public enum UniversalLinksHelper {
private struct Link {
let regex: Regex
let displayMode: DisplayMode
Expand Down Expand Up @@ -57,7 +55,7 @@ enum UniversalLinksHelper {
}

@discardableResult
static func handleURL(_ url: URL) async -> Bool {
public static func handleURL(_ url: URL) async -> Bool {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
DDLogError("[UniversalLinksHelper] Failed to process url:\(url)")
return false
Expand Down Expand Up @@ -217,4 +215,3 @@ enum UniversalLinksHelper {
}
}
}
#endif

0 comments on commit 909c5b7

Please sign in to comment.