You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecentList.native.js 6.5KB

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