Skip to content

Commit

Permalink
refactor: minor Kt code cleanup (#2886)
Browse files Browse the repository at this point in the history
**Summary:**
While working on my fork I noticed a few places which could be a bit
polished in Kt code. Reduces nesting, removes unnecessary checks and
other minor styling.

**Test plan:**
- Doesn't change the flow in any way so simply smoke testing the Example
app.
  • Loading branch information
kot331107 authored Dec 6, 2024
1 parent a1ec2e4 commit 615368b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
64 changes: 23 additions & 41 deletions android/src/amazon/java/com/dooboolab/rniap/RNIapAmazonModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,30 @@ class RNIapAmazonModule(

@ReactMethod
fun verifyLicense(promise: Promise) {
if (BuildConfig.IS_AMAZON_DRM_ENABLED) {
Log.d(TAG, "Amazon's DRM is enabled")
try {
LicensingService.verifyLicense(reactApplicationContext) { licenseResponse ->
when (
val status: LicenseResponse.RequestStatus =
licenseResponse.requestStatus
) {
LicenseResponse.RequestStatus.LICENSED -> {
Log.d(TAG, "LicenseResponse status: $status")
promise.resolve("LICENSED")
}
LicenseResponse.RequestStatus.NOT_LICENSED -> {
Log.d(TAG, "LicenseResponse status: $status")
promise.resolve("NOT_LICENSED")
}
LicenseResponse.RequestStatus.EXPIRED -> {
Log.d(TAG, "LicenseResponse status: $status")
promise.resolve("EXPIRED")
}
LicenseResponse.RequestStatus.ERROR_VERIFICATION -> {
Log.d(TAG, "LicenseResponse status: $status")
promise.resolve("ERROR_VERIFICATION")
}
LicenseResponse.RequestStatus.ERROR_INVALID_LICENSING_KEYS -> {
Log.d(TAG, "LicenseResponse status: $status")
promise.resolve("ERROR_INVALID_LICENSING_KEYS")
}
LicenseResponse.RequestStatus.UNKNOWN_ERROR -> {
Log.d(TAG, "LicenseResponse status: $status")
promise.resolve("UNKNOWN_ERROR")
}
}
}
} catch (exception: Exception) {
promise.reject("Error while attempting to check for License", exception)
}
} else {
if (!BuildConfig.IS_AMAZON_DRM_ENABLED) {
Log.d(TAG, "Amazon's DRM is disabled")
promise.resolve("NOT_LICENSED")
return
}

Log.d(TAG, "Amazon's DRM is enabled")
try {
LicensingService.verifyLicense(reactApplicationContext) { licenseResponse ->
val status = licenseResponse.requestStatus.also {
Log.d(TAG, "LicenseResponse status: $it")
}
when (status) {
LicenseResponse.RequestStatus.LICENSED -> "LICENSED"
LicenseResponse.RequestStatus.NOT_LICENSED -> "NOT_LICENSED"
LicenseResponse.RequestStatus.EXPIRED -> "EXPIRED"
LicenseResponse.RequestStatus.ERROR_VERIFICATION -> "ERROR_VERIFICATION"
LicenseResponse.RequestStatus.ERROR_INVALID_LICENSING_KEYS -> "ERROR_INVALID_LICENSING_KEYS"
LicenseResponse.RequestStatus.UNKNOWN_ERROR -> "UNKNOWN_ERROR"
else -> null
}?.let { promise.resolve(it) }
}
} catch (exception: Exception) {
promise.reject("Error while attempting to check for License", exception)
}
}

Expand Down Expand Up @@ -130,9 +114,7 @@ class RNIapAmazonModule(
val skuSize = skuArr.size()
while (ii < skuSize) {
val sku = skuArr.getString(ii)
if (sku is String) {
productSkus.add(sku)
}
productSkus.add(sku)
ii++
}
PromiseUtils.addPromiseForKey(PROMISE_GET_PRODUCT_DATA, promise)
Expand Down
10 changes: 4 additions & 6 deletions android/src/play/java/com/dooboolab/rniap/RNIapModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class RNIapModule(
val skuList = mutableListOf<QueryProductDetailsParams.Product>()
for (i in 0 until skuArr.size()) {
if (skuArr.getType(i) == ReadableType.String) {
skuArr.getString(i)?.let { sku ->
skuArr.getString(i).let { sku ->
skuList.add(
QueryProductDetailsParams.Product
.newBuilder()
Expand Down Expand Up @@ -497,7 +497,7 @@ class RNIapModule(
}
var productDetailParams = BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(selectedSku)
if (type == BillingClient.ProductType.SUBS) {
offerTokenArr.getString(index)?.let { offerToken ->
offerTokenArr.getString(index).let { offerToken ->
// null check for older versions of RN
productDetailParams = productDetailParams.setOfferToken(offerToken)
}
Expand All @@ -523,10 +523,8 @@ class RNIapModule(
}
subscriptionUpdateParamsBuilder.setSubscriptionReplacementMode(replacementMode)
}
if (purchaseToken != null) {
val subscriptionUpdateParams = subscriptionUpdateParamsBuilder.build()
builder.setSubscriptionUpdateParams(subscriptionUpdateParams)
}
val subscriptionUpdateParams = subscriptionUpdateParamsBuilder.build()
builder.setSubscriptionUpdateParams(subscriptionUpdateParams)
}
if (obfuscatedAccountId != null) {
builder.setObfuscatedAccountId(obfuscatedAccountId)
Expand Down

0 comments on commit 615368b

Please sign in to comment.