Skip to content

Commit 3016cc2

Browse files
author
Larry Tin
committed
Add NSObject+GDDataDrivenView
1 parent fb951db commit 3016cc2

16 files changed

Lines changed: 85 additions & 17 deletions

Example/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ SPEC CHECKSUMS:
5555
Expecta+Snapshots: c343f410c7a6392f3e22e78f94c44b6c0749a516
5656
FBSnapshotTestCase: 918c55861356ee83aee7843d759f55a18ff6982b
5757
GDChannel: e26b61a1f156eb4b657516d80d90bdad2db46c56
58-
GDDataDrivenView: 131234691a702326e9f5d2631dd5187bdde0fbb2
58+
GDDataDrivenView: af8cfcf56028e138662a6bd4fe17ccd9423ea3ea
5959
Mantle: bc40bb061d8c2c6fb48d5083e04d928c3b7f73d9
6060
MQTTKit: a4e6b8aa92db1aeae917d2b773fd72a7749937c4
6161
Protobuf: c032ce1bdf7a266acfd640fbfd8bdb166127c7f5

GDDataDrivenView.podspec

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ TODO: Add long description of the pod here.
5252

5353
s.subspec 'UIViewController' do |sp|
5454
sp.dependency 'GDDataDrivenView/MVP'
55+
sp.dependency 'GDDataDrivenView/Generated'
5556
sp.dependency 'GDChannel', '~> 0.6'
5657
sp.dependency 'Aspects', '~> 1.4.1'
5758

5859
sp.source_files = 'GDDataDrivenView/Classes/UIViewController/**/*'
59-
sp.requires_arc = ['GDDataDrivenView/Classes/UIViewController/GDDViewControllerHelper.*', 'GDDataDrivenView/Classes/UIViewController/UIViewController+GDDataDrivenView.*']
60+
end
61+
62+
s.subspec 'Generated' do |sp|
63+
sp.dependency 'Protobuf', '~> 3.0'
64+
65+
sp.requires_arc = false
66+
sp.source_files = 'GDDataDrivenView/Generated/**/*'
6067
end
6168
end

GDDataDrivenView/Classes/Renders/GDDUnknownCellRender.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#import "GDDRender.h"
77

88
@interface GDDUnknownCellRender : UITableViewCell <GDDRender, GDDPresenter>
9+
910
@property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
1011

11-
+ (UITableView *)findTableViewOfCell:(UITableViewCell *)cell;
1212
@end

GDDataDrivenView/Classes/Renders/GDDUnknownCellRender.m

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44

55
#import "GDDUnknownCellRender.h"
6-
6+
#import "UITableViewCell+GDDRender.h"
77

88
@implementation GDDUnknownCellRender {
99
// BOOL _didSetupConstraints;
@@ -67,7 +67,7 @@ - (void)handleTap:(UITapGestureRecognizer *)sender {
6767
[self.descriptionLabel addConstraint:_heightConstraint];
6868
// NSLog(@"_heightConstraint");
6969
}
70-
UITableView *tableView = [self.class findTableViewOfCell:self];
70+
UITableView *tableView = [self nearestTableView];
7171
// [tableView reloadData];
7272
// NSLog(@"reloadRowsAtIndexPaths");
7373
[tableView reloadRowsAtIndexPaths:@[[tableView indexPathForCell:self]] withRowAnimation:UITableViewRowAnimationAutomatic];
@@ -81,11 +81,4 @@ - (void)handleTap:(UITapGestureRecognizer *)sender {
8181
return self;
8282
}
8383

84-
+ (UITableView *)findTableViewOfCell:(UITableViewCell *)cell {
85-
UIView *view = cell.superview;
86-
do {
87-
view = view.superview;
88-
} while (![view isKindOfClass:UITableView.class]);
89-
return view;
90-
}
9184
@end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// Created by Larry Tin on 16/9/12.
3+
//
4+
5+
#import <Foundation/Foundation.h>
6+
7+
@interface UITableViewCell (GDDRender)
8+
- (UITableView *)nearestTableView;
9+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Created by Larry Tin on 16/9/12.
3+
//
4+
5+
#import "UITableViewCell+GDDRender.h"
6+
7+
8+
@implementation UITableViewCell (GDDRender)
9+
10+
- (UITableView *)nearestTableView {
11+
UIView *view = self.superview;
12+
do {
13+
view = view.superview;
14+
} while (![view isKindOfClass:UITableView.class]);
15+
return view;
16+
}
17+
18+
@end

GDDataDrivenView/Classes/UIViewController/GDDViewControllerHelper.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ + (void)show:(UIViewController *)controller message:(id <GDCMessage>)message {
3333
if (!controller) {
3434
return;
3535
}
36-
id <GDDPresenter> presenter = [self findOrCreatePresenterForViewController:controller];
3736
void (^messageHandler)() = ^{
37+
id <GDDPresenter> presenter = [self findOrCreatePresenterForViewController:controller];
3838
if (presenter) {
3939
NSParameterAssert([controller conformsToProtocol:@protocol(GDDRender)]);
4040
[presenter update:(id <GDDRender>) controller withData:message.payload];
@@ -334,7 +334,7 @@ + (UIViewController *)getVisibleOrChildViewController:(UIViewController *)parent
334334
Class presenterClass;
335335
NSString *viewControllerClassName = NSStringFromClass(controller.class);
336336
NSString *presenterClassName;
337-
NSString *const renderSuffix = @"ViewContrller";
337+
NSString *const renderSuffix = @"ViewController";
338338
if ([viewControllerClassName hasSuffix:renderSuffix]) {
339339
presenterClassName = [viewControllerClassName substringToIndex:viewControllerClassName.length - renderSuffix.length];
340340
presenterClass = NSClassFromString([NSString stringWithFormat:@"%@Presenter", presenterClassName]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Created by Larry Tin on 16/9/12.
3+
//
4+
5+
#import <Foundation/Foundation.h>
6+
#import "NSObject+GDChannel.h"
7+
8+
@interface NSObject (GDDataDrivenView)
9+
10+
- (void)subscribeLocalToSelf:(NSArray<NSString *> *)topics;
11+
12+
@end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by Larry Tin on 16/9/12.
3+
//
4+
5+
#import "NSObject+GDDataDrivenView.h"
6+
#import "Aspects.h"
7+
8+
@implementation NSObject (GDDataDrivenView)
9+
10+
- (void)subscribeLocalToSelf:(NSArray<NSString *> *)topics {
11+
NSMutableArray *consumers = [NSMutableArray arrayWithCapacity:topics.count];
12+
for (NSString *topic in topics) {
13+
[consumers addObject:[self subscribe:self toOne:topic]];
14+
}
15+
[self aspect_hookSelector:NSSelectorFromString(@"dealloc") withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) {
16+
for (id <GDCMessageConsumer> consumer in consumers) {
17+
[consumer unsubscribe];
18+
}
19+
[consumers removeAllObjects];
20+
} error:NULL];
21+
}
22+
23+
- (id <GDCMessageConsumer>)subscribe:(NSObject <GDCMessageHandler> *)handler toOne:(NSString *)topic {
24+
__weak id <GDCMessageHandler> weakHandler = handler;
25+
return [self.bus subscribeLocal:topic handler:^(id <GDCMessage> message) {
26+
[weakHandler handleMessage:message];
27+
}];
28+
}
29+
@end

GDDataDrivenView/Classes/UIViewController/GoodowBool.pbobjc.h renamed to GDDataDrivenView/Generated/GoodowBool.pbobjc.h

File renamed without changes.

0 commit comments

Comments
 (0)