Skip to content

Commit a9e539a

Browse files
Merge pull request #5 from godot-x/fix-android-fetch
fix android fetch
2 parents 9563e92 + f65bd45 commit a9e539a

4 files changed

Lines changed: 65 additions & 22 deletions

File tree

README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,36 @@ func _on_purchase_result(data):
135135
revenuecat.fetch_offerings()
136136
137137
# Products (for custom UI)
138-
revenuecat.fetch_products(["premium_monthly", "premium_yearly"])
139138
revenuecat.products.connect(_on_products_received)
139+
revenuecat.fetch_products(["premium_monthly", "premium_yearly"])
140140
141-
func _on_products_received(data):
142-
if data["error"].is_empty():
143-
var products = data["products"]
144-
for product in products:
145-
print("ID: ", product["id"])
146-
print("Title: ", product["title"])
147-
print("Price: ", product["price"])
148-
else:
149-
print("Error: ", data["error"])
141+
func _on_products_received(data: Dictionary):
142+
var error = data.get("error", "")
143+
if error != "":
144+
print("Error: ", error)
145+
return
146+
147+
var raw_products = data.get("products", null)
148+
if raw_products == null:
149+
return
150+
151+
var products_list: Array = []
152+
153+
# ios - native array
154+
if raw_products is Array:
155+
products_list = raw_products
156+
157+
# android - array list
158+
elif raw_products is JavaObject:
159+
var count: int = raw_products.call("size")
160+
for i in range(count):
161+
products_list.append(raw_products.call("get", i))
162+
163+
# process products
164+
for product in products_list:
165+
print("ID: ", product["id"])
166+
print("Title: ", product["title"])
167+
print("Price: ", product["price"])
150168
```
151169

152170
### Purchases Flows

addons/godotx_revenue_cat/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="Godotx RevenueCat"
44
description="RevenueCat integration for Godot"
55
author="Paulo Coutinho"
6-
version="1.4.0"
6+
version="1.5.0"
77
script="export_plugin.gd"

scripts/Main.gd

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,32 @@ func _on_offerings_received(data):
257257
log_message(dict_to_string(data))
258258

259259

260-
func _on_products_received(data):
260+
func _on_products_received(data: Dictionary):
261261
log_message("🔔 SIGNAL: products")
262-
if data.has("error") and not data["error"].is_empty():
263-
log_message(" ❌ Error: %s" % data["error"])
264-
if data.has("products"):
265-
log_message(array_to_string(data["products"]))
262+
263+
var error = data.get("error", "")
264+
if error != "":
265+
log_message(" ❌ Error: %s" % error)
266+
return
267+
268+
var raw_products = data.get("products", null)
269+
if raw_products == null:
270+
log_message(" ⚠️ No products found")
271+
return
272+
273+
var products_list: Array = []
274+
275+
# ios - native array
276+
if raw_products is Array:
277+
products_list = raw_products
278+
279+
# android - array list
280+
elif raw_products is JavaObject:
281+
var count: int = raw_products.call("size")
282+
for i in range(count):
283+
products_list.append(raw_products.call("get", i))
284+
285+
log_message(array_to_string(products_list))
266286

267287

268288
func _on_login_finished(data):

source/android/revenue_cat/src/main/java/com/godotx/revenuecat/RevenueCatPlugin.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.godotengine.godot.Godot
2525
import org.godotengine.godot.plugin.GodotPlugin
2626
import org.godotengine.godot.plugin.SignalInfo
2727
import org.godotengine.godot.plugin.UsedByGodot
28+
import java.util.ArrayList
2829

2930
class RevenueCatPlugin(godot: Godot) : GodotPlugin(godot) {
3031

@@ -266,25 +267,29 @@ class RevenueCatPlugin(godot: Godot) : GodotPlugin(godot) {
266267
fun fetch_products(ids: Array<String>) {
267268
Purchases.sharedInstance.getProductsWith(
268269
productIds = ids.toList(),
270+
269271
onError = { error ->
270272
val result = Dictionary()
271-
result["products"] = arrayOf<Any>()
272-
result["error"] = error.message
273+
result["products"] = ArrayList<Dictionary>()
274+
result["error"] = error.message ?: ""
273275
emitOnMain("products", result)
274276
},
277+
275278
onGetStoreProducts = { products ->
276-
val arr = products.map { p ->
279+
val list = ArrayList<Dictionary>()
280+
281+
for (p in products) {
277282
val d = Dictionary()
278283
d["id"] = p.id
279284
d["title"] = p.title
280285
d["description"] = p.description
281286
d["price"] = p.price.formatted
282287
d["amount"] = p.price.amountMicros / 1_000_000.0
283-
d
284-
}.toTypedArray()
288+
list.add(d)
289+
}
285290

286291
val result = Dictionary()
287-
result["products"] = arr
292+
result["products"] = list
288293
result["error"] = ""
289294
emitOnMain("products", result)
290295
}

0 commit comments

Comments
 (0)