-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
153 lines (135 loc) · 3.95 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as React from 'react';
import {
StyleSheet,
View,
Text,
Image,
TouchableHighlight,
Vibration,
Alert,
Platform,
} from 'react-native';
import SaveBase64Image from 'react-native-save-base-sixty-four';
import { dachshund, pomeranian, frenchie } from './images';
import { hasStoragePermissions } from './permissions';
export default function App() {
const [hasPermissions, setHasPermissions] = React.useState<boolean>(false);
const showPermissionsRequired = React.useCallback(() => {
const title = 'Permissions Required';
const message =
'Unable to perform this operation until you accept the permissions';
Alert.alert(title, message, undefined, { cancelable: true });
}, []);
const showFailure = React.useCallback(() => {
const title = 'Fail 😭';
const message = 'The image failed to save to your gallery';
Alert.alert(title, message, undefined, { cancelable: true });
}, []);
const handlePress = React.useCallback(
(image: string, fileName: string) => () => {
if (!hasPermissions && Platform.OS === 'android') {
// < Android Q crashes wihtout permissions granted - optionally only show for < Q
return showPermissionsRequired();
}
Vibration.vibrate(100);
try {
SaveBase64Image.share(image, {
fileName,
});
} catch (e) {
showFailure();
}
},
[hasPermissions, showFailure, showPermissionsRequired]
);
const handleLongPress = React.useCallback(
(image: string, fileName: string) => () => {
if (!hasPermissions && Platform.OS === 'android') {
// < Android Q crashes wihtout permissions granted - optionally only show for < Q
return showPermissionsRequired();
}
SaveBase64Image.save(image, {
fileName,
}).then((success) => {
if (success) {
Alert.alert(
'Success 😄',
'The image was successfully saved to your gallery',
undefined,
{ cancelable: true }
);
} else {
showFailure();
}
});
Vibration.vibrate(200);
},
[hasPermissions, showFailure, showPermissionsRequired]
);
React.useEffect(function askForPermissionsOnMount() {
if (Platform.OS === 'ios') {
return;
}
hasStoragePermissions().then((result) => {
setHasPermissions(result);
});
}, []);
return (
<View style={styles.container}>
<Text style={styles.text}>Tap to see the share sheet.</Text>
<Text style={styles.text}>Long press to save to your Pictures</Text>
<View style={styles.imageWrapper}>
<TouchableHighlight
onPress={handlePress(dachshund, 'Dachshund')}
onLongPress={handleLongPress(dachshund, 'Dachshund')}
>
<Image
style={styles.image}
source={{ uri: `data:image/png;base64,${dachshund}` }}
/>
</TouchableHighlight>
<TouchableHighlight
onPress={handlePress(pomeranian, 'Pomeranian')}
onLongPress={handleLongPress(pomeranian, 'Pomeranian')}
>
<Image
style={styles.image}
source={{ uri: `data:image/png;base64,${pomeranian}` }}
/>
</TouchableHighlight>
<TouchableHighlight
onPress={handlePress(frenchie, 'French Bulldog')}
onLongPress={handleLongPress(frenchie, 'French Bulldog')}
>
<Image
style={styles.image}
source={{ uri: `data:image/png;base64,${frenchie}` }}
/>
</TouchableHighlight>
</View>
</View>
);
}
const styles = StyleSheet.create({
text: {
fontSize: 20,
textAlign: 'center',
marginBottom: 10,
},
image: {
width: 100,
height: 150,
margin: 8,
},
container: {
padding: 10,
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fc6',
},
imageWrapper: {
marginTop: 10,
flexDirection: 'row',
},
});