2020-05-08 17:04:37 +00:00
|
|
|
import { Alert, Linking } from 'react-native';
|
2022-08-19 19:53:40 +00:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
2020-02-03 18:28:18 +00:00
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import I18n from '../../../i18n';
|
|
|
|
import { isFDroidBuild, STORE_REVIEW_LINK } from '../../constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { showErrorAlert } from './info';
|
|
|
|
import { isIOS } from './deviceInfo';
|
|
|
|
import { events, logEvent } from './log';
|
2020-02-03 18:28:18 +00:00
|
|
|
|
|
|
|
const store = isIOS ? 'App Store' : 'Play Store';
|
|
|
|
|
|
|
|
const reviewKey = 'reviewKey';
|
|
|
|
const reviewDelay = 2000;
|
|
|
|
const numberOfDays = 7;
|
|
|
|
const numberOfPositiveEvent = 5;
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const daysBetween = (date1: Date, date2: Date): number => {
|
2020-02-03 18:28:18 +00:00
|
|
|
const one_day = 1000 * 60 * 60 * 24;
|
|
|
|
const date1_ms = date1.getTime();
|
|
|
|
const date2_ms = date2.getTime();
|
|
|
|
const difference_ms = date2_ms - date1_ms;
|
|
|
|
return Math.round(difference_ms / one_day);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCancelPress = () => {
|
|
|
|
try {
|
|
|
|
const data = JSON.stringify({ doneReview: true });
|
|
|
|
return AsyncStorage.setItem(reviewKey, data);
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
export const onReviewPress = async (): Promise<void> => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SE_REVIEW_THIS_APP);
|
2020-02-03 18:28:18 +00:00
|
|
|
await onCancelPress();
|
|
|
|
try {
|
|
|
|
const supported = await Linking.canOpenURL(STORE_REVIEW_LINK);
|
|
|
|
if (supported) {
|
|
|
|
Linking.openURL(STORE_REVIEW_LINK);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SE_REVIEW_THIS_APP_F);
|
2020-02-03 18:28:18 +00:00
|
|
|
showErrorAlert(I18n.t('Review_app_unable_store', { store }));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onAskMeLaterPress = () => {
|
|
|
|
try {
|
|
|
|
const data = JSON.stringify({ lastReview: new Date().getTime() });
|
|
|
|
return AsyncStorage.setItem(reviewKey, data);
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onReviewButton = { text: I18n.t('Review_app_yes'), onPress: onReviewPress };
|
|
|
|
const onAskMeLaterButton = { text: I18n.t('Review_app_later'), onPress: onAskMeLaterPress };
|
|
|
|
const onCancelButton = { text: I18n.t('Review_app_no'), onPress: onCancelPress };
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const askReview = () =>
|
|
|
|
Alert.alert(
|
|
|
|
I18n.t('Review_app_title'),
|
|
|
|
I18n.t('Review_app_desc', { store }),
|
|
|
|
isIOS ? [onReviewButton, onAskMeLaterButton, onCancelButton] : [onAskMeLaterButton, onCancelButton, onReviewButton],
|
|
|
|
{
|
|
|
|
cancelable: true,
|
|
|
|
onDismiss: onAskMeLaterPress
|
|
|
|
}
|
|
|
|
);
|
2020-02-03 18:28:18 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const tryReview = async () => {
|
|
|
|
const data = (await AsyncStorage.getItem(reviewKey)) || '{}';
|
2020-02-03 18:28:18 +00:00
|
|
|
const reviewData = JSON.parse(data);
|
|
|
|
const { lastReview = 0, doneReview = false } = reviewData;
|
|
|
|
const lastReviewDate = new Date(lastReview);
|
|
|
|
|
|
|
|
// if ask me later was pressed earlier, we can ask for review only after {{numberOfDays}} days
|
|
|
|
// if there's no review and it wasn't dismissed by the user
|
|
|
|
if (daysBetween(lastReviewDate, new Date()) >= numberOfDays && !doneReview) {
|
|
|
|
setTimeout(askReview, reviewDelay);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReviewApp {
|
|
|
|
positiveEventCount = 0;
|
|
|
|
|
|
|
|
pushPositiveEvent = () => {
|
2023-04-10 14:59:00 +00:00
|
|
|
if (isFDroidBuild || process.env.RUNNING_E2E_TESTS === 'true') {
|
2023-03-07 12:28:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.positiveEventCount >= numberOfPositiveEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.positiveEventCount += 1;
|
|
|
|
if (this.positiveEventCount === numberOfPositiveEvent) {
|
|
|
|
tryReview();
|
2020-02-03 18:28:18 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-03 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Review = new ReviewApp();
|