Skip to content

Commit 583adf2

Browse files
committed
update demo
1 parent ae2eff4 commit 583adf2

File tree

4 files changed

+310
-57
lines changed

4 files changed

+310
-57
lines changed

example/app/routes/Chat/index.js

Lines changed: 168 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,141 @@ var MessageListView = IMUI.MessageList;
2626
const AuroraIController = IMUI.AuroraIMUIController;
2727
const window = Dimensions.get('window');
2828

29+
import JMessage from 'jmessage-react-plugin';
30+
2931
import Translations from '../../resource/Translations'
3032

3133
var themsgid = 1
3234

33-
function constructNormalMessage() {
34-
35-
var message = {}
36-
message.msgId = themsgid.toString()
37-
themsgid += 1
38-
message.status = "send_going"
39-
message.isOutgoing = true
40-
message.timeString = ""
41-
var user = {
42-
userId: "",
43-
displayName: "",
44-
avatarPath: ""
45-
}
46-
message.fromUser = user
47-
48-
return message
49-
}
5035

5136
export default class Chat extends Component {
5237
constructor(props) {
5338
super(props);
5439
this.state = { inputViewLayout: {width:window.width, height:86,}};
5540

5641
this.updateLayout = this.updateLayout.bind(this);
42+
this.conversation = this.props.navigation.state.params.conversation
43+
44+
JMessage.getMyInfo((myInfo) => {
45+
this.myInfo = myInfo
46+
})
5747
}
5848

59-
componentDidMount() {
60-
AuroraIController.addMessageListDidLoadListener(() => {
61-
62-
var messages = []
63-
for(var i=0; i<14; i++){
64-
var message = constructNormalMessage()
65-
message.msgType = "text"
66-
message.text = "" + i
67-
AuroraIController.insertMessagesToTop([message])
49+
convertJMessageToAuroraMsg(jmessage) {
50+
var auroraMsg = {}
51+
auroraMsg.msgType = jmessage.type
52+
auroraMsg.msgId = jmessage.id
53+
54+
if (jmessage.type === 'text') {
55+
auroraMsg.text = jmessage.text
56+
// auroraMsg.text = "jmessage.text"
6857
}
69-
AuroraIController.insertMessagesToTop(messages)
70-
});
58+
59+
if (jmessage.type === 'image') {
60+
auroraMsg.mediaPath = jmessage.thumbPath
61+
}
62+
63+
if (jmessage.type === 'voice') {
64+
auroraMsg.mediaPath = jmessage.path
65+
auroraMsg.duration = jmessage.duration
66+
}
67+
68+
var user = {
69+
userId: "",
70+
displayName: "",
71+
avatarPath: ""
72+
}
73+
user.userId = jmessage.from.username
74+
user.displayName = jmessage.from.nickname
75+
user.avatarPath = jmessage.from.avatarThumbPath
76+
auroraMsg.fromUser = user
77+
auroraMsg.status = "send_going"
78+
79+
auroraMsg.isOutgoing = true
80+
81+
if (this.myInfo.username === jmessage.from.username) {
82+
auroraMsg.isOutgoing = true
83+
} else {
84+
auroraMsg.isOutgoing = false
85+
}
86+
87+
auroraMsg.timeString = ""
88+
89+
return auroraMsg
90+
}
91+
92+
getNormalMessage () {
93+
var message = {}
94+
95+
if (this.conversation.conversationType === 'single') {
96+
message.type = 'single'
97+
message.username = this.conversation.key
98+
} else {
99+
message.type = 'group'
100+
message.groupId = this.conversation.key
101+
}
102+
return message
103+
}
104+
105+
componentDidMount() {
106+
// Alert.alert(this.props.navigation.state.params.key)
107+
108+
var parames = {
109+
110+
'from': 0, // 开始的消息下标。
111+
'limit': 10 // 要获取的消息数。比如当 from = 0, limit = 10 时,是获取第 0 - 9 条历史消息。
112+
}
113+
// Alert.alert('conversation', this.conversation)
114+
if (this.conversation.conversationType === 'single') {
115+
parames.type = 'single'
116+
parames.username = this.conversation.key
117+
} else {
118+
parames.type = 'group'
119+
parames.groupId = this.conversation.key
120+
}
121+
this.messageListDidLoadCallback = () => {
122+
123+
JMessage.getHistoryMessages(parames, (messages) => {
124+
125+
var auroraMessages = messages.map((message) => {
126+
var normalMessage = this.convertJMessageToAuroraMsg(message)
127+
return normalMessage
128+
})
129+
AuroraIController.insertMessagesToTop(auroraMessages)
130+
}, (error) => {
131+
Alert.alert('error!', JSON.stringify(error))
132+
})
133+
134+
this.receiveMessageCallBack = (message) => {
135+
136+
if (this.conversation.conversationType === 'single') {
137+
if (message.target.type === 'user' ) {
138+
if (message.from.username === this.conversation.key) {
139+
var msg = this.convertJMessageToAuroraMsg(message)
140+
AuroraIController.appendMessages([msg])
141+
}
142+
Alert.alert('message.target.username', message.target.username)
143+
Alert.alert('this.conversation.key', this.conversation.key)
144+
// Alert.alert("1111:", JSON.stringify(message))
145+
}
146+
} else {
147+
if (message.target.type === 'group') {
148+
if (message.from.id === this.conversation.key) {
149+
var msg = this.convertJMessageToAuroraMsg(message)
150+
AuroraIController.appendMessages([msg])
151+
}
152+
}
153+
}
154+
}
155+
JMessage.addReceiveMessageListener(this.receiveMessageCallBack)
156+
}
157+
AuroraIController.addMessageListDidLoadListener(this.messageListDidLoadCallback)
158+
}
159+
160+
componentWillUnmount() {
161+
Alert.alert("Component", "will unmount")
162+
JMessage.removeReceiveMessageListener(this.receiveMessageCallBack)
163+
AuroraIController.removeMessageListDidLoadListener(this.messageListDidLoadCallback)
71164
}
72165

73166
updateLayout(layout) {
@@ -100,36 +193,45 @@ export default class Chat extends Component {
100193

101194
onSendText = (text) => {
102195

103-
var message = constructNormalMessage()
104-
105-
message.msgType = "text"
196+
var message = this.getNormalMessage()
106197
message.text = text
107-
message.timeString = 'fsdafafaf'
108-
109-
AuroraIController.appendMessages([message])
110-
AuroraIController.scrollToBottom(true)
198+
JMessage.sendTextMessage(message, (message) => {
199+
var auroraMsg = this.convertJMessageToAuroraMsg(message)
200+
AuroraIController.appendMessages([auroraMsg])
201+
AuroraIController.scrollToBottom(true)
202+
}, (error) => {
203+
Alert.alert(JSON.stringify(error))
204+
})
111205
}
112206

113207
onTakePicture = (mediaPath) => {
208+
var message = this.getNormalMessage()
209+
message.path = mediaPath
210+
JMessage.sendImageMessage(message, (message) => {
211+
var auroraMsg = this.convertJMessageToAuroraMsg(message)
212+
AuroraIController.appendMessages([auroraMsg])
213+
AuroraIController.scrollToBottom(true)
214+
}, (error) => {
215+
Alert.alert(JSON.stringify(error))
216+
})
114217

115-
var message = constructNormalMessage()
116-
message.msgType = "image"
117-
message.mediaPath = mediaPath
118-
119-
AuroraIController.appendMessages([message])
120-
AuroraIController.scrollToBottom(true)
121218
}
122219

123220
onStartRecordVoice = (e) => {
124221
console.log("on start record voice")
125222
}
126223

127224
onFinishRecordVoice = (mediaPath, duration) => {
128-
var message = constructNormalMessage()
129-
message.msgType = "voice"
130-
message.mediaPath = mediaPath
131-
message.duration = duration
132-
AuroraIController.appendMessages([message])
225+
226+
var message = this.getNormalMessage()
227+
message.path = mediaPath
228+
JMessage.sendVoiceMessage(message, (message) => {
229+
var auroraMsg = this.convertJMessageToAuroraMsg(message)
230+
AuroraIController.appendMessages([auroraMsg])
231+
AuroraIController.scrollToBottom(true)
232+
}, (error) => {
233+
Alert.alert(JSON.stringify(error))
234+
})
133235
}
134236

135237
onCancelRecordVoice = () => {
@@ -141,12 +243,18 @@ export default class Chat extends Component {
141243
}
142244

143245
onFinishRecordVideo = (mediaPath) => {
144-
var message = constructNormalMessage()
145-
146-
message.msgType = "video"
147-
message.mediaPath = mediaPath
148246

149-
AuroraIController.appendMessages([message])
247+
var message = this.getNormalMessage()
248+
message.path = mediaPath
249+
message.extras = {type: "video"}
250+
message.fileName = "video"
251+
JMessage.sendFileMessage(message, (message) => {
252+
var auroraMsg = this.convertJMessageToAuroraMsg(message)
253+
AuroraIController.appendMessages([auroraMsg])
254+
AuroraIController.scrollToBottom(true)
255+
}, (error) => {
256+
Alert.alert(JSON.stringify(error))
257+
})
150258
}
151259

152260
onSendGalleryFiles = (mediaFiles) => {
@@ -160,11 +268,15 @@ export default class Chat extends Component {
160268
* 代码用例不做裁剪操作。
161269
*/
162270
for(index in mediaFiles) {
163-
var message = constructNormalMessage()
164-
message.msgType = "image"
165-
message.mediaPath = mediaFiles[index].mediaPath
166-
AuroraIController.appendMessages([message])
167-
AuroraIController.scrollToBottom(true)
271+
var message = this.getNormalMessage()
272+
message.path = mediaFiles[index].mediaPath
273+
JMessage.sendImageMessage(message, (message) => {
274+
var auroraMsg = this.convertJMessageToAuroraMsg(message)
275+
AuroraIController.appendMessages([auroraMsg])
276+
AuroraIController.scrollToBottom(true)
277+
}, (error) => {
278+
Alert.alert(JSON.stringify(error))
279+
})
168280
}
169281
}
170282

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict';
2+
3+
import React from 'react';
4+
import ReactNative from 'react-native';
5+
import JMessage from 'jmessage-react-plugin';
6+
7+
import FormButton from '../../views/FormButton'
8+
9+
const {
10+
View,
11+
Text,
12+
TouchableHighlight,
13+
StyleSheet,
14+
Button,
15+
Alert,
16+
TextInput,
17+
FlatList,
18+
} = ReactNative;
19+
20+
class MyListItem extends React.PureComponent {
21+
_onPress = () => {
22+
this.props.onPressItem(this.props.id);
23+
};
24+
25+
render() {
26+
return (
27+
<View>
28+
<SomeOtherWidget
29+
{...this.props}
30+
onPress={this._onPress}
31+
/>
32+
</View>)
33+
}
34+
}
35+
36+
37+
var count = 0
38+
export default class ConversationList extends React.Component {
39+
40+
constructor(props) {
41+
super(props);
42+
this.state = {
43+
data: [{key:'a'}, {key:'b'}]
44+
}
45+
this._onPress = this._onPress.bind(this)
46+
}
47+
48+
componentWillMount() {
49+
JMessage.getConversations((result) => {
50+
51+
var data = result.map((conversaion) =>
52+
{
53+
var item
54+
if (conversaion.conversationType === 'single') {
55+
item = {key: conversaion.target.username}
56+
} else {
57+
item = {key: conversaion.target.id}
58+
Alert.alert('conversaion', JSON.stringify(conversaion))
59+
}
60+
61+
item.conversationType = conversaion.conversationType
62+
if (conversaion.latestMessage.type === 'text') {
63+
item.latestMessageString = conversaion.latestMessage.text
64+
}
65+
66+
if (conversaion.latestMessage.type === 'image') {
67+
item.latestMessageString = '[图片]'
68+
}
69+
70+
if (conversaion.latestMessage.type === 'voice') {
71+
item.latestMessageString = '[语言]'
72+
}
73+
74+
if (conversaion.latestMessage.type === 'file') {
75+
item.latestMessageString = '[文件]'
76+
}
77+
78+
return item
79+
})
80+
this.setState({data: data})
81+
}, (error) => {
82+
Alert.alert(JSON.stringify(error))
83+
})
84+
}
85+
86+
_onPress() {
87+
}
88+
89+
render() {
90+
this.listView = <FlatList
91+
data = { this.state.data }
92+
renderItem = { ({item}) => (
93+
<View>
94+
<TouchableHighlight
95+
onPress={ () => {
96+
this.props.navigation.navigate('Chat', {conversation: item})
97+
}}>
98+
<Text>{item.key}</Text>
99+
100+
</TouchableHighlight>
101+
</View>
102+
) }
103+
>
104+
105+
</FlatList>
106+
return (
107+
<View>
108+
<Button
109+
title = "Add"
110+
onPress={this._onPress}>
111+
112+
</Button>
113+
{ this.listView }
114+
</View>)
115+
}
116+
}

0 commit comments

Comments
 (0)