Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle notification (position, topOffset and width) in landscape mode #151

Merged
merged 7 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
//do wyrzucenia
import { Dimensions, Platform } from 'react-native'
import { Platform } from 'react-native'

const isAndroid = Platform.OS === 'android'
const maxLongPressDragDistance = 300
const { width, height } = Dimensions.get('window')
const notificationSideMargin = 14
const notificationWidth = width - notificationSideMargin * 2
const initialOffsetX = -(notificationWidth + 2 * notificationSideMargin)
const initialOffsetY = -300
const targetOffsetX = width
const targetOffsetY = true ? 50 : 10
const maxNotificationWidth = 343

export const Constants = {
maxLongPressDragDistance,
notificationSideMargin,
notificationWidth,
initialOffsetX,
initialOffsetY,
targetOffsetX,
targetOffsetY,
isAndroid,
height,
width,
maxNotificationWidth,
}
11 changes: 10 additions & 1 deletion src/core/hooks/useNotificationsStates.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useWindowDimensions } from 'react-native'
import { useNotificationConfig } from './useNotificationConfig'
import { useReducer, useRef, useState } from 'react'
import { getTopOffset, mergeConfigs } from '../utils/pickers'
Expand All @@ -6,14 +7,21 @@ import { queueReducer } from '../utils/queueReducer'
export const useNotificationsStates = () => {
const panHandlerRef = useRef(null)
const longPressHandlerRef = useRef(null)
const { height: windowHeight, width: windowWidth } = useWindowDimensions()
const globalConfig = useNotificationConfig()
const [notificationsQueue, dispatch] = useReducer(queueReducer, [])
const [notificationHeight, setNotificationHeight] = useState(0)

const isPortaitMode = windowHeight > windowWidth
const notificationEvent = notificationsQueue[0]
const config = mergeConfigs(globalConfig, notificationEvent)

const topOffset = getTopOffset(config, notificationHeight)
const topOffset = getTopOffset({
globalConfig: config,
notificationHeight,
isPortaitMode,
windowHeight,
})

return {
config,
Expand All @@ -24,6 +32,7 @@ export const useNotificationsStates = () => {
notificationsQueue,
longPressHandlerRef,
setNotificationHeight,
isPortaitMode,
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/core/renderers/GestureHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { ReactNode } from 'react'
import { useWindowDimensions } from 'react-native'
import { PanGestureHandler } from 'react-native-gesture-handler'
import type { AnimationAPI } from '../hooks/useAnimationAPI'
import type { NotificationState } from '../hooks/useNotificationsStates'
Expand All @@ -10,12 +11,20 @@ type Props = {
children: ReactNode
state: Pick<
NotificationState,
'longPressHandlerRef' | 'panHandlerRef' | 'setNotificationHeight' | 'topOffset'
| 'longPressHandlerRef'
| 'panHandlerRef'
| 'setNotificationHeight'
| 'topOffset'
| 'isPortaitMode'
>
animationAPI: Pick<AnimationAPI, 'dragGestureHandler' | 'handleDragStateChange' | 'dragStyles'>
}

export const GestureHandler = ({ children, state, animationAPI }: Props) => {
const { width } = useWindowDimensions()
const notificationWidth = state.isPortaitMode
? width - Constants.notificationSideMargin * 2
: Constants.maxNotificationWidth
return (
<PanGestureHandler
ref={state.panHandlerRef}
Expand All @@ -28,7 +37,10 @@ export const GestureHandler = ({ children, state, animationAPI }: Props) => {
animationAPI.dragStyles,
styles.container,
Constants.isAndroid ? styles.containerAndroid : styles.containerIos,
{ top: state.topOffset },
{
top: state.topOffset,
width: notificationWidth,
},
]}>
{children}
</Animated.View>
Expand Down
22 changes: 15 additions & 7 deletions src/core/utils/pickers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { Constants } from '../config'
import type { NotificationsConfig, Variant, VariantsMap } from '../../types'
import type { EmitParam } from '../services/types'
import type { DefaultKeys } from '../../defaultConfig/defaultConfig'
import type { DefaultStylesConfigs } from '../../defaultConfig/types'
import type { KeyType } from '../../types/misc'

export const getTopOffset = (
globalConfig: NotificationsConfig<VariantsMap>,
type GetOffsetTopProps = {
globalConfig: NotificationsConfig<VariantsMap>
notificationHeight: number
) => {
isPortaitMode: boolean
windowHeight: number
}

export const getTopOffset = ({
globalConfig,
notificationHeight,
isPortaitMode,
windowHeight,
}: GetOffsetTopProps) => {
const isNotch = globalConfig.isNotch
const extraSpace = 50
const topPosition = isNotch ? extraSpace : 10
const topPosition = isNotch && isPortaitMode ? extraSpace : 10
const notificationPosition = globalConfig.notificationPosition

switch (notificationPosition) {
case 'top':
return topPosition
case 'center':
return Constants.height / 2 - (notificationHeight ? notificationHeight / 2 : 75)
return windowHeight / 2 - (notificationHeight ? notificationHeight / 2 : 75)
case 'bottom':
return Constants.height - (notificationHeight ? notificationHeight + extraSpace : 150)
return windowHeight - (notificationHeight ? notificationHeight + extraSpace : 150)
default:
return topPosition
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/utils/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Constants } from '../config'
export const styles = StyleSheet.create({
container: {
position: 'absolute',
width: Constants.notificationWidth,
minHeight: 0,
left: 0,
backgroundColor: 'transparent',
Expand All @@ -13,11 +12,13 @@ export const styles = StyleSheet.create({
justifyContent: 'flex-start',
},
containerIos: {
left: Constants.notificationSideMargin,
left: `auto`,
right: Constants.notificationSideMargin,
top: 50,
},
containerAndroid: {
left: Constants.notificationSideMargin,
left: `auto`,
right: Constants.notificationSideMargin,
top: 30,
},
boxWrapper: {
Expand Down
3 changes: 1 addition & 2 deletions src/defaultConfig/defaultGestureConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Constants } from '../core/config'
import type { GestureConfig } from '../types/gestures'

export const AndroidGestureConfig: GestureConfig = {
direction: 'x',
activationDistances: Constants.width * 0.4,
activationDistances: 50,
activationVelocities: 2000,
}

Expand Down