Remove duplication in RightPanelViewModel

This commit is contained in:
RMidhunSuresh 2021-06-27 18:10:50 +05:30
parent bcfd1bd135
commit e935423814

View File

@ -9,42 +9,38 @@ export class RightPanelViewModel extends ViewModel {
this._setupNavigation(); this._setupNavigation();
} }
get roomDetailsViewModel() { return this._roomDetailsViewModel; } get activeViewModel() { return this._activeViewModel; }
get memberListViewModel() { return this._memberListViewModel; }
get activeViewModel() { return this._roomDetailsViewModel ?? this._memberListViewModel; } async _memberArguments() {
const list = await this._room.loadMemberList();
const room = this._room;
return {members: list.members, powerLevels: room.powerLevels, mediaRepository: room.mediaRepository};
}
_setupNavigation() { _setupNavigation() {
const details = this.navigation.observe("details"); this._hookSegmentToToggler("details", RoomDetailsViewModel, () => { return {room: this._room}; });
this.track(details.subscribe(() => this._toggleRoomDetailsPanel())); this._hookSegmentToToggler("members", MemberListViewModel, () => this._memberArguments());
this._toggleRoomDetailsPanel();
const members = this.navigation.observe("members");
this.track(members.subscribe(() => this._toggleMemberListPanel()));
this._toggleMemberListPanel();
} }
_toggleRoomDetailsPanel() { _hookSegmentToToggler(segment, viewmodel, argCreator) {
this._roomDetailsViewModel = this.disposeTracked(this._roomDetailsViewModel); const observable = this.navigation.observe(segment);
const enable = !!this.navigation.path.get("details")?.value; const toggler = this._setupToggler(segment, viewmodel, argCreator);
if (enable) { this.track(observable.subscribe(() => toggler()));
const room = this._room;
this._roomDetailsViewModel = this.track(new RoomDetailsViewModel(this.childOptions({room})));
}
this.emitChange("roomDetailsViewModel");
} }
async _toggleMemberListPanel() { _setupToggler(segment, viewmodel, argCreator) {
this._memberListViewModel = this.disposeTracked(this._memberListViewModel); const toggler = async (skipDispose = false) => {
const enable = !!this.navigation.path.get("members")?.value; if (!skipDispose) {
if (enable) { this._activeViewModel = this.disposeTracked(this._activeViewModel);
const list = await this._room.loadMemberList(); }
const members = list.members; const enable = !!this.navigation.path.get(segment)?.value;
const powerLevels = this._room.powerLevels; if (enable) {
const mediaRepository = this._room.mediaRepository; const args = await argCreator();
this._memberListViewModel = this.track( this._activeViewModel = this.track(new viewmodel(this.childOptions(args)));
new MemberListViewModel(this.childOptions({members, powerLevels, mediaRepository})) }
); this.emitChange("activeViewModel");
} };
this.emitChange("memberListViewModel"); toggler(true);
return toggler;
} }
} }