Skip to content

Commit

Permalink
fix: Make MaBulle version work without IAP
Browse files Browse the repository at this point in the history
In b056a90 we added code to
dynamically remove IAP package using white_label mechanism

This correctly removed the IAP package and allowed to build the app

But since then, we added some react-native components that use the
module and would break when removing the IAP package (react-native
module is available, but the native part is not loaded)

To make them work without IAP package, we choose to move the import
behind a proxy file. Then we can override this file using white_labels
and instead import stubbed methods

The mandatory methods are `withIAPContext` and `useIAP` that wrap the
entire components and `getSubscriptions` that is used to enable/disable
IAP features

Other methods can still be exported as is as they would never be called
when IAP features are disabled

Related commit: b056a90
Related PR: #1188
  • Loading branch information
Ldoppea committed Mar 21, 2024
1 parent 13de86b commit 5da0a29
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/app/domain/iap/services/availableOffers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Platform } from 'react-native'
import { getSubscriptions as iapGetSubscriptions } from 'react-native-iap'

import Minilog from 'cozy-minilog'

import { getSubscriptions as iapGetSubscriptions } from '/app/domain/iap/services/iapModule'
import { getErrorMessage } from '/libs/functions/getErrorMessage'

const log = Minilog('💳 Available IAP Offers')
Expand Down
14 changes: 7 additions & 7 deletions src/app/domain/iap/services/clouderyOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { EventEmitter } from 'events'

import { Dispatch, SetStateAction } from 'react'
import { Linking, Platform } from 'react-native'
import type { WebViewNavigation } from 'react-native-webview/lib/WebViewTypes'

import CozyClient from 'cozy-client'
import type { InstanceInfo } from 'cozy-client/types/types'
import Minilog from 'cozy-minilog'

import {
clearTransactionIOS,
ProrationModesAndroid,
requestSubscription,
Subscription,
SubscriptionAndroid,
SubscriptionOffer
} from 'react-native-iap'
import type { WebViewNavigation } from 'react-native-webview/lib/WebViewTypes'

import CozyClient from 'cozy-client'
import type { InstanceInfo } from 'cozy-client/types/types'
import Minilog from 'cozy-minilog'

} from '/app/domain/iap/services/iapModule'
import { BuyingState } from '/app/view/IAP/hooks/useClouderyOffer'
import { getErrorMessage } from '/libs/functions/getErrorMessage'

Expand Down
1 change: 1 addition & 0 deletions src/app/domain/iap/services/iapModule.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'react-native-iap'
2 changes: 1 addition & 1 deletion src/app/view/IAP/ClouderyOffer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState } from 'react'
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'
import { withIAPContext } from 'react-native-iap'
import WebView from 'react-native-webview'
import type { WebViewNavigation } from 'react-native-webview/lib/WebViewTypes'

import { FlagshipUI } from 'cozy-intent'

import { withIAPContext } from '/app/domain/iap/services/iapModule'
import { ScreenIndexes, useFlagshipUI } from '/app/view/FlagshipUI'
import { useClouderyOffer } from '/app/view/IAP/hooks/useClouderyOffer'
import { IapProgress } from '/app/view/IAP/IapProgress'
Expand Down
2 changes: 1 addition & 1 deletion src/app/view/IAP/hooks/useClouderyOffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Dispatch, SetStateAction, useEffect, useState } from 'react'
import { initConnection, useIAP } from 'react-native-iap'
import { getLocales } from 'react-native-localize'
import Toast from 'react-native-toast-message'
import type { WebViewNavigation } from 'react-native-webview/lib/WebViewTypes'
Expand All @@ -15,6 +14,7 @@ import {
buySubscription,
formatOffers
} from '/app/domain/iap/services/clouderyOffer'
import { initConnection, useIAP } from '/app/domain/iap/services/iapModule'
import { getErrorMessage } from '/libs/functions/getErrorMessage'
import { t } from '/locales/i18n'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'react-native-iap'
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react'
import { Subscription } from 'react-native-iap'

export {
clearTransactionIOS,
ProrationModesAndroid,
requestSubscription
} from 'react-native-iap'

export type {
Subscription,
SubscriptionAndroid,
SubscriptionOffer
} from 'react-native-iap'

export { initConnection } from 'react-native-iap'

export const getSubscriptions = ({
skus
}: {
skus: string[]
}): Promise<Subscription[]> => {
return Promise.resolve([])
}

export function withIAPContext(Component: React.ComponentType) {
return function WrapperComponent(
props: JSX.IntrinsicAttributes
): JSX.Element {
return <Component {...props} />
}
}

export const useIAP = (): useIAPResult => {
const subscriptions: Subscription[] = []
const getSubscriptions = ({ skus }: { skus: string[] }): Promise<void> => {
return Promise.resolve()
}
return { subscriptions, getSubscriptions }
}

interface useIAPResult {
subscriptions: Subscription[]
getSubscriptions: ({ skus }: { skus: string[] }) => Promise<void>
}

0 comments on commit 5da0a29

Please sign in to comment.