From b36b395a0bb92bcdf8175dd5918a8da8bee95fb2 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Fri, 23 Aug 2019 16:04:25 +0200 Subject: [PATCH] MM-18024 MM-18025 Fix race condition when push notification opens the app and credentials are not set (#3171) --- app/components/post_list/post_list.js | 23 ++++++++++++++++--- app/init/global_event_handler.js | 3 ++- app/mattermost.js | 12 +++++----- .../push_notifications.ios.js | 5 +++- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/components/post_list/post_list.js b/app/components/post_list/post_list.js index b1e69f432..d55b77126 100644 --- a/app/components/post_list/post_list.js +++ b/app/components/post_list/post_list.js @@ -134,6 +134,10 @@ export default class PostList extends PureComponent { } } + getItemCount = () => { + return this.props.postIds.length; + }; + handleClosePermalink = () => { const {actions} = this.props; actions.selectFocusedPostId(''); @@ -301,9 +305,8 @@ export default class PostList extends PureComponent { }, 250); }; - scrollToInitialIndexIfNeeded = (index) => { - if (!this.hasDoneInitialScroll && this.flatListRef?.current) { - this.hasDoneInitialScroll = true; + scrollToIndex = (index) => { + if (this.flatListRef?.current) { this.animationFrameInitialIndex = requestAnimationFrame(() => { this.flatListRef.current.scrollToIndex({ animated: false, @@ -315,6 +318,20 @@ export default class PostList extends PureComponent { } }; + scrollToInitialIndexIfNeeded = (index, count = 0) => { + if (!this.hasDoneInitialScroll && this.flatListRef?.current) { + this.hasDoneInitialScroll = true; + + if (index > 0 && index <= this.getItemCount()) { + this.scrollToIndex(index); + } else if (count < 3) { + setTimeout(() => { + this.scrollToInitialIndexIfNeeded(index, count + 1); + }, 300); + } + } + }; + showPermalinkView = (postId) => { const {actions} = this.props; diff --git a/app/init/global_event_handler.js b/app/init/global_event_handler.js index c0a013946..eed01a277 100644 --- a/app/init/global_event_handler.js +++ b/app/init/global_event_handler.js @@ -181,7 +181,8 @@ class GlobalEventHandler { } if (this.launchApp) { - this.launchApp(); + const credentials = await getAppCredentials(); + this.launchApp(credentials); } }; diff --git a/app/mattermost.js b/app/mattermost.js index ee74af5e5..f1a35084b 100644 --- a/app/mattermost.js +++ b/app/mattermost.js @@ -26,8 +26,9 @@ const sharedExtensionStarted = Platform.OS === 'android' && MattermostShare.isOp export const store = configureStore(initialState); const init = async () => { + const credentials = await getAppCredentials(); if (EphemeralStore.appStarted) { - launchApp(); + launchApp(credentials); return; } @@ -44,17 +45,16 @@ const init = async () => { } if (!EphemeralStore.appStarted) { - launchAppAndAuthenticateIfNeeded(); + launchAppAndAuthenticateIfNeeded(credentials); } }; -const launchApp = async () => { +const launchApp = async (credentials) => { telemetry.start([ 'start:select_server_screen', 'start:channel_screen', ]); - const credentials = await getAppCredentials(); if (credentials) { store.dispatch(loadMe()); store.dispatch(resetToChannel({skipMetrics: true})); @@ -66,9 +66,9 @@ const launchApp = async () => { EphemeralStore.appStarted = true; }; -const launchAppAndAuthenticateIfNeeded = async () => { +const launchAppAndAuthenticateIfNeeded = async (credentials) => { await emmProvider.handleManagedConfig(store); - await launchApp(); + await launchApp(credentials); if (emmProvider.enabled) { if (emmProvider.jailbreakProtection) { diff --git a/app/push_notifications/push_notifications.ios.js b/app/push_notifications/push_notifications.ios.js index 3aa56756d..2130d8582 100644 --- a/app/push_notifications/push_notifications.ios.js +++ b/app/push_notifications/push_notifications.ios.js @@ -127,7 +127,10 @@ class PushNotification { ...notification.getData(), message: notification.getMessage(), }; - this.handleNotification(info, false, userInteraction); + + if (!userInteraction) { + this.handleNotification(info, false, userInteraction); + } }; onNotificationReceivedForeground = (notification) => {