2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Support pinned locally in filters.

This commit is contained in:
John Preston
2020-03-17 17:04:30 +04:00
parent 483d4e5a4e
commit e27a8fe058
30 changed files with 409 additions and 155 deletions

View File

@@ -64,6 +64,7 @@ public:
Filler(
not_null<SessionController*> controller,
not_null<PeerData*> peer,
FilterId filterId,
const PeerMenuCallback &addAction,
PeerMenuSource source);
void fill();
@@ -84,6 +85,7 @@ private:
not_null<SessionController*> _controller;
not_null<PeerData*> _peer;
FilterId _filterId = 0;
const PeerMenuCallback &_addAction;
PeerMenuSource _source;
@@ -115,7 +117,7 @@ private:
};
History *FindWastedPin(not_null<Data::Session*> data, Data::Folder *folder) {
const auto &order = data->pinnedChatsOrder(folder);
const auto &order = data->pinnedChatsOrder(folder, FilterId());
for (const auto &pinned : order) {
if (const auto history = pinned.history()) {
if (history->peer->isChat()
@@ -134,24 +136,25 @@ void AddChatMembers(
AddParticipantsBoxController::Start(navigation, chat);
}
bool PinnedLimitReached(Dialogs::Key key) {
bool PinnedLimitReached(Dialogs::Key key, FilterId filterId) {
Expects(key.entry()->folderKnown());
const auto entry = key.entry();
const auto owner = &entry->owner();
const auto folder = entry->folder();
const auto pinnedCount = owner->pinnedChatsCount(folder);
const auto pinnedMax = owner->pinnedChatsLimit(folder);
const auto pinnedCount = owner->pinnedChatsCount(folder, filterId);
const auto pinnedMax = owner->pinnedChatsLimit(folder, filterId);
if (pinnedCount < pinnedMax) {
return false;
}
// Some old chat, that was converted, maybe is still pinned.
if (const auto wasted = FindWastedPin(owner, folder)) {
owner->setChatPinned(wasted, false);
owner->setChatPinned(key, true);
const auto wasted = filterId ? nullptr : FindWastedPin(owner, folder);
if (wasted) {
owner->setChatPinned(wasted, FilterId(), false);
owner->setChatPinned(key, FilterId(), true);
entry->session().api().savePinnedOrder(folder);
} else {
auto errorText = tr::lng_error_pinned_max(
const auto errorText = tr::lng_error_pinned_max(
tr::now,
lt_count,
pinnedMax);
@@ -165,12 +168,12 @@ void TogglePinnedDialog(Dialogs::Key key) {
return;
}
const auto owner = &key.entry()->owner();
const auto isPinned = !key.entry()->isPinnedDialog();
if (isPinned && PinnedLimitReached(key)) {
const auto isPinned = !key.entry()->isPinnedDialog(0);
if (isPinned && PinnedLimitReached(key, 0)) {
return;
}
owner->setChatPinned(key, isPinned);
owner->setChatPinned(key, FilterId(), isPinned);
const auto flags = isPinned
? MTPmessages_ToggleDialogPin::Flag::f_pinned
: MTPmessages_ToggleDialogPin::Flag(0);
@@ -194,13 +197,50 @@ void TogglePinnedDialog(Dialogs::Key key) {
}
}
void TogglePinnedDialog(Dialogs::Key key, FilterId filterId) {
if (!filterId) {
return TogglePinnedDialog(key);
}
const auto owner = &key.entry()->owner();
const auto isPinned = !key.entry()->isPinnedDialog(filterId);
if (isPinned && PinnedLimitReached(key, filterId)) {
return;
}
owner->setChatPinned(key, filterId, isPinned);
// #TODO pinned save data and to server
//const auto flags = isPinned
// ? MTPmessages_ToggleDialogPin::Flag::f_pinned
// : MTPmessages_ToggleDialogPin::Flag(0);
//if (const auto history = key.history()) {
// history->session().api().request(MTPmessages_ToggleDialogPin(
// MTP_flags(flags),
// MTP_inputDialogPeer(key.history()->peer->input)
// )).done([=](const MTPBool &result) {
// owner->notifyPinnedDialogsOrderUpdated();
// }).send();
//} else if (const auto folder = key.folder()) {
// folder->session().api().request(MTPmessages_ToggleDialogPin(
// MTP_flags(flags),
// MTP_inputDialogPeerFolder(MTP_int(folder->id()))
// )).send();
//}
if (isPinned) {
if (const auto main = App::main()) {
main->dialogsToUp();
}
}
}
Filler::Filler(
not_null<SessionController*> controller,
not_null<PeerData*> peer,
FilterId filterId,
const PeerMenuCallback &addAction,
PeerMenuSource source)
: _controller(controller)
, _peer(peer)
, _filterId(filterId)
, _addAction(addAction)
, _source(source) {
}
@@ -242,27 +282,29 @@ bool Filler::showTogglePin() {
}
void Filler::addTogglePin() {
auto peer = _peer;
const auto filterId = _filterId;
const auto peer = _peer;
auto isPinned = false;
if (auto history = peer->owner().historyLoaded(peer)) {
isPinned = history->isPinnedDialog();
if (const auto history = peer->owner().historyLoaded(peer)) {
isPinned = history->isPinnedDialog(filterId);
}
auto pinText = [](bool isPinned) {
const auto pinText = [](bool isPinned) {
return isPinned
? tr::lng_context_unpin_from_top(tr::now)
: tr::lng_context_pin_to_top(tr::now);
};
auto pinToggle = [=] {
TogglePinnedDialog(peer->owner().history(peer));
const auto pinToggle = [=] {
TogglePinnedDialog(peer->owner().history(peer), filterId);
};
auto pinAction = _addAction(pinText(isPinned), pinToggle);
const auto pinAction = _addAction(pinText(isPinned), pinToggle);
const auto lifetime = Ui::CreateChild<rpl::lifetime>(pinAction);
Notify::PeerUpdateViewer(
peer,
Notify::PeerUpdate::Flag::ChatPinnedChanged
) | rpl::start_with_next([peer, pinAction, pinText] {
auto isPinned = peer->owner().history(peer)->isPinnedDialog();
) | rpl::start_with_next([=] {
const auto history = peer->owner().history(peer);
const auto isPinned = history->isPinnedDialog(filterId);
pinAction->setText(pinText(isPinned));
}, *lifetime);
}
@@ -1041,9 +1083,10 @@ Fn<void()> DeleteAndLeaveHandler(not_null<PeerData*> peer) {
void FillPeerMenu(
not_null<SessionController*> controller,
not_null<PeerData*> peer,
FilterId filterId,
const PeerMenuCallback &callback,
PeerMenuSource source) {
Filler filler(controller, peer, callback, source);
Filler filler(controller, peer, filterId, callback, source);
filler.fill();
}

View File

@@ -39,6 +39,7 @@ using PeerMenuCallback = Fn<QAction*(
void FillPeerMenu(
not_null<SessionController*> controller,
not_null<PeerData*> peer,
FilterId filterId,
const PeerMenuCallback &addAction,
PeerMenuSource source);
void FillFolderMenu(