Adding in saving avatars

This commit is contained in:
talksik
2018-08-24 11:57:09 -07:00
parent 629b3fd7ee
commit 04ee616bc8
2 changed files with 39 additions and 9 deletions
+35 -5
View File
@@ -7,7 +7,8 @@ import {
Dimensions, Dimensions,
View, View,
Text, Text,
DrawerLayoutAndroid DrawerLayoutAndroid,
AsyncStorage
} from 'react-native'; } from 'react-native';
import { import {
Actions Actions
@@ -32,14 +33,43 @@ export default class Main extends Component<Props> {
constructor(props) { constructor(props) {
super(props); super(props);
var avatarRandom = Math.floor(Math.random() * allAvatarLinks.length);
this.state = { this.state = {
position: { position: {
long: props.long, long: props.long,
lat: props.lat lat: props.lat
}, }
avatar: avatarRandom }
}
componentWillMount() {
this.retrieveAvatar();
}
retrieveAvatar = async () => {
try {
const value = await AsyncStorage.getItem('avatar');
if (value !== null) {
// We have data!!
console.log("Have avatar: " + value);
} else {
this.storeAvatar(Math.floor(Math.random() * allAvatarLinks.length).toString());
}
this.setState({
avatar: parseInt(value)
});
} catch (error) {
// Error retrieving data
console.log(error);
}
}
storeAvatar = async (link) => {
try {
await AsyncStorage.setItem('avatar', link);
} catch (error) {
// Error saving data
console.log(error);
} }
} }
+4 -4
View File
@@ -41,7 +41,7 @@ export default class Chat extends Component<Props> {
messagesDOM: [], messagesDOM: [],
prevMessageUser: null, prevMessageUser: null,
userId: null, userId: null,
avatar: null
}; };
/** /**
* todo fix AsyncStorage * todo fix AsyncStorage
@@ -50,9 +50,9 @@ export default class Chat extends Component<Props> {
console.log(props); console.log(props);
} }
storeUserId = async (id) => { storeUserId = async (location, item) => {
try { try {
await AsyncStorage.setItem('userId', id.toString()); await AsyncStorage.setItem(location, item);
} catch (error) { } catch (error) {
// Error saving data // Error saving data
console.log(error); console.log(error);
@@ -72,7 +72,7 @@ export default class Chat extends Component<Props> {
this.socket.emit('userId', {user_id: value, newUser: true, position: this.props.position, avatar: this.props.avatar}); this.socket.emit('userId', {user_id: value, newUser: true, position: this.props.position, avatar: this.props.avatar});
this.socket.on('getNewUserId', (result) => { this.socket.on('getNewUserId', (result) => {
console.log('New id: ' + result.user_id); console.log('New id: ' + result.user_id);
this.storeUserId(result.user_id); this.storeUserId('userId', result.user_id.toString());
id = result.user_id; id = result.user_id;
}); });
} }