Skip to content

Commit ecca785

Browse files
committed
Update all uses of KVO to the block-based method
1 parent cf0a77b commit ecca785

9 files changed

+123
-244
lines changed

Classes/Controllers/PBGitHistoryController.m

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,48 @@ - (void)loadView {
8888

8989
self.selectedCommitDetailsIndex = [[NSUserDefaults standardUserDefaults] integerForKey:kHistorySelectedDetailIndexKey];
9090

91-
[commitController addObserver:self forKeyPath:@"selection" options:0 context:@"commitChange"];
92-
[commitController addObserver:self forKeyPath:@"arrangedObjects.@count" options:NSKeyValueObservingOptionInitial context:@"updateCommitCount"];
93-
[treeController addObserver:self forKeyPath:@"selection" options:0 context:@"treeChange"];
91+
[commitController addObserver:self keyPath:@"selection" options:0 block:^(MAKVONotification *notification) {
92+
PBGitHistoryController *observer = notification.observer;
93+
[observer updateKeys];
94+
}];
95+
96+
[commitController addObserver:self keyPath:@"arrangedObjects.@count" options:NSKeyValueObservingOptionInitial block:^(MAKVONotification *notification) {
97+
PBGitHistoryController *observer = notification.observer;
98+
[observer reselectCommitAfterUpdate];
99+
}];
100+
101+
[treeController addObserver:self keyPath:@"selection" options:0 block:^(MAKVONotification *notification) {
102+
PBGitHistoryController *observer = notification.observer;
103+
[observer updateQuicklookForce: NO];
104+
[observer saveFileBrowserSelection];
105+
}];
106+
107+
[repository.revisionList addObserver:self keyPath:@"isUpdating" options:0 block:^(MAKVONotification *notification) {
108+
PBGitHistoryController *observer = notification.observer;
109+
[observer reselectCommitAfterUpdate];
110+
}];
111+
112+
[repository addObserver:self keyPath:@"currentBranch" options:0 block:^(MAKVONotification *notification) {
113+
PBGitHistoryController *observer = notification.observer;
114+
// Reset the sorting
115+
if ([[observer.commitController sortDescriptors] count]) {
116+
[observer.commitController setSortDescriptors:[NSArray array]];
117+
[observer.commitController rearrangeObjects];
118+
}
119+
120+
[observer updateBranchFilterMatrix];
121+
}];
94122

95-
[repository.revisionList addObserver:self forKeyPath:@"isUpdating" options:0 context:@"revisionListUpdating"];
96-
[repository addObserver:self forKeyPath:@"currentBranch" options:0 context:@"branchChange"];
97-
[repository addObserver:self forKeyPath:@"refs" options:0 context:@"updateRefs"];
98-
[repository addObserver:self forKeyPath:@"currentBranchFilter" options:0 context:@"branchFilterChange"];
123+
[repository addObserver:self keyPath:@"refs" options:0 block:^(MAKVONotification *notification) {
124+
PBGitHistoryController *observer = notification.observer;
125+
[observer.commitController rearrangeObjects];
126+
}];
127+
128+
[repository addObserver:self keyPath:@"currentBranchFilter" options:0 block:^(MAKVONotification *notification) {
129+
PBGitHistoryController *observer = notification.observer;
130+
[PBGitDefaults setBranchFilter:observer.repository.currentBranchFilter];
131+
[observer updateBranchFilterMatrix];
132+
}];
99133

100134
forceSelectionUpdate = YES;
101135
NSSize cellSpacing = [commitList intercellSpacing];
@@ -131,9 +165,10 @@ - (void)loadView {
131165
// listen for updates
132166
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_repositoryUpdatedNotification:) name:PBGitRepositoryEventNotification object:repository];
133167

134-
__unsafe_unretained PBGitHistoryController *weakSelf = self;
168+
__weak typeof(self) weakSelf = self;
135169
commitList.findPanelActionBlock = ^(id sender) {
136-
[weakSelf.view.window makeFirstResponder:weakSelf->searchField];
170+
__strong typeof(weakSelf) strongSelf = weakSelf;
171+
[weakSelf.view.window makeFirstResponder:strongSelf->searchField];
137172
};
138173

139174
[super awakeFromNib];
@@ -147,6 +182,15 @@ - (void) _repositoryUpdatedNotification:(NSNotification *)notification {
147182
}
148183
}
149184

