Skip to content

Commit

Permalink
fix: ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Jun 12, 2024
1 parent eb2cd30 commit 513a6c6
Show file tree
Hide file tree
Showing 12 changed files with 434 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import com.amazon.device.iap.model.FulfillmentResult
import com.amazon.device.iap.model.RequestId

interface PurchasingServiceProxy {
fun registerListener(var0: Context?, var1: PurchasingListener?)
fun registerListener(
var0: Context?,
var1: PurchasingListener?,
)

fun getUserData(): RequestId

Expand All @@ -16,5 +19,8 @@ interface PurchasingServiceProxy {

fun getPurchaseUpdates(var0: Boolean): RequestId

fun notifyFulfillment(var0: String?, var1: FulfillmentResult?)
fun notifyFulfillment(
var0: String?,
var1: FulfillmentResult?,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,21 @@ import com.amazon.device.iap.model.FulfillmentResult
import com.amazon.device.iap.model.RequestId

class PurchasingServiceProxyAmazonImpl : PurchasingServiceProxy {
override fun registerListener(var0: Context?, var1: PurchasingListener?) {
return PurchasingService.registerListener(var0, var1)
}
override fun registerListener(
var0: Context?,
var1: PurchasingListener?,
) = PurchasingService.registerListener(var0, var1)

override fun getUserData(): RequestId {
return PurchasingService.getUserData()
}
override fun getUserData(): RequestId = PurchasingService.getUserData()

override fun purchase(var0: String?): RequestId {
return PurchasingService.purchase(var0)
}
override fun purchase(var0: String?): RequestId = PurchasingService.purchase(var0)

override fun getProductData(var0: Set<String?>?): RequestId {
return PurchasingService.getProductData(var0)
}
override fun getProductData(var0: Set<String?>?): RequestId = PurchasingService.getProductData(var0)

override fun getPurchaseUpdates(var0: Boolean): RequestId {
return PurchasingService.getPurchaseUpdates(var0)
}
override fun getPurchaseUpdates(var0: Boolean): RequestId = PurchasingService.getPurchaseUpdates(var0)

override fun notifyFulfillment(var0: String?, var1: FulfillmentResult?) {
return PurchasingService.notifyFulfillment(var0, var1)
}
override fun notifyFulfillment(
var0: String?,
var1: FulfillmentResult?,
) = PurchasingService.notifyFulfillment(var0, var1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class RNIapAmazonListener(
var eventSender: EventSender?,
var purchasingService: PurchasingServiceProxy?,
) : PurchasingListener {

override fun onProductDataResponse(response: ProductDataResponse) {
when (response.requestStatus) {
ProductDataResponse.RequestStatus.SUCCESSFUL -> {
Expand Down Expand Up @@ -170,7 +169,10 @@ class RNIapAmazonListener(
}
}

private fun receiptToMap(userData: UserData, receipt: Receipt): WritableMap {
private fun receiptToMap(
userData: UserData,
receipt: Receipt,
): WritableMap {
val item = Arguments.createMap()
item.putString("productId", receipt.sku)
item.putDouble("transactionDate", receipt.purchaseDate.time.toDouble())
Expand Down
74 changes: 45 additions & 29 deletions android/src/amazon/java/com/dooboolab/rniap/RNIapAmazonModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,33 @@ class RNIapAmazonModule(
private val reactContext: ReactApplicationContext,
private val purchasingService: PurchasingServiceProxy = PurchasingServiceProxyAmazonImpl(),
private var eventSender: EventSender? = null,
) :
ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return TAG
}
) : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String = TAG

@ReactMethod
fun initConnection(promise: Promise) {
if (RNIapActivityListener.amazonListener == null) {
promise.safeReject(PromiseUtils.E_DEVELOPER_ERROR, Exception("RNIapActivityListener is not registered in your MainActivity.onCreate"))
promise.safeReject(
PromiseUtils.E_DEVELOPER_ERROR,
Exception("RNIapActivityListener is not registered in your MainActivity.onCreate"),
)
return
}
if (eventSender == null) {
eventSender = object : EventSender {
private val rctDeviceEventEmitter = reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)

override fun sendEvent(eventName: String, params: WritableMap?) {
rctDeviceEventEmitter
.emit(eventName, params)
eventSender =
object : EventSender {
private val rctDeviceEventEmitter =
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)

override fun sendEvent(
eventName: String,
params: WritableMap?,
) {
rctDeviceEventEmitter
.emit(eventName, params)
}
}
}
}
RNIapActivityListener.amazonListener?.eventSender = eventSender
RNIapActivityListener.amazonListener?.purchasingService = purchasingService
Expand Down Expand Up @@ -109,7 +114,11 @@ class RNIapAmazonModule(
}

@ReactMethod
fun getItemsByType(type: String?, skuArr: ReadableArray, promise: Promise) {
fun getItemsByType(
type: String?,
skuArr: ReadableArray,
promise: Promise,
) {
val productSkus: MutableSet<String> = HashSet()
var ii = 0
val skuSize = skuArr.size()
Expand Down Expand Up @@ -171,7 +180,10 @@ class RNIapAmazonModule(
* From https://amazon.developer.forums.answerhub.com/questions/175720/how-to-open-store-subscription-screen-directly-use.html?childToView=179402#answer-179402
*/
@ReactMethod
fun deepLinkToSubscriptions(isAmazonDevice: Boolean, promise: Promise) {
fun deepLinkToSubscriptions(
isAmazonDevice: Boolean,
promise: Promise,
) {
if (isAmazonDevice) {
val intent =
Intent("android.intent.action.VIEW", Uri.parse("amzn://apps/library/subscriptions"))
Expand Down Expand Up @@ -210,22 +222,26 @@ class RNIapAmazonModule(

const val TAG = "RNIapAmazonModule"
}

init {
val lifecycleEventListener: LifecycleEventListener = object : LifecycleEventListener {
/**
* From https://developer.amazon.com/docs/in-app-purchasing/iap-implement-iap.html#getpurchaseupdates-responses
* We should fetch updates on resume
*/
override fun onHostResume() {
if (RNIapActivityListener.hasListener) {
purchasingService.getUserData()
purchasingService.getPurchaseUpdates(false)
val lifecycleEventListener: LifecycleEventListener =
object : LifecycleEventListener {
/**
* From https://developer.amazon.com/docs/in-app-purchasing/iap-implement-iap.html#getpurchaseupdates-responses
* We should fetch updates on resume
*/
override fun onHostResume() {
if (RNIapActivityListener.hasListener) {
purchasingService.getUserData()
purchasingService.getPurchaseUpdates(false)
}
}

override fun onHostPause() {}

override fun onHostDestroy() {
}
}
override fun onHostPause() {}
override fun onHostDestroy() {
}
}
reactContext.addLifecycleEventListener(lifecycleEventListener)
}
}
5 changes: 1 addition & 4 deletions android/src/amazon/java/com/dooboolab/rniap/RNIapPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import com.facebook.react.uimanager.ViewManager
import java.util.ArrayList

class RNIapPackage : ReactPackage {

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> = emptyList()

override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
val modules: MutableList<NativeModule> = ArrayList()
Expand Down
11 changes: 9 additions & 2 deletions android/src/main/java/com/dooboolab/rniap/PromiseUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import java.util.HashMap

object PromiseUtils {
private val promises = HashMap<String, MutableList<Promise>>()
fun addPromiseForKey(key: String, promise: Promise) {

fun addPromiseForKey(
key: String,
promise: Promise,
) {
promises.getOrPut(key) { mutableListOf() }.add(promise)
}

fun resolvePromisesForKey(key: String, value: Any?) {
fun resolvePromisesForKey(
key: String,
value: Any?,
) {
promises[key]?.forEach { promise ->
promise.safeResolve(value)
}
Expand Down
21 changes: 15 additions & 6 deletions android/src/main/java/com/dooboolab/rniap/PromiseUtlis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ fun Promise.safeResolve(value: Any?) {

fun Promise.safeReject(message: String) = this.safeReject(message, null, null)

fun Promise.safeReject(code: String?, message: String?) = this.safeReject(code, message, null)

fun Promise.safeReject(code: String?, throwable: Throwable?) =
this.safeReject(code, null, throwable)

fun Promise.safeReject(code: String?, message: String?, throwable: Throwable?) {
fun Promise.safeReject(
code: String?,
message: String?,
) = this.safeReject(code, message, null)

fun Promise.safeReject(
code: String?,
throwable: Throwable?,
) = this.safeReject(code, null, throwable)

fun Promise.safeReject(
code: String?,
message: String?,
throwable: Throwable?,
) {
try {
this.reject(code, message, throwable)
} catch (oce: ObjectAlreadyConsumedException) {
Expand Down
Loading

0 comments on commit 513a6c6

Please sign in to comment.