forked from react-native-share/react-native-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (104 loc) · 2.59 KB
/
index.js
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
import React from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
BackAndroid,
NativeModules,
Platform,
ActionSheetIOS,
} from 'react-native';
import Overlay from './components/Overlay';
import Sheet from './components/Sheet';
import Button from './components/Button';
const styles = StyleSheet.create({
actionSheetContainer: {
flex: 1,
paddingTop: 10,
paddingBottom: 0,
justifyContent: "flex-end",
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
buttonContainer: {
overflow: 'hidden',
backgroundColor: 'white',
paddingBottom: 5,
paddingTop: 5
}
});
class RNShare {
static open(options) {
return new Promise((resolve, reject) => {
if (Platform.OS === "ios") {
ActionSheetIOS.showShareActionSheetWithOptions(options, (error) => {
return reject({ error: error });
}, (success, activityType) => {
if(success) {
return resolve({
app: activityType
});
} else {
reject({ error: "User did not share" });
}
});
} else {
NativeModules.RNShare.open(options,(e) => {
return reject({ error: e });
},(e) => {
resolve({
message: e
});
});
}
});
}
static shareSingle(options){
if (Platform.OS === "ios" || Platform.OS === "android") {
return new Promise((resolve, reject) => {
NativeModules.RNShare.shareSingle(options,(e) => {
return reject({ error: e });
},(e) => {
return resolve({
message: e
});
});
});
} else {
throw new Exception("not implemented");
}
}
}
class ShareSheet extends React.Component {
componentDidMount() {
BackAndroid.addEventListener('hardwareBackPress',() => {
if (this.props.visible) {
this.props.onCancel();
return true;
}
return false;
});
}
render(){
return (
<Overlay visible={this.props.visible} {...this.props}>
<View style={styles.actionSheetContainer}>
<TouchableOpacity
style={{flex:1}}
onPress={this.props.onCancel}>
</TouchableOpacity>
<Sheet visible={this.props.visible}>
<View style={styles.buttonContainer}>
{this.props.children}
</View>
</Sheet>
</View>
</Overlay>
)
}
}
module.exports = RNShare;
module.exports.Overlay = Overlay;
module.exports.Sheet = Sheet;
module.exports.Button = Button;
module.exports.ShareSheet = ShareSheet;