您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RecentList.native.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import React from 'react';
  2. import { ListView, Text, TouchableHighlight, View } from 'react-native';
  3. import { connect } from 'react-redux';
  4. import { Icon } from '../../base/font-icons';
  5. import AbstractRecentList from './AbstractRecentList';
  6. import styles, { UNDERLAY_COLOR } from './styles';
  7. /**
  8. * The native container rendering the list of the recently joined rooms.
  9. *
  10. * @extends AbstractRecentList
  11. */
  12. class RecentList extends AbstractRecentList {
  13. /**
  14. * Initializes a new {@code RecentList} instance.
  15. */
  16. constructor() {
  17. super();
  18. // Bind event handlers so they are only bound once per instance.
  19. this._getAvatarStyle = this._getAvatarStyle.bind(this);
  20. this._onSelect = this._onSelect.bind(this);
  21. this._renderConfDuration = this._renderConfDuration.bind(this);
  22. this._renderRow = this._renderRow.bind(this);
  23. this._renderServerInfo = this._renderServerInfo.bind(this);
  24. }
  25. /**
  26. * Implements React's {@link Component#render()}. Renders a list of recently
  27. * joined rooms.
  28. *
  29. * @inheritdoc
  30. * @returns {ReactElement}
  31. */
  32. render() {
  33. if (!this.state.dataSource.getRowCount()) {
  34. return null;
  35. }
  36. return (
  37. <View style = { styles.container }>
  38. <ListView
  39. dataSource = { this.state.dataSource }
  40. enableEmptySections = { true }
  41. renderRow = { this._renderRow } />
  42. </View>
  43. );
  44. }
  45. /**
  46. * Assembles the style array of the avatar based on if the conference was a
  47. * home or remote server conference (based on current app setting).
  48. *
  49. * @param {Object} recentListEntry - The recent list entry being rendered.
  50. * @private
  51. * @returns {Array<Object>}
  52. */
  53. _getAvatarStyle(recentListEntry) {
  54. const avatarStyles = [ styles.avatar ];
  55. if (recentListEntry.baseURL !== this.props._homeServer) {
  56. avatarStyles.push(
  57. this._getColorForServerName(recentListEntry.serverName));
  58. }
  59. return avatarStyles;
  60. }
  61. /**
  62. * Returns a style (color) based on the server name, so then the same server
  63. * will always be rendered with the same avatar color.
  64. *
  65. * @param {string} serverName - The recent list entry being rendered.
  66. * @private
  67. * @returns {Object}
  68. */
  69. _getColorForServerName(serverName) {
  70. let nameHash = 0;
  71. for (let i = 0; i < serverName.length; i++) {
  72. nameHash += serverName.codePointAt(i);
  73. }
  74. return styles[`avatarRemoteServer${(nameHash % 5) + 1}`];
  75. }
  76. /**
  77. * Renders the conference duration if available.
  78. *
  79. * @param {Object} recentListEntry - The recent list entry being rendered.
  80. * @private
  81. * @returns {ReactElement}
  82. */
  83. _renderConfDuration({ conferenceDurationString }) {
  84. if (conferenceDurationString) {
  85. return (
  86. <View style = { styles.infoWithIcon } >
  87. <Icon
  88. name = 'timer'
  89. style = { styles.inlineIcon } />
  90. <Text style = { styles.confLength }>
  91. { conferenceDurationString }
  92. </Text>
  93. </View>
  94. );
  95. }
  96. return null;
  97. }
  98. /**
  99. * Renders the server info component based on if the entry was on a
  100. * different server or not.
  101. *
  102. * @param {Object} recentListEntry - The recent list entry being rendered.
  103. * @private
  104. * @returns {ReactElement}
  105. */
  106. _renderServerInfo(recentListEntry) {
  107. if (recentListEntry.baseURL !== this.props._homeServer) {
  108. return (
  109. <View style = { styles.infoWithIcon } >
  110. <Icon
  111. name = 'public'
  112. style = { styles.inlineIcon } />
  113. <Text style = { styles.serverName }>
  114. { recentListEntry.serverName }
  115. </Text>
  116. </View>
  117. );
  118. }
  119. return null;
  120. }
  121. /**
  122. * Renders the list of recently joined rooms.
  123. *
  124. * @param {Object} data - The row data to be rendered.
  125. * @private
  126. * @returns {ReactElement}
  127. */
  128. _renderRow(data) {
  129. return (
  130. <TouchableHighlight
  131. onPress = { this._onSelect(data.conference) }
  132. underlayColor = { UNDERLAY_COLOR } >
  133. <View style = { styles.row } >
  134. <View style = { styles.avatarContainer } >
  135. <View style = { this._getAvatarStyle(data) } >
  136. <Text style = { styles.avatarContent }>
  137. { data.initials }
  138. </Text>
  139. </View>
  140. </View>
  141. <View style = { styles.detailsContainer } >
  142. <Text
  143. numberOfLines = { 1 }
  144. style = { styles.roomName }>
  145. { data.room }
  146. </Text>
  147. <View style = { styles.infoWithIcon } >
  148. <Icon
  149. name = 'event_note'
  150. style = { styles.inlineIcon } />
  151. <Text style = { styles.date }>
  152. { data.dateString }
  153. </Text>
  154. </View>
  155. {
  156. this._renderConfDuration(data)
  157. }
  158. {
  159. this._renderServerInfo(data)
  160. }
  161. </View>
  162. </View>
  163. </TouchableHighlight>
  164. );
  165. }
  166. }
  167. /**
  168. * Maps (parts of) the Redux state to the associated RecentList's props.
  169. *
  170. * @param {Object} state - The Redux state.
  171. * @private
  172. * @returns {{
  173. * _homeServer: string
  174. * }}
  175. */
  176. function _mapStateToProps(state) {
  177. return {
  178. /**
  179. * The default server name based on which we determine the render
  180. * method.
  181. *
  182. * @private
  183. * @type {string}
  184. */
  185. _homeServer: state['features/app'].app._getDefaultURL()
  186. };
  187. }
  188. export default connect(_mapStateToProps)(RecentList);