Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

Commit 7629faa

Browse files
authored
Objective C builders support chaining style. (#202)
1 parent 81ea591 commit 7629faa

22 files changed

Lines changed: 528 additions & 28 deletions

LayoutKitObjCSampleApp/RotationLayout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88

99
#import <UIKit/UIKit.h>
10-
#import <LayoutKitObjC/LayoutKitObjC-Swift.h>
10+
#import <LayoutKitObjC/LayoutKitObjC.h>
1111

1212
@interface RotationLayout: NSObject <LOKLayout>
1313

LayoutKitObjCSampleApp/RotationLayout.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88

99
#import <Foundation/Foundation.h>
10+
#import <LayoutKitObjC/LayoutKitObjC.h>
1011

1112
#import "RotationLayout.h"
1213

LayoutKitObjCSampleApp/ViewController.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88

99
#import <UIKit/UIKit.h>
10-
#import <LayoutKit/LayoutKit-Swift.h>
1110

1211
@interface ViewController : UIViewController
1312

LayoutKitObjCSampleApp/ViewController.m

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ - (void)viewDidLoad {
6868
collectionView.delegate = self.adapter;
6969
collectionView.dataSource = self.adapter;
7070

71-
LOKSizeLayoutBuilder *cellLayoutBuilder = [LOKSizeLayoutBuilder withSublayout:self.helloWorldLayout];
72-
cellLayoutBuilder.width = self.view.bounds.size.width;
73-
LOKSizeLayout *cellLayout = [cellLayoutBuilder build];
71+
__auto_type cellLayout = [LOKSizeLayoutBuilder withSublayout:self.helloWorldLayout].withWidth(self.view.bounds.size.width).layout;
7472

7573
NSArray *items = @[
7674
cellLayout, cellLayout, cellLayout, cellLayout,
@@ -106,26 +104,31 @@ - (void)viewDidLayoutSubviews {
106104
}
107105

108106
+ (id<LOKLayout>)makeHelloLayout {
109-
LOKLabelLayoutBuilder *labelLayoutBuilderA = [LOKLabelLayoutBuilder withString:@"Hello"];
110-
labelLayoutBuilderA.viewClass = MyLabelView.class;
111-
labelLayoutBuilderA.configure = ^(UIView * _Nonnull label) {
107+
__auto_type labelLayoutA =
108+
[LOKLabelLayoutBuilder withString:@"Hello"]
109+
.withViewClass([MyLabelView class])
110+
.withConfig(^(UIView * _Nonnull label) {
112111
label.backgroundColor = UIColor.whiteColor;
113-
};
114-
LOKLabelLayout *labelLayoutA = [labelLayoutBuilderA build];
115-
LOKLabelLayoutBuilder *labelLayoutBuilderB = [LOKLabelLayoutBuilder withString:@"world!"];
116-
labelLayoutBuilderB.viewClass = MyLabelView.class;
117-
labelLayoutBuilderB.configure = ^(UIView * _Nonnull label) {
112+
})
113+
.layout;
114+
115+
__auto_type labelLayoutB = [LOKLabelLayoutBuilder withString:@"world!"]
116+
.withViewClass([MyLabelView class])
117+
.withConfig(^(UIView * _Nonnull label) {
118118
label.backgroundColor = UIColor.whiteColor;
119-
};
120-
LOKLabelLayout *labelLayoutB = [labelLayoutBuilderB build];
121-
LOKStackLayoutBuilder *stackLayoutBuilder = [LOKStackLayoutBuilder withSublayouts:@[labelLayoutA, labelLayoutB]];
122-
stackLayoutBuilder.axis = LOKAxisHorizontal;
123-
stackLayoutBuilder.spacing = 10;
124-
LOKInsetLayoutBuilder *insetLayoutBuilder = [LOKInsetLayoutBuilder withInsets:UIEdgeInsetsMake(20, 20, 20, 20) around:[stackLayoutBuilder build]];
125-
insetLayoutBuilder.alignment = LOKAlignment.fill;
126-
insetLayoutBuilder.viewClass = LabelBackgroundView.class;
127-
insetLayoutBuilder.configure = ^(UIView * _Nonnull view) { };
128-
return [[RotationLayout alloc] initWithSublayout:[insetLayoutBuilder build]
119+
})
120+
.layout;
121+
122+
__auto_type layout = [LOKStackLayoutBuilder withSublayouts:@[labelLayoutA, labelLayoutB]]
123+
.withAxis(LOKAxisHorizontal)
124+
.withSpacing(10)
125+
.withInsets(UIEdgeInsetsMake(20, 20, 20, 20))
126+
.withAlignment(LOKAlignment.fill)
127+
.withViewClass(LabelBackgroundView.class)
128+
.withConfig(^(UIView * _Nonnull view) { })
129+
.layout;
130+
131+
return [[RotationLayout alloc] initWithSublayout:layout
129132
alignment:LOKAlignment.center
130133
viewReuseId:nil];
131134
}

Sources/LayoutKitObjC.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ FOUNDATION_EXPORT double LayoutKitObjCVersionNumber;
1515
FOUNDATION_EXPORT const unsigned char LayoutKitObjCVersionString[];
1616

1717
// In this header, you should import all the public headers of your framework using statements like #import <LayoutKitObjC/PublicHeader.h>
18+
1819
#import "LOKBaseLayoutBuilder.h"
1920
#if __has_include("LOKButtonLayoutBuilder.h")
2021
#import "LOKButtonLayoutBuilder.h"
@@ -29,3 +30,7 @@ FOUNDATION_EXPORT const unsigned char LayoutKitObjCVersionString[];
2930
#if __has_include("LOKTextViewLayoutBuilder.h")
3031
#import "LOKTextViewLayoutBuilder.h"
3132
#endif
33+
34+
#if __has_include("LayoutKitObjC-Swift.h")
35+
#import "LayoutKitObjC-Swift.h"
36+
#endif

Sources/ObjCSupport/Builders/LOKBaseLayoutBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef NSView View;
2323
@class LOKAlignment;
2424
@class LOKFlexibility;
2525
@protocol LOKLayout;
26+
@class LOKInsetLayoutBuilder;
2627

2728
@interface LOKBaseLayoutBuilder : NSObject
2829

@@ -34,4 +35,8 @@ typedef NSView View;
3435

3536
- (nonnull id<LOKLayout>)build;
3637

38+
@property (nonatomic, nonnull, readonly) LOKInsetLayoutBuilder * _Nonnull(^withInsets)(EdgeInsets);
39+
40+
@property (nonatomic, nonnull) id<LOKLayout> layout;
41+
3742
@end

Sources/ObjCSupport/Builders/LOKBaseLayoutBuilder.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@
77
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88

99
#import "LOKBaseLayoutBuilder.h"
10+
#import "LOKInsetLayoutBuilder.h"
1011

1112
@implementation LOKBaseLayoutBuilder
1213

1314
- (id<LOKLayout>)build {
15+
NSAssert(NO, @"This method is expected to be overridden by a superclass.");
1416
return nil;
1517
}
1618

19+
- (id<LOKLayout>)layout {
20+
return [self build];
21+
}
22+
23+
- (LOKInsetLayoutBuilder * _Nonnull (^)(EdgeInsets))withInsets {
24+
return ^LOKInsetLayoutBuilder *(EdgeInsets insets){
25+
return [LOKInsetLayoutBuilder withInsets:insets around:[self build]];
26+
};
27+
}
28+
1729
@end

Sources/ObjCSupport/Builders/LOKButtonLayoutBuilder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ typedef NS_ENUM(NSInteger, LOKButtonLayoutType) {
3030
@property (nonatomic) CGSize imageSize;
3131
@property (nonatomic, nullable) NSValue *contentEdgeInsets;
3232

33+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withType)(LOKButtonLayoutType);
34+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withFont)(UIFont * _Nullable);
35+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withImage)(UIImage * _Nullable);
36+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withImageSize)(CGSize);
37+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withContentEdgeInsets)(NSValue * _Nullable);
38+
39+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withAlignment)(LOKAlignment * _Nonnull);
40+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withFlexibility)(LOKFlexibility * _Nonnull);
41+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withViewReuseId)(NSString * _Nonnull);
42+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withViewClass)(Class _Nonnull);
43+
44+
@property (nonatomic, nonnull, readonly) LOKButtonLayoutBuilder * _Nonnull(^withConfig)( void(^ _Nonnull)(View *_Nonnull));
45+
3346
- (nonnull LOKButtonLayout *)build;
3447

