2020-02-19 20:52:05 +00:00
|
|
|
import React from 'react';
|
2021-11-17 19:55:22 +00:00
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
2020-10-30 13:59:44 +00:00
|
|
|
import { FlatList, Linking } from 'react-native';
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2022-05-03 16:12:43 +00:00
|
|
|
import { SettingsStackParamList } from '../stacks/types';
|
|
|
|
import { IBaseScreen } from '../definitions';
|
2020-02-19 20:52:05 +00:00
|
|
|
import I18n from '../i18n';
|
2022-05-03 16:12:43 +00:00
|
|
|
import { withTheme } from '../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2020-02-19 20:52:05 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2020-02-19 20:52:05 +00:00
|
|
|
import { DEFAULT_BROWSER_KEY } from '../utils/openLink';
|
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2022-04-07 13:13:19 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { events, logEvent } from '../utils/log';
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2021-11-17 19:55:22 +00:00
|
|
|
type TValue = 'inApp' | 'systemDefault:' | 'googlechrome:' | 'firefox:' | 'brave:';
|
|
|
|
|
|
|
|
interface IBrowsersValues {
|
|
|
|
title: string;
|
|
|
|
value: TValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_BROWSERS: IBrowsersValues[] = [
|
2020-02-19 20:52:05 +00:00
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
title: 'In_app',
|
2020-02-19 20:52:05 +00:00
|
|
|
value: 'inApp'
|
|
|
|
},
|
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
title: isIOS ? 'Safari' : 'Browser',
|
2020-02-19 20:52:05 +00:00
|
|
|
value: 'systemDefault:'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-11-17 19:55:22 +00:00
|
|
|
const BROWSERS: IBrowsersValues[] = [
|
2020-02-19 20:52:05 +00:00
|
|
|
{
|
|
|
|
title: 'Chrome',
|
|
|
|
value: 'googlechrome:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Firefox',
|
|
|
|
value: 'firefox:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Brave',
|
|
|
|
value: 'brave:'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-11-17 19:55:22 +00:00
|
|
|
interface IDefaultBrowserViewState {
|
2022-05-03 16:12:43 +00:00
|
|
|
browser: string | null;
|
|
|
|
supported: IBrowsersValues[];
|
2021-11-17 19:55:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 16:12:43 +00:00
|
|
|
type IDefaultBrowserViewProps = IBaseScreen<SettingsStackParamList, 'DefaultBrowserView'>;
|
2021-11-17 19:55:22 +00:00
|
|
|
|
|
|
|
class DefaultBrowserView extends React.Component<IDefaultBrowserViewProps, IDefaultBrowserViewState> {
|
|
|
|
static navigationOptions = (): StackNavigationOptions => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Default_browser')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2021-11-17 19:55:22 +00:00
|
|
|
constructor(props: IDefaultBrowserViewProps) {
|
2020-02-19 20:52:05 +00:00
|
|
|
super(props);
|
2020-05-08 16:37:49 +00:00
|
|
|
this.state = {
|
|
|
|
browser: null,
|
|
|
|
supported: []
|
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
if (isIOS) {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
componentDidMount() {
|
|
|
|
const browser = UserPreferences.getString(DEFAULT_BROWSER_KEY);
|
2020-08-19 17:14:22 +00:00
|
|
|
this.setState({ browser });
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init = () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
BROWSERS.forEach(browser => {
|
2020-02-19 20:52:05 +00:00
|
|
|
const { value } = browser;
|
2021-09-13 20:41:05 +00:00
|
|
|
Linking.canOpenURL(value).then(installed => {
|
2020-02-19 20:52:05 +00:00
|
|
|
if (installed) {
|
2022-05-03 16:12:43 +00:00
|
|
|
this.setState(({ supported }) => ({ supported: [...supported, browser] }));
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2021-11-17 19:55:22 +00:00
|
|
|
isSelected = (value: TValue) => {
|
2020-02-19 20:52:05 +00:00
|
|
|
const { browser } = this.state;
|
2021-07-12 19:19:25 +00:00
|
|
|
if (!browser && value === 'systemDefault:') {
|
2020-02-19 20:52:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return browser === value;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
changeDefaultBrowser = (newBrowser: TValue) => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.DB_CHANGE_DEFAULT_BROWSER, { browser: newBrowser });
|
2020-02-19 20:52:05 +00:00
|
|
|
try {
|
2022-01-11 14:23:43 +00:00
|
|
|
const browser = newBrowser || 'systemDefault:';
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.setString(DEFAULT_BROWSER_KEY, browser);
|
2020-02-19 20:52:05 +00:00
|
|
|
this.setState({ browser });
|
|
|
|
} catch {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.DB_CHANGE_DEFAULT_BROWSER_F);
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
|
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' color={themes[theme].tintColor} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2021-11-17 19:55:22 +00:00
|
|
|
renderItem = ({ item }: { item: IBrowsersValues }) => {
|
2020-02-19 20:52:05 +00:00
|
|
|
const { title, value } = item;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2020-07-24 19:23:34 +00:00
|
|
|
title={I18n.t(title, { defaultValue: title })}
|
2020-02-19 20:52:05 +00:00
|
|
|
onPress={() => this.changeDefaultBrowser(value)}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`default-browser-view-${title}`}
|
2022-01-17 16:10:39 +00:00
|
|
|
right={() => (this.isSelected(value) ? this.renderIcon() : null)}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2020-02-19 20:52:05 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
renderHeader = () => (
|
|
|
|
<>
|
|
|
|
<List.Header title='Choose_where_you_want_links_be_opened' />
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
2020-02-19 20:52:05 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { supported } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='default-browser-view'>
|
|
|
|
<StatusBar />
|
2020-02-19 20:52:05 +00:00
|
|
|
<FlatList
|
|
|
|
data={DEFAULT_BROWSERS.concat(supported)}
|
|
|
|
keyExtractor={item => item.value}
|
2020-10-30 13:59:44 +00:00
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
2020-02-19 20:52:05 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
2020-10-30 13:59:44 +00:00
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
2020-02-19 20:52:05 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(DefaultBrowserView);
|