-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONViewController.m
More file actions
executable file
·143 lines (127 loc) · 5.41 KB
/
Copy pathJSONViewController.m
File metadata and controls
executable file
·143 lines (127 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// JSONViewController.m
// JSONViewExample
//
// Created by moises on 7/26/18.
// Copyright © 2018 pie33.com. All rights reserved.
//
#import "JSONViewController.h"
#import "JSONViewControllerRouter.h"
#import "UIView+JSONView.h"
@import YogaKit;
@interface JSONViewController ()
@property (nonatomic) NSDictionary *viewDictionary;
@property (nonatomic, readonly) dispatch_queue_t parseQueue;
@property (nonatomic) UIView *containedView;
@property (nonatomic) UIScrollView *scrollContainer;
@property (nonatomic) UIActivityIndicatorView *activityIndicatorView;
@end
@implementation JSONViewController
@synthesize parseQueue = _parseQueue;
- (dispatch_queue_t) parseQueue {
if (!_parseQueue) {
_parseQueue = dispatch_queue_create("com.json.parsequeue", DISPATCH_QUEUE_SERIAL);
}
return _parseQueue;
}
- (instancetype) initWithJSONDictionary:(NSDictionary*)jsonDictionary {
if ((self = [super init])) {
_parseQueue = dispatch_queue_create("com.json.parsequeue", DISPATCH_QUEUE_SERIAL);
_viewDictionary = jsonDictionary;
}
return self;
}
- (void) updateWithJSONDictionary:(NSDictionary*)jsonDictionary {
[self.activityIndicatorView stopAnimating];
__weak typeof (self) weakSelf = self;
dispatch_async(self.parseQueue, ^{
typeof (self) strongSelf = weakSelf;
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.containedView removeFromSuperview];
strongSelf.containedView = [[UIView alloc] initWithFrame:CGRectZero];
[strongSelf.containedView configureWithDictionary:jsonDictionary];
if (strongSelf.isViewLoaded) {
[strongSelf updateView];
}
});
});
}
- (void) updateView {
if (!self.containedView.superview) {
[self.scrollContainer addSubview:self.containedView];
self.scrollContainer.backgroundColor = self.containedView.backgroundColor ?: [UIColor whiteColor];
}
self.title = self.containedView.title;
[self.scrollContainer configureLayoutWithBlock:^(YGLayout * _Nonnull layout) {
layout.isEnabled = YES;
layout.width = YGPointValue(self.view.bounds.size.width);
layout.height = YGPointValue(self.view.bounds.size.height);
layout.flexDirection = YGFlexDirectionColumn;
layout.alignItems = YGAlignStretch;
layout.justifyContent = YGJustifyFlexStart;
}];
[self.scrollContainer.yoga applyLayoutPreservingOrigin:YES];
CGSize contentSize = CGSizeMake(self.containedView.bounds.size.width,
self.containedView.bounds.size.height + self.containedView.frame.origin.y);
self.scrollContainer.contentSize = contentSize;
[self.activityIndicatorView stopAnimating];
}
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self updateView];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self updateWithJSONDictionary:self.viewDictionary];
self.scrollContainer = [[UIScrollView alloc] initWithFrame:CGRectZero];
self.scrollContainer.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.scrollContainer];
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicatorView.translatesAutoresizingMaskIntoConstraints = NO;
self.activityIndicatorView.hidesWhenStopped = YES;
[self.view addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
NSDictionary *views = @{@"scroll" : self.scrollContainer};
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scroll]|"
options:0
metrics:nil
views:views]
];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scroll]|"
options:0
metrics:nil
views:views]
];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.activityIndicatorView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.activityIndicatorView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
}
- (void)didMoveToParentViewController:(UIViewController *)parent {
[super didMoveToParentViewController:parent];
if (parent == nil) {
[[JSONViewControllerRouter sharedInstance] removedFromStack:self];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end