Migrate Media::Player::Instance to rpl.

This commit is contained in:
John Preston 2021-11-19 15:55:09 +04:00
parent 92e2b91f81
commit 74cfaff100
9 changed files with 86 additions and 72 deletions

View file

@ -634,11 +634,13 @@ HistoryWidget::HistoryWidget(
} }
}, lifetime()); }, lifetime());
subscribe(Media::Player::instance()->switchToNextNotifier(), [this](const Media::Player::Instance::Switch &pair) { using MediaSwitch = Media::Player::Instance::Switch;
if (pair.from.type() == AudioMsgId::Type::Voice) { Media::Player::instance()->switchToNextEvents(
scrollToCurrentVoiceMessage(pair.from.contextId(), pair.to); ) | rpl::filter([=](const MediaSwitch &pair) {
} return (pair.from.type() == AudioMsgId::Type::Voice);
}); }) | rpl::start_with_next([=](const MediaSwitch &pair) {
scrollToCurrentVoiceMessage(pair.from.contextId(), pair.to);
}, lifetime());
using PeerUpdateFlag = Data::PeerUpdate::Flag; using PeerUpdateFlag = Data::PeerUpdate::Flag;
session().changes().peerUpdates( session().changes().peerUpdates(

View file

@ -333,20 +333,8 @@ MainWidget::MainWidget(
QCoreApplication::instance()->installEventFilter(this); QCoreApplication::instance()->installEventFilter(this);
subscribe(Media::Player::instance()->playerWidgetOver(), [this](bool over) { Media::Player::instance()->tracksFinished(
if (over) { ) | rpl::start_with_next([=](AudioMsgId::Type type) {
if (_playerPlaylist->isHidden()) {
auto position = mapFromGlobal(QCursor::pos()).x();
auto bestPosition = _playerPlaylist->bestPositionFor(position);
if (rtl()) bestPosition = position + 2 * (position - bestPosition) - _playerPlaylist->width();
updateMediaPlaylistPosition(bestPosition);
}
_playerPlaylist->showFromOther();
} else {
_playerPlaylist->hideFromOther();
}
});
subscribe(Media::Player::instance()->tracksFinishedNotifier(), [this](AudioMsgId::Type type) {
if (type == AudioMsgId::Type::Voice) { if (type == AudioMsgId::Type::Voice) {
const auto songState = Media::Player::instance()->getState(AudioMsgId::Type::Song); const auto songState = Media::Player::instance()->getState(AudioMsgId::Type::Song);
if (!songState.id || IsStoppedOrStopping(songState.state)) { if (!songState.id || IsStoppedOrStopping(songState.state)) {
@ -358,7 +346,7 @@ MainWidget::MainWidget(
closeBothPlayers(); closeBothPlayers();
} }
} }
}); }, lifetime());
_controller->adaptive().changes( _controller->adaptive().changes(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
@ -846,6 +834,21 @@ void MainWidget::createPlayer() {
not_null<const HistoryItem*> item) { not_null<const HistoryItem*> item) {
_controller->showPeerHistoryAtItem(item); _controller->showPeerHistoryAtItem(item);
}); });
_player->entity()->togglePlaylistRequests(
) | rpl::start_with_next([=](bool shown) {
if (!shown) {
_playerPlaylist->hideFromOther();
return;
} else if (_playerPlaylist->isHidden()) {
auto position = mapFromGlobal(QCursor::pos()).x();
auto bestPosition = _playerPlaylist->bestPositionFor(position);
if (rtl()) bestPosition = position + 2 * (position - bestPosition) - _playerPlaylist->width();
updateMediaPlaylistPosition(bestPosition);
}
_playerPlaylist->showFromOther();
}, _player->lifetime());
_playerVolume.create(this); _playerVolume.create(this);
Media::Player::PrepareVolumeDropdown( Media::Player::PrepareVolumeDropdown(
_playerVolume.data(), _playerVolume.data(),

View file

@ -299,12 +299,12 @@ FloatController::Item::Item(
FloatController::FloatController(not_null<FloatDelegate*> delegate) FloatController::FloatController(not_null<FloatDelegate*> delegate)
: _delegate(delegate) : _delegate(delegate)
, _parent(_delegate->floatPlayerWidget()) { , _parent(_delegate->floatPlayerWidget()) {
subscribe(Media::Player::instance()->trackChangedNotifier(), [=]( Media::Player::instance()->trackChanged(
AudioMsgId::Type type) { ) | rpl::filter([=](AudioMsgId::Type type) {
if (type == AudioMsgId::Type::Voice) { return (type == AudioMsgId::Type::Voice);
checkCurrent(); }) | rpl::start_with_next([=] {
} checkCurrent();
}); }, _lifetime);
startDelegateHandling(); startDelegateHandling();
} }

View file

@ -262,6 +262,8 @@ private:
rpl::event_stream<FullMsgId> _closeEvents; rpl::event_stream<FullMsgId> _closeEvents;
rpl::lifetime _delegateLifetime; rpl::lifetime _delegateLifetime;
rpl::lifetime _lifetime;
}; };
} // namespace Player } // namespace Player

View file

@ -182,7 +182,7 @@ void Instance::setCurrent(const AudioMsgId &audioId) {
data->migrated = nullptr; data->migrated = nullptr;
data->session = nullptr; data->session = nullptr;
} }
_trackChangedNotifier.notify(data->type, true); _trackChanged.fire_copy(data->type);
refreshPlaylist(data); refreshPlaylist(data);
} }
} }
@ -369,7 +369,7 @@ bool Instance::moveInPlaylist(
if (const auto media = item->media()) { if (const auto media = item->media()) {
if (const auto document = media->document()) { if (const auto document = media->document()) {
if (autonext) { if (autonext) {
_switchToNextNotifier.notify({ _switchToNextStream.fire({
data->current, data->current,
item->fullId() item->fullId()
}); });
@ -558,8 +558,8 @@ void Instance::stop(AudioMsgId::Type type) {
void Instance::stopAndClear(not_null<Data*> data) { void Instance::stopAndClear(not_null<Data*> data) {
stop(data->type); stop(data->type);
_tracksFinishedNotifier.notify(data->type);
*data = Data(data->type, data->overview); *data = Data(data->type, data->overview);
_tracksFinished.fire_copy(data->type);
} }
void Instance::playPause(AudioMsgId::Type type) { void Instance::playPause(AudioMsgId::Type type) {
@ -744,15 +744,19 @@ void Instance::emitUpdate(AudioMsgId::Type type, CheckCallback check) {
streamed->progress.updateState(state); streamed->progress.updateState(state);
} }
} }
auto finished = false;
_updatedNotifier.fire_copy({state}); _updatedNotifier.fire_copy({state});
if (data->isPlaying && state.state == State::StoppedAtEnd) { if (data->isPlaying && state.state == State::StoppedAtEnd) {
if (data->repeat.current() == RepeatMode::One) { if (data->repeat.current() == RepeatMode::One) {
play(data->current); play(data->current);
} else if (!moveInPlaylist(data, 1, true)) { } else if (!moveInPlaylist(data, 1, true)) {
_tracksFinishedNotifier.notify(type); finished = true;
} }
} }
data->isPlaying = !IsStopped(state.state); data->isPlaying = !IsStopped(state.state);
if (finished) {
_tracksFinished.fire_copy(type);
}
} }
} }

View file

@ -188,31 +188,30 @@ public:
FullMsgId to; FullMsgId to;
}; };
base::Observable<Switch> &switchToNextNotifier() { [[nodiscard]] rpl::producer<Switch> switchToNextEvents() const {
return _switchToNextNotifier; return _switchToNextStream.events();
} }
base::Observable<bool> &playerWidgetOver() { [[nodiscard]] rpl::producer<AudioMsgId::Type> tracksFinished() const {
return _playerWidgetOver; return _tracksFinished.events();
} }
base::Observable<AudioMsgId::Type> &tracksFinishedNotifier() { [[nodiscard]] rpl::producer<AudioMsgId::Type> trackChanged() const {
return _tracksFinishedNotifier; return _trackChanged.events();
}
base::Observable<AudioMsgId::Type> &trackChangedNotifier() {
return _trackChangedNotifier;
} }
rpl::producer<> playlistChanges(AudioMsgId::Type type) const; [[nodiscard]] rpl::producer<> playlistChanges(
AudioMsgId::Type type) const;
rpl::producer<TrackState> updatedNotifier() const { [[nodiscard]] rpl::producer<TrackState> updatedNotifier() const {
return _updatedNotifier.events(); return _updatedNotifier.events();
} }
rpl::producer<> stops(AudioMsgId::Type type) const; [[nodiscard]] rpl::producer<> stops(AudioMsgId::Type type) const;
rpl::producer<> startsPlay(AudioMsgId::Type type) const; [[nodiscard]] rpl::producer<> startsPlay(AudioMsgId::Type type) const;
rpl::producer<Seeking> seekingChanges(AudioMsgId::Type type) const; [[nodiscard]] rpl::producer<Seeking> seekingChanges(
AudioMsgId::Type type) const;
bool pauseGifByRoundVideo() const; [[nodiscard]] bool pauseGifByRoundVideo() const;
void documentLoadProgress(DocumentData *document); void documentLoadProgress(DocumentData *document);
@ -323,11 +322,9 @@ private:
Data _voiceData; Data _voiceData;
bool _roundPlaying = false; bool _roundPlaying = false;
base::Observable<Switch> _switchToNextNotifier; rpl::event_stream<Switch> _switchToNextStream;
base::Observable<bool> _playerWidgetOver; rpl::event_stream<AudioMsgId::Type> _tracksFinished;
base::Observable<AudioMsgId::Type> _tracksFinishedNotifier; rpl::event_stream<AudioMsgId::Type> _trackChanged;
base::Observable<AudioMsgId::Type> _trackChangedNotifier;
rpl::event_stream<AudioMsgId::Type> _playerStopped; rpl::event_stream<AudioMsgId::Type> _playerStopped;
rpl::event_stream<AudioMsgId::Type> _playerStartedPlay; rpl::event_stream<AudioMsgId::Type> _playerStartedPlay;
rpl::event_stream<TrackState> _updatedNotifier; rpl::event_stream<TrackState> _updatedNotifier;

View file

@ -324,23 +324,26 @@ Widget::Widget(QWidget *parent, not_null<Main::Session*> session)
instance()->updateVoicePlaybackSpeed(); instance()->updateVoicePlaybackSpeed();
}, lifetime()); }, lifetime());
subscribe(instance()->trackChangedNotifier(), [this](AudioMsgId::Type type) { instance()->trackChanged(
if (type == _type) { ) | rpl::filter([=](AudioMsgId::Type type) {
handleSongChange(); return (type == _type);
updateControlsVisibility(); }) | rpl::start_with_next([=](AudioMsgId::Type type) {
updateLabelsGeometry(); handleSongChange();
updateControlsVisibility();
updateLabelsGeometry();
}, lifetime());
instance()->tracksFinished(
) | rpl::filter([=](AudioMsgId::Type type) {
return (type == AudioMsgId::Type::Voice);
}) | rpl::start_with_next([=](AudioMsgId::Type type) {
_voiceIsActive = false;
const auto currentSong = instance()->current(AudioMsgId::Type::Song);
const auto songState = instance()->getState(AudioMsgId::Type::Song);
if (currentSong == songState.id && !IsStoppedOrStopping(songState.state)) {
setType(AudioMsgId::Type::Song);
} }
}); }, lifetime());
subscribe(instance()->tracksFinishedNotifier(), [this](AudioMsgId::Type type) {
if (type == AudioMsgId::Type::Voice) {
_voiceIsActive = false;
const auto currentSong = instance()->current(AudioMsgId::Type::Song);
const auto songState = instance()->getState(AudioMsgId::Type::Song);
if (currentSong == songState.id && !IsStoppedOrStopping(songState.state)) {
setType(AudioMsgId::Type::Song);
}
}
});
instance()->updatedNotifier( instance()->updatedNotifier(
) | rpl::start_with_next([=](const TrackState &state) { ) | rpl::start_with_next([=](const TrackState &state) {
@ -521,8 +524,7 @@ void Widget::updateOverLabelsState(bool over) {
_labelsOver = over; _labelsOver = over;
auto pressShowsItem = _labelsOver && (_type == AudioMsgId::Type::Voice); auto pressShowsItem = _labelsOver && (_type == AudioMsgId::Type::Voice);
setCursor(pressShowsItem ? style::cur_pointer : style::cur_default); setCursor(pressShowsItem ? style::cur_pointer : style::cur_default);
auto showPlaylist = over && (_type == AudioMsgId::Type::Song); _togglePlaylistRequests.fire(over && (_type == AudioMsgId::Type::Song));
instance()->playerWidgetOver().notify(showPlaylist, true);
} }
void Widget::updatePlayPrevNextPositions() { void Widget::updatePlayPrevNextPositions() {

View file

@ -50,12 +50,16 @@ public:
void showShadow(); void showShadow();
void hideShadow(); void hideShadow();
QPoint getPositionForVolumeWidget() const; [[nodiscard]] QPoint getPositionForVolumeWidget() const;
void volumeWidgetCreated(Dropdown *widget); void volumeWidgetCreated(Dropdown *widget);
QPoint getPositionForRepeatWidget() const; [[nodiscard]] QPoint getPositionForRepeatWidget() const;
void repeatWidgetCreated(Dropdown *widget); void repeatWidgetCreated(Dropdown *widget);
[[nodiscard]] rpl::producer<bool> togglePlaylistRequests() const {
return _togglePlaylistRequests.events();
}
~Widget(); ~Widget();
protected: protected:
@ -114,6 +118,7 @@ private:
bool _labelsOver = false; bool _labelsOver = false;
bool _labelsDown = false; bool _labelsDown = false;
rpl::event_stream<bool> _togglePlaylistRequests;
class PlayButton; class PlayButton;
class SpeedButton; class SpeedButton;

View file

@ -96,8 +96,7 @@ SystemMediaControlsManager::SystemMediaControlsManager(
_lifetimeDownload.destroy(); _lifetimeDownload.destroy();
}, _lifetime); }, _lifetime);
auto trackChanged = base::ObservableViewer( auto trackChanged = mediaPlayer->trackChanged(
mediaPlayer->trackChangedNotifier()
) | rpl::filter([=](AudioMsgId::Type audioType) { ) | rpl::filter([=](AudioMsgId::Type audioType) {
return audioType == type; return audioType == type;
}); });