forked from Expensify/react-native-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.kt
More file actions
148 lines (131 loc) · 5.5 KB
/
Copy pathUtils.kt
File metadata and controls
148 lines (131 loc) · 5.5 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.expensify.wallet
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.google.android.gms.tapandpay.issuer.UserAddress
import com.expensify.wallet.model.CardData
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
object Utils {
fun ReadableMap.toCardData(): CardData? {
val addressMap = this.getMap("userAddress") ?: return null
val userAddress = UserAddress.newBuilder()
.setName(addressMap.getString("name") ?: "")
.setAddress1(addressMap.getString("addressOne") ?: "")
.setAddress2(addressMap.getString("addressTwo") ?: "")
.setLocality(addressMap.getString("locality") ?: "")
.setAdministrativeArea(addressMap.getString("administrativeArea") ?: "")
.setCountryCode(addressMap.getString("countryCode") ?: "")
.setPostalCode(addressMap.getString("postalCode") ?: "")
.setPhoneNumber(addressMap.getString("phoneNumber") ?: "")
.build()
return CardData(
network = this.getString("network") ?: "",
opaquePaymentCard = this.getString("opaquePaymentCard") ?: "",
cardHolderName = this.getString("cardHolderName") ?: "",
lastDigits = this.getString("lastDigits") ?: "",
userAddress = userAddress
)
}
suspend fun getAsyncResult(
resultType: Class<String>,
getPromiseOperation: (Promise) -> Unit
): Deferred<String> = coroutineScope {
async {
withContext(Dispatchers.IO) {
suspendCancellableCoroutine { continuation ->
val promise = object : Promise {
@Deprecated(
"Prefer passing a module-specific error code to JS. Using this method will pass the error code UNSPECIFIED",
replaceWith = ReplaceWith("reject(code, message)")
)
override fun reject(message: String) {
continuation.resumeWithException(
Exception("Error: $message")
)
}
override fun reject(code: String?, userInfo: WritableMap) {
val errorMessage = "Error: ${code ?: "Unknown code"}\nUserInfo: $userInfo"
continuation.resumeWithException(
Exception(errorMessage)
)
}
override fun reject(code: String?, message: String?) {
val errorMessage = "Error: ${code ?: "Unknown code"}\nMessage: ${message ?: "No message provided"}"
continuation.resumeWithException(
Exception(errorMessage)
)
}
override fun reject(code: String?, message: String?, userInfo: WritableMap) {
val errorMessage =
"Error: ${code ?: "Unknown code"}\nMessage: ${message ?: "No message provided"}\nUserInfo: $userInfo"
continuation.resumeWithException(
Exception(errorMessage)
)
}
override fun reject(code: String?, message: String?, throwable: Throwable?) {
val errorMessage = "Error: ${code ?: "Unknown code"}\nMessage: ${message ?: "No message provided"}"
continuation.resumeWithException(
throwable ?: Exception(errorMessage)
)
}
override fun reject(code: String?, throwable: Throwable?) {
val errorMessage = "Error: ${code ?: "Unknown code"}"
continuation.resumeWithException(
throwable ?: Exception(errorMessage)
)
}
override fun reject(code: String?, throwable: Throwable?, userInfo: WritableMap) {
val errorMessage = "Error: ${code ?: "Unknown code"}\nUserInfo: $userInfo"
continuation.resumeWithException(
throwable ?: Exception(errorMessage)
)
}
override fun reject(
code: String?,
message: String?,
throwable: Throwable?,
userInfo: WritableMap?
) {
val errorMessage = buildString {
append("Error: ${code ?: "Unknown code"}")
if (message != null) append("\nMessage: $message")
if (userInfo != null) append("\nUserInfo: $userInfo")
}
continuation.resumeWithException(
throwable ?: Exception(errorMessage)
)
}
override fun reject(throwable: Throwable) {
continuation.resumeWithException(
throwable
)
}
override fun reject(throwable: Throwable, userInfo: WritableMap) {
val errorMessage = "Exception occurred\nUserInfo: $userInfo"
continuation.resumeWithException(
Exception(errorMessage, throwable)
)
}
override fun resolve(value: Any?) {
if (resultType.isInstance(value)) {
continuation.resume(value as String)
} else {
continuation.resumeWithException(
RuntimeException("Expected result of type ${resultType.simpleName}, but got ${value?.javaClass?.simpleName}")
)
}
}
}
getPromiseOperation(promise)
}
}
}
}
}