-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathindex.tsx
More file actions
255 lines (238 loc) · 8.59 KB
/
index.tsx
File metadata and controls
255 lines (238 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React from 'react';
import {
Text,
View,
TouchableOpacity,
Alert,
ScrollView,
Platform,
} from 'react-native';
import Qonversion, { PurchaseResult } from '@qonversion/react-native-sdk';
import Snackbar from 'react-native-snackbar';
import { AppContext } from '../../store/AppStore';
import SkeletonLoader from '../../components/SkeletonLoader';
import styles from './styles';
const EntitlementsScreen: React.FC = () => {
const context = React.useContext(AppContext);
if (!context) return null;
const { state, dispatch } = context;
const loadEntitlements = async () => {
try {
console.log('🔄 [Qonversion] Starting checkEntitlements() call...');
dispatch({ type: 'SET_LOADING', payload: true });
const entitlements =
await Qonversion.getSharedInstance().checkEntitlements();
console.log(
'✅ [Qonversion] checkEntitlements() call successful:',
Object.fromEntries(entitlements)
);
dispatch({ type: 'SET_ENTITLEMENTS', payload: entitlements });
} catch (error: any) {
console.error('❌ [Qonversion] checkEntitlements() call failed:', error);
Alert.alert('Error', error.message);
} finally {
dispatch({ type: 'SET_LOADING', payload: false });
}
};
const setDeferredPurchasesListener = () => {
console.log('🔄 [Qonversion] Setting deferred purchases listener...');
Qonversion.getSharedInstance().setDeferredPurchasesListener({
onDeferredPurchaseCompleted(purchaseResult: PurchaseResult) {
console.log(
'📡 [Qonversion] Deferred purchase completed via listener:',
purchaseResult
);
if (purchaseResult.entitlements) {
dispatch({
type: 'SET_ENTITLEMENTS',
payload: purchaseResult.entitlements,
});
}
},
});
console.log(
'✅ [Qonversion] Deferred purchases listener set successfully'
);
Snackbar.show({
text: 'Deferred purchases listener set successfully!',
duration: Snackbar.LENGTH_SHORT,
});
};
const restore = async () => {
try {
console.log('🔄 [Qonversion] Starting restore() call...');
dispatch({ type: 'SET_LOADING', payload: true });
const entitlements = await Qonversion.getSharedInstance().restore();
console.log('✅ [Qonversion] restore() call successful:', Object.fromEntries(entitlements));
dispatch({ type: 'SET_ENTITLEMENTS', payload: entitlements });
Snackbar.show({
text: 'Purchases restored successfully!',
duration: Snackbar.LENGTH_SHORT,
});
} catch (error: any) {
console.error('❌ [Qonversion] restore() call failed:', error);
Alert.alert('Error', error.message);
} finally {
dispatch({ type: 'SET_LOADING', payload: false });
}
};
const syncHistoricalData = async () => {
try {
console.log('🔄 [Qonversion] Starting syncHistoricalData() call...');
dispatch({ type: 'SET_LOADING', payload: true });
Qonversion.getSharedInstance().syncHistoricalData();
console.log('✅ [Qonversion] syncHistoricalData() call successful');
Snackbar.show({
text: 'Historical data synced successfully!',
duration: Snackbar.LENGTH_SHORT,
});
} catch (error: any) {
console.error('❌ [Qonversion] syncHistoricalData() call failed:', error);
Alert.alert('Error', error.message);
} finally {
dispatch({ type: 'SET_LOADING', payload: false });
}
};
const syncStoreKit2Purchases = async () => {
if (Platform.OS !== 'ios') {
Alert.alert('Error', 'This method is iOS only');
return;
}
try {
Qonversion.getSharedInstance().syncStoreKit2Purchases();
Snackbar.show({
text: 'StoreKit 2 purchases synced!',
duration: Snackbar.LENGTH_SHORT,
});
} catch (error: any) {
Alert.alert('Error', error.message);
}
};
const syncPurchases = async () => {
if (Platform.OS !== 'android') {
Alert.alert('Error', 'This method is Android only');
return;
}
try {
Qonversion.getSharedInstance().syncPurchases();
Snackbar.show({
text: 'Purchases synced!',
duration: Snackbar.LENGTH_SHORT,
});
} catch (error: any) {
Alert.alert('Error', error.message);
}
};
const formatDate = (date: Date) => {
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
};
const renderContent = () => {
return (
<ScrollView
style={styles.container}
contentContainerStyle={styles.contentContainer}
>
<TouchableOpacity style={styles.button} onPress={loadEntitlements}>
<Text style={styles.buttonText}>Load Entitlements</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={setDeferredPurchasesListener}
>
<Text style={styles.buttonText}>
Set Deferred Purchases Listener
</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={restore}>
<Text style={styles.buttonText}>Restore</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={syncHistoricalData}>
<Text style={styles.buttonText}>Sync Historical Data</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={syncStoreKit2Purchases}
>
<Text style={styles.buttonText}>
Sync StoreKit 2 Purchases (iOS Only)
</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={syncPurchases}>
<Text style={styles.buttonText}>Sync Purchases (Android Only)</Text>
</TouchableOpacity>
{/* Entitlements Status Section */}
<View style={styles.statusContainer}>
{state.loading ? (
// Loading state - show skeleton only for entitlements section
<View style={styles.listContainer}>
<Text style={styles.sectionTitle}>Your Entitlements</Text>
<SkeletonLoader />
</View>
) : state.entitlements === null ? (
// Not loaded state
<View style={styles.emptyStateContainer}>
<Text style={styles.emptyStateTitle}>No Entitlements Loaded</Text>
<Text style={styles.emptyStateText}>
Tap "Load Entitlements" to fetch your current entitlements from
the server.
</Text>
</View>
) : state.entitlements.size === 0 ? (
// Empty entitlements state
<View style={styles.emptyStateContainer}>
<Text style={styles.emptyStateTitle}>No Entitlements Found</Text>
<Text style={styles.emptyStateText}>
You don't have any active entitlements. Try restoring purchases
or check your subscription status.
</Text>
</View>
) : (
// Entitlements list
<View style={styles.listContainer}>
<Text style={styles.sectionTitle}>Your Entitlements</Text>
{Array.from(state.entitlements.entries()).map(
([id, entitlement]) => (
<TouchableOpacity
key={id}
style={styles.listItem}
onPress={() => {
dispatch({
type: 'SET_SELECTED_ENTITLEMENT',
payload: entitlement,
});
dispatch({
type: 'PUSH_SCREEN',
payload: 'entitlementDetail',
});
}}
>
<Text style={styles.listItemTitle}>{entitlement.id}</Text>
<Text style={styles.listItemSubtitle}>
Status: {entitlement.isActive ? 'Active' : 'Inactive'}
</Text>
<Text style={styles.listItemSubtitle}>
Started: {formatDate(entitlement.startedDate)}
</Text>
{entitlement.expirationDate && (
<Text style={styles.listItemSubtitle}>
Expires: {formatDate(entitlement.expirationDate)}
</Text>
)}
</TouchableOpacity>
)
)}
</View>
)}
</View>
</ScrollView>
);
};
return renderContent();
};
export default EntitlementsScreen;