Created
August 3, 2023 12:45
-
-
Save pSapien/454ea1b43f59477b571e89d83229e5fb to your computer and use it in GitHub Desktop.
react-native hooks that uses the native binding for yodomas sdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from 'react'; | |
import { NativeModules, NativeEventEmitter, EmitterSubscription, Button } from 'react-native'; | |
const { Yodo1MASAds } = NativeModules; | |
const Constants = { | |
EVENT_NAME: 'yodo1ad', | |
INIT_SUCCESS: 'init-success', | |
INIT_FAILURE: 'init-failure', | |
REWARD_LOAD_SUCCESS: 'reward-load-success', | |
REWARD_LOAD_FAILURE: 'reward-load-failure', | |
REWARD_OPENED_SUCCESS: 'reward-open-success', | |
REWARD_OPENED_FAILURE: 'reward-open-failure', | |
REWARD_CLOSED: 'reward-closed', | |
REWARD_EARNED: 'reward-earned', | |
INTERSTITIAL_LOAD_SUCCESS: 'interstitial-load-success', | |
INTERSTITIAL_LOAD_FAILURE: 'interstitial-load-failure', | |
INTERSTITIAL_OPENED_SUCCESS: 'interstitial-open-success', | |
INTERSTITIAL_OPENED_FAILURE: 'interstitial-open-failure', | |
INTERSTITIAL_CLOSED: 'interstitial-closed', | |
INTERSTITIAL_EARNED: 'interstitial-earned', | |
} as const; | |
const nativeYodoEmitter = new NativeEventEmitter(NativeModules.Yodo1MASAds); | |
type YodoEvent = { | |
type: typeof Constants[keyof typeof Constants]; | |
error: string; | |
}; | |
type State = (() => void) | null; | |
class Subscription { | |
subscribers = new Set<(args: State) => void>(); | |
state: State = null; | |
getState() { | |
return this.state as State; | |
} | |
subscribe(subFn: () => void) { | |
this.subscribers.add(subFn); | |
return () => { | |
this.subscribers.delete(subFn); | |
}; | |
} | |
dispatch = (subValue: State) => { | |
this.state = subValue; | |
this.subscribers.forEach(s => s(subValue)); | |
}; | |
} | |
type RewardedAdReward = { type: 'diamonds', amount: number }; | |
type ReturnPromiseFn = ((value: RewardedAdReward | null) => void) | null; | |
type ReturnInterFn = () => void; | |
class AdManager { | |
key: string; | |
interAdSubscription = new Subscription(); | |
rewardedAdSubscription = new Subscription(); | |
initializedCalled = false; | |
returnRewardPromiseFn: ReturnPromiseFn = null; | |
returnInterPromiseFn: ReturnInterFn = null; | |
constructor(key: string) { | |
this.key = key; | |
} | |
public init = () => { | |
let yodoSub: EmitterSubscription | null = null; | |
if (!this.initializedCalled) { | |
yodoSub = nativeYodoEmitter.addListener('yodo1ad', (event: YodoEvent) => { | |
console.log('this is the yodoad', event); | |
switch (event.type) { | |
case Constants.INIT_SUCCESS: | |
Yodo1MASAds.loadRewardedAds(); | |
Yodo1MASAds.loadInterstitialAds(); | |
break; | |
case Constants.INTERSTITIAL_LOAD_SUCCESS: | |
this.interAdSubscription.dispatch(() => this.showInterAd); | |
break; | |
case Constants.INTERSTITIAL_OPENED_SUCCESS: | |
this.interAdSubscription.dispatch(null); | |
break; | |
case Constants.INTERSTITIAL_CLOSED: | |
case Constants.INTERSTITIAL_LOAD_FAILURE: | |
case Constants.INTERSTITIAL_OPENED_FAILURE: | |
this.returnInterPromiseFn(); | |
this.returnInterPromiseFn = null; | |
Yodo1MASAds.loadInterstitialAds(); | |
break; | |
case Constants.REWARD_LOAD_SUCCESS: | |
this.rewardedAdSubscription.dispatch(() => this.showRewardAd); | |
break; | |
case Constants.REWARD_OPENED_SUCCESS: | |
this.rewardedAdSubscription.dispatch(null); | |
break; | |
case Constants.REWARD_EARNED: | |
this.returnRewardPromiseFn({ type: 'diamonds', amount: 10 }); | |
this.returnRewardPromiseFn = null; | |
break; | |
case Constants.REWARD_CLOSED: | |
case Constants.REWARD_LOAD_FAILURE: | |
case Constants.REWARD_OPENED_FAILURE: | |
if (this.returnRewardPromiseFn) this.returnRewardPromiseFn(null); | |
this.returnRewardPromiseFn = null; | |
Yodo1MASAds.loadRewardedAds(); | |
break; | |
default: | |
break; | |
} | |
}); | |
Yodo1MASAds.initSdk(this.key); | |
this.initializedCalled = true; | |
} | |
return () => { | |
if (yodoSub) yodoSub.remove(); | |
}; | |
}; | |
private showRewardAd = async () => { | |
return new Promise<RewardedAdReward | null>((resolve) => { | |
this.returnRewardPromiseFn = resolve; | |
Yodo1MASAds.showRewardedAds(); | |
}); | |
} | |
private showInterAd = async () => { | |
return new Promise<void>((resolve) => { | |
this.returnInterPromiseFn = resolve; | |
Yodo1MASAds.showInterstitialAds(); | |
}); | |
} | |
} | |
const adManager = new AdManager(global.YODO_ADMOB_APP_KEY); | |
export function useInitAdmob(): null { | |
useEffect(() => adManager.init(), []); | |
return null; | |
} | |
export function useInterstitialAd() { | |
const [showFn, setShowFn] = useState(adManager.interAdSubscription.getState()); | |
useEffect(() => adManager.interAdSubscription.subscribe(setShowFn), []); | |
return showFn; | |
} | |
export function useRewardAd() { | |
const [showFn, setShowFn] = useState(adManager.rewardedAdSubscription.getState()); | |
useEffect(() => adManager.rewardedAdSubscription.subscribe(setShowFn), []); | |
return showFn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment