Skip to content

Commit a013c5b

Browse files
committed
Restore ads implementation
1 parent a1db817 commit a013c5b

1 file changed

Lines changed: 58 additions & 57 deletions

File tree

App.tsx

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import * as Notifications from 'expo-notifications';
1111
import { db } from './firebase.config';
1212
import { doc, setDoc } from 'firebase/firestore';
1313
import AsyncStorage from '@react-native-async-storage/async-storage';
14-
// Ads implementation - COMMENTED OUT
15-
// import { useInterstitialAd } from './hooks/useInterstitialAd';
16-
// import { useExitAd } from './hooks/useExitAd';
17-
// import MobileAds from 'react-native-google-mobile-ads';
14+
// Ads implementation
15+
import { useInterstitialAd } from './hooks/useInterstitialAd';
16+
import { useExitAd } from './hooks/useExitAd';
17+
import MobileAds from 'react-native-google-mobile-ads';
1818

1919
// Create context for notifications navigation
2020
const NotificationsContext = createContext<{
@@ -140,7 +140,7 @@ const AppContent = () => {
140140
const [notification, setNotification] = useState<Notifications.Notification | false>(false);
141141
const notificationListener = useRef<Notifications.Subscription>();
142142
const responseListener = useRef<Notifications.Subscription>();
143-
// const [previousRoute, setPreviousRoute] = useState<string>('Home'); // COMMENTED OUT - only used for ads
143+
const [previousRoute, setPreviousRoute] = useState<string>('Home');
144144
const [showLanguageSelection, setShowLanguageSelection] = useState(false);
145145
const [isCheckingFirstLaunch, setIsCheckingFirstLaunch] = useState(true);
146146

@@ -163,31 +163,32 @@ const AppContent = () => {
163163
checkFirstLaunch();
164164
}, []);
165165

166-
// Track AdMob initialization state - COMMENTED OUT
167-
// const [adMobInitialized, setAdMobInitialized] = useState(false);
168-
169-
// Initialize Google Mobile Ads - COMMENTED OUT
170-
// useEffect(() => {
171-
// console.log('🚀 Starting AdMob initialization...');
172-
// MobileAds()
173-
// .initialize()
174-
// .then(adapterStatuses => {
175-
// console.log('✅ AdMob initialized successfully:', adapterStatuses);
176-
// setAdMobInitialized(true);
177-
// })
178-
// .catch(error => {
179-
// console.error('❌ AdMob initialization error:', error);
180-
// console.error('Error details:', JSON.stringify(error));
181-
// // Still set to true to allow ads to attempt loading
182-
// setAdMobInitialized(true);
183-
// });
184-
// }, []);
185-
186-
// Interstitial ad hook for tab navigation - COMMENTED OUT
187-
// const { showAd, isLoaded } = useInterstitialAd();
188-
189-
// Exit ad hook - shows rewarded interstitial when user presses back button - COMMENTED OUT
190-
// useExitAd({ enabled: adMobInitialized });
166+
// Track AdMob initialization state
167+
const [adMobInitialized, setAdMobInitialized] = useState(false);
168+
169+
// Initialize Google Mobile Ads
170+
useEffect(() => {
171+
console.log('🚀 Starting AdMob initialization...');
172+
MobileAds()
173+
.initialize()
174+
.then(adapterStatuses => {
175+
console.log('✅ AdMob initialized successfully:', adapterStatuses);
176+
setAdMobInitialized(true);
177+
})
178+
.catch(error => {
179+
console.error('❌ AdMob initialization error:', error);
180+
console.error('Error details:', JSON.stringify(error));
181+
// Still set to true to allow ads to attempt loading
182+
setAdMobInitialized(true);
183+
});
184+
}, []);
185+
186+
// Interstitial ad hook for tab navigation - ONLY AFTER initialization
187+
const { showAd, isLoaded } = useInterstitialAd();
188+
189+
// Exit ad hook - shows rewarded interstitial when user presses back button
190+
// ONLY enable after AdMob is initialized
191+
useExitAd({ enabled: adMobInitialized });
191192

192193
useEffect(() => {
193194
registerForPushNotificationsAsync().then(token => {
@@ -224,32 +225,32 @@ const AppContent = () => {
224225
};
225226
}, []);
226227

227-
// Handle navigation state changes to show interstitial ads - COMMENTED OUT
228-
// const handleNavigationStateChange = (state: any) => {
229-
// if (!state) return;
230-
//
231-
// const currentRoute = state.routes[state.index]?.name;
232-
//
233-
// // Log navigation changes
234-
// if (currentRoute && currentRoute !== previousRoute) {
235-
// console.log(`📱 Tab changed: ${previousRoute} → ${currentRoute}`);
236-
// console.log('Ad state - isLoaded:', isLoaded, 'adMobInitialized:', adMobInitialized);
237-
// }
238-
//
239-
// // Show interstitial ad occasionally when switching tabs (not every time to avoid annoyance)
240-
// // Show ad 30% of the time when changing tabs
241-
// const randomValue = Math.random();
242-
// if (currentRoute && currentRoute !== previousRoute && isLoaded && randomValue < 0.3) {
243-
// console.log(`🎲 Random value: ${randomValue.toFixed(2)} < 0.3 - Showing interstitial ad`);
244-
// showAd();
245-
// } else if (currentRoute && currentRoute !== previousRoute && isLoaded) {
246-
// console.log(`🎲 Random value: ${randomValue.toFixed(2)} >= 0.3 - Skipping ad this time`);
247-
// } else if (currentRoute && currentRoute !== previousRoute && !isLoaded) {
248-
// console.log('⚠️ Tab changed but ad not loaded yet');
249-
// }
250-
//
251-
// setPreviousRoute(currentRoute);
252-
// };
228+
// Handle navigation state changes to show interstitial ads
229+
const handleNavigationStateChange = (state: any) => {
230+
if (!state) return;
231+
232+
const currentRoute = state.routes[state.index]?.name;
233+
234+
// Log navigation changes
235+
if (currentRoute && currentRoute !== previousRoute) {
236+
console.log(`📱 Tab changed: ${previousRoute}${currentRoute}`);
237+
console.log('Ad state - isLoaded:', isLoaded, 'adMobInitialized:', adMobInitialized);
238+
}
239+
240+
// Show interstitial ad occasionally when switching tabs (not every time to avoid annoyance)
241+
// Show ad 30% of the time when changing tabs
242+
const randomValue = Math.random();
243+
if (currentRoute && currentRoute !== previousRoute && isLoaded && randomValue < 0.3) {
244+
console.log(`🎲 Random value: ${randomValue.toFixed(2)} < 0.3 - Showing interstitial ad`);
245+
showAd();
246+
} else if (currentRoute && currentRoute !== previousRoute && isLoaded) {
247+
console.log(`🎲 Random value: ${randomValue.toFixed(2)} >= 0.3 - Skipping ad this time`);
248+
} else if (currentRoute && currentRoute !== previousRoute && !isLoaded) {
249+
console.log('⚠️ Tab changed but ad not loaded yet');
250+
}
251+
252+
setPreviousRoute(currentRoute);
253+
};
253254

254255
return (
255256
<NotificationsContext.Provider value={{ openNotifications: () => setShowNotificationsScreen(true) }}>
@@ -275,7 +276,7 @@ const AppContent = () => {
275276
<StatusBar style="dark" />
276277
</SafeAreaView>
277278
) : (
278-
<NavigationContainer>
279+
<NavigationContainer onStateChange={handleNavigationStateChange}>
279280
<SafeAreaView style={{ flex: 1, backgroundColor: '#FFFFFF' }}>
280281

281282
<Tab.Navigator

0 commit comments

Comments
 (0)