Skip to content

Commit 310cc87

Browse files
committed
added support for deep copying and so collection immutability
1 parent 9dbfc67 commit 310cc87

File tree

9 files changed

+159
-3
lines changed

9 files changed

+159
-3
lines changed

EPMapExtensions/EPAnnotationArray.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ - (void)addAnnotation:(id <MKAnnotation>)annotation
3737

3838
- (NSArray *)allAnnotations
3939
{
40-
return [_annotationArray copy];
40+
return [[NSArray alloc] initWithArray:_annotationArray copyItems:YES];
4141
}
4242

4343
- (void)removeAllAnnotations

EPMapExtensions/EPMapView.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050

5151
- (MKCoordinateRegion)centeredRegionForAnnotationConformingToProtocols:(NSArray *)protocols;
5252

53+
- (MKCoordinateRegion)centeredRegionForAnnotationOfKindOfClass:(Class)cls;
54+
55+
- (MKCoordinateRegion)centeredRegionForAnnotationOfKindOfClasses:(NSArray *)classes;
56+
5357
///---------------------------------------------
5458
/// @name Getting visible annotations
5559
///---------------------------------------------
@@ -74,6 +78,12 @@
7478

7579
- (NSUInteger)visibleAnnotationsConformingToProtocols:(NSArray *)protocols;
7680

81+
82+
- (NSUInteger)visibleAnnotationsOfKindOfClass:(Class)cls;
83+
84+
- (NSUInteger)visibleAnnotationOfKindOfClasses:(NSArray *)classes;
85+
86+
7787
///---------------------------------------------
7888
/// @name Resetting annotation
7989
///---------------------------------------------

EPMapExtensions/EPMapViewDelegate.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
//
88

99
#import "EPMapViewDelegate.h"
10+
#import "EPMapViewDecorator.h"
1011

1112
@implementation EPMapViewDelegate
1213

14+
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
15+
{
16+
return [_mapViewDecorator mapView:mapView viewForAnnotation:annotation];
17+
}
18+
1319
@end

EPMapExtensions/EPPointOfInterest.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
`EPPointOfInterest` represent an annotation object responding to `MKAnnotation` protocol
1414
*/
1515

16-
@interface EPPointOfInterest : NSObject <MKAnnotation>
16+
@interface EPPointOfInterest : NSObject <MKAnnotation, NSCopying>
1717

1818
///-----------------------------------------------------
1919
/// @name Creation
@@ -32,3 +32,10 @@
3232
@property (readonly) CLLocationCoordinate2D coordinate;
3333

3434
@end
35+
36+
37+
@interface EPPointOfInterest (EPDraggablePointOfInterest)
38+
39+
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
40+
41+
@end

EPMapExtensions/EPPointOfInterest.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
1818
return self;
1919
}
2020

21+
- (id)copyWithZone:(NSZone *)zone
22+
{
23+
EPPointOfInterest *newPOI = [[[self class] allocWithZone:zone] init];
24+
newPOI->_coordinate = _coordinate;
25+
return newPOI;
26+
}
27+
28+
@end
29+
30+
@implementation EPPointOfInterest (EPDraggablePointOfInterest)
31+
32+
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
33+
{
34+
_coordinate = newCoordinate;
35+
}
36+
2137
@end

EPMapExtensions/EPUserPosition.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,54 @@
99
#import <Foundation/Foundation.h>
1010
#import <MapKit/MapKit.h>
1111

12+
/**
13+
`EPUserPosition` represent the user location
14+
*/
15+
1216
@interface EPUserPosition : NSObject {
1317
CLLocation *_userPosition;
1418
}
1519

20+
21+
///-------------------------------------------------
22+
/// Creating object
23+
///-------------------------------------------------
24+
25+
/**
26+
@param userLocation Takes the user location
27+
*/
28+
1629
- (id)initWithUserPosition:(CLLocation *)userLocation;
1730

31+
///-------------------------------------------------
32+
/// Updating user position
33+
///-------------------------------------------------
34+
35+
/**
36+
Takes the fresh user position
37+
@param newUserLocation New acquired user location
38+
*/
39+
1840
- (void)updatePosition:(CLLocation *)newUserLocation;
1941

42+
/**
43+
Contains the last valid user position
44+
*/
45+
2046
@property (nonatomic, readonly) CLLocation *userPosition; //contains the last valid user position
47+
48+
/**
49+
Contains the timestamp of the last valid user position
50+
*/
51+
52+
@property (nonatomic, readonly) NSDate *lastValidPositionDate;
53+
54+
/**
55+
56+
*/
57+
2158
@property (nonatomic) CLLocationDistance minThresholdToUpdatePosition; //meters
22-
@property (nonatomic, readonly) NSDate *lastValidPositionDate; //
59+
2360
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
2461

