|
| 1 | +// /** |
| 2 | +// * Sample React Native App |
| 3 | +// * https://github.com/facebook/react-native |
| 4 | +// * @flow |
| 5 | +// */ |
| 6 | + |
| 7 | +import React, { Component } from 'react'; |
| 8 | +import { |
| 9 | + AppRegistry, |
| 10 | + StyleSheet, |
| 11 | + Text, |
| 12 | + View, |
| 13 | + TouchableHighlight, |
| 14 | + NativeModules, |
| 15 | + requireNativeComponent, |
| 16 | + Alert, |
| 17 | + Dimensions, |
| 18 | + DeviceEventEmitter |
| 19 | +} from 'react-native'; |
| 20 | + |
| 21 | +var ReactNative = require('react-native'); |
| 22 | + |
| 23 | +import IMUI from 'aurora-imui-react-native' |
| 24 | +var InputView = IMUI.ChatInput; |
| 25 | +var MessageListView = IMUI.MessageList; |
| 26 | +const AuroraIController = IMUI.AuroraIMUIController; |
| 27 | +const window = Dimensions.get('window'); |
| 28 | + |
| 29 | +import Translations from '../../resource/Translations' |
| 30 | + |
| 31 | +var themsgid = 1 |
| 32 | + |
| 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 | +} |
| 50 | + |
| 51 | +export default class Chat extends Component { |
| 52 | + constructor(props) { |
| 53 | + super(props); |
| 54 | + this.state = { inputViewLayout: {width:window.width, height:86,}}; |
| 55 | + |
| 56 | + this.updateLayout = this.updateLayout.bind(this); |
| 57 | + } |
| 58 | + |
| 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]) |
| 68 | + } |
| 69 | + AuroraIController.insertMessagesToTop(messages) |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + updateLayout(layout) { |
| 74 | + this.setState({inputViewLayout: layout}) |
| 75 | + } |
| 76 | + |
| 77 | + onAvatarClick = (message) => { |
| 78 | + console.log(message) |
| 79 | + } |
| 80 | + |
| 81 | + onMsgClick = (message) => { |
| 82 | + console.log(message) |
| 83 | + } |
| 84 | + |
| 85 | + onStatusViewClick = (message) => { |
| 86 | + console.log(message) |
| 87 | + message.status = 'send_succeed' |
| 88 | + message.fromUser.avatarPath = message.mediaPath |
| 89 | + AuroraIController.updateMessage(message) |
| 90 | + } |
| 91 | + |
| 92 | + onBeginDragMessageList = () => { |
| 93 | + this.updateLayout({width:window.width, height:86,}) |
| 94 | + AuroraIController.hidenFeatureView(true) |
| 95 | + } |
| 96 | + |
| 97 | + onPullToRefresh = () => { |
| 98 | + console.log("on pull to refresh") |
| 99 | + } |
| 100 | + |
| 101 | + onSendText = (text) => { |
| 102 | + |
| 103 | + var message = constructNormalMessage() |
| 104 | + |
| 105 | + message.msgType = "text" |
| 106 | + message.text = text |
| 107 | + message.timeString = 'fsdafafaf' |
| 108 | + |
| 109 | + AuroraIController.appendMessages([message]) |
| 110 | + AuroraIController.scrollToBottom(true) |
| 111 | + } |
| 112 | + |
| 113 | + onTakePicture = (mediaPath) => { |
| 114 | + |
| 115 | + var message = constructNormalMessage() |
| 116 | + message.msgType = "image" |
| 117 | + message.mediaPath = mediaPath |
| 118 | + |
| 119 | + AuroraIController.appendMessages([message]) |
| 120 | + AuroraIController.scrollToBottom(true) |
| 121 | + } |
| 122 | + |
| 123 | + onStartRecordVoice = (e) => { |
| 124 | + console.log("on start record voice") |
| 125 | + } |
| 126 | + |
| 127 | + onFinishRecordVoice = (mediaPath, duration) => { |
| 128 | + var message = constructNormalMessage() |
| 129 | + message.msgType = "voice" |
| 130 | + message.mediaPath = mediaPath |
| 131 | + message.duration = duration |
| 132 | + AuroraIController.appendMessages([message]) |
| 133 | + } |
| 134 | + |
| 135 | + onCancelRecordVoice = () => { |
| 136 | + console.log("on cancel record voice") |
| 137 | + } |
| 138 | + |
| 139 | + onStartRecordVideo = () => { |
| 140 | + console.log("on start record video") |
| 141 | + } |
| 142 | + |
| 143 | + onFinishRecordVideo = (mediaPath) => { |
| 144 | + var message = constructNormalMessage() |
| 145 | + |
| 146 | + message.msgType = "video" |
| 147 | + message.mediaPath = mediaPath |
| 148 | + |
| 149 | + AuroraIController.appendMessages([message]) |
| 150 | + } |
| 151 | + |
| 152 | + onSendGalleryFiles = (mediaFiles) => { |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * WARN: 这里返回的是原图,直接插入大会话列表会很大且耗内存. |
| 157 | + * 应该做裁剪操作后再插入到 messageListView 中, |
| 158 | + * 一般的 IM SDK 会提供裁剪操作,或者开发者手动进行裁剪。 |
| 159 | + * |
| 160 | + * 代码用例不做裁剪操作。 |
| 161 | + */ |
| 162 | + 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) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + onSwitchToMicrophoneMode = () => { |
| 172 | + this.updateLayout({width:window.width, height:256,}) |
| 173 | + } |
| 174 | + |
| 175 | + onSwitchToGalleryMode = () => { |
| 176 | + this.updateLayout({width:window.width, height:256,}) |
| 177 | + } |
| 178 | + |
| 179 | + onSwitchToCameraMode = () => { |
| 180 | + this.updateLayout({width:window.width, height:256,}) |
| 181 | + } |
| 182 | + |
| 183 | + onShowKeyboard = (keyboard_height) => { |
| 184 | + var inputViewHeight = keyboard_height + 86 |
| 185 | + this.updateLayout({width:window.width, height:inputViewHeight,}) |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + onInitPress() { |
| 190 | + console.log('on click init push '); |
| 191 | + this.updateAction(); |
| 192 | + } |
| 193 | + |
| 194 | + render() { |
| 195 | + return ( |
| 196 | + <View style={styles.container}> |
| 197 | + <MessageListView style={styles.messageList} |
| 198 | + onAvatarClick={this.onAvatarClick} |
| 199 | + onMsgClick={this.onMsgClick} |
| 200 | + onStatusViewClick={this.onStatusViewClick} |
| 201 | + onTapMessageCell={this.onTapMessageCell} |
| 202 | + onBeginDragMessageList={this.onBeginDragMessageList} |
| 203 | + onPullToRefresh={this.onPullToRefresh} |
| 204 | + avatarSize={{width:40,height:40}} |
| 205 | + sendBubbleTextSize={18} |
| 206 | + sendBubbleTextColor={"000000"} |
| 207 | + sendBubblePadding={{left:10,top:10,right:10,bottom:10}} |
| 208 | + /> |
| 209 | + <InputView style={this.state.inputViewLayout} |
| 210 | + onSendText={this.onSendText} |
| 211 | + onTakePicture={this.onTakePicture} |
| 212 | + onStartRecordVoice={this.onStartRecordVoice} |
| 213 | + onFinishRecordVoice={this.onFinishRecordVoice} |
| 214 | + onCancelRecordVoice={this.onCancelRecordVoice} |
| 215 | + onStartRecordVideo={this.onStartRecordVideo} |
| 216 | + onFinishRecordVideo={this.onFinishRecordVideo} |
| 217 | + onSendGalleryFiles={this.onSendGalleryFiles} |
| 218 | + onSwitchToMicrophoneMode={this.onSwitchToMicrophoneMode} |
| 219 | + onSwitchToGalleryMode={this.onSwitchToGalleryMode} |
| 220 | + onSwitchToCameraMode={this.onSwitchToCameraMode} |
| 221 | + onShowKeyboard={this.onShowKeyboard} |
| 222 | + /> |
| 223 | + </View> |
| 224 | + ); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +const styles = StyleSheet.create({ |
| 229 | + container: { |
| 230 | + flex: 1, |
| 231 | + justifyContent: 'center', |
| 232 | + alignItems: 'center', |
| 233 | + backgroundColor: '#F5FCFF', |
| 234 | + }, |
| 235 | + messageList: { |
| 236 | + backgroundColor: 'red', |
| 237 | + flex: 1, |
| 238 | + marginTop: 0, |
| 239 | + width: window.width, |
| 240 | + margin:0, |
| 241 | + }, |
| 242 | + inputView: { |
| 243 | + backgroundColor: 'green', |
| 244 | + width: window.width, |
| 245 | + height:100, |
| 246 | + |
| 247 | + }, |
| 248 | + btnStyle: { |
| 249 | + marginTop: 10, |
| 250 | + borderWidth: 1, |
| 251 | + borderColor: '#3e83d7', |
| 252 | + borderRadius: 8, |
| 253 | + backgroundColor: '#3e83d7' |
| 254 | + } |
| 255 | +}); |
| 256 | + |
0 commit comments