Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/deeplink api #338

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LeadKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "LeadKit"
s.version = "1.31.0"
s.version = "1.33.0"
s.summary = "iOS framework with a bunch of tools for rapid development"
s.homepage = "https://github.com/TouchInstinct/LeadKit"
s.license = "Apache License, Version 2.0"
Expand Down
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let package = Package(
.library(name: "TIKeychainUtils", targets: ["TIKeychainUtils"]),
.library(name: "TITableKitUtils", targets: ["TITableKitUtils"]),
.library(name: "TILogging", targets: ["TILogging"]),
.library(name: "TIDeeplink", targets: ["TIDeeplink"]),

// MARK: - Networking

Expand All @@ -32,7 +33,7 @@ let package = Package(

.library(name: "TIMapUtils", targets: ["TIMapUtils"]),
.library(name: "TIAppleMapUtils", targets: ["TIAppleMapUtils"]),

// MARK: - Elements
.library(name: "OTPSwiftView", targets: ["OTPSwiftView"]),
.library(name: "TITransitions", targets: ["TITransitions"]),
Expand Down Expand Up @@ -65,6 +66,7 @@ let package = Package(
.target(name: "TIKeychainUtils", dependencies: ["TIFoundationUtils", "KeychainAccess"], path: "TIKeychainUtils/Sources"),
.target(name: "TITableKitUtils", dependencies: ["TIUIElements", "TableKit"], path: "TITableKitUtils/Sources"),
.target(name: "TILogging", dependencies: ["TIUIElements", "TISwiftUtils", "TIUIKitCore"], path: "TILogging/Sources"),
.target(name: "TIDeeplink", dependencies: ["TIFoundationUtils"], path: "TIDeeplink/Sources"),

// MARK: - Networking
.target(name: "TINetworking", dependencies: ["TIFoundationUtils", "Alamofire"], path: "TINetworking/Sources"),
Expand All @@ -74,7 +76,7 @@ let package = Package(
// MARK: - Maps
.target(name: "TIMapUtils", dependencies: [], path: "TIMapUtils/Sources"),
.target(name: "TIAppleMapUtils", dependencies: ["TIMapUtils"], path: "TIAppleMapUtils/Sources"),

// MARK: - Elements
.target(name: "OTPSwiftView", dependencies: ["TIUIElements"], path: "OTPSwiftView/Sources"),
.target(name: "TITransitions", path: "TITransitions/Sources"),
Expand Down
2 changes: 1 addition & 1 deletion TIAppleMapUtils/TIAppleMapUtils.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'TIAppleMapUtils'
s.version = '1.31.0'
s.version = '1.33.0'
s.summary = 'Set of helpers for map objects clustering and interacting using Apple MapKit.'
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
2 changes: 1 addition & 1 deletion TIAuth/TIAuth.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'TIAuth'
s.version = '1.31.0'
s.version = '1.33.0'
s.summary = 'Login, registration, confirmation and other related actions'
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation
import UIKit

open class BaseNavigationStackDeeplinkHandler<ControllerKeeper: RootViewControllerKeeper>: DeeplinkHandler {

public typealias DeeplinkType = ControllerKeeper.DeeplinkHandler.DeeplinkType
public typealias Handler = ControllerKeeper.DeeplinkHandler

public var rootViewControllerKeeper: ControllerKeeper?

// MARK: - DeeplinkHandler

open func handle(deeplink: DeeplinkType) -> Operation? {
let handler = findHandler(for: deeplink)
return handler?.handle(deeplink: deeplink)
}

// MARK: - Open methods

open func findHandler(for deeplink: DeeplinkType) -> Handler? {

guard let rootController = rootViewControllerKeeper?.rootDeeplinkHandlerController else {
return nil
}

return rootController.findHandler(for: deeplink)
}
}
29 changes: 29 additions & 0 deletions TIDeepLink/Sources/DeeplinkHandler/DeeplinkHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public protocol DeeplinkHandler {
associatedtype DeeplinkType: Hashable

func handle(deeplink: DeeplinkType) -> Operation?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import UIKit

public typealias DeeplinkHandlerViewController = DeeplinkHandler & UIViewController

public extension UIViewController {

func findHandler<DeeplinkHandler: DeeplinkHandlerViewController>(
for deeplink: DeeplinkHandler.DeeplinkType
) -> DeeplinkHandler? {

if let deeplinksHandler = self as? DeeplinkHandler,
let _ = deeplinksHandler.handle(deeplink: deeplink) {
return deeplinksHandler
}

if let deeplinksHandler: DeeplinkHandler = presentedViewController?.findHandler(for: deeplink) {
return deeplinksHandler
}

return findHandlerInViewHierarchy(for: deeplink)
}

private func findHandlerInViewHierarchy<DeeplinkHandler: DeeplinkHandlerViewController>(
for deeplink: DeeplinkHandler.DeeplinkType
) -> DeeplinkHandler? {

switch self {
case let navController as UINavigationController:
let deeplinksHandler: DeeplinkHandler? = navController.viewControllers.reversed().findHandler(for: deeplink)
return deeplinksHandler

case let tabController as UITabBarController:
if let deeplinksHandler: DeeplinkHandler = tabController.selectedViewController?.findHandler(for: deeplink) {
return deeplinksHandler
} else if var tabControllers = tabController.viewControllers {
if tabController.selectedIndex != NSNotFound {
tabControllers.remove(at: tabController.selectedIndex)
}

if let deeplinksHandler: DeeplinkHandler = tabControllers.findHandler(for: deeplink) {
return deeplinksHandler
}
}

default:
return nil
}

return nil
petropavel13 marked this conversation as resolved.
Show resolved Hide resolved
}
}

private extension Sequence where Element: UIViewController {
func findHandler<DeeplinkHandler: DeeplinkHandlerViewController>(
for deeplink: DeeplinkHandler.DeeplinkType
) -> DeeplinkHandler? {

for controller in self {
if let deeplinksHandler: DeeplinkHandler = controller.findHandler(for: deeplink) {
return deeplinksHandler
}
}
return nil
}
}
29 changes: 29 additions & 0 deletions TIDeepLink/Sources/DeeplinkHandler/RootViewControllerKeeper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import UIKit

public protocol RootViewControllerKeeper {
associatedtype DeeplinkHandler: DeeplinkHandlerViewController

var rootDeeplinkHandlerController: DeeplinkHandler { get }
}
29 changes: 29 additions & 0 deletions TIDeepLink/Sources/DeeplinkMapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

public protocol DeeplinkMapper {
associatedtype DeeplinkType

func map(url: URL) -> DeeplinkType?
}
105 changes: 105 additions & 0 deletions TIDeeplink/Sources/TIDeeplinkService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation
import TIFoundationUtils

open class TIDeeplinksService<DeeplinkType: Hashable,
Mapper: DeeplinkMapper,
Handler: DeeplinkHandler> where Mapper.DeeplinkType == Handler.DeeplinkType,
Mapper.DeeplinkType == DeeplinkType {

// MARK: - Private properties

private var operationQueue: OperationQueue

private var deeplinkQueue: [DeeplinkType] = []
private var inProcessingSet: Set<DeeplinkType> = []

// MARK: - Public properties

public var deeplinkMapper: Mapper?
public var deeplinkHandler: Handler?

// MARK: - Init

public init(operationQueue: OperationQueue = .main) {
self.operationQueue = operationQueue
}

// MARK: - Open methods

open func configure(mapper: Mapper, handler: Handler) {
deeplinkMapper = mapper
deeplinkHandler = handler
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай лучше и Mapper и Handler в init


@discardableResult
open func deferredHandle(url: URL) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут можно подписать в комменте к методу, что значит возвращаемый bool

let deeplink = deeplinkMapper?.map(url: url)

guard let deeplink = deeplink else {
return false
}

deeplinkQueue.append(deeplink)

return true
}

open func reset() {
operationQueue.cancelAllOperations()
deeplinkQueue.removeAll()
inProcessingSet.removeAll()
}

open func tryHandle() {
guard !deeplinkQueue.isEmpty else {
return
}

handle()
}

open func handle() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handlePendingDeeplinks

guard let deeplink = deeplinkQueue.first else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

также можно сразу проверить let handler

return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

один метод тут лишний


deeplinkQueue.remove(at: .zero)

guard !inProcessingSet.contains(deeplink),
let operation = deeplinkHandler?.handle(deeplink: deeplink) else {
return
}

inProcessingSet.formUnion([deeplink])

let competionOperation = BlockOperation { [weak self] in
self?.inProcessingSet.subtract([deeplink])
}

competionOperation.addDependency(operation)

competionOperation.add(to: operationQueue)
}
}
Loading