185+
- (void)reselectCommitAfterUpdate {
186+
[self updateStatus];
187+
188+
if ([self.repository.currentBranch isSimpleRef])
189+
[self selectCommit:[self.repository OIDForRef:self.repository.currentBranch.ref]];
190+
else
191+
[self selectCommit:self.firstCommit.OID];
192+
}
193+
150194
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
151195
NSTableRowView *view = [tableView rowViewAtRow:row makeIfNecessary:NO];
152196

@@ -309,56 +353,6 @@ - (void) saveFileBrowserSelection
309353
}
310354
}
311355

312-
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
313-
{
314-
NSString* strContext = (__bridge NSString*)context;
315-
if ([strContext isEqualToString: @"commitChange"]) {
316-
[self updateKeys];
317-
[self restoreFileBrowserSelection];
318-
return;
319-
}
320-
321-
if ([strContext isEqualToString: @"treeChange"]) {
322-
[self updateQuicklookForce: NO];
323-
[self saveFileBrowserSelection];
324-
return;
325-
}
326-
327-
if([strContext isEqualToString:@"branchChange"]) {
328-
// Reset the sorting
329-
if ([[commitController sortDescriptors] count]) {
330-
[commitController setSortDescriptors:[NSArray array]];
331-
[commitController rearrangeObjects];
332-
}
333-
334-
[self updateBranchFilterMatrix];
335-
return;
336-
}
337-
338-
if([strContext isEqualToString:@"updateRefs"]) {
339-
[commitController rearrangeObjects];
340-
return;
341-
}
342-
343-
if ([strContext isEqualToString:@"branchFilterChange"]) {
344-
[PBGitDefaults setBranchFilter:repository.currentBranchFilter];
345-
[self updateBranchFilterMatrix];
346-
return;
347-
}
348-
349-
if([strContext isEqualToString:@"updateCommitCount"] || [(__bridge NSString *)context isEqualToString:@"revisionListUpdating"]) {
350-
[self updateStatus];
351-
352-
if ([repository.currentBranch isSimpleRef])
353-
[self selectCommit:[repository OIDForRef:repository.currentBranch.ref]];
354-
else
355-
[self selectCommit:self.firstCommit.OID];
356-
return;
357-
}
358-
359-
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
360-
}
361-
362356
- (IBAction) openSelectedFile:(id)sender
363357
{
364358
NSArray* selectedFiles = [treeController selectedObjects];
@@ -563,17 +557,7 @@ - (BOOL) hasNonlinearPath
563557

564558
- (void)closeView
565559
{
566-
if (commitController) {
567-
[[NSNotificationCenter defaultCenter] removeObserver:self];
568-
[commitController removeObserver:self forKeyPath:@"selection"];
569-
[commitController removeObserver:self forKeyPath:@"arrangedObjects.@count"];
570-
[treeController removeObserver:self forKeyPath:@"selection"];
571-
572-
[repository.revisionList removeObserver:self forKeyPath:@"isUpdating"];
573-
[repository removeObserver:self forKeyPath:@"currentBranch"];
574-
[repository removeObserver:self forKeyPath:@"refs"];
575-
[repository removeObserver:self forKeyPath:@"currentBranchFilter"];
576-
}
560+
[[NSNotificationCenter defaultCenter] removeObserver:self];
577561

578562
[webHistoryController closeView];
579563
[fileView closeView];

Classes/Controllers/PBGitSidebarController.m

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,41 @@ - (void)awakeFromNib
6868
window.contentView = self.view;
6969
[self populateList];
7070

71-
[repository addObserver:self forKeyPath:@"currentBranch" options:0 context:@"currentBranchChange"];
72-
[repository addObserver:self forKeyPath:@"branches" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:@"branchesModified"];
73-
[repository addObserver:self forKeyPath:@"stashes" options:0 context:@"stashesModified"];
71+
[repository addObserver:self keyPath:@"currentBranch" options:0 block:^(MAKVONotification *notification) {
72+
PBGitSidebarController *observer = notification.observer;
73+
NSInteger row = observer.sourceView.selectedRow;
74+
[observer.sourceView reloadData];
75+
[observer.sourceView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
76+
[observer selectCurrentBranch];
77+
}];
78+
79+
[repository addObserver:self keyPath:@"branches" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) block:^(MAKVONotification *notification) {
80+
PBGitSidebarController *observer = notification.observer;
81+
if (notification.kind == NSKeyValueChangeInsertion) {
82+
NSArray *newRevSpecs = notification.newValue;
83+
for (PBGitRevSpecifier *rev in newRevSpecs) {
84+
PBSourceViewItem *item = [observer addRevSpec:rev];
85+
[observer.sourceView PBExpandItem:item expandParents:YES];
86+
}
87+
}
88+
else if (notification.kind == NSKeyValueChangeRemoval) {
89+
NSArray *removedRevSpecs = notification.oldValue;
90+
for (PBGitRevSpecifier *rev in removedRevSpecs)
91+
[observer removeRevSpec:rev];
92+
}
93+
}];
94+
95+
[repository addObserver:self keyPath:@"stashes" options:0 block:^(MAKVONotification *notification) {
96+
PBGitSidebarController *observer = notification.observer;
97+
for (PBSourceViewGitStashItem *stashItem in observer->stashes.sortedChildren)
98+
[observer->stashes removeChild:stashItem];
99+
100+
for (PBGitStash *stash in observer.repository.stashes)
101+
[observer->stashes addChild: [PBSourceViewGitStashItem itemWithStash:stash]];
102+
103+
[observer.sourceView expandItem:observer->stashes];
104+
[observer.sourceView reloadItem:observer->stashes reloadChildren:YES];
105+
}];
74106

75107
[sourceView setTarget:self];
76108
[sourceView setDoubleAction:@selector(doubleClicked:)];
@@ -93,60 +125,6 @@ - (void)dealloc
93125
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSOutlineViewItemWillCollapseNotification object:sourceView];
94126
}
95127

