Skip to content

Commit c36726f

Browse files
Merge pull request #87 from tombailey/improve-example-app
Improve example app
2 parents 4bb26d8 + 60fab02 commit c36726f

File tree

1 file changed

+77
-58
lines changed

1 file changed

+77
-58
lines changed

example/App.js

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,81 @@
88
*/
99

1010
import React, {Component} from 'react';
11-
import {Platform, StyleSheet, Text, View} from 'react-native';
11+
import {Platform, StyleSheet, Button, Text, TextInput, View} from 'react-native';
1212

1313
import RNSecureKeyStore, {ACCESSIBLE} from "react-native-secure-key-store";
1414

15-
const instructions = Platform.select({
16-
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
17-
android:
18-
'Double tap R on your keyboard to reload,\n' +
19-
'Shake or press menu button for dev menu',
20-
});
21-
2215
type Props = {};
2316
export default class App extends Component<Props> {
24-
render() {
17+
state = {
18+
alias: 'hello',
19+
value: 'world'
20+
};
2521

26-
RNSecureKeyStore.set("key1", "value1", {accessible: ACCESSIBLE.ALWAYS_THIS_DEVICE_ONLY})
27-
.then((res) => {
28-
console.log(res);
29-
}, (err) => {
30-
console.log(err);
31-
});
22+
getValue() {
23+
RNSecureKeyStore.get(this.state.alias)
24+
.then((value) => {
25+
this.setState({
26+
value,
27+
});
28+
})
29+
.catch(console.error);
30+
}
3231

33-
RNSecureKeyStore.set("key2", "value2", {accessible: ACCESSIBLE.ALWAYS_THIS_DEVICE_ONLY})
34-
.then((res) => {
35-
console.log(res);
36-
}, (err) => {
37-
console.log(err);
38-
});
32+
setValue() {
33+
RNSecureKeyStore.set(this.state.alias, this.state.value, {})
34+
.then(() => this.getValue())
35+
.catch(console.error);
36+
}
3937

40-
RNSecureKeyStore.get("key1")
41-
.then((res) => {
42-
console.log(res);
43-
}, (err) => {
44-
console.log(err);
45-
});
38+
removeValue() {
39+
RNSecureKeyStore.remove(this.state.alias)
40+
.then(() => this.getValue())
41+
.catch(console.error);
42+
}
4643

47-
RNSecureKeyStore.get("key2")
48-
.then((res) => {
49-
console.log(res);
50-
}, (err) => {
51-
console.log(err);
52-
});
44+
render() {
45+
return (
46+
<View style={styles.container}>
47+
<View style={styles.row}>
48+
<Text>Alias:</Text>
49+
<TextInput
50+
style={styles.textInput}
51+
onChangeText={alias => this.setState({alias})}
52+
value={this.state.alias}
53+
/>
54+
</View>
5355

54-
RNSecureKeyStore.remove("key1")
55-
.then((res) => {
56-
console.log(res);
57-
}, (err) => {
58-
console.log(err);
59-
});
56+
<View style={styles.row}>
57+
<Text>Value:</Text>
58+
<TextInput
59+
style={styles.textInput}
60+
onChangeText={value => this.setState({value})}
61+
value={this.state.value}
62+
/>
63+
</View>
6064

61-
RNSecureKeyStore.remove("key2")
62-
.then((res) => {
63-
console.log(res);
64-
}, (err) => {
65-
console.log(err);
66-
});
65+
<View style={styles.row}>
66+
<View style={styles.button}>
67+
<Button
68+
onPress={() => this.getValue()}
69+
title='Get'
70+
/>
71+
</View>
72+
<View style={styles.button}>
73+
<Button
74+
onPress={() => this.setValue()}
75+
title='Set'
76+
/>
77+
</View>
78+
<View style={styles.button}>
79+
<Button
80+
onPress={() => this.removeValue()}
81+
title='Remove'
82+
/>
83+
</View>
84+
</View>
6785

68-
return (
69-
<View style={styles.container}>
70-
<Text style={styles.welcome}>Welcome to React Native!</Text>
71-
<Text style={styles.instructions}>To get started, edit App.js</Text>
72-
<Text style={styles.instructions}>{instructions}</Text>
7386
</View>
7487
);
7588
}
@@ -82,14 +95,20 @@ const styles = StyleSheet.create({
8295
alignItems: 'center',
8396
backgroundColor: '#F5FCFF',
8497
},
85-
welcome: {
86-
fontSize: 20,
87-
textAlign: 'center',
88-
margin: 10,
98+
row: {
99+
flexDirection: 'row',
100+
justifyContent: 'space-between',
101+
alignItems: 'center',
102+
marginTop: 20
89103
},
90-
instructions: {
91-
textAlign: 'center',
92-
color: '#333333',
93-
marginBottom: 5,
104+
textInput: {
105+
height: 40,
106+
width: 200,
107+
borderColor: 'gray',
108+
borderWidth: 1,
109+
marginLeft: 10
94110
},
111+
button: {
112+
marginLeft: 10
113+
}
95114
});

0 commit comments

Comments
 (0)