summaryrefslogtreecommitdiffstats
path: root/macosx/HBQueueItemWorkingView.m
diff options
context:
space:
mode:
authorDamiano Galassi <[email protected]>2019-07-25 08:10:15 +0200
committerDamiano Galassi <[email protected]>2019-07-25 08:10:15 +0200
commit5396c8b3bd401d4f574dc57de68cd4b3cc560d9f (patch)
treee62fff38a4dc4f2cfe2c7fccd265daa1778b3141 /macosx/HBQueueItemWorkingView.m
parentc16151f9edf16376de254a43130a5f0371ee8a55 (diff)
MacGui: improve queue init.
Diffstat (limited to 'macosx/HBQueueItemWorkingView.m')
-rw-r--r--macosx/HBQueueItemWorkingView.m70
1 files changed, 47 insertions, 23 deletions
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];
}
}