Skip to content

Commit

Permalink
fix: useBooleanWithHaptics, fix some other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Jan 15, 2023
1 parent 00b70be commit 82dad4b
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 70 deletions.
4 changes: 4 additions & 0 deletions src/hooks/useAppState.native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useEffect, useState } from 'react'
import { AppState } from 'react-native'

import type { AppStateStatus } from 'react-native'

export const getCurrentState = (): AppStateStatus => AppState.currentState

export const useAppState = () => {
const [appState, setAppState] = useState(AppState.currentState)

Expand Down
25 changes: 25 additions & 0 deletions src/hooks/useBooleanWithHaptics.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as Haptics from 'expo-haptics'
import { useCallback, useState } from 'react'
import { Platform } from 'react-native'

export function useBooleanWithHaptics(initialValue = false) {
const [value, setValue] = useState(initialValue)

const on = useCallback(() => {
if (Platform.OS === 'ios') {
void Haptics.selectionAsync()
}
setValue(true)
}, [])

const off = useCallback(() => {
if (Platform.OS === 'ios') {
void Haptics.selectionAsync()
}
setValue(false)
}, [])

return [value, on, off] as const
}

export default useBooleanWithHaptics
26 changes: 3 additions & 23 deletions src/hooks/useBooleanWithHaptics.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import * as Haptics from 'expo-haptics'
import { useCallback, useState } from 'react'
import { Platform } from 'react-native'
import useBoolean from './useBoolean'

export function useBooleanWithHaptics(initialValue = false) {
const [value, setValue] = useState(initialValue)
export const useBooleanWithHaptics = useBoolean

const on = useCallback(() => {
if (Platform.OS === 'ios') {
void Haptics.selectionAsync()
}
setValue(true)
}, [])

const off = useCallback(() => {
if (Platform.OS === 'ios') {
void Haptics.selectionAsync()
}
setValue(false)
}, [])

return [value, on, off] as const
}

export default useBooleanWithHaptics
export default useBoolean
3 changes: 0 additions & 3 deletions src/hooks/useBooleanWithHaptics.web.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/hooks/useConfirm.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useCallback, useContext } from 'react'
import { Alert } from 'react-native'

import StringsContext from '../contexts/Strings'

export function useConfirm() {
const { Cancel, OK } = useContext(StringsContext)

return useCallback(async (title: string, message?: string) => new Promise<boolean>((resolve) => {
Alert.alert(
title,
message,
[
{ text: Cancel, onPress: () => resolve(false), style: 'cancel' },
{ text: OK, onPress: () => resolve(true) },
],
{ cancelable: false },
)
}), [Cancel, OK])
}

export default useConfirm
22 changes: 5 additions & 17 deletions src/hooks/useConfirm.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { useCallback, useContext } from 'react'
import { Alert } from 'react-native'

import StringsContext from '../contexts/Strings'
const confirm = async (title: string, message?: string) => new Promise<boolean>((resolve) => {
// eslint-disable-next-line no-alert
resolve(window.confirm([title, message].filter(Boolean).join('\n')))
})

export function useConfirm() {
const { Cancel, OK } = useContext(StringsContext)

return useCallback(async (title: string, message?: string) => new Promise<boolean>((resolve) => {
Alert.alert(
title,
message,
[
{ text: Cancel, onPress: () => resolve(false), style: 'cancel' },
{ text: OK, onPress: () => resolve(true) },
],
{ cancelable: false },
)
}), [Cancel, OK])
return confirm
}

export default useConfirm
10 changes: 0 additions & 10 deletions src/hooks/useConfirm.web.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/hooks/useToggleWithHaptics.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useCallback, useState } from 'react'

import doHaptics from '../utils/doHaptics'

export function useToggleWithHaptics(initialValue = false, enableHaptics: boolean | undefined = undefined) {
const [value, setValue] = useState(initialValue)

const toggle = useCallback(() => {
void doHaptics(enableHaptics)
setValue((prev) => !prev)
}, [enableHaptics])

return [value, toggle] as const
}

export default useToggleWithHaptics
17 changes: 3 additions & 14 deletions src/hooks/useToggleWithHaptics.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { useCallback, useState } from 'react'
import useToggle from './useToggle'

import doHaptics from '../utils/doHaptics'
export const useToggleWithHaptics = useToggle

export function useToggleWithHaptics(initialValue = false, enableHaptics: boolean | undefined = undefined) {
const [value, setValue] = useState(initialValue)

const toggle = useCallback(() => {
void doHaptics(enableHaptics)
setValue((prev) => !prev)
}, [enableHaptics])

return [value, toggle] as const
}

export default useToggleWithHaptics
export default useToggle
3 changes: 0 additions & 3 deletions src/hooks/useToggleWithHaptics.web.tsx

This file was deleted.

0 comments on commit 82dad4b

Please sign in to comment.