Skip to content

Commit

Permalink
Refactor native DirectoryPicker with specific handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ray committed Jul 16, 2024
1 parent dbb1899 commit 0e5544d
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions apps/app/components/dialogs/DirectoryPickerDialog.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Label,
Paragraph,
Switch,
useThemeName,
} from 'tamagui'
import { YGroup } from 'tamagui'
import RNFS from 'react-native-fs'
Expand All @@ -23,50 +22,65 @@ import { useI18n } from '../../hooks/use18n'

const PRIVATE_DOWNLOAD_DIR_FILE_URI = 'file://' + PRIVATE_DOWNLOAD_DIR
const ROOT_FILE_URI = 'file://' + RNFS.ExternalStorageDirectoryPath
const PUBLIC_FOLDERS = ['Documents', 'Download']

export const DirectoryPickerDialog = ({ onSelect }) => {
const i18n = useI18n()
const [directories, setDirectories] = useState<string[]>([])
const [currentFileUri, setCurrentFileUri] = useState(ROOT_FILE_URI)
const theme = useThemeName()

useEffect(() => {
if (currentFileUri === PRIVATE_DOWNLOAD_DIR_FILE_URI) {
return
}
readDirectory(ROOT_FILE_URI)
}, [])

const readDirectories = async () => {
const readDirectory = async (path: string) => {
try {
const entries =
currentFileUri === ROOT_FILE_URI
? ['Documents', 'Download'] // Public folders
: await FileSystem.readDirectoryAsync(currentFileUri)

const directories = currentFileUri !== ROOT_FILE_URI ? ['..'] : []
path === ROOT_FILE_URI
? PUBLIC_FOLDERS
: await FileSystem.readDirectoryAsync(path)
const directories = path === ROOT_FILE_URI ? [] : ['..']

for (const entry of entries) {
const fileUri = `${currentFileUri}/${entry}`
const fileUri = `${path}/${entry}`
const info = await FileSystem.getInfoAsync(fileUri)
if (info.isDirectory) {
directories.push(entry)
}
}

setDirectories([...directories.sort()])
} catch (e) {
console.error(e)
}
}

readDirectories()
}, [currentFileUri])
const handleChangeDirectory = async (path: string) => {
let newCurrentFileUri = ''

const handleChangeDirectory = (directory: string) => {
if (directory === '..') {
setCurrentFileUri((cur) => {
const directories = cur.split('/')
const lastDir = directories[directories.length - 1]
return cur.substring(0, cur.length - `/${lastDir}`.length)
})
if (path === '..') {
const directories = currentFileUri.split('/')
const lastDir = directories[directories.length - 1]
newCurrentFileUri = currentFileUri.substring(
0,
currentFileUri.length - `/${lastDir}`.length,
)
} else {
setCurrentFileUri((cur) => `${cur}/${directory}`)
newCurrentFileUri = `${currentFileUri}/${path}`
}

setCurrentFileUri(newCurrentFileUri)
readDirectory(newCurrentFileUri)
}

const handlePrivateDirectoryCheckChange = (isChecked: boolean) => {
const newDirectory = isChecked
? PRIVATE_DOWNLOAD_DIR_FILE_URI
: ROOT_FILE_URI

setCurrentFileUri(newDirectory)

readDirectory(newDirectory)
}

const currentPath = currentFileUri.substring('file://'.length)
Expand All @@ -82,16 +96,12 @@ export const DirectoryPickerDialog = ({ onSelect }) => {
trigger={<Button icon={FolderOpen} />}
>
<YStack gap="$4" pt="$4" f={1}>
<XStack alignItems="center" jc="center" space="$4">
<XStack alignItems="center" jc="center" gap="$4">
<Switch
id={'use-private-dir-switch'}
checked={currentPath === PRIVATE_DOWNLOAD_DIR}
onCheckedChange={(checked) => {
if (checked) {
setCurrentFileUri(PRIVATE_DOWNLOAD_DIR_FILE_URI)
} else {
setCurrentFileUri(ROOT_FILE_URI)
}
handlePrivateDirectoryCheckChange(checked)
}}
>
<Switch.Thumb animation="quick" />
Expand Down

0 comments on commit 0e5544d

Please sign in to comment.