Skip to content

Commit

Permalink
Use of cached managed config in components (mattermost#2758)
Browse files Browse the repository at this point in the history
  • Loading branch information
enahum authored May 1, 2019
1 parent 0708a53 commit 6eeb830
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 163 deletions.
4 changes: 2 additions & 2 deletions app/components/at_mention/at_mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down
2 changes: 1 addition & 1 deletion app/components/markdown/markdown_image/markdown_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down
2 changes: 1 addition & 1 deletion app/components/markdown/markdown_link/markdown_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down
29 changes: 1 addition & 28 deletions app/components/post_list/post_list_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,13 @@ 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);
this.props.actions.setDeepLinkURL('');
}
}

componentWillUnmount() {
this.mounted = false;
mattermostManaged.removeEventListener(this.listenerId);
}

handleClosePermalink = () => {
const {actions} = this.props;
actions.selectFocusedPostId('');
Expand Down Expand Up @@ -163,7 +149,7 @@ export default class PostListBase extends PureComponent {
highlightPinnedOrFlagged: this.props.highlightPinnedOrFlagged,
isSearchResult: this.props.isSearchResult,
location: this.props.location,
managedConfig: this.state.managedConfig,
managedConfig: mattermostManaged.getCachedConfig(),
navigator: this.props.navigator,
onHashtagPress: this.props.onHashtagPress,
onPermalinkPress: this.handlePermalinkPress,
Expand Down Expand Up @@ -192,19 +178,6 @@ export default class PostListBase extends PureComponent {
);
};

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;

Expand Down
22 changes: 11 additions & 11 deletions app/mattermost_managed/mattermost-managed.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions app/mattermost_managed/mattermost-managed.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 {
Expand Down
31 changes: 2 additions & 29 deletions app/screens/flagged_posts/flagged_posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,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) => {
Expand Down Expand Up @@ -159,7 +143,7 @@ export default class FlaggedPosts extends PureComponent {

renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;

if (isDateLine(item)) {
return (
<DateHeader
Expand All @@ -186,7 +170,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}
Expand All @@ -196,17 +180,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;

Expand Down
27 changes: 1 addition & 26 deletions app/screens/pinned_posts/pinned_posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,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;
Expand Down Expand Up @@ -161,7 +148,6 @@ export default class PinnedPosts extends PureComponent {

renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;
if (isDateLine(item)) {
return (
<DateHeader
Expand All @@ -187,7 +173,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}
Expand All @@ -197,17 +183,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;

Expand Down
31 changes: 2 additions & 29 deletions app/screens/recent_mentions/recent_mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,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) => {
Expand Down Expand Up @@ -159,7 +143,7 @@ export default class RecentMentions extends PureComponent {

renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
const {managedConfig} = this.state;

if (isDateLine(item)) {
return (
<DateHeader
Expand All @@ -185,7 +169,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}
/>
Expand All @@ -194,17 +178,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;

Expand Down
Loading

0 comments on commit 6eeb830

Please sign in to comment.