support opening all rooms of a group in OpenRoomsStore

using new view_group_grid action
pull/21833/head
Bruno Windels 2018-11-07 16:32:12 +01:00
parent d7924ad1a8
commit 6ec6303b97
1 changed files with 87 additions and 25 deletions

View File

@ -16,9 +16,21 @@ limitations under the License.
import MatrixDispatcher from '../matrix-dispatcher'; import MatrixDispatcher from '../matrix-dispatcher';
import dis from '../dispatcher'; import dis from '../dispatcher';
import {RoomViewStore} from './RoomViewStore'; import {RoomViewStore} from './RoomViewStore';
import GroupStore from './GroupStore';
import {Store} from 'flux/utils'; import {Store} from 'flux/utils';
import MatrixClientPeg from '../MatrixClientPeg'; import MatrixClientPeg from '../MatrixClientPeg';
function matchesRoom(payload, roomStore) {
if (!roomStore) {
return false;
}
if (payload.room_alias) {
return payload.room_alias === roomStore.getRoomAlias();
}
return payload.room_id === roomStore.getRoomId();
}
/** /**
* A class for keeping track of the RoomViewStores of the rooms shown on the screen. * A class for keeping track of the RoomViewStores of the rooms shown on the screen.
* Routes the dispatcher actions to the store of currently active room. * Routes the dispatcher actions to the store of currently active room.
@ -29,21 +41,30 @@ class OpenRoomsStore extends Store {
// Initialise state // Initialise state
this._state = { this._state = {
room: { rooms: [],
store: null, currentIndex: null,
dispatcher: null group_id: null,
},
}; };
this._forwardingEvent = null; this._forwardingEvent = null;
} }
getRoomStore() { getRoomStores() {
return this._state.room.store; return this._state.rooms.map((r) => r.store);
} }
getCurrentRoomStore() { getCurrentRoomStore() {
return this.getRoomStore(); // just one room for now const currentRoom = this._getCurrentRoom();
if (currentRoom) {
return currentRoom.store;
}
}
_getCurrentRoom() {
const index = this._state.currentIndex;
if (index !== null && index < this._state.rooms.length) {
return this._state.rooms[index];
}
} }
_setState(newState) { _setState(newState) {
@ -51,30 +72,41 @@ class OpenRoomsStore extends Store {
this.__emitChange(); this.__emitChange();
} }
_cleanupRoom() { _hasRoom(payload) {
return this._roomIndex(payload) !== -1;
}
_roomIndex(payload) {
return this._state.rooms.findIndex((r) => matchesRoom(payload, r.store));
}
_cleanupRooms() {
const room = this._state.room; const room = this._state.room;
room.dispatcher.unregister(room.store.getDispatchToken()); this._state.rooms.forEach((room) => {
room.dispatcher.unregister(room.store.getDispatchToken());
});
this._setState({ this._setState({
room: { rooms: [],
store: null, group_id: null,
dispatcher: null currentIndex: null
},
}); });
} }
_createRoom() { _createRoom() {
const dispatcher = new MatrixDispatcher(); const dispatcher = new MatrixDispatcher();
this._setState({ this._setState({
room: { rooms: [{
store: new RoomViewStore(dispatcher), store: new RoomViewStore(dispatcher),
dispatcher, dispatcher,
}, }],
currentIndex: 0,
}); });
} }
_forwardAction(payload) { _forwardAction(payload) {
if (this._state.room.dispatcher) { const currentRoom = this._getCurrentRoom();
this._state.room.dispatcher.dispatch(payload, true); if (currentRoom) {
currentRoom.dispatcher.dispatch(payload, true);
} }
} }
@ -101,6 +133,10 @@ class OpenRoomsStore extends Store {
} }
} }
_setCurrentGroupRoom(index) {
this._setState({currentIndex: index});
}
__onDispatch(payload) { __onDispatch(payload) {
switch (payload.action) { switch (payload.action) {
// view_room: // view_room:
@ -115,14 +151,15 @@ class OpenRoomsStore extends Store {
this._resolveRoomAlias(payload); this._resolveRoomAlias(payload);
} }
const currentStore = this.getCurrentRoomStore(); const currentStore = this.getCurrentRoomStore();
if (currentStore && if (matchesRoom(payload, currentStore)) {
(!payload.room_alias || payload.room_alias !== currentStore.getRoomAlias()) && if (this._hasRoom(payload)) {
(!currentStore.getRoomId() || payload.room_id !== currentStore.getRoomId()) const roomIndex = this._roomIndex(payload);
) { this._setState({currentIndex: roomIndex});
console.log("OpenRoomsStore: _cleanupRoom"); } else {
this._cleanupRoom(); this._cleanupRooms();
}
} }
if (!this._state.room.store) { if (!this.getCurrentRoomStore()) {
console.log("OpenRoomsStore: _createRoom"); console.log("OpenRoomsStore: _createRoom");
this._createRoom(); this._createRoom();
} }
@ -140,7 +177,7 @@ class OpenRoomsStore extends Store {
case 'view_my_groups': case 'view_my_groups':
case 'view_group': case 'view_group':
this._forwardAction(payload); this._forwardAction(payload);
this._cleanupRoom(); this._cleanupRooms();
break; break;
case 'will_join': case 'will_join':
case 'cancel_join': case 'cancel_join':
@ -155,6 +192,31 @@ class OpenRoomsStore extends Store {
case 'forward_event': case 'forward_event':
this._forwardingEvent = payload.event; this._forwardingEvent = payload.event;
break; break;
case 'view_group_grid':
if (payload.group_id !== this._state.group_id) {
this._cleanupRooms();
// TODO: register to GroupStore updates
const rooms = GroupStore.getGroupRooms(payload.group_id);
const roomStores = rooms.map((room) => {
const dispatcher = new MatrixDispatcher();
const store = new RoomViewStore(dispatcher);
// set room id of store
dispatcher.dispatch({
action: 'view_room',
room_id: room.roomId
}, true);
return {
store,
dispatcher,
};
});
this._setState({
rooms: roomStores,
group_id: payload.group_id,
});
this._setCurrentGroupRoom(0);
}
break;
} }
} }
} }