-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatView.js
145 lines (132 loc) · 4.01 KB
/
ChatView.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
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
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
ScrollView,
InteractionManager,
Image,
Platform
} from 'react-native';
var ChatInput = require('./ChatInput');
var styles = require('./Styles');
var getChatHistory = require('./getChatHistory');
var setUpWebsocket = require('./setUpWebsocket');
var ImageSelection = require('./ImageSelection');
class ChatView extends Component {
constructor(props) {
super(props);
this.state = {
chatRows: [],
showLoading: true,
ws: null,
scrollViewHeight: 0,
scrollContentHeight: 0
};
}
populateChatHistory(response) {
this.setState({showLoading: false});
for (var i = 0; i < response.length; i++) {
this.addRow(response[i][1],
response[i][0] == 'Champ');
}
}
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
getChatHistory(this.props.title)
.then((response) => response.json())
.then((responseJson) => this.populateChatHistory(responseJson))
.catch((error) => console.warn(error))
setUpWebsocket(this);
});
}
componentWillUnmount() {
this.state.ws.close();
}
addRow(message, me) {
if (me)
this.addMyMessage(message);
else
this.addFriendMessage(message);
this.setState({chatRows: this.state.chatRows});
return message
}
addMyMessage(message) {
if (message.indexOf("__IMAGE__") < 0) {
this.state.chatRows.push(
<View key={this.state.chatRows.length} style={styles.myMessageRow}>
<View style={styles.myMessageBox}>
<Text style={styles.myMessage}>{message}</Text>
</View>
</View>
);
} else if (Platform.OS === 'android'){
this.postImage(message.slice(message.indexOf("content")));
}
}
addFriendMessage(message) {
this.state.chatRows.push(
<View key={this.state.chatRows.length} style={styles.friendMessageRow}>
<View style={styles.friendMessageBox}>
<Text style={styles.friendMessage}>{message}</Text>
</View>
</View>
);
}
postImage(assetUri) {
this.state.chatRows.push(
<View key={this.state.chatRows.length} style={styles.myMessageRow}>
<View style={styles.myMessageBox}>
<Image
source={{uri: assetUri}}
style={styles.imageStyle}
/>
</View>
</View>
);
this.setState({chatRows: this.state.chatRows});
}
selectImage(props, context) {
props.navigator.push({
name: 'Image Selection',
component: ImageSelection,
passProps: {context: this}
})
};
scrollToBottom() {
this.refs.chatScrollView.scrollTo(
{y: this.scrollContentHeight - this.scrollViewHeight, animated: true}
)
}
render() {
var loading = !this.state.showLoading ? null :
<View ref="loading" style={styles.loadingContainer}>
<Text style={styles.loading}>Loading chat history...</Text>
</View>;
var imageButtonPlaceholder = !(Platform.OS === 'android') ? null:
<Text style={styles.imageButton} onPress={() => this.selectImage(this.props, this)}>Images</Text>
return (
<View style={styles.chatContainer}>
<View style={styles.chatTitleContainer}>
<Text style={styles.chatBackButton} onPress={() => this.props.navigator.pop()}>Back</Text>
<Text style={styles.chatTitle}>{this.props.title}</Text>
{imageButtonPlaceholder}
</View>
<View style={styles.chatListSeparator}/>
{loading}
<ScrollView ref="chatScrollView" style={styles.chatMessages}
onContentSizeChange={(width, height) => {
this.scrollContentHeight = height
this.scrollToBottom()
}}
onLayout={event => this.scrollViewHeight = event.nativeEvent.layout.height}
>
{this.state.chatRows}
</ScrollView>
<View style={styles.separator}/>
<ChatInput style={styles.chatInput} parent={this}/>
</View>
)
};
}
module.exports = ChatView;