96-
- (void)closeView
97-
{
98-
[repository removeObserver:self forKeyPath:@"currentBranch"];
99-
[repository removeObserver:self forKeyPath:@"branches"];
100-
[repository removeObserver:self forKeyPath:@"stashes"];
101-
102-
[super closeView];
103-
}
104-
105-
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
106-
{
107-
if ([@"currentBranchChange" isEqualToString:(__bridge NSString*)context]) {
108-
NSInteger row = sourceView.selectedRow;
109-
[sourceView reloadData];
110-
[sourceView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
111-
[self selectCurrentBranch];
112-
return;
113-
}
114-
115-
if ([@"branchesModified" isEqualToString:(__bridge NSString*)context]) {
116-
NSInteger changeKind = [(NSNumber *)[change objectForKey:NSKeyValueChangeKindKey] intValue];
117-
118-
if (changeKind == NSKeyValueChangeInsertion) {
119-
NSArray *newRevSpecs = [change objectForKey:NSKeyValueChangeNewKey];
120-
for (PBGitRevSpecifier *rev in newRevSpecs) {
121-
PBSourceViewItem *item = [self addRevSpec:rev];
122-
[sourceView PBExpandItem:item expandParents:YES];
123-
}
124-
}
125-
else if (changeKind == NSKeyValueChangeRemoval) {
126-
NSArray *removedRevSpecs = [change objectForKey:NSKeyValueChangeOldKey];
127-
for (PBGitRevSpecifier *rev in removedRevSpecs)
128-
[self removeRevSpec:rev];
129-
}
130-
return;
131-
}
132-
133-
if ([@"stashesModified" isEqualToString:(__bridge NSString*)context]) {
134-
135-
for (PBSourceViewGitStashItem *stashItem in stashes.sortedChildren)
136-
[stashes removeChild:stashItem];
137-
138-
for (PBGitStash *stash in repository.stashes)
139-
[stashes addChild: [PBSourceViewGitStashItem itemWithStash:stash]];
140-
141-
[sourceView expandItem:stashes];
142-
[sourceView reloadItem:stashes reloadChildren:YES];
143-
144-
return;
145-
}
146-
147-
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
148-
}
149-
150128
- (PBSourceViewItem *) selectedItem
151129
{
152130
NSInteger index = [sourceView selectedRow];

Classes/Controllers/PBGitWindowController.m

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ - (void)windowWillClose:(NSNotification *)notification
8282

8383
[self.historyViewController closeView];
8484
[self.commitViewController closeView];
85-
86-
if (contentController)
87-
[contentController removeObserver:self forKeyPath:@"status"];
8885
}
8986

9087
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
@@ -158,7 +155,7 @@ - (void) changeContentController:(PBViewController *)controller
158155
return;
159156

160157
if (contentController)
161-
[contentController removeObserver:self forKeyPath:@"status"];
158+
[contentController removeObserver:self keyPath:@"status"];
162159

163160
[self removeAllContentSubViews];
164161

@@ -170,7 +167,9 @@ - (void) changeContentController:(PBViewController *)controller
170167
// [self setNextResponder: contentController];
171168
[[self window] makeFirstResponder:[contentController firstResponder]];
172169
[contentController updateView];
173-
[contentController addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial context:@"statusChange"];
170+
[contentController addObserver:self keyPath:@"status" options:NSKeyValueObservingOptionInitial block:^(MAKVONotification *notification) {
171+
[self updateStatus];
172+
}];
174173
}
175174

