Skip to content

Commit a33fb89

Browse files
committed
array cursor can now send notifications and thus control a tableview
1 parent a77d86d commit a33fb89

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

MPWFoundationUI/Classes/MPWTableView.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@protocol MPWStorage,MPWIdentifying;
1212
@class MPWReference;
1313

14-
@interface MPWTableView : NSTableView <NSTableViewDataSource,ModelDidChange,NSTableViewDelegate>
14+
@interface MPWTableView : NSTableView <NSTableViewDataSource,ModelDidChange,SelectionDidChange,NSTableViewDelegate>
1515

1616
@property (nonatomic, strong, nullable) MPWReference *binding;
1717
@property (nonatomic, strong, nullable) MPWArrayCursor *cursor;

MPWFoundationUI/Classes/MPWTableView.m

+7
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
239239
return [self objects][[self selectedRow]];
240240
}
241241

242+
-(void)selectionDidChange:something
243+
{
244+
if ( self.cursor && self.cursor.offset != [self selectedRow]) {
245+
[self selectRow:self.cursor.offset byExtendingSelection:NO];
246+
}
247+
}
248+
242249
- (void)tableViewSelectionDidChange:(NSNotification *)notification
243250
{
244251
if (self.cursor ) {

Stores.subproj/MPWArrayCursor.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN
1414
-initWithArray:anArray;
1515

1616
@property (nonatomic, assign) long offset;
17+
@property (nonatomic, weak) id <Streaming> selectionChanges;
18+
@property (readonly) NSArray *base;
1719

1820

1921

Stores.subproj/MPWArrayCursor.m

+41-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ @interface MPWArrayCursor()
1515
@end
1616

1717
@implementation MPWArrayCursor
18+
{
19+
long offset;
20+
}
21+
22+
@dynamic offset;
23+
24+
-(long)offset
25+
{
26+
return offset;
27+
}
28+
29+
-(void)setOffset:(long)newOffset
30+
{
31+
offset=newOffset;
32+
[self.selectionChanges writeObject:self];
33+
}
1834

1935
+(instancetype)cursorWithArray:(NSMutableArray*)newarray
2036
{
@@ -86,29 +102,46 @@ + (instancetype)referenceWithIdentifier:(id)anIdentifier inStore:(id)aStore {
86102

87103
@implementation MPWArrayCursor(testing)
88104

89-
+(void)testCanGetValueAtTheInitialisedOffset
105+
+(instancetype)_testCursor
90106
{
91-
NSArray *testArray=@[ @"a", @"b"];
107+
NSMutableArray *testArray=[[@[ @"a", @"b"] mutableCopy] autorelease];
92108
MPWArrayCursor *cursor1=[self cursorWithArray:testArray];
109+
return cursor1;
110+
}
111+
112+
+(void)testCanGetValueAtOffset
113+
{
114+
MPWArrayCursor *cursor1=[self _testCursor];
93115
IDEXPECT( [cursor1 value], @"a", @"offset 0");
94116
cursor1.offset=1;
95117
IDEXPECT( [cursor1 value], @"b", @"offset 1");
96118
}
97119

98-
+(void)testCanSetValueAtTheInitialisedOffset
120+
+(void)testCanSetValueAtOffset
99121
{
100-
NSMutableArray *testArray=[[@[ @"a", @"b"] mutableCopy] autorelease];
101-
MPWArrayCursor *cursor1=[self cursorWithArray:testArray];
122+
MPWArrayCursor *cursor1=[self _testCursor];
102123
IDEXPECT( [cursor1 value], @"a", @"offset 0");
103124
cursor1.value = @"new value";
104-
IDEXPECT( [testArray firstObject], @"new value", @"did set");
125+
IDEXPECT( cursor1.value, @"new value", @"did set");
126+
}
127+
128+
+(void)testCanBeNotified
129+
{
130+
MPWArrayCursor *cursor1=[self _testCursor];
131+
NSMutableArray *notifications=[NSMutableArray array];
132+
cursor1.selectionChanges=notifications;
133+
INTEXPECT(notifications.count,0,@"no notifications");
134+
cursor1.offset = 1;
135+
INTEXPECT(notifications.count,1,@"got a notification");
136+
105137
}
106138

107139
+(NSArray*)testSelectors
108140
{
109141
return @[
110-
@"testCanGetValueAtTheInitialisedOffset",
111-
@"testCanSetValueAtTheInitialisedOffset",
142+
@"testCanGetValueAtOffset",
143+
@"testCanSetValueAtOffset",
144+
@"testCanBeNotified",
112145
];
113146
}
114147

0 commit comments

Comments
 (0)