about summary refs log tree commit diff
path: root/src/lib/images.ts
diff options
context:
space:
mode:
authorAryan Goharzad <arrygoo@gmail.com>2023-01-25 18:25:34 -0500
committerGitHub <noreply@github.com>2023-01-25 17:25:34 -0600
commiteb33c3fa812cc087db14a6b6ba743e982b26c462 (patch)
treed098f7a804c67755f39e95bbbfd56887bacf476c /src/lib/images.ts
parentadf328b50ce98c5ebd3282fe897ddfdcd0de8011 (diff)
downloadvoidsky-eb33c3fa812cc087db14a6b6ba743e982b26c462.tar.zst
Saves image on long press (#83)
* Saves image on long press

* Adds save on long press

* Forking lightbox

* move to wrapper only to the bottom sheet to reduce impact of this change

* lint

* lint

* lint

* Use official `share` API

* Clean up cache after download

* comment

* comment

* Reduce swipe close velocity

* Updates per feedback

* lint

* bugfix

* Adds delayed press-in for TouchableOpacity
Diffstat (limited to 'src/lib/images.ts')
-rw-r--r--src/lib/images.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/images.ts b/src/lib/images.ts
index 9fc1cbc34..8d5eaded0 100644
--- a/src/lib/images.ts
+++ b/src/lib/images.ts
@@ -1,5 +1,9 @@
 import RNFetchBlob from 'rn-fetch-blob'
 import ImageResizer from '@bam.tech/react-native-image-resizer'
+import {Share} from 'react-native'
+import RNFS from 'react-native-fs'
+
+import * as Toast from '../view/com/util/Toast'
 
 export interface DownloadAndResizeOpts {
   uri: string
@@ -128,3 +132,21 @@ export function scaleDownDimensions(dim: Dim, max: Dim): Dim {
   }
   return {width: dim.width * hScale, height: dim.height * hScale}
 }
+
+export const saveImageModal = async ({uri}: {uri: string}) => {
+  const downloadResponse = await RNFetchBlob.config({
+    fileCache: true,
+  }).fetch('GET', uri)
+
+  const imagePath = downloadResponse.path()
+  const base64Data = await downloadResponse.readFile('base64')
+  const result = await Share.share({
+    url: 'data:image/png;base64,' + base64Data,
+  })
+  if (result.action === Share.sharedAction) {
+    Toast.show('Image saved to gallery')
+  } else if (result.action === Share.dismissedAction) {
+    // dismissed
+  }
+  RNFS.unlink(imagePath)
+}