Use of cached managed config in components (#2758)

This commit is contained in:
Elias Nahum 2019-05-01 18:34:09 -04:00
parent 55d029bde3
commit 584efe2b12
No known key found for this signature in database
GPG key ID: E038DB71E0B61702
11 changed files with 34 additions and 164 deletions

View file

@ -97,9 +97,9 @@ export default class AtMention extends React.PureComponent {
handleLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'false') {
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});
const actionText = formatMessage({id: 'mobile.mention.copy_mention', defaultMessage: 'Copy Mention'});

View file

@ -82,7 +82,7 @@ export default class MarkdownCodeBlock extends React.PureComponent {
handleLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});

View file

@ -144,7 +144,7 @@ export default class MarkdownImage extends React.Component {
handleLinkLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});

View file

@ -99,7 +99,7 @@ export default class MarkdownLink extends PureComponent {
handleLongPress = async () => {
const {formatMessage} = this.context.intl;
const config = await mattermostManaged.getLocalConfig();
const config = mattermostManaged.getCachedConfig();
if (config.copyAndPasteProtection !== 'true') {
const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'});

View file

@ -57,15 +57,6 @@ export default class PostListBase extends PureComponent {
siteURL: '',
};
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.mounted = true;
this.setManagedConfig();
}
componentDidUpdate() {
if (this.props.deepLinkURL) {
this.handleDeepLink(this.props.deepLinkURL);
@ -73,11 +64,6 @@ export default class PostListBase extends PureComponent {
}
}
componentWillUnmount() {
this.mounted = false;
mattermostManaged.removeEventListener(this.listenerId);
}
handleClosePermalink = () => {
const {actions} = this.props;
actions.selectFocusedPostId('');
@ -173,7 +159,6 @@ export default class PostListBase extends PureComponent {
shouldRenderReplyButton,
location,
} = this.props;
const {managedConfig} = this.state;
const highlight = highlightPostId === postId;
return (
@ -190,25 +175,12 @@ export default class PostListBase extends PureComponent {
shouldRenderReplyButton={shouldRenderReplyButton}
onPress={onPostPress}
navigator={navigator}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
location={location}
/>
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
if (this.mounted) {
this.setState({
managedConfig: nextConfig,
});
}
};
showPermalinkView = (postId) => {
const {actions, navigator} = this.props;

View file

@ -8,12 +8,12 @@ import JailMonkey from 'jail-monkey';
const {MattermostManaged} = NativeModules;
const listeners = [];
let localConfig;
let cachedConfig = {};
export default {
addEventListener: (name, callback) => {
const listener = DeviceEventEmitter.addListener(name, (config) => {
localConfig = config;
cachedConfig = config;
if (callback && typeof callback === 'function') {
callback(config);
}
@ -36,17 +36,17 @@ export default {
},
authenticate: LocalAuth.auth,
blurAppScreen: MattermostManaged.blurAppScreen,
getConfig: MattermostManaged.getConfig,
getLocalConfig: async () => {
if (!localConfig) {
try {
localConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
getConfig: async () => {
try {
cachedConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
return localConfig || {};
return cachedConfig;
},
getCachedConfig: () => {
return cachedConfig;
},
isDeviceSecure: async () => {
try {

View file

@ -8,12 +8,12 @@ const {BlurAppScreen, MattermostManaged} = NativeModules;
const mattermostManagedEmitter = new NativeEventEmitter(MattermostManaged);
const listeners = [];
let localConfig;
let cachedConfig = {};
export default {
addEventListener: (name, callback) => {
const listener = mattermostManagedEmitter.addListener(name, (config) => {
localConfig = config;
cachedConfig = config;
if (callback && typeof callback === 'function') {
callback(config);
}
@ -36,17 +36,17 @@ export default {
},
authenticate: LocalAuth.authenticate,
blurAppScreen: BlurAppScreen.enabled,
getConfig: MattermostManaged.getConfig,
getLocalConfig: async () => {
if (!localConfig) {
try {
localConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
getConfig: async () => {
try {
cachedConfig = await MattermostManaged.getConfig();
} catch (error) {
// do nothing...
}
return localConfig || {};
return cachedConfig;
},
getCachedConfig: () => {
return cachedConfig;
},
isDeviceSecure: async () => {
try {

View file

@ -56,22 +56,6 @@ export default class FlaggedPosts extends PureComponent {
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
props.actions.clearSearch();
props.actions.getFlaggedPosts();
this.state = {
managedConfig: {},
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.setManagedConfig();
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
goToThread = (post) => {
@ -158,7 +142,7 @@ export default class FlaggedPosts extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
@ -185,7 +169,7 @@ export default class FlaggedPosts extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
showFullDate={false}
skipFlaggedHeader={true}
skipPinnedHeader={true}
@ -195,17 +179,6 @@ export default class FlaggedPosts extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;

View file

@ -55,27 +55,14 @@ export default class PinnedPosts extends PureComponent {
super(props);
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
this.state = {
managedConfig: {},
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
const {actions, currentChannelId} = this.props;
this.setManagedConfig();
actions.clearSearch();
actions.getPinnedPosts(currentChannelId);
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
goToThread = (post) => {
const {actions, navigator, theme} = this.props;
const channelId = post.channel_id;
@ -160,7 +147,6 @@ export default class PinnedPosts extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
@ -186,7 +172,7 @@ export default class PinnedPosts extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
showFullDate={false}
skipFlaggedHeader={false}
skipPinnedHeader={true}
@ -196,17 +182,6 @@ export default class PinnedPosts extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;

View file

@ -56,22 +56,6 @@ export default class RecentMentions extends PureComponent {
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
props.actions.clearSearch();
props.actions.getRecentMentions();
this.state = {
managedConfig: {},
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.setManagedConfig();
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
goToThread = (post) => {
@ -158,7 +142,7 @@ export default class RecentMentions extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
@ -184,7 +168,7 @@ export default class RecentMentions extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
showFullDate={false}
skipPinnedHeader={true}
/>
@ -193,17 +177,6 @@ export default class RecentMentions extends PureComponent {
);
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;

View file

@ -93,18 +93,11 @@ export default class Search extends PureComponent {
channelName: '',
cursorPosition: 0,
value: props.initialValue,
managedConfig: {},
recent: props.recent,
};
}
componentWillMount() {
this.listenerId = mattermostManaged.addEventListener('managedConfigDidChange', this.setManagedConfig);
}
componentDidMount() {
this.setManagedConfig();
if (this.props.initialValue) {
this.search(this.props.initialValue);
}
@ -148,10 +141,6 @@ export default class Search extends PureComponent {
}
}
componentWillUnmount() {
mattermostManaged.removeEventListener(this.listenerId);
}
archivedIndicator = (postID, style) => {
const channelIsArchived = this.props.archivedPostIds.includes(postID);
let archivedIndicator = null;
@ -366,7 +355,6 @@ export default class Search extends PureComponent {
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
const style = getStyleFromTheme(theme);
if (item.id) {
@ -403,7 +391,7 @@ export default class Search extends PureComponent {
navigator={this.props.navigator}
onHashtagPress={this.handleHashtagPress}
onPermalinkPress={this.handlePermalinkPress}
managedConfig={managedConfig}
managedConfig={mattermostManaged.getCachedConfig()}
skipPinnedHeader={true}
/>
{separator}
@ -459,17 +447,6 @@ export default class Search extends PureComponent {
this.search(this.state.value.trim());
};
setManagedConfig = async (config) => {
let nextConfig = config;
if (!nextConfig) {
nextConfig = await mattermostManaged.getLocalConfig();
}
this.setState({
managedConfig: nextConfig,
});
};
showPermalinkView = (postId, isPermalink) => {
const {actions, navigator} = this.props;