176175
- (void) showCommitView:(id)sender
@@ -239,16 +238,6 @@ - (void) updateStatus
239238
}
240239
}
241240

242-
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
243-
{
244-
if ([(__bridge NSString *)context isEqualToString:@"statusChange"]) {
245-
[self updateStatus];
246-
return;
247-
}
248-
249-
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
250-
}
251-
252241
- (void)setHistorySearch:(NSString *)searchString mode:(PBHistorySearchMode)mode
253242
{
254243
[_historyViewController setHistorySearch:searchString mode:mode];

Classes/Controllers/PBHistorySearchController.m

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ - (void)showSearchRewindPanelReverse:(BOOL)isReversed;
4141
#define kGitXRegexSearchLabel NSLocalizedString(@"Commit (pickaxe regex)", @"Option in Search menu to use the pickaxe search with regular expressions")
4242
#define kGitXPathSearchLabel NSLocalizedString(@"File path", @"Option in Search menu to search for file paths in the commit")
4343

44-
#define kGitXSearchArrangedObjectsContext @"GitXSearchArrangedObjectsContext"
45-
46-
4744
@implementation PBHistorySearchController
4845

4946
@synthesize historyController;
@@ -129,26 +126,12 @@ - (void)awakeFromNib
129126

130127
[self updateUI];
131128

132-
[commitController addObserver:self forKeyPath:@"arrangedObjects" options:0 context:kGitXSearchArrangedObjectsContext];
133-
}
134-
135-
- (void)dealloc {
136-
[commitController removeObserver:self forKeyPath:@"arrangedObjects" context:kGitXSearchArrangedObjectsContext];
137-
}
138-
139-
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
140-
{
141-
if ([(__bridge NSString *)context isEqualToString:kGitXSearchArrangedObjectsContext]) {
129+
[commitController addObserver:self keyPath:@"arrangedObjects" options:0 block:^(MAKVONotification *notification) {
142130
// the objects in the commitlist changed so the result indexes are no longer valid
143131
[self clearSearch];
144-
return;
145-
}
146-
147-
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
132+
}];
148133
}
149134

150-
151-
152135
#pragma mark -
153136
#pragma mark Private methods
154137

Classes/Controllers/PBWebDiffController.m

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,17 @@ - (void) awakeFromNib
1515
{
1616
startFile = @"diff";
1717
[super awakeFromNib];
18-
[diffController addObserver:self forKeyPath:@"diff" options:0 context:@"ChangedDiff"];
18+
[diffController addObserver:self keyPath:@"diff" options:0 block:^(MAKVONotification *notification) {
19+
PBDiffWindowController *target = notification.target;
20+
[notification.observer showDiff:target.diff];
21+
}];
1922
}
2023

21-
- (void)closeView
22-
{
23-
[diffController removeObserver:self forKeyPath:@"diff"];
24-
25-
[super closeView];
26-
}
27-
28-
2924
- (void) didLoad
3025
{
3126
[self showDiff:diffController.diff];
3227
}
3328

34-
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
35-
{
36-
if ([(__bridge NSString *)context isEqualToString: @"ChangedDiff"])
37-
[self showDiff:diffController.diff];
38-
}
39-
4029
- (void) showDiff: (NSString *) diff
4130
{
4231
if (diff == nil || !finishedLoading)

0 commit comments

Comments
 (0)