Skip to content

Commit 377fe77

Browse files
committed
update example
1 parent 7ee3b2a commit 377fe77

File tree

1 file changed

+125
-80
lines changed

1 file changed

+125
-80
lines changed

example/app/routes/Chat/index.js

Lines changed: 125 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import Translations from '../../resource/Translations'
3535

3636
var themsgid = 1
3737

38-
3938
export default class Chat extends Component {
4039

4140
static navigationOptions = {
@@ -68,7 +67,6 @@ export default class Chat extends Component {
6867

6968
if (jmessage.type === 'text') {
7069
auroraMsg.text = jmessage.text
71-
// auroraMsg.text = "jmessage.text"
7270
}
7371

7472
if (jmessage.type === 'image') {
@@ -79,7 +77,13 @@ export default class Chat extends Component {
7977
auroraMsg.mediaPath = jmessage.path
8078
auroraMsg.duration = jmessage.duration
8179
}
82-
80+
81+
if (jmessage.type === 'file') {
82+
auroraMsg.mediaPath = jmessage.path
83+
auroraMsg.duration = jmessage.duration
84+
auroraMsg.msgType = "video"
85+
}
86+
8387
var user = {
8488
userId: "1",
8589
displayName: "",
@@ -96,7 +100,7 @@ export default class Chat extends Component {
96100
}
97101
auroraMsg.fromUser = user
98102
console.log("from user: " + JSON.stringify(auroraMsg.fromUser))
99-
auroraMsg.status = "send_going"
103+
auroraMsg.status = "send_succeed"
100104

101105
auroraMsg.isOutgoing = true
102106

@@ -125,14 +129,12 @@ export default class Chat extends Component {
125129
}
126130

127131
componentDidMount() {
128-
// Alert.alert(this.props.navigation.state.params.key)
129-
130132
var parames = {
131133

132134
'from': 0, // 开始的消息下标。
133135
'limit': 10 // 要获取的消息数。比如当 from = 0, limit = 10 时,是获取第 0 - 9 条历史消息。
134136
}
135-
// Alert.alert('conversation', this.conversation)
137+
136138
if (this.conversation.conversationType === 'single') {
137139
parames.type = 'single'
138140
parames.username = this.conversation.key
@@ -143,7 +145,6 @@ export default class Chat extends Component {
143145
this.messageListDidLoadCallback = () => {
144146

145147
JMessage.getHistoryMessages(parames, (messages) => {
146-
147148
var auroraMessages = messages.map((message) => {
148149
var normalMessage = this.convertJMessageToAuroraMsg(message)
149150
if (normalMessage.msgType === "unknow") {
@@ -155,7 +156,6 @@ export default class Chat extends Component {
155156
AuroraIController.insertMessagesToTop(auroraMessages)
156157
} else {
157158
AuroraIController.insertMessagesToTop(auroraMessages)
158-
// AuroraIController.scrollToBottom(true)
159159
}
160160
}, (error) => {
161161
Alert.alert('error!', JSON.stringify(error))
@@ -171,7 +171,6 @@ export default class Chat extends Component {
171171
}
172172
Alert.alert('message.target.username', message.target.username)
173173
Alert.alert('this.conversation.key', this.conversation.key)
174-
// Alert.alert("1111:", JSON.stringify(message))
175174
}
176175
} else {
177176
if (message.target.type === 'group') {
@@ -210,7 +209,7 @@ export default class Chat extends Component {
210209
isDismissMenuContainer: true,
211210
inputViewLayout: {
212211
width: Dimensions.get('window').width,
213-
height: 100
212+
height: 86
214213
},
215214
shouldExpandMenuContainer: false,
216215
});
@@ -260,58 +259,59 @@ export default class Chat extends Component {
260259
onSendText = (text) => {
261260

262261
var message = this.getNormalMessage()
263-
264262
message.text = text
265-
JMessage.sendTextMessage(message, (message) => {
266-
var auroraMsg = this.convertJMessageToAuroraMsg(message)
263+
message.messageType = "text"
264+
265+
JMessage.createSendMessage(message, (msg) => {
266+
var auroraMsg = this.convertJMessageToAuroraMsg(msg)
267+
auroraMsg.status = 'send_going'
267268
AuroraIController.appendMessages([auroraMsg])
268269
AuroraIController.scrollToBottom(true)
269-
}, (error) => {
270-
Alert.alert(JSON.stringify(error))
271-
})
272-
}
273-
274-
onTakePicture = (mediaPath) => {
275-
var message = this.getNormalMessage()
276-
message.path = mediaPath
277-
message.messageType = "image"
278-
JMessage.createSendMessage(message, (message) => {
279-
Alert.alert("the message:", JSON.stringify(message))
280-
// {
281-
// * 'id': Number, // message id
282-
// * 'type': String, // 'single' / 'group'
283-
// * 'groupId': String, // 当 type = group 时,groupId 不能为空
284-
// * 'username': String, // 当 type = single 时,username 不能为空
285-
// * 'appKey': String, // 当 type = single 时,用于指定对象所属应用的 appKey。如果为空,默认为当前应用。
286-
// * 'messageSendingOptions': MessageSendingOptions // Optional. MessageSendingOptions 对象
287-
// * }
288-
var msg = {}
289-
msg.id = message.id
290-
console.log(JSON.stringify(message))
291-
// msg.type = message.target.type
292-
if (message.target.type === 'user') {
293-
msg.username = message.target.username
270+
271+
if (this.conversation.conversationType === 'single') {
294272
msg.type = 'single'
273+
msg.username = this.conversation.key
295274
} else {
296-
msg.groupId = message.target.id
297275
msg.type = 'group'
276+
msg.groupId = this.conversation.key
298277
}
299278

300-
JMessage.sendMessage(msg,(message) => {
301-
302-
},(error) => {
303-
304-
},(progress) => {
305-
// console.log("" + progress)
279+
JMessage.sendMessage(msg, (jmessage) => {
280+
281+
var auroraMsg = this.convertJMessageToAuroraMsg(jmessage)
282+
AuroraIController.updateMessage(auroraMsg)
283+
}, (error) => {
306284
})
307285
})
308-
// JMessage.sendImageMessage(message, (message) => {
309-
// var auroraMsg = this.convertJMessageToAuroraMsg(message)
310-
// AuroraIController.appendMessages([auroraMsg])
311-
// AuroraIController.scrollToBottom(true)
312-
// }, (error) => {
313-
// Alert.alert(JSON.stringify(error))
314-
// })
286+
}
287+
288+
onTakePicture = (mediaPath) => {
289+
var message = this.getNormalMessage()
290+
message.text = text
291+
message.messageType = "image"
292+
message.path = mediaPath
293+
294+
JMessage.createSendMessage(message, (msg) => {
295+
var auroraMsg = this.convertJMessageToAuroraMsg(msg)
296+
auroraMsg.status = 'send_going'
297+
AuroraIController.appendMessages([auroraMsg])
298+
AuroraIController.scrollToBottom(true)
299+
300+
if (this.conversation.conversationType === 'single') {
301+
msg.type = 'single'
302+
msg.username = this.conversation.key
303+
} else {
304+
msg.type = 'group'
305+
msg.groupId = this.conversation.key
306+
}
307+
308+
JMessage.sendMessage(msg, (jmessage) => {
309+
var auroraMsg = this.convertJMessageToAuroraMsg(jmessage)
310+
AuroraIController.updateMessage(auroraMsg)
311+
}, (error) => {
312+
Alert.alert('send image fail')
313+
})
314+
})
315315

316316
}
317317

@@ -320,16 +320,31 @@ export default class Chat extends Component {
320320
}
321321

322322
onFinishRecordVoice = (mediaPath, duration) => {
323-
324323
var message = this.getNormalMessage()
324+
message.messageType = "voice"
325325
message.path = mediaPath
326-
JMessage.sendVoiceMessage(message, (message) => {
327-
var auroraMsg = this.convertJMessageToAuroraMsg(message)
326+
327+
JMessage.createSendMessage(message, (msg) => {
328+
var auroraMsg = this.convertJMessageToAuroraMsg(msg)
329+
auroraMsg.status = 'send_going'
328330
AuroraIController.appendMessages([auroraMsg])
329331
AuroraIController.scrollToBottom(true)
330-
}, (error) => {
331-
Alert.alert(JSON.stringify(error))
332-
})
332+
333+
if (this.conversation.conversationType === 'single') {
334+
msg.type = 'single'
335+
msg.username = this.conversation.key
336+
} else {
337+
msg.type = 'group'
338+
msg.groupId = this.conversation.key
339+
}
340+
341+
JMessage.sendMessage(msg, (jmessage) => {
342+
var auroraMsg = this.convertJMessageToAuroraMsg(jmessage)
343+
AuroraIController.updateMessage(auroraMsg)
344+
}, (error) => {
345+
Alert.alert('send image fail')
346+
})
347+
})
333348
}
334349

335350
onCancelRecordVoice = () => {
@@ -341,39 +356,59 @@ export default class Chat extends Component {
341356
}
342357

343358
onFinishRecordVideo = (mediaPath) => {
344-
345359
var message = this.getNormalMessage()
360+
message.messageType = "file"
346361
message.path = mediaPath
347-
message.extras = {type: "video"}
348-
message.fileName = "video"
349-
JMessage.sendFileMessage(message, (message) => {
350-
var auroraMsg = this.convertJMessageToAuroraMsg(message)
362+
363+
JMessage.createSendMessage(message, (msg) => {
364+
var auroraMsg = this.convertJMessageToAuroraMsg(msg)
365+
auroraMsg.status = 'send_going'
351366
AuroraIController.appendMessages([auroraMsg])
352367
AuroraIController.scrollToBottom(true)
353-
}, (error) => {
354-
Alert.alert(JSON.stringify(error))
368+
369+
if (this.conversation.conversationType === 'single') {
370+
msg.type = 'single'
371+
msg.username = this.conversation.key
372+
} else {
373+
msg.type = 'group'
374+
msg.groupId = this.conversation.key
375+
}
376+
377+
JMessage.sendMessage(msg, (jmessage) => {
378+
var auroraMsg = this.convertJMessageToAuroraMsg(jmessage)
379+
AuroraIController.updateMessage(auroraMsg)
380+
}, (error) => {
381+
Alert.alert('send image fail')
382+
})
355383
})
356384
}
357385

358386
onSendGalleryFiles = (mediaFiles) => {
359-
360-
361-
/**
362-
* WARN: 这里返回的是原图,直接插入大会话列表会很大且耗内存.
363-
* 应该做裁剪操作后再插入到 messageListView 中,
364-
* 一般的 IM SDK 会提供裁剪操作,或者开发者手动进行裁剪。
365-
*
366-
* 代码用例不做裁剪操作。
367-
*/
368387
for(index in mediaFiles) {
369388
var message = this.getNormalMessage()
389+
message.messageType = "image"
370390
message.path = mediaFiles[index].mediaPath
371-
JMessage.sendImageMessage(message, (message) => {
372-
var auroraMsg = this.convertJMessageToAuroraMsg(message)
391+
392+
JMessage.createSendMessage(message, (msg) => {
393+
var auroraMsg = this.convertJMessageToAuroraMsg(msg)
394+
auroraMsg.status = 'send_going'
373395
AuroraIController.appendMessages([auroraMsg])
374396
AuroraIController.scrollToBottom(true)
375-
}, (error) => {
376-
Alert.alert(JSON.stringify(error))
397+
398+
if (this.conversation.conversationType === 'single') {
399+
msg.type = 'single'
400+
msg.username = this.conversation.key
401+
} else {
402+
msg.type = 'group'
403+
msg.groupId = this.conversation.key
404+
}
405+
406+
JMessage.sendMessage(msg, (jmessage) => {
407+
var auroraMsg = this.convertJMessageToAuroraMsg(jmessage)
408+
AuroraIController.updateMessage(auroraMsg)
409+
}, (error) => {
410+
Alert.alert('send image fail')
411+
})
377412
})
378413
}
379414
}
@@ -395,14 +430,23 @@ export default class Chat extends Component {
395430
} else {
396431
this.updateLayout({width:window.width, height:338,})
397432
}
398-
399433
}
400434

401435
onShowKeyboard = (keyboard_height) => {
402436
var inputViewHeight = keyboard_height + 86
403437
this.updateLayout({width:window.width, height:inputViewHeight,})
404438
}
405439

440+
onSwitchToEmojiMode = () => {
441+
if (Platform.OS == "android") {
442+
this.updateLayout({width:window.width, height: 338})
443+
this.setState({
444+
shouldExpandMenuContainer: true
445+
})
446+
} else {
447+
this.updateLayout({width:window.width, height:338,})
448+
}
449+
}
406450

407451
onInitPress() {
408452
console.log('on click init push ');
@@ -437,6 +481,7 @@ export default class Chat extends Component {
437481
onStartRecordVideo={this.onStartRecordVideo}
438482
onFinishRecordVideo={this.onFinishRecordVideo}
439483
onSendGalleryFiles={this.onSendGalleryFiles}
484+
onSwitchToEmojiMode={this.onSwitchToEmojiMode}
440485
onSwitchToMicrophoneMode={this.onSwitchToMicrophoneMode}
441486
onSwitchToGalleryMode={this.onSwitchToGalleryMode}
442487
onSwitchToCameraMode={this.onSwitchToCameraMode}

0 commit comments

Comments
 (0)