From 62a6812259257f7bfdb1fbbc51920187db767f3d Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 2 May 2019 16:26:47 +0300 Subject: [PATCH] Added ability to update pinned dialogs. --- Telegram/SourceFiles/data/data_session.cpp | 5 ++-- .../platform/mac/main_window_mac.mm | 15 ++++++---- Telegram/SourceFiles/platform/mac/touchbar.mm | 28 +++++++++++++++---- .../SourceFiles/window/window_peer_menu.cpp | 4 ++- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 16ff1ca92d..e5a8850289 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -1414,6 +1414,7 @@ void Session::setChatPinned(const Dialogs::Key &key, bool pinned) { const auto list = chatsList(key.entry()->folder())->pinned(); list->setPinned(key, pinned); + notifyPinnedDialogsOrderUpdated(); } void Session::setPinnedFromDialog(const Dialogs::Key &key, bool pinned) { @@ -1430,7 +1431,6 @@ void Session::setPinnedFromDialog(const Dialogs::Key &key, bool pinned) { void Session::applyPinnedChats( Data::Folder *folder, const QVector &list) { - notifyPinnedDialogsOrderUpdated(); for (const auto &peer : list) { peer.match([&](const MTPDdialogPeer &data) { const auto history = this->history(peerFromMTP(data.vpeer)); @@ -1446,6 +1446,7 @@ void Session::applyPinnedChats( }); } chatsList(folder)->pinned()->applyList(this, list); + notifyPinnedDialogsOrderUpdated(); } void Session::applyDialogs( @@ -1490,7 +1491,6 @@ void Session::applyDialog( void Session::applyDialog( Data::Folder *requestFolder, const MTPDdialogFolder &data) { - notifyPinnedDialogsOrderUpdated(); if (requestFolder) { LOG(("API Error: requestFolder != nullptr for dialogFolder.")); } @@ -1525,6 +1525,7 @@ void Session::reorderTwoPinnedChats( Expects(key1.entry()->folder() == key2.entry()->folder()); chatsList(key1.entry()->folder())->pinned()->reorder(key1, key2); + notifyPinnedDialogsOrderUpdated(); } bool Session::checkEntitiesAndViewsUpdate(const MTPDmessage &data) { diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index e10af3e813..78c4047e16 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -399,12 +399,15 @@ MainWindow::MainWindow() subscribe(Core::App().authSessionChanged(), [this] { if (AuthSession::Exists()) { - Auth().data().pinnedDialogsOrderUpdated( - ) | rpl::start_with_next([this] { - if (auto view = reinterpret_cast(winId())) { - // Create TouchBar. - [NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES; - _private->_touchBar = [[TouchBar alloc] init:view]; + Auth().data().chatsListChanges( + ) | rpl::start_with_next([this](Data::Folder* folder) { + // We need only common pinned dialogs. + if (!folder && !_private->_touchBar) { + if (auto view = reinterpret_cast(winId())) { + // Create TouchBar. + [NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES; + _private->_touchBar = [[TouchBar alloc] init:view]; + } } }, lifetime()); } diff --git a/Telegram/SourceFiles/platform/mac/touchbar.mm b/Telegram/SourceFiles/platform/mac/touchbar.mm index 065b5821e4..218dc85633 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar.mm +++ b/Telegram/SourceFiles/platform/mac/touchbar.mm @@ -66,6 +66,7 @@ NSImage *qt_mac_create_nsimage(const QPixmap &pm); - (NSImage *) getPinImage; - (void)buttonActionPin:(NSButton *)sender; - (void)updatePeerData; +- (void)updatePinnedDialog; @end // @interface PinnedDialogButton @@ -100,12 +101,19 @@ auto lifetime = rpl::lifetime(); button.image = [self getPinImage]; }; - Notify::PeerUpdateViewer( - self.peer, - Notify::PeerUpdate::Flag::PhotoChanged + if (self.peer) { + Notify::PeerUpdateViewer( + self.peer, + Notify::PeerUpdate::Flag::PhotoChanged + ) | rpl::start_with_next([=] { + self.waiting = true; + updateImage(); + }, lifetime); + } + + Auth().data().pinnedDialogsOrderUpdated( ) | rpl::start_with_next([=] { - self.waiting = true; - updateImage(); + [self updatePinnedDialog]; }, lifetime); base::ObservableViewer( @@ -119,6 +127,13 @@ auto lifetime = rpl::lifetime(); return self; } +- (void) updatePinnedDialog { + [self updatePeerData]; + NSButton *button = self.view; + button.image = [self getPinImage]; + [button setHidden:(self.number > Auth().data().pinnedChatsOrder(nullptr).size())]; +} + - (id) initSavedMessages { self = [super initWithIdentifier:savedMessages]; if (!self) { @@ -193,7 +208,8 @@ auto lifetime = rpl::lifetime(); return static_cast(qt_mac_create_nsimage(*pix)); } if (!self.peer) { - return nil; + // Random picture. + return [NSImage imageNamed:NSImageNameTouchBarAddTemplate]; } self.waiting = !self.peer->userpicLoaded(); auto pixmap = self.peer->genUserpic(kIdealIconSize); diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 04f084171d..aa2f016d34 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -157,7 +157,9 @@ void TogglePinnedDialog(Dialogs::Key key) { history->session().api().request(MTPmessages_ToggleDialogPin( MTP_flags(flags), MTP_inputDialogPeer(key.history()->peer->input) - )).send(); + )).done([=](const MTPBool &result) { + Auth().data().notifyPinnedDialogsOrderUpdated(); + }).send(); } else if (const auto folder = key.folder()) { folder->session().api().request(MTPmessages_ToggleDialogPin( MTP_flags(flags),