This repository was archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapViewController.m
More file actions
154 lines (118 loc) · 6.05 KB
/
MapViewController.m
File metadata and controls
154 lines (118 loc) · 6.05 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
144
145
146
147
148
149
150
151
152
153
154
//
// Dixie
// Copyright 2015 Skyscanner Limited
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.
#import "MapViewController.h"
@implementation MapViewController {
CLLocationManager *locationManager;
Dixie *myDixie;
DixieProfileEntry *previousEntry;
NSArray *mockLocations;
CLLocation *actualMockLocation;
MKPointAnnotation *actualAnnocation;
}
- (void) viewDidLoad {
[super viewDidLoad];
// Setup location manager
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager requestWhenInUseAuthorization];
[locationManager setDistanceFilter:100];
[locationManager startUpdatingLocation];
self.mapView.showsUserLocation = NO;
// Set annotation
actualAnnocation = [[MKPointAnnotation alloc] init];
actualAnnocation.coordinate = locationManager.location.coordinate;
[self.mapView addAnnotation:actualAnnocation];
[self.mapView setCenterCoordinate:locationManager.location.coordinate animated:YES];
// Initialise Dixie
myDixie = [Dixie new];
// Create mock locations
CLLocation *Shanghai = [[CLLocation alloc] initWithLatitude:31.2 longitude:121.5];
CLLocation *Moscow = [[CLLocation alloc] initWithLatitude:55.75 longitude:37.616667];
CLLocation *Tokyo = [[CLLocation alloc] initWithLatitude:35.683333 longitude:139.683333];
CLLocation *MexicoCity = [[CLLocation alloc] initWithLatitude:19.433333 longitude:-99.133333];
CLLocation *NewYorkCity = [[CLLocation alloc] initWithLatitude:40.7127 longitude:-74.0059];
CLLocation *London = [[CLLocation alloc] initWithLatitude:51.507222 longitude:-0.1275];
CLLocation *RioDeJeneiro = [[CLLocation alloc] initWithLatitude:-22.908333 longitude:-43.196389];
CLLocation *LosAngeles = [[CLLocation alloc] initWithLatitude:34.05 longitude:-118.25];
mockLocations = [NSArray arrayWithObjects:Shanghai, Moscow, Tokyo, MexicoCity, NewYorkCity, London, RioDeJeneiro, LosAngeles, nil];
}
- (void) viewDidDisappear:(BOOL)animated {
// Revert Dixie when change a Tab - next time it will start with the original state
myDixie.RevertIt(previousEntry);
// Update map
[self.mapView setCenterCoordinate:locationManager.location.coordinate animated:YES];
actualAnnocation.coordinate = locationManager.location.coordinate;
[locationManager stopUpdatingLocation];
}
- (IBAction)dixieChangeLocation:(id)sender {
// Set mock location and annocation
actualMockLocation = [mockLocations objectAtIndex:(arc4random() % 8)];
[self.mapView setCenterCoordinate:actualMockLocation.coordinate animated:YES];
actualAnnocation.coordinate = actualMockLocation.coordinate;
// Create a block provider
DixieBaseChaosProvider *provider = [DixieBlockChaosProvider block:^(DixieBaseChaosProvider *chaosProvider,
id victim,
DixieCallEnvironment *environment) {
// chaos: return with the actualMockLocation
NSMutableArray* myArguments = [environment.arguments mutableCopy];
NSArray *argument = [NSArray arrayWithObjects:actualMockLocation, nil];
myArguments[1] = argument;
environment.arguments = myArguments;
// forward the chaos
[chaosProvider forwardChaosOf:victim environment:environment to:[DixieNonChaosProvider new]];
}];
// Change locationManager's didUpdateToLocation:fromLocation: method
DixieProfileEntry *entry = [DixieProfileEntry entry:[self class] selector:@selector(locationManager:didUpdateLocations:) chaosProvider:provider];
// Revert the previously set entry
if (previousEntry != nil) {
myDixie.RevertIt(previousEntry);
}
// Set and apply the Entry
myDixie.Profile(entry).Apply();
// Save the previous entry - to be able to revert it
previousEntry = entry;
// Apply the chaos
myDixie.Profile(entry).Apply();
// Call locationManager to make sure that the didUpdateLocations is called
[locationManager startUpdatingLocation];
}
- (IBAction)dixieRevertChanges:(id)sender {
// Revert the previousy entry
myDixie.RevertIt(previousEntry);
// Reset Map
[self.mapView setCenterCoordinate:locationManager.location.coordinate animated:YES];
actualAnnocation.coordinate = locationManager.location.coordinate;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"didUpdateLocations: %@", locations);
CLLocation *currentLocation = [locations objectAtIndex:0];
if (currentLocation != nil) {
// update the lon/lat labels
self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
// update the annotation
actualAnnocation.coordinate = currentLocation.coordinate;
[self.mapView setCenterCoordinate:currentLocation.coordinate animated:YES];
}
}
@end