From 7dff10f6fd38c901c309db4800a40e11ecfcae6d Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 10 May 2019 12:50:47 +0300 Subject: [PATCH] Refactored code for lifetimes in touchbar. --- Telegram/SourceFiles/platform/mac/touchbar.h | 4 +- Telegram/SourceFiles/platform/mac/touchbar.mm | 45 +++++++++++-------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Telegram/SourceFiles/platform/mac/touchbar.h b/Telegram/SourceFiles/platform/mac/touchbar.h index de8625b7d6..464f649603 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar.h +++ b/Telegram/SourceFiles/platform/mac/touchbar.h @@ -35,7 +35,9 @@ static NSTouchBarItemIdentifier _Nullable previousItem = [NSString stringWithFor static NSTouchBarItemIdentifier _Nullable closePlayer = [NSString stringWithFormat:@"%@.closePlayer", BASE_ID]; static NSTouchBarItemIdentifier _Nullable currentPosition = [NSString stringWithFormat:@"%@.currentPosition", BASE_ID]; -@interface TouchBar : NSTouchBar +@interface TouchBar : NSTouchBar { + rpl::lifetime lifetime; +} @property TouchBarType touchBarType; @property TouchBarType touchBarTypeBeforeLock; diff --git a/Telegram/SourceFiles/platform/mac/touchbar.mm b/Telegram/SourceFiles/platform/mac/touchbar.mm index e9f0f3d560..4077eddca5 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar.mm +++ b/Telegram/SourceFiles/platform/mac/touchbar.mm @@ -43,6 +43,8 @@ constexpr auto kArchiveId = -1; NSImage *qt_mac_create_nsimage(const QPixmap &pm); @interface PinnedDialogButton : NSCustomTouchBarItem { + rpl::lifetime lifetime; + rpl::lifetime userpicChangedLifetime; } @property(nonatomic, assign) int number; @@ -59,8 +61,6 @@ NSImage *qt_mac_create_nsimage(const QPixmap &pm); @implementation PinnedDialogButton : NSCustomTouchBarItem -auto lifetime = rpl::lifetime(); - - (id) init:(int)num { if (num == kSavedMessagesId) { self = [super initWithIdentifier:savedMessages]; @@ -90,27 +90,36 @@ auto lifetime = rpl::lifetime(); return self; } - if (self.peer) { - Notify::PeerUpdateViewer( - self.peer, - Notify::PeerUpdate::Flag::PhotoChanged - ) | rpl::start_with_next([=] { - self.waiting = true; - [self updatePinnedDialog]; - }, lifetime); - } - base::ObservableViewer( - Auth().downloaderTaskFinished() + Auth().downloaderTaskFinished() ) | rpl::start_with_next([=] { if (self.waiting) { [self updatePinnedDialog]; } - }, lifetime); + }, self->lifetime); return self; } +// Setter of peer. +- (void) setPeer:(PeerData *)newPeer { + if (_peer == newPeer) { + return; + } + _peer = newPeer; + self->userpicChangedLifetime.destroy(); + if (!_peer) { + return; + } + Notify::PeerUpdateViewer( + _peer, + Notify::PeerUpdate::Flag::PhotoChanged + ) | rpl::start_with_next([=] { + self.waiting = true; + [self updatePinnedDialog]; + }, self->userpicChangedLifetime); +} + - (void) updatePinnedDialog { NSButton *button = self.view; button.image = [self getPinImage]; @@ -229,7 +238,7 @@ auto lifetime = rpl::lifetime(); } else { [self setTouchBar:TouchBarType::AudioPlayer]; } - }, lifetime); + }, self->lifetime); Core::App().passcodeLockChanges( ) | rpl::start_with_next([=](bool locked) { @@ -239,19 +248,19 @@ auto lifetime = rpl::lifetime(); } else { [self setTouchBar:self.touchBarTypeBeforeLock]; } - }, lifetime); + }, self->lifetime); Auth().data().pinnedDialogsOrderUpdated( ) | rpl::start_with_next([self] { [self updatePinnedButtons]; - }, lifetime); + }, self->lifetime); Auth().data().chatsListChanges( ) | rpl::filter([](Data::Folder *folder) { return folder && folder->chatsList(); }) | rpl::start_with_next([=](Data::Folder *folder) { [self toggleArchiveButton:folder->chatsList()->empty()]; - }, lifetime); + }, self->lifetime); [self updatePinnedButtons];