diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/hooks/useOpenLink.ts | 23 | ||||
-rw-r--r-- | src/lib/media/manip.ts | 27 | ||||
-rw-r--r-- | src/lib/media/manip.web.ts | 21 |
3 files changed, 52 insertions, 19 deletions
diff --git a/src/lib/hooks/useOpenLink.ts b/src/lib/hooks/useOpenLink.ts index a949dacc6..28c1bca3d 100644 --- a/src/lib/hooks/useOpenLink.ts +++ b/src/lib/hooks/useOpenLink.ts @@ -12,16 +12,18 @@ import { toNiceDomain, } from '#/lib/strings/url-helpers' import {isNative} from '#/platform/detection' -import {useModalControls} from '#/state/modals' import {useInAppBrowser} from '#/state/preferences/in-app-browser' import {useTheme} from '#/alf' +import {useDialogContext} from '#/components/Dialog' import {useSheetWrapper} from '#/components/Dialog/sheet-wrapper' +import {useGlobalDialogsControlContext} from '#/components/dialogs/Context' export function useOpenLink() { - const {openModal} = useModalControls() const enabled = useInAppBrowser() const t = useTheme() const sheetWrapper = useSheetWrapper() + const dialogContext = useDialogContext() + const {inAppBrowserConsentControl} = useGlobalDialogsControlContext() const openLink = useCallback( async (url: string, override?: boolean, shouldProxy?: boolean) => { @@ -42,10 +44,17 @@ export function useOpenLink() { if (isNative && !url.startsWith('mailto:')) { if (override === undefined && enabled === undefined) { - openModal({ - name: 'in-app-browser-consent', - href: url, - }) + // consent dialog is a global dialog, and while it's possible to nest dialogs, + // the actual components need to be nested. sibling dialogs on iOS are not supported. + // thus, check if we're in a dialog, and if so, close the existing dialog before opening the + // consent dialog -sfn + if (dialogContext.isWithinDialog) { + dialogContext.close(() => { + inAppBrowserConsentControl.open(url) + }) + } else { + inAppBrowserConsentControl.open(url) + } return } else if (override ?? enabled) { await sheetWrapper( @@ -62,7 +71,7 @@ export function useOpenLink() { } Linking.openURL(url) }, - [enabled, openModal, t, sheetWrapper], + [enabled, inAppBrowserConsentControl, t, sheetWrapper, dialogContext], ) return openLink diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts index 7f052068d..f6ef8347d 100644 --- a/src/lib/media/manip.ts +++ b/src/lib/media/manip.ts @@ -178,15 +178,20 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise<Image> { height: imageRes.height, }) - for (let i = 0; i < 9; i++) { - // nearest 10th - const quality = Math.round((1 - 0.1 * i) * 10) / 10 + let minQualityPercentage = 0 + let maxQualityPercentage = 101 // exclusive + let newDataUri + + while (maxQualityPercentage - minQualityPercentage > 1) { + const qualityPercentage = Math.round( + (maxQualityPercentage + minQualityPercentage) / 2, + ) const resizeRes = await manipulateAsync( localUri, [{resize: newDimensions}], { format: SaveFormat.JPEG, - compress: quality, + compress: qualityPercentage / 100, }, ) @@ -198,8 +203,8 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise<Image> { } if (fileInfo.size < opts.maxSize) { - safeDeleteAsync(imageRes.uri) - return { + minQualityPercentage = qualityPercentage + newDataUri = { path: normalizePath(resizeRes.uri), mime: 'image/jpeg', size: fileInfo.size, @@ -207,9 +212,17 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise<Image> { height: resizeRes.height, } } else { - safeDeleteAsync(resizeRes.uri) + maxQualityPercentage = qualityPercentage } + + safeDeleteAsync(resizeRes.uri) } + + if (newDataUri) { + safeDeleteAsync(imageRes.uri) + return newDataUri + } + throw new Error( `This image is too big! We couldn't compress it down to ${opts.maxSize} bytes`, ) diff --git a/src/lib/media/manip.web.ts b/src/lib/media/manip.web.ts index 4761f2fe0..ffef7314d 100644 --- a/src/lib/media/manip.web.ts +++ b/src/lib/media/manip.web.ts @@ -72,17 +72,28 @@ interface DoResizeOpts { async function doResize(dataUri: string, opts: DoResizeOpts): Promise<RNImage> { let newDataUri - for (let i = 0; i <= 10; i++) { - newDataUri = await createResizedImage(dataUri, { + let minQualityPercentage = 0 + let maxQualityPercentage = 101 //exclusive + + while (maxQualityPercentage - minQualityPercentage > 1) { + const qualityPercentage = Math.round( + (maxQualityPercentage + minQualityPercentage) / 2, + ) + const tempDataUri = await createResizedImage(dataUri, { width: opts.width, height: opts.height, - quality: 1 - i * 0.1, + quality: qualityPercentage / 100, mode: opts.mode, }) - if (getDataUriSize(newDataUri) < opts.maxSize) { - break + + if (getDataUriSize(tempDataUri) < opts.maxSize) { + minQualityPercentage = qualityPercentage + newDataUri = tempDataUri + } else { + maxQualityPercentage = qualityPercentage } } + if (!newDataUri) { throw new Error('Failed to compress image') } |