3548
@end

Sources/ObjCSupport/Builders/LOKButtonLayoutBuilder.m

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,74 @@ - (LOKButtonLayout *)build {
3232
config:self.configure];
3333
}
3434

35+
- (LOKButtonLayoutBuilder * _Nonnull (^)(LOKButtonLayoutType))withType {
36+
return ^LOKButtonLayoutBuilder *(LOKButtonLayoutType type){
37+
self.type = type;
38+
return self;
39+
};
40+
}
41+
42+
- (LOKButtonLayoutBuilder * _Nonnull (^)(UIFont * _Nullable))withFont {
43+
return ^LOKButtonLayoutBuilder *(UIFont * font){
44+
self.font = font;
45+
return self;
46+
};
47+
}
48+
49+
- (LOKButtonLayoutBuilder * _Nonnull (^)(UIImage * _Nullable))withImage {
50+
return ^LOKButtonLayoutBuilder *(UIImage * image){
51+
self.image = image;
52+
return self;
53+
};
54+
}
55+
56+
- (LOKButtonLayoutBuilder * _Nonnull (^)(CGSize))withImageSize {
57+
return ^LOKButtonLayoutBuilder *(CGSize imageSize){
58+
self.imageSize = imageSize;
59+
return self;
60+
};
61+
}
62+
63+
- (LOKButtonLayoutBuilder * _Nonnull (^)(NSValue * _Nullable))withContentEdgeInsets {
64+
return ^LOKButtonLayoutBuilder *(NSValue * _Nullable contentEdgeInsets){
65+
self.contentEdgeInsets = contentEdgeInsets;
66+
return self;
67+
};
68+
}
69+
70+
- (LOKButtonLayoutBuilder * _Nonnull (^)(LOKAlignment * _Nonnull))withAlignment {
71+
return ^LOKButtonLayoutBuilder *(LOKAlignment * alignment){
72+
self.alignment = alignment;
73+
return self;
74+
};
75+
}
76+
77+
- (LOKButtonLayoutBuilder * _Nonnull (^)(LOKFlexibility * _Nonnull))withFlexibility {
78+
return ^LOKButtonLayoutBuilder *(LOKFlexibility * flexibility){
79+
self.flexibility = flexibility;
80+
return self;
81+
};
82+
}
83+
84+
- (LOKButtonLayoutBuilder * _Nonnull (^)(NSString * _Nonnull))withViewReuseId {
85+
return ^LOKButtonLayoutBuilder *(NSString * viewReuseId){
86+
self.viewReuseId = viewReuseId;
87+
return self;
88+
};
89+
}
90+
91+
- (LOKButtonLayoutBuilder * _Nonnull (^)(Class _Nonnull))withViewClass {
92+
return ^LOKButtonLayoutBuilder *(Class viewClass){
93+
self.viewClass = viewClass;
94+
return self;
95+
};
96+
}
97+
98+
- (LOKButtonLayoutBuilder * _Nonnull (^)(void(^ _Nonnull)(View *_Nonnull)))withConfig {
99+
return ^LOKButtonLayoutBuilder *(void(^ _Nonnull config)(View *_Nonnull)){
100+
self.configure = config;
101+
return self;
102+
};
103+
}
104+
35105
@end

Sources/ObjCSupport/Builders/LOKInsetLayoutBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
@property (nonatomic) EdgeInsets insets;
1818
@property (nonatomic, nonnull) id<LOKLayout> sublayout;
1919

20+
@property (nonatomic, nonnull, readonly) LOKInsetLayoutBuilder * _Nonnull(^withAlignment)(LOKAlignment * _Nonnull);
21+
@property (nonatomic, nonnull, readonly) LOKInsetLayoutBuilder * _Nonnull(^withFlexibility)(LOKFlexibility * _Nonnull);
22+
@property (nonatomic, nonnull, readonly) LOKInsetLayoutBuilder * _Nonnull(^withViewReuseId)(NSString * _Nonnull);
23+
@property (nonatomic, nonnull, readonly) LOKInsetLayoutBuilder * _Nonnull(^withViewClass)(Class _Nonnull);
24+
25+
@property (nonatomic, nonnull, readonly) LOKInsetLayoutBuilder * _Nonnull(^withConfig)( void(^ _Nonnull)(View *_Nonnull));
26+
2027
- (nonnull LOKInsetLayout *)build;
2128

2229
@end

0 commit comments

Comments
 (0)