From 5396c8b3bd401d4f574dc57de68cd4b3cc560d9f Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Thu, 25 Jul 2019 08:10:15 +0200 Subject: MacGui: improve queue init. --- macosx/HBQueueItemWorkingView.m | 70 +++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 23 deletions(-) (limited to 'macosx/HBQueueItemWorkingView.m') diff --git a/macosx/HBQueueItemWorkingView.m b/macosx/HBQueueItemWorkingView.m index 4a217a78a..91e26a028 100644 --- a/macosx/HBQueueItemWorkingView.m +++ b/macosx/HBQueueItemWorkingView.m @@ -23,35 +23,59 @@ @implementation HBQueueItemWorkingView +- (void)setUpObservers +{ + NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter; + + self.progressToken = [center addObserverForName:HBQueueProgressNotification + object:nil + queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) + { + NSString *progressInfo = note.userInfo[HBQueueProgressNotificationInfoKey]; + double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue]; + + self.progressField.attributedStringValue = progressInfo.HB_smallMonospacedString; + self.progressBar.doubleValue = progress; + }]; + + self.completedToken = [center addObserverForName:HBQueueDidCompleteItemNotification + object:nil + queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) + { + HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey]; + if (completedItem == self.item) { + [self removeObservers]; + } + }]; +} + +- (void)removeObservers +{ + if (self.progressToken) + { + [NSNotificationCenter.defaultCenter removeObserver:self.progressToken]; + self.progressToken = nil; + } + if (self.completedToken) + { + [NSNotificationCenter.defaultCenter removeObserver:self.completedToken]; + self.completedToken = nil; + } +} + +- (void)dealloc +{ + [self removeObservers]; +} + - (void)setItem:(HBQueueItem *)item { [super setItem:item]; if (item.state == HBQueueItemStateWorking) { - NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter; - - self.progressToken = [center addObserverForName:HBQueueProgressNotification - object:nil - queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) - { - NSString *progressInfo = note.userInfo[HBQueueProgressNotificationInfoKey]; - double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue]; - - self.progressField.attributedStringValue = progressInfo.HB_smallMonospacedString; - self.progressBar.doubleValue = progress; - }]; - - self.completedToken = [center addObserverForName:HBQueueDidCompleteItemNotification - object:nil - queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) - { - HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey]; - if (completedItem == self.item) { - [center removeObserver:self.progressToken]; - [center removeObserver:self.completedToken]; - } - }]; + [self removeObservers]; + [self setUpObservers]; } } -- cgit v1.2.3