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

Commit 4ceab33

Browse files
Further clean up for ASDK 2.0
1 parent 50f652a commit 4ceab33

2 files changed

Lines changed: 57 additions & 43 deletions

File tree

AsyncDisplayKit/ASMapNode.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
@interface ASMapNode : ASImageNode
1313

14-
- (instancetype)initWithRegion:(MKCoordinateRegion)region NS_DESIGNATED_INITIALIZER;
14+
/**
15+
The current region of ASMapNode. This can be set at any time and ASMapNode will animate the change.
16+
*/
17+
@property (nonatomic, assign) MKCoordinateRegion region;
1518

1619
/**
1720
This is the MKMapView that is the live map part of ASMapNode. This will be nil if .liveMap = NO. Note, MKMapView is *not* thread-safe.

AsyncDisplayKit/ASMapNode.mm

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ @interface ASMapNode()
1717
MKMapSnapshotter *_snapshotter;
1818
MKMapSnapshotOptions *_options;
1919
NSArray *_annotations;
20-
ASDisplayNode *_mapNode;
2120
CLLocationCoordinate2D _centerCoordinateOfMap;
2221
}
2322
@end
2423

2524
@implementation ASMapNode
2625

27-
@synthesize liveMap = _liveMap;
2826
@synthesize needsMapReloadOnBoundsChange = _needsMapReloadOnBoundsChange;
2927
@synthesize mapDelegate = _mapDelegate;
28+
@synthesize region = _region;
3029

3130
#pragma mark - Lifecycle
32-
- (instancetype)initWithRegion:(MKCoordinateRegion)region
31+
- (instancetype)init
3332
{
3433
if (!(self = [super init])) {
3534
return nil;
@@ -38,19 +37,20 @@ - (instancetype)initWithRegion:(MKCoordinateRegion)region
3837
self.clipsToBounds = YES;
3938

4039
_needsMapReloadOnBoundsChange = YES;
41-
_liveMap = NO;
40+
4241
_centerCoordinateOfMap = kCLLocationCoordinate2DInvalid;
42+
_region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(43.432858, 13.183671), MKCoordinateSpanMake(0.2, 0.2));
4343

4444
_options = [[MKMapSnapshotOptions alloc] init];
45-
_options.region = region;
45+
_options.region = _region;
4646

4747
return self;
4848
}
4949

5050
- (void)didLoad
5151
{
5252
[super didLoad];
53-
if (self.isLiveMap && !_mapNode) {
53+
if ([self wasLiveMapPreviously]) {
5454
self.userInteractionEnabled = YES;
5555
[self addLiveMap];
5656
}
@@ -59,7 +59,7 @@ - (void)didLoad
5959
- (void)fetchData
6060
{
6161
[super fetchData];
62-
if (_liveMap && !_mapNode) {
62+
if ([self wasLiveMapPreviously]) {
6363
[self addLiveMap];
6464
} else {
6565
[self setUpSnapshotter];
@@ -70,27 +70,27 @@ - (void)fetchData
7070
- (void)clearFetchedData
7171
{
7272
[super clearFetchedData];
73-
[self removeLiveMap];
73+
if (self.isLiveMap) {
74+
[self removeLiveMap];
75+
}
7476
}
7577

7678
#pragma mark - Settings
7779

7880
- (BOOL)isLiveMap
7981
{
80-
ASDN::MutexLocker l(_propertyLock);
81-
return _liveMap;
82+
return (_mapView != nil);
8283
}
8384

8485
- (void)setLiveMap:(BOOL)liveMap
8586
{
86-
ASDN::MutexLocker l(_propertyLock);
87-
if (liveMap == _liveMap) {
88-
return;
89-
}
90-
_liveMap = liveMap;
9187
liveMap ? [self addLiveMap] : [self removeLiveMap];
9288
}
9389

90+
- (BOOL)wasLiveMapPreviously
91+
{
92+
return CLLocationCoordinate2DIsValid(_centerCoordinateOfMap);
93+
}
9494

9595
- (BOOL)needsMapReloadOnBoundsChange
9696
{
@@ -104,6 +104,24 @@ - (void)setNeedsMapReloadOnBoundsChange:(BOOL)needsMapReloadOnBoundsChange
104104
_needsMapReloadOnBoundsChange = needsMapReloadOnBoundsChange;
105105
}
106106

107+
- (MKCoordinateRegion)region
108+
{
109+
ASDN::MutexLocker l(_propertyLock);
110+
return _region;
111+
}
112+
113+
- (void)setRegion:(MKCoordinateRegion)region
114+
{
115+
ASDN::MutexLocker l(_propertyLock);
116+
_region = region;
117+
if (self.isLiveMap) {
118+
[_mapView setRegion:_region animated:YES];
119+
} else {
120+
_options.region = _region;
121+
[self resetSnapshotter];
122+
[self takeSnapshot];
123+
}
124+
}
107125

108126
#pragma mark - Snapshotter
109127

@@ -164,55 +182,49 @@ - (void)resetSnapshotter
164182
#pragma mark - Actions
165183
- (void)addLiveMap
166184
{
167-
if (self.isNodeLoaded && !_mapNode) {
185+
ASDisplayNodeAssertMainThread();
186+
if (!self.isLiveMap) {
168187
__weak ASMapNode *weakSelf = self;
169-
_mapNode = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
170-
_mapView = [[MKMapView alloc] initWithFrame:CGRectZero];
171-
_mapView.delegate = _mapDelegate;
172-
[_mapView setRegion:_options.region];
173-
[_mapView addAnnotations:_annotations];
174-
[weakSelf setNeedsLayout];
175-
return _mapView;
176-
}];
177-
[self addSubnode:_mapNode];
188+
_mapView = [[MKMapView alloc] initWithFrame:CGRectZero];
189+
_mapView.delegate = weakSelf.mapDelegate;
190+
[_mapView setRegion:_options.region];
191+
[_mapView addAnnotations:_annotations];
192+
[weakSelf setNeedsLayout];
193+
[weakSelf.view addSubview:_mapView];
178194

179195
if (CLLocationCoordinate2DIsValid(_centerCoordinateOfMap)) {
180196
[_mapView setCenterCoordinate:_centerCoordinateOfMap];
197+
} else {
198+
_centerCoordinateOfMap = _options.region.center;
181199
}
182200
}
183201
}
184202

185203
- (void)removeLiveMap
186204
{
187-
if (_mapNode) {
188-
_centerCoordinateOfMap = _mapView.centerCoordinate;
189-
[_mapNode removeFromSupernode];
190-
_mapView = nil;
191-
_mapNode = nil;
192-
}
205+
_centerCoordinateOfMap = _mapView.centerCoordinate;
206+
[_mapView removeFromSuperview];
207+
_mapView = nil;
193208
}
194209

195210
- (void)setAnnotations:(NSArray *)annotations
196211
{
197212
ASDN::MutexLocker l(_propertyLock);
198213
_annotations = [annotations copy];
199-
if (annotations.count != _annotations.count) {
200-
// Redraw
201-
[self setNeedsDisplay];
202-
if (_mapView) {
203-
[_mapView removeAnnotations:_mapView.annotations];
204-
[_mapView addAnnotations:annotations];
205-
}
214+
if (self.isLiveMap) {
215+
[_mapView removeAnnotations:_mapView.annotations];
216+
[_mapView addAnnotations:annotations];
217+
} else {
218+
[self takeSnapshot];
206219
}
207220
}
208221

209-
210222
#pragma mark - Layout
211223
// Layout isn't usually needed in the box model, but since we are making use of MKMapView which is hidden in an ASDisplayNode this is preferred.
212224
- (void)layout
213225
{
214226
[super layout];
215-
if (_mapView) {
227+
if (self.isLiveMap) {
216228
_mapView.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, self.calculatedSize.height);
217229
} else {
218230
// If our bounds.size is different from our current snapshot size, then let's request a new image from MKMapSnapshotter.
@@ -223,5 +235,4 @@ - (void)layout
223235
}
224236
}
225237
}
226-
227-
@end
238+
@end

0 commit comments

Comments
 (0)