Skip to content

Commit

Permalink
simplify to playHaptic
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok committed Apr 11, 2024
1 parent 77f70a1 commit 38ebcdb
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 58 deletions.
47 changes: 5 additions & 42 deletions src/lib/haptics.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
import {
impactAsync,
ImpactFeedbackStyle,
notificationAsync,
NotificationFeedbackType,
selectionAsync,
} from 'expo-haptics'
import {impactAsync, ImpactFeedbackStyle} from 'expo-haptics'

import {isIOS, isWeb} from 'platform/detection'

const hapticImpact: ImpactFeedbackStyle = isIOS
? ImpactFeedbackStyle.Medium
: ImpactFeedbackStyle.Light // Users said the medium impact was too strong on Android; see APP-537s

export class Haptics {
static default(disabled: boolean) {
if (disabled || isWeb) {
return
}

impactAsync(hapticImpact)
}
static impact(type: ImpactFeedbackStyle = hapticImpact, disabled: boolean) {
if (disabled || isWeb) {
return
}
impactAsync(type)
}
static selection(disabled: boolean) {
if (disabled || isWeb) {
return
}
selectionAsync()
}
static notification = (
type: 'success' | 'warning' | 'error',
enabled: boolean,
) => {
if (!enabled || isWeb) {
return
}
switch (type) {
case 'success':
return notificationAsync(NotificationFeedbackType.Success)
case 'warning':
return notificationAsync(NotificationFeedbackType.Warning)
case 'error':
return notificationAsync(NotificationFeedbackType.Error)
}
export function playHaptic(disabled: boolean) {
if (disabled || isWeb) {
return
}
impactAsync(hapticImpact)
}
4 changes: 2 additions & 2 deletions src/screens/Profile/Header/ProfileHeaderLabeler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {Haptics} from '#/lib/haptics'
import {isAppLabeler} from '#/lib/moderation'
import {pluralize} from '#/lib/strings/helpers'
import {logger} from '#/logger'
Expand All @@ -21,6 +20,7 @@ import {useLikeMutation, useUnlikeMutation} from '#/state/queries/like'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useSession} from '#/state/session'
import {useAnalytics} from 'lib/analytics/analytics'
import {playHaptic} from 'lib/haptics'
import {useProfileShadow} from 'state/cache/profile-shadow'
import {useHapticsDisabled} from 'state/preferences/disable-haptics'
import {ProfileMenu} from '#/view/com/profile/ProfileMenu'
Expand Down Expand Up @@ -95,7 +95,7 @@ let ProfileHeaderLabeler = ({
return
}
try {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)

if (likeUri) {
await unlikeMod({uri: likeUri})
Expand Down
8 changes: 4 additions & 4 deletions src/view/com/util/post-ctrls/PostCtrls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {HITSLOP_10, HITSLOP_20} from '#/lib/constants'
import {Haptics} from '#/lib/haptics'
import {playHaptic} from '#/lib/haptics'
import {CommentBottomArrow, HeartIcon, HeartIconSolid} from '#/lib/icons'
import {makeProfileLink} from '#/lib/routes/links'
import {shareUrl} from '#/lib/sharing'
Expand Down Expand Up @@ -86,7 +86,7 @@ let PostCtrls = ({
const onPressToggleLike = React.useCallback(async () => {
try {
if (!post.viewer?.like) {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)
await queueLike()
} else {
await queueUnlike()
Expand All @@ -102,7 +102,7 @@ let PostCtrls = ({
closeModal()
try {
if (!post.viewer?.repost) {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)
await queueRepost()
} else {
await queueUnrepost()
Expand Down Expand Up @@ -131,7 +131,7 @@ let PostCtrls = ({
indexedAt: post.indexedAt,
},
})
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)
}, [
closeModal,
openComposer,
Expand Down
8 changes: 4 additions & 4 deletions src/view/screens/ProfileFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {truncateAndInvalidate} from '#/state/queries/util'
import {useSession} from '#/state/session'
import {useComposerControls} from '#/state/shell/composer'
import {useAnalytics} from 'lib/analytics/analytics'
import {Haptics} from 'lib/haptics'
import {playHaptic} from 'lib/haptics'
import {usePalette} from 'lib/hooks/usePalette'
import {useSetTitle} from 'lib/hooks/useSetTitle'
import {ComposeIcon2} from 'lib/icons'
Expand Down Expand Up @@ -203,7 +203,7 @@ export function ProfileFeedScreenInner({

const onToggleSaved = React.useCallback(async () => {
try {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)

if (isSaved) {
await removeFeed({uri: feedInfo.uri})
Expand Down Expand Up @@ -235,7 +235,7 @@ export function ProfileFeedScreenInner({

const onTogglePinned = React.useCallback(async () => {
try {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)

if (isPinned) {
await unpinFeed({uri: feedInfo.uri})
Expand Down Expand Up @@ -540,7 +540,7 @@ function AboutSection({

const onToggleLiked = React.useCallback(async () => {
try {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)

if (isLiked && likeUri) {
await unlikeFeed({uri: likeUri})
Expand Down
4 changes: 2 additions & 2 deletions src/view/screens/ProfileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {truncateAndInvalidate} from '#/state/queries/util'
import {useSession} from '#/state/session'
import {useSetMinimalShellMode} from '#/state/shell'
import {useComposerControls} from '#/state/shell/composer'
import {Haptics} from 'lib/haptics'
import {playHaptic} from 'lib/haptics'
import {usePalette} from 'lib/hooks/usePalette'
import {useSetTitle} from 'lib/hooks/useSetTitle'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
Expand Down Expand Up @@ -266,7 +266,7 @@ function Header({rkey, list}: {rkey: string; list: AppBskyGraphDefs.ListView}) {
const isSaved = preferences?.feeds?.saved?.includes(list.uri)

const onTogglePinned = React.useCallback(async () => {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)

try {
if (isPinned) {
Expand Down
4 changes: 2 additions & 2 deletions src/view/screens/SavedFeeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '#/state/queries/preferences'
import {useSetMinimalShellMode} from '#/state/shell'
import {useAnalytics} from 'lib/analytics/analytics'
import {Haptics} from 'lib/haptics'
import {playHaptic} from 'lib/haptics'
import {usePalette} from 'lib/hooks/usePalette'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {CommonNavigatorParams} from 'lib/routes/types'
Expand Down Expand Up @@ -198,7 +198,7 @@ function ListItem({
const isPending = isPinPending || isUnpinPending

const onTogglePinned = React.useCallback(async () => {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)

try {
resetSaveFeedsMutationState()
Expand Down
4 changes: 2 additions & 2 deletions src/view/shell/bottom-bar/BottomBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {BottomTabBarProps} from '@react-navigation/bottom-tabs'
import {StackActions} from '@react-navigation/native'

import {useAnalytics} from '#/lib/analytics/analytics'
import {Haptics} from '#/lib/haptics'
import {playHaptic} from '#/lib/haptics'
import {useDedupe} from '#/lib/hooks/useDedupe'
import {useMinimalShellMode} from '#/lib/hooks/useMinimalShellMode'
import {useNavigationTabState} from '#/lib/hooks/useNavigationTabState'
Expand Down Expand Up @@ -106,7 +106,7 @@ export function BottomBar({navigation}: BottomTabBarProps) {
}, [onPressTab])

const onLongPressProfile = React.useCallback(() => {
Haptics.default(isHapticsDisabled)
playHaptic(isHapticsDisabled)
accountSwitchControl.open()
}, [accountSwitchControl, isHapticsDisabled])

Expand Down

0 comments on commit 38ebcdb

Please sign in to comment.