-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeeplinkCredentialManager.swift
110 lines (93 loc) · 3.63 KB
/
DeeplinkCredentialManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// DeeplinkCredentialManager.swift
// DeepLinkingManager
//
// Created by N.A Shashank on 29/11/18.
//
import UIKit
class DeeplinkCredentialManager:DeeplinkingManagerDelegate {
private var customType:CustomType?
var key:String?
weak var delegate:DeeplinkCredentialManagerDelegate?
enum CustomType {
case checkout(Mode)
case customui(Mode)
static func instanceFrom(host:String,path:String) -> CustomType {
// since checkout and custom ui are using the same keys there is a common mode but in future
// if it splits change it here
var mode:Mode
switch path {
case let strTemp where strTemp.contains("live") :
mode = DeeplinkCredentialManager.Mode.live(Credentials.liveKey.rawValue)
case let strTemp where strTemp.contains("debug") :
mode = DeeplinkCredentialManager.Mode.debug(Credentials.debugKey.rawValue)
case let strTemp where strTemp.contains("partner") :
mode = DeeplinkCredentialManager.Mode.partner(Credentials.partnerKey.rawValue)
default :
mode = DeeplinkCredentialManager.Mode.custom(path)
}
guard host.lowercased() == "checkout" else{
return CustomType.customui(mode)
}
return CustomType.checkout(mode)
}
var key:String {
var strTemp = String()
switch self {
case CustomType.checkout(let mode) :
strTemp = mode.key
case CustomType.customui(let mode) :
strTemp = mode.key
}
return strTemp
}
}
enum Mode {
case live(String)
case debug(String)
case partner(String)
case custom(String)
var key :String {
var tmpKey = String()
switch self {
case Mode.live(let path),Mode.debug(let path),Mode.partner(let path) :
tmpKey = path
case Mode.custom(let path) :
tmpKey = self.keyFromCustomMode(str: path)
}
return tmpKey
}
func keyFromCustomMode(str:String) -> String {
var arrComponents = str.components(separatedBy: "&")
guard arrComponents.count >= 2 else {
return String()
}
let path = arrComponents[1]
let arrRequiredComponents = path.components(separatedBy: "=")
guard arrRequiredComponents.count >= 2 else{
return String()
}
return arrRequiredComponents[1]
}
}
func handleSchemeWith(host: String?, path: String?, options: [UIApplication.OpenURLOptionsKey : Any]) {
guard let unwrappedHost = host,let unwrappedPath = path else{
self.delegate?.failedToParseUrl(error: DeeplinkingError.failedToParseDeeplink)
return
}
let type = CustomType.instanceFrom(host: unwrappedHost, path: unwrappedPath)
self.customType = type
self.key = type.key
}
func valueFrom<ReturnType,Type>(keyPath:KeyPath<DeeplinkCredentialManager,Type>,defaultValue:ReturnType) -> ReturnType {
let value = self[keyPath:keyPath]
let mirror = Mirror(reflecting: value)
guard mirror.displayStyle == Mirror.DisplayStyle.optional,mirror.children.count == 1,let child = mirror.children.first?.value as? ReturnType else{
return defaultValue
}
return child
}
deinit {
print("\(#file) deallocated")
}
}