Чтобы улучшить структуру моего приложения, я перемещаю большинство функций In App Purchase в специальный класс со статическими методами и переменными.
Когда я пытаюсь использовать этот класс функции кажутся вызванными дважды, хотя в моем коде я вызываю их только один раз . Когда я отлаживал его с помощью удаленной отладки в Chrome и с точкой останова в (1), он достигал этой точки дважды и оба раза, когда я смотрел на трассировку стека, она вызывалась с componentDidMount()
в App.js
.
В App.js
У меня есть это:
import IAPManager from "./helpers/IAPManager";
...
async componentDidMount() {
await IAPManager.prepare(); // (1) debug breakpoint
}
async componentWillUnmount() {
await IAPManager.endConnection();
}
И я также называю методы IAPManager
в другом компоненте:
import IAPManager from "../../helpers/IAPManager";
...
async componentDidMount() {
const isConnected = await NetInfo.isConnected.fetch();
if (!isConnected) {
console.log("Not connected to the internet");
Alert.alert(
"No connection",
"Uh oh, looks like you aren't connected to the internet, which you need to be to take advantage of Premium",
[{text: "OK"}]
);
} else {
let hasPurchasedPremium;
try {
hasPurchasedPremium = await IAPManager.isPremiumUnlocked();
} catch (e) {
console.log("Failed to check if premium is unlocked", e);
hasPurchasedPremium = false;
}
this.setState({ hasPurchasedPremium: hasPurchasedPremium });
if (hasPurchasedPremium) {
console.log("Has purchased premium");
} else {
console.log("Hasn't purchased premium");
const premiumProduct = await IAPManager.getPremiumProductInfo();
this.setState({ premiumProduct: premiumProduct });
}
}
}
Класс IAPManager
выглядит следующим образом:
import * as InAppPurchase from 'react-native-iap';
import { AsyncStorage, Platform } from "react-native";
const itemSKUs = Platform.select({
android: [
"unlock_premium"
]
});
export default class IAPManager {
static hasUnlockedPremium = undefined;
static productInfo = undefined;
static async prepare() {
await InAppPurchase.prepare();
console.log("done preparing");
}
static async endConnection() {
await InAppPurchase.endConnection();
}
static async getProductInfo() {
console.log("getProductInfo");
try {
const products = await InAppPurchase.getProducts(itemSKUs);
IAPManager.productInfo = products;
console.log("finished call to get products", IAPManager.productInfo);
} catch (e) {
console.log(e);
}
}
static async getPremiumProductInfo() {
console.log("getPremiumProductInfo");
if (IAPManager.productInfo === undefined) {
await this.getProductInfo();
}
const premiumProduct = IAPManager.productInfo.filter(product => product.productId === "unlock_premium");
return premiumProduct[0];
}
static async isPremiumUnlocked() {
console.log("isPremiumUnlocked");
if (IAPManager.hasUnlockedPremium !== undefined) {
return IAPManager.hasUnlockedPremium;
}
IAPManager.hasUnlockedPremium = true; // TODO: remove this when done testing
return IAPManager.hasUnlockedPremium;
}
}
И наконец, журналы, которые я получаю, выглядят так:
05-04 23:58:34.354 15867 20588 I ReactNativeJS: isPremiumUnlocked
05-04 23:58:34.370 15867 20588 I ReactNativeJS: Hasn't purchased premium
05-04 23:58:34.370 15867 20588 I ReactNativeJS: getPremiumProductInfo
05-04 23:58:34.384 15867 20588 I ReactNativeJS: isPremiumUnlocked
05-04 23:58:34.385 15867 20588 I ReactNativeJS: getProductInfo
05-04 23:58:34.388 15867 20588 I ReactNativeJS: isPremiumUnlocked
05-04 23:58:34.403 15867 20588 I ReactNativeJS: Hasn't purchased premium
05-04 23:58:34.403 15867 20588 I ReactNativeJS: getPremiumProductInfo
05-04 23:58:34.419 15867 20588 I ReactNativeJS: isPremiumUnlocked
05-04 23:58:34.422 15867 20588 I ReactNativeJS: getProductInfo
05-04 23:58:34.424 15867 20588 I ReactNativeJS: 'finished call to get products', [ { description: 'With premium you can set more than two timers at the same time.',
05-04 23:58:34.424 15867 20588 I ReactNativeJS: title: 'Unlock Premium',
05-04 23:58:34.424 15867 20588 I ReactNativeJS: localizedPrice: '19,00 kr',
05-04 23:58:34.424 15867 20588 I ReactNativeJS: type: 'inapp',
05-04 23:58:34.424 15867 20588 I ReactNativeJS: currency: 'SEK',
05-04 23:58:34.424 15867 20588 I ReactNativeJS: price: '19.00',
05-04 23:58:34.424 15867 20588 I ReactNativeJS: productId: 'unlock_premium' } ]
05-04 23:58:34.426 15867 20588 I ReactNativeJS: done preparing
05-04 23:58:34.666 15867 20588 I ReactNativeJS: 'finished call to get products', [ { description: 'With premium you can set more than two timers at the same time.',
05-04 23:58:34.666 15867 20588 I ReactNativeJS: title: 'Unlock Premium',
05-04 23:58:34.666 15867 20588 I ReactNativeJS: localizedPrice: '19,00 kr',
05-04 23:58:34.666 15867 20588 I ReactNativeJS: type: 'inapp',
05-04 23:58:34.666 15867 20588 I ReactNativeJS: currency: 'SEK',
05-04 23:58:34.666 15867 20588 I ReactNativeJS: price: '19.00',
05-04 23:58:34.666 15867 20588 I ReactNativeJS: productId: 'unlock_premium' } ]
05-04 23:58:34.668 15867 20588 I ReactNativeJS: done preparing
Спасибо, что прочитали это далеко, и я ценю любую помощь:)!