react-native-pure-chat-view
Version:
react native chat view
468 lines (402 loc) • 12.7 kB
JavaScript
import React, {
PureComponent,
} from 'react'
import ReactNative, {
UIManager,
requireNativeComponent
} from 'react-native'
import PropTypes from 'prop-types'
class ChatView extends PureComponent {
static propTypes = {
currentUserId: PropTypes.string.isRequired,
leftUserNameVisible: PropTypes.bool.isRequired,
rightUserNameVisible: PropTypes.bool.isRequired,
userAvatarWidth: PropTypes.number.isRequired,
userAvatarHeight: PropTypes.number.isRequired,
userAvatarBorderRadius: PropTypes.number.isRequired,
onReady: PropTypes.func,
onListClick: PropTypes.func,
onUserNameClick: PropTypes.func,
onUserAvatarClick: PropTypes.func,
onContentClick: PropTypes.func,
onFailureClick: PropTypes.func,
onLinkClick: PropTypes.func,
onCopyClick: PropTypes.func,
onShareClick: PropTypes.func,
onRecallClick: PropTypes.func,
onDeleteClick: PropTypes.func,
onLoadMore: PropTypes.func,
onRecordAudioDurationLessThanMinDuration: PropTypes.func,
onRecordAudioExternalStorageNotWritable: PropTypes.func,
onRecordAudioPermissionsNotGranted: PropTypes.func,
onRecordAudioPermissionsGranted: PropTypes.func,
onRecordAudioPermissionsDenied: PropTypes.func,
onRecordVideoDurationLessThanMinDuration: PropTypes.func,
onRecordVideoExternalStorageNotWritable: PropTypes.func,
onRecordVideoPermissionsNotGranted: PropTypes.func,
onRecordVideoPermissionsGranted: PropTypes.func,
onRecordVideoPermissionsDenied: PropTypes.func,
onSendAudio: PropTypes.func,
onSendVideo: PropTypes.func,
onSendPhoto: PropTypes.func,
onSendText: PropTypes.func,
onTextChange: PropTypes.func,
onPhotoFeatureClick: PropTypes.func,
onFileFeatureClick: PropTypes.func,
onUserFeatureClick: PropTypes.func,
onMovieFeatureClick: PropTypes.func,
onPhoneFeatureClick: PropTypes.func,
onLocationFeatureClick: PropTypes.func,
onLift: PropTypes.func,
onFall: PropTypes.func,
}
appendMessage(message, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.appendMessage,
[message, scrollToBottom ? true : false],
)
}
appendMessages(messages, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.appendMessages,
[messages, scrollToBottom ? true : false],
)
}
prependMessage(message, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.prependMessage,
[message, scrollToBottom ? true : false],
)
}
prependMessages(messages, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.prependMessages,
[messages, scrollToBottom ? true : false],
)
}
removeMessage(messageId, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.removeMessage,
[messageId, scrollToBottom ? true : false],
)
}
removeAllMessages() {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.removeAllMessages,
null,
)
}
updateMessage(messageId, message, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.updateMessage,
[messageId, message, scrollToBottom ? true : false],
)
}
setAllMessages(messages, scrollToBottom) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.setAllMessages,
[messages, scrollToBottom ? true : false],
)
}
stopAudio() {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.stopAudio,
null,
)
}
loadMoreComplete(hasMoreMessage) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.loadMoreComplete,
[hasMoreMessage ? true : false],
)
}
scrollToBottom(animated) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.scrollToBottom,
[animated ? true : false],
)
}
setText(text) {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.setText,
[text]
)
}
resetInput() {
UIManager.dispatchViewManagerCommand(
this.getNativeNode(),
UIManager.RNTChatView.Commands.resetInput,
null
)
}
handleReady = () => {
let { onReady } = this.props
if (onReady) {
onReady()
}
}
handleListClick = () => {
let { onListClick } = this.props
if (onListClick) {
onListClick()
}
}
handleUserNameClick = (event) => {
let { onUserNameClick } = this.props
if (onUserNameClick) {
onUserNameClick(event.nativeEvent)
}
}
handleUserAvatarClick = (event) => {
let { onUserAvatarClick } = this.props
if (onUserAvatarClick) {
onUserAvatarClick(event.nativeEvent)
}
}
handleContentClick = (event) => {
let { onContentClick } = this.props
if (onContentClick) {
onContentClick(event.nativeEvent)
}
}
handleFailureClick = (event) => {
let { onFailureClick } = this.props
if (onFailureClick) {
onFailureClick(event.nativeEvent)
}
}
handleLinkClick = (event) => {
let { onLinkClick } = this.props
if (onLinkClick) {
onLinkClick(event.nativeEvent)
}
}
handleCopyClick = (event) => {
let { onCopyClick } = this.props
if (onCopyClick) {
onCopyClick(event.nativeEvent)
}
}
handleShareClick = (event) => {
let { onShareClick } = this.props
if (onShareClick) {
onShareClick(event.nativeEvent)
}
}
handleRecallClick = (event) => {
let { onRecallClick } = this.props
if (onRecallClick) {
onRecallClick(event.nativeEvent)
}
}
handleDeleteClick = (event) => {
let { onDeleteClick } = this.props
if (onDeleteClick) {
onDeleteClick(event.nativeEvent)
}
}
handleLoadMore = () => {
let { onLoadMore } = this.props
if (onLoadMore) {
onLoadMore()
}
}
handleRecordAudioDurationLessThanMinDuration = () => {
let { onRecordAudioDurationLessThanMinDuration } = this.props
if (onRecordAudioDurationLessThanMinDuration) {
onRecordAudioDurationLessThanMinDuration()
}
}
handleRecordAudioExternalStorageNotWritable = () => {
let { onRecordAudioExternalStorageNotWritable } = this.props
if (onRecordAudioExternalStorageNotWritable) {
onRecordAudioExternalStorageNotWritable()
}
}
handleRecordAudioPermissionsNotGranted = () => {
let { onRecordAudioPermissionsNotGranted } = this.props
if (onRecordAudioPermissionsNotGranted) {
onRecordAudioPermissionsNotGranted()
}
}
handleRecordAudioPermissionsGranted = () => {
let { onRecordAudioPermissionsGranted } = this.props
if (onRecordAudioPermissionsGranted) {
onRecordAudioPermissionsGranted()
}
}
handleRecordAudioPermissionsDenied = () => {
let { onRecordAudioPermissionsDenied } = this.props
if (onRecordAudioPermissionsDenied) {
onRecordAudioPermissionsDenied()
}
}
handleRecordVideoDurationLessThanMinDuration = () => {
let { onRecordVideoDurationLessThanMinDuration } = this.props
if (onRecordVideoDurationLessThanMinDuration) {
onRecordVideoDurationLessThanMinDuration()
}
}
handleRecordVideoExternalStorageNotWritable = () => {
let { onRecordVideoExternalStorageNotWritable } = this.props
if (onRecordVideoExternalStorageNotWritable) {
onRecordVideoExternalStorageNotWritable()
}
}
handleRecordVideoPermissionsNotGranted = () => {
let { onRecordVideoPermissionsNotGranted } = this.props
if (onRecordVideoPermissionsNotGranted) {
onRecordVideoPermissionsNotGranted()
}
}
handleRecordVideoPermissionsGranted = () => {
let { onRecordVideoPermissionsGranted } = this.props
if (onRecordVideoPermissionsGranted) {
onRecordVideoPermissionsGranted()
}
}
handleRecordVideoPermissionsDenied = () => {
let { onRecordVideoPermissionsDenied } = this.props
if (onRecordVideoPermissionsDenied) {
onRecordVideoPermissionsDenied()
}
}
handleSendAudio = (event) => {
let { onSendAudio } = this.props
if (onSendAudio) {
onSendAudio(event.nativeEvent)
}
}
handleSendVideo = (event) => {
let { onSendVideo } = this.props
if (onSendVideo) {
onSendVideo(event.nativeEvent)
}
}
handleSendPhoto = (event) => {
let { onSendPhoto } = this.props
if (onSendPhoto) {
onSendPhoto(event.nativeEvent)
}
}
handleSendText = (event) => {
let { onSendText } = this.props
if (onSendText) {
onSendText(event.nativeEvent)
}
}
handleTextChange = (event) => {
let { onTextChange } = this.props
if (onTextChange) {
onTextChange(event.nativeEvent)
}
}
handlePhotoFeatureClick = () => {
let { onPhotoFeatureClick } = this.props
if (onPhotoFeatureClick) {
onPhotoFeatureClick()
}
}
handleFileFeatureClick = () => {
let { onFileFeatureClick } = this.props
if (onFileFeatureClick) {
onFileFeatureClick()
}
}
handleUserFeatureClick = () => {
let { onUserFeatureClick } = this.props
if (onUserFeatureClick) {
onUserFeatureClick()
}
}
handleMovieFeatureClick = () => {
let { onMovieFeatureClick } = this.props
if (onMovieFeatureClick) {
onMovieFeatureClick()
}
}
handlePhoneFeatureClick = () => {
let { onPhoneFeatureClick } = this.props
if (onPhoneFeatureClick) {
onPhoneFeatureClick()
}
}
handleLocationFeatureClick = () => {
let { onLocationFeatureClick } = this.props
if (onLocationFeatureClick) {
onLocationFeatureClick()
}
}
handleLift = () => {
let { onLift } = this.props
if (onLift) {
onLift()
}
}
handleFall = () => {
let { onFall } = this.props
if (onFall) {
onFall()
}
}
getNativeNode() {
return ReactNative.findNodeHandle(this.refs.chatView);
}
render() {
return (
<RNTChatView
{...this.props}
ref="chatView"
onReady={this.handleReady}
onListClick={this.handleListClick}
onUserNameClick={this.handleUserNameClick}
onUserAvatarClick={this.handleUserAvatarClick}
onContentClick={this.handleContentClick}
onFailureClick={this.handleFailureClick}
onLinkClick={this.handleLinkClick}
onCopyClick={this.handleCopyClick}
onShareClick={this.handleShareClick}
onRecallClick={this.handleRecallClick}
onDeleteClick={this.handleDeleteClick}
onLoadMore={this.handleLoadMore}
onRecordAudioDurationLessThanMinDuration={this.handleRecordAudioDurationLessThanMinDuration}
onRecordAudioExternalStorageNotWritable={this.handleRecordAudioExternalStorageNotWritable}
onRecordAudioPermissionsNotGranted={this.handleRecordAudioPermissionsNotGranted}
onRecordAudioPermissionsGranted={this.handleRecordAudioPermissionsGranted}
onRecordAudioPermissionsDenied={this.handleRecordAudioPermissionsDenied}
onRecordVideoDurationLessThanMinDuration={this.handleRecordVideoDurationLessThanMinDuration}
onRecordVideoExternalStorageNotWritable={this.handleRecordVideoExternalStorageNotWritable}
onRecordVideoPermissionsNotGranted={this.handleRecordVideoPermissionsNotGranted}
onRecordVideoPermissionsGranted={this.handleRecordVideoPermissionsGranted}
onRecordVideoPermissionsDenied={this.handleRecordVideoPermissionsDenied}
onSendAudio={this.handleSendAudio}
onSendVideo={this.handleSendVideo}
onSendPhoto={this.handleSendPhoto}
onSendText={this.handleSendText}
onTextChange={this.handleTextChange}
onPhotoFeatureClick={this.handlePhotoFeatureClick}
onFileFeatureClick={this.handleFileFeatureClick}
onUserFeatureClick={this.handleUserFeatureClick}
onMovieFeatureClick={this.handleMovieFeatureClick}
onPhoneFeatureClick={this.handlePhoneFeatureClick}
onLocationFeatureClick={this.handleLocationFeatureClick}
onLift={this.handleLift}
onFall={this.handleFall}
/>
)
}
}
const RNTChatView = requireNativeComponent('RNTChatView', ChatView)
export default ChatView