2562
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// EPAnnotationArrayTest.h
3+
// iOSMapExtension
4+
//
5+
// Created by Eriprando Pacces on 5/20/13.
6+
// Copyright (c) 2013 it.reply. All rights reserved.
7+
//
8+
9+
#import <SenTestingKit/SenTestingKit.h>
10+
11+
@interface EPAnnotationArrayTest : SenTestCase
12+
13+
@end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// EPAnnotationArrayTest.m
3+
// iOSMapExtension
4+
//
5+
// Created by Eriprando Pacces on 5/20/13.
6+
// Copyright (c) 2013 it.reply. All rights reserved.
7+
//
8+
9+
#import "EPAnnotationArrayTest.h"
10+
#import "EPAnnotationArray.h"
11+
#import "EPPointOfInterest.h"
12+
13+
@implementation EPAnnotationArrayTest {
14+
EPAnnotationArray *_annotationArray;
15+
EPPointOfInterest *_poi;
16+
}
17+
18+
- (void)setUp
19+
{
20+
_annotationArray = [[EPAnnotationArray alloc] init];
21+
_poi = [[EPPointOfInterest alloc] initWithCoordinate:CLLocationCoordinate2DMake(22, 22)];
22+
}
23+
24+
- (void)testAddAnnotation
25+
{
26+
[_annotationArray addAnnotation:_poi];
27+
STAssertTrue([_annotationArray count] == 1, @"annotation array count shold be equal to 1");
28+
EPPointOfInterest *poi = [_annotationArray allAnnotations][0];
29+
STAssertTrue(poi.coordinate.latitude == 22 && poi.coordinate.longitude == 22, @"the array should contain annotation just added");
30+
}
31+
32+
- (void)testAddNonAnnotation
33+
{
34+
STAssertThrows([_annotationArray addAnnotation:(id<MKAnnotation>)@"ciao"], @"should raise a bad argument exception");
35+
}
36+
37+
- (void)testAddBadAnnotations
38+
{
39+
STAssertThrows([_annotationArray addAnnotations:(@[@"foo", @"meow", @"bar"])], @"should raise a bad argument exception");
40+
}
41+
42+
- (void)testAddAnnotations
43+
{
44+
NSArray *annotations = [NSArray arrayWithObjects:_poi, _poi, _poi, nil];
45+
[_annotationArray addAnnotations:annotations];
46+
STAssertTrue([_annotationArray count] == 3, @"annotation collection should contain three elements");
47+
EPPointOfInterest *poi = [_annotationArray allAnnotations][1];
48+
STAssertTrue(poi.coordinate.longitude == 22 && poi.coordinate.longitude == 22, @"collection should contain annotation just added");
49+
}
50+
51+
- (void)testMutability
52+
{
53+
[_annotationArray addAnnotation:_poi];
54+
EPPointOfInterest *poi = [_annotationArray allAnnotations][0];
55+
[poi setCoordinate:CLLocationCoordinate2DMake(1, 2)];
56+
EPPointOfInterest *poi2 = [_annotationArray allAnnotations][0];
57+
STAssertTrue(poi2.coordinate.longitude != poi.coordinate.longitude && poi2.coordinate.latitude != poi.coordinate.latitude,
58+
@"coordinates should be different");
59+
}
60+
61+
@end

iOSMapExtension.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/* End PBXAggregateTarget section */
2323

2424
/* Begin PBXBuildFile section */
25+
48DBE5AC1749856C0014BE48 /* EPAnnotationArrayTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 48DBE5AB1749856C0014BE48 /* EPAnnotationArrayTest.m */; };
2526
845619711749748F00CC2D0D /* EPMapViewDecorator.m in Sources */ = {isa = PBXBuildFile; fileRef = 845619701749748F00CC2D0D /* EPMapViewDecorator.m */; };
2627
848EB05217464444009639D0 /* YAAnnotationView.m in Sources */ = {isa = PBXBuildFile; fileRef = 848EB05117464444009639D0 /* YAAnnotationView.m */; };
2728
84BE5F8C1738005E0067CC40 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 84BE5F8B1738005E0067CC40 /* UIKit.framework */; };
@@ -84,6 +85,8 @@
8485
/* End PBXCopyFilesBuildPhase section */
8586

8687
/* Begin PBXFileReference section */
88+
48DBE5AA1749856C0014BE48 /* EPAnnotationArrayTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EPAnnotationArrayTest.h; sourceTree = "<group>"; };
89+
48DBE5AB1749856C0014BE48 /* EPAnnotationArrayTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EPAnnotationArrayTest.m; sourceTree = "<group>"; };
8790
8456196F1749748F00CC2D0D /* EPMapViewDecorator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EPMapViewDecorator.h; sourceTree = "<group>"; };
8891
845619701749748F00CC2D0D /* EPMapViewDecorator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EPMapViewDecorator.m; sourceTree = "<group>"; };
8992
848EB05017464444009639D0 /* YAAnnotationView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YAAnnotationView.h; sourceTree = "<group>"; };
@@ -261,6 +264,8 @@
261264
84BE5FFE17380C7B0067CC40 /* EPMapViewDecoratorTest.m */,
262265
848EB05017464444009639D0 /* YAAnnotationView.h */,
263266
848EB05117464444009639D0 /* YAAnnotationView.m */,
267+
48DBE5AA1749856C0014BE48 /* EPAnnotationArrayTest.h */,
268+
48DBE5AB1749856C0014BE48 /* EPAnnotationArrayTest.m */,
264269
);
265270
path = EPMapExtensionsTests;
266271
sourceTree = "<group>";
@@ -442,6 +447,7 @@
442447
files = (
443448
84BE5FFF17380C7B0067CC40 /* EPMapViewDecoratorTest.m in Sources */,
444449
848EB05217464444009639D0 /* YAAnnotationView.m in Sources */,
450+
48DBE5AC1749856C0014BE48 /* EPAnnotationArrayTest.m in Sources */,
445451
);
446452
runOnlyForDeploymentPostprocessing = 0;
447453
};

0 commit comments

Comments
 (0)