Skip to content

Commit

Permalink
[feat]optimize Dimensions change event
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderXL committed Aug 20, 2024
1 parent ba66a52 commit 829e00d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
14 changes: 14 additions & 0 deletions packages/core/src/platform/createApp.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { ref } from '../observer/ref'

const appHooksMap = makeMap(mergeLifecycle(wxLifecycle.LIFECYCLE).app)

function getOrientation (window = ReactNative.Dimensions.get('window')) {
return window.width > window.height ? 'landscape' : 'portrait'
}

function filterOptions (options, appData) {
const newOptions = {}
Object.keys(options).forEach(key => {
Expand Down Expand Up @@ -115,8 +119,18 @@ export default function createApp (option, config = {}) {
})
}
})

let count = 0
let lastOrientation = getOrientation()
const resizeSubScription = ReactNative.Dimensions.addEventListener('change', ({ window }) => {
const orientation = getOrientation(window)
if (orientation === lastOrientation) return
lastOrientation = orientation
global.__mpxAppFocusedState.value = `resize${count++}`
})
return () => {
changeSubscription()
resizeSubScription && resizeSubScription.remove()
}
}, [])

Expand Down
21 changes: 1 addition & 20 deletions packages/core/src/platform/patch/react/getDefaultOptions.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ import { BEFOREUPDATE, ONLOAD, UPDATED, ONSHOW, ONHIDE, ONRESIZE } from '../../.
import mergeOptions from '../../../core/mergeOptions'
import { queueJob } from '../../../observer/scheduler'

function getOrientation (window = ReactNative.Dimensions.get('window')) {
return window.width > window.height ? 'landscape' : 'portrait'
}

function getSystemInfo () {
const window = ReactNative.Dimensions.get('window')
const screen = ReactNative.Dimensions.get('screen')
return {
deviceOrientation: getOrientation(),
deviceOrientation: window.width > window.height ? 'landscape' : 'portrait',
size: {
screenWidth: screen.width,
screenHeight: screen.height,
Expand Down Expand Up @@ -303,25 +299,10 @@ function usePageStatus (navigation, route) {
}
})

let count = 0
let lastOrientation = getOrientation()
const resizeSubScription = ReactNative.Dimensions.addEventListener(
'change',
({ window }) => {
const orientation = getOrientation(window)
if (orientation === lastOrientation) return
lastOrientation = orientation
if (isFocused) {
setPageStatus(route.name, `resize${count++}`)
}
}
)

return () => {
focusSubscription()
blurSubscription()
appFocusedStateWatcher()
resizeSubScription && resizeSubScription.remove()
}
}, [navigation])
}
Expand Down

0 comments on commit 829e00d

Please sign in to comment.