Skip to content

Commit

Permalink
Move the IS_ANDROID check in the withPicker functions up 🫣
Browse files Browse the repository at this point in the history
(technically `showPicker` functions are defined, but i don't wanna implement them on the kt side atm)
  • Loading branch information
MarmadileManteater committed Jan 29, 2025
1 parent 214da07 commit ca2e263
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/renderer/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,25 @@ export async function readFileWithPicker(
) {
let file

if (process.env.IS_ANDROID) {
const extensions = []
const values = Object.values(acceptedTypes)
for (const value of values) {
if (Array.isArray(value)) {
extensions.push(...value.map(extension => extension.substring(1)))
} else {
extensions.push(value.substring(1))
}
}
const dialogResponse = await requestOpenDialog(extensions)
if (!dialogResponse.canceled) {
file = dialogResponse
}
// Only supported in Electron and desktop Chromium browsers
// https://developer.mozilla.org/en-US/docs/Web/API/Window/showOpenFilePicker#browser_compatibility
// As we know it is supported in Electron, adding the build flag means we can skip the runtime check in Electron
// and allow terser to remove the unused else block
if (process.env.IS_ELECTRON || 'showOpenFilePicker' in window) {
} else if (process.env.IS_ELECTRON || 'showOpenFilePicker' in window) {
try {
/** @type {FileSystemFileHandle[]} */
const [handle] = await window.showOpenFilePicker({
Expand All @@ -289,20 +303,6 @@ export async function readFileWithPicker(

throw error
}
} else if (process.env.IS_ANDROID) {
const extensions = []
const values = Object.values(acceptedTypes)
for (const value of values) {
if (Array.isArray(value)) {
extensions.push(...value.map(extension => extension.substring(1)))
} else {
extensions.push(value.substring(1))
}
}
const dialogResponse = await requestOpenDialog(extensions)
if (!dialogResponse.canceled) {
file = dialogResponse
}
} else {
/** @type {File|null} */
const fallbackFile = await new Promise((resolve) => {
Expand Down Expand Up @@ -364,11 +364,18 @@ export async function writeFileWithPicker(
rememberDirectoryId,
startInDirectory
) {
if (process.env.IS_ANDROID) {
const response = await requestSaveDialog(fileName, 'application/octet-stream')
if (!response.canceled) {
await writeFile(response.uri, content)
return true
}
return false
// Only supported in Electron and desktop Chromium browsers
// https://developer.mozilla.org/en-US/docs/Web/API/Window/showOpenFilePicker#browser_compatibility
// As we know it is supported in Electron, adding the build flag means we can skip the runtime check in Electron
// and allow terser to remove the unused else block
if (process.env.IS_ELECTRON || 'showSaveFilePicker' in window) {
} else if (process.env.IS_ELECTRON || 'showSaveFilePicker' in window) {
let writableFileStream

try {
Expand Down Expand Up @@ -403,13 +410,6 @@ export async function writeFileWithPicker(
}

return true
} else if (process.env.IS_ANDROID) {
const response = await requestSaveDialog(fileName, 'application/octet-stream')
if (!response.canceled) {
await writeFile(response.uri, content)
return true
}
return false
} else {
if (typeof content === 'string') {
content = new Blob([content], { type: mimeType })
Expand Down

0 comments on commit ca2e263

Please sign in to comment.