Skip to content

Commit 768bf9d

Browse files
committed
Create Location Picker
1 parent cd2ddaf commit 768bf9d

File tree

9 files changed

+203
-92
lines changed

9 files changed

+203
-92
lines changed

ChatSDK.podspec

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,66 @@ Pod::Spec.new do |s|
1010

1111
s.platform = :ios, '8.0'
1212
s.requires_arc = true
13-
13+
1414
s.default_subspec = 'UI'
15-
15+
1616
s.subspec 'Core' do |core|
1717

18-
core.source_files = ['ChatSDKCore/Classes/**/*']
19-
core.resource_bundles = {
20-
'ChatCore' => ['ChatSDKCore/Assets/**/*']
21-
}
18+
core.source_files = ['ChatSDKCore/Classes/**/*']
19+
core.resource_bundles = {
20+
'ChatCore' => ['ChatSDKCore/Assets/**/*']
21+
}
22+
23+
core.dependency 'RXPromise', '~> 1.0'
24+
# core.dependency 'PromiseKit'
25+
core.dependency 'Reachability', '~> 3.0'
26+
core.dependency 'AFNetworking', '~>3.1.0'
2227

23-
core.dependency 'RXPromise', '~> 1.0'
24-
# core.dependency 'PromiseKit'
25-
core.dependency 'Reachability', '~> 3.0'
26-
core.dependency 'AFNetworking', '~>3.1.0'
28+
# core.frameworks = 'CoreLocation'
29+
core.frameworks = 'SafariServices'
2730

28-
# core.frameworks = 'CoreLocation'
29-
core.frameworks = 'SafariServices'
30-
3131
end
3232

3333
s.subspec 'CoreData' do |cd|
34-
35-
cd.source_files = ['ChatSDKCoreData/Classes/**/*']
34+
35+
cd.source_files = ['ChatSDKCoreData/Classes/**/*']
3636
cd.resource_bundles = {
3737
'ChatCoreData' => ['ChatSDKCoreData/Assets/**/*']
38-
}
39-
38+
}
39+
4040
cd.frameworks = 'UIKit', 'CoreData'
4141
cd.dependency 'ChatSDK/Core'
42-
42+
4343
end
4444

45-
s.subspec 'UI' do |ui|
46-
47-
ui.source_files = ['ChatSDKUI/Classes/**/*']
48-
ui.resource_bundles = {
49-
'ChatUI' => ['ChatSDKUI/Assets/**/*', 'ChatSDKUI/Interface/**/*']
50-
}
51-
52-
ui.dependency 'MBProgressHUD', '~> 1.0'
53-
ui.dependency 'VENTokenField', '~> 2.0'
54-
ui.dependency 'SDWebImage', '~> 4.0'
55-
ui.dependency 'StaticDataTableViewController', '~> 2.0'
56-
ui.dependency 'CountryPicker', '~> 1.0'
57-
ui.dependency 'DateTools', '~> 1.0'
58-
ui.dependency 'TOCropViewController', '~> 2.0'
59-
ui.dependency 'Hakawai', '~> 5.0.0'
60-
ui.dependency 'ChatSDKKeepLayout'
61-
ui.dependency 'Toast', '~>4.0.0'
62-
63-
ui.dependency 'ChatSDK/CoreData'
64-
65-
ui.frameworks = 'CoreLocation'
66-
45+
s.subspec 'UI' do |ui|
46+
47+
ui.source_files = ['ChatSDKUI/Classes/**/*']
48+
ui.resource_bundles = {
49+
'ChatUI' => ['ChatSDKUI/Assets/**/*', 'ChatSDKUI/Interface/**/*']
50+
}
51+
52+
ui.dependency 'MBProgressHUD', '~> 1.0'
53+
ui.dependency 'VENTokenField', '~> 2.0'
54+
ui.dependency 'SDWebImage', '~> 4.0'
55+
ui.dependency 'StaticDataTableViewController', '~> 2.0'
56+
ui.dependency 'CountryPicker', '~> 1.0'
57+
ui.dependency 'DateTools', '~> 1.0'
58+
ui.dependency 'TOCropViewController', '~> 2.0'
59+
ui.dependency 'Hakawai', '~> 5.0.0'
60+
ui.dependency 'ChatSDKKeepLayout'
61+
ui.dependency 'Toast', '~>4.0.0'
62+
ui.dependency 'Mapbox-iOS-SDK', '~> 3.7'
63+
64+
ui.dependency 'ChatSDK/CoreData'
65+
66+
ui.frameworks = 'CoreLocation'
67+
6768
end
6869

6970
# s.subspec 'FirebaseAdapter' do |fb|
7071
#
71-
# fb.source_files = ['ChatSDKFirebase/FirebaseNetworkAdapter/Classes/**/*']
72+
# fb.source_files = ['ChatSDKFirebase/FirebaseNetworkAdapter/Classes/**/*']
7273
#
7374
# fb.dependency 'Firebase/Core'
7475
# fb.dependency 'Firebase/Auth'
@@ -78,7 +79,7 @@ Pod::Spec.new do |s|
7879
#
7980
# # fb.vendored_frameworks = "ChatSDKFirebaseAdapter/Frameworks/FirebaseAdapter.framework"
8081
#
81-
# fb.dependency 'ChatSDK/Core'
82+
# fb.dependency 'ChatSDK/Core'
8283
#
8384
# end
8485

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//
22
// BSelectLocationAction.h
3-
// AFNetworking
3+
// ChatSDK
44
//
55
// Created by Ben on 12/11/17.
66
//
77

88
#import <Foundation/Foundation.h>
99
#import <ChatSDK/PAction.h>
1010
#import <CoreLocation/CoreLocation.h>
11+
#import <ChatSDK/BLocationPickerController.h>
1112

1213
@protocol CLLocationManagerDelegate;
1314

14-
@interface BSelectLocationAction : NSObject<PAction, CLLocationManagerDelegate> {
15-
CLLocationManager * _locationManager;
16-
RXPromise * _promise;
17-
}
15+
@interface BSelectLocationAction : NSObject<PAction, BLocationPickerControllerDelegate>
16+
17+
- (instancetype)initWithViewController:(UIViewController *)controller;
1818

1919
@end
Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
11
//
22
// BSelectLocationAction.m
3-
// AFNetworking
3+
// ChatSDK
44
//
55
// Created by Ben on 12/11/17.
66
//
77

88
#import "BSelectLocationAction.h"
99
#import <ChatSDK/ChatCore.h>
1010

11+
@interface BSelectLocationAction() {
12+
RXPromise * _promise;
13+
__weak UIViewController * _controller;
14+
UINavigationController * _navController;
15+
BLocationPickerController * _picker;
16+
}
17+
@end
18+
1119
@implementation BSelectLocationAction
1220

13-
-(RXPromise *) execute {
14-
15-
if(_promise) {
16-
return _promise;
17-
}
18-
else {
19-
_promise = [RXPromise new];
21+
- (instancetype)initWithViewController:(UIViewController *)controller {
22+
if ((self = [self init])) {
23+
_controller = controller;
2024
}
21-
22-
if(!_locationManager) {
23-
_locationManager = [[CLLocationManager alloc] init];
24-
25-
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
26-
[_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
27-
[_locationManager requestWhenInUseAuthorization];
28-
}
29-
30-
_locationManager.delegate = self;
31-
_locationManager.distanceFilter = kCLDistanceFilterNone;
32-
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
33-
}
34-
[_locationManager startUpdatingLocation];
35-
36-
return _promise;
25+
return self;
3726
}
3827

39-
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
28+
- (RXPromise *)execute {
29+
_promise = [RXPromise new];
4030

41-
[manager stopUpdatingLocation];
42-
if(_locationManager) {
43-
_locationManager = nil;
44-
45-
CLLocation * location = locations.lastObject;
46-
[_promise resolveWithResult:location];
31+
if (!_picker) {
32+
_picker = [[BLocationPickerController alloc] init];
33+
_picker.delegate = self;
4734
}
48-
if(_promise) {
49-
[_promise resolveWithResult: Nil];
35+
36+
if (!_navController) {
37+
_navController = [[UINavigationController alloc] initWithRootViewController:_picker];
5038
}
51-
_promise = Nil;
39+
40+
[_controller presentViewController:_navController animated:YES completion:nil];
41+
42+
return _promise;
43+
}
44+
45+
- (void)locationPickerController:(id)locationPicker didSelectLocation:(CLLocation *)location {
46+
[_promise resolveWithResult:location];
47+
}
48+
49+
- (void)locationPickerControllerDidCancel:(id)locationPicker {
50+
[_promise rejectWithReason:nil];
5251
}
5352

5453
@end
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
//
22
// BLocationOption.h
3-
// Pods
3+
// ChatSDK
44
//
55
// Created by Benjamin Smiley-andrews on 17/12/2016.
66
//
77
//
88

99
#import <ChatSDK/BChatOption.h>
1010

11-
@class RXPromise;
12-
@class BSelectLocationAction;
13-
14-
@interface BLocationChatOption : BChatOption {
15-
BSelectLocationAction * _action;
16-
}
11+
@interface BLocationChatOption : BChatOption
1712

1813
@end

ChatSDKUI/Classes/Components/ChatOptions/BLocationChatOption.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// BLocationOption.m
3-
// Pods
3+
// ChatSDK
44
//
55
// Created by Benjamin Smiley-andrews on 17/12/2016.
66
//
@@ -10,6 +10,13 @@
1010
#import <ChatSDK/ChatCore.h>
1111
#import <ChatSDK/ChatUI.h>
1212

13+
@class BLocationPickerControllerDelegate;
14+
15+
@interface BLocationChatOption() {
16+
BSelectLocationAction * action;
17+
}
18+
@end
19+
1320
@implementation BLocationChatOption
1421

1522
@synthesize parent;
@@ -23,10 +30,11 @@ -(NSString *) title {
2330
}
2431

2532
-(RXPromise *) execute {
26-
if(_action == Nil) {
27-
_action = [[BSelectLocationAction alloc] init];
33+
if (!action) {
34+
action = [[BSelectLocationAction alloc] initWithViewController:self.parent.delegate.currentViewController];
2835
}
29-
return [_action execute].thenOnMain(^id(id location) {
36+
37+
return [action execute].thenOnMain(^id(CLLocation * location) {
3038
return [self.parent.delegate sendLocationMessage:location];
3139
}, Nil);
3240
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// BLocationPickerController.h
3+
// ChatSDK
4+
//
5+
// Created by Pepe Becker on 24.04.18.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
#import <CoreLocation/CoreLocation.h>
10+
11+
@protocol BLocationPickerControllerDelegate <NSObject>
12+
13+
- (void)locationPickerController:(id)locationPicker didSelectLocation:(CLLocation *)location;
14+
- (void)locationPickerControllerDidCancel:(id)locationPicker;
15+
16+
@end
17+
18+
@interface BLocationPickerController : UIViewController
19+
20+
@property (nonatomic, weak) id <BLocationPickerControllerDelegate> delegate;
21+
22+
@end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// BLocationPickerController.m
3+
// ChatSDK
4+
//
5+
// Created by Pepe Becker on 24.04.18.
6+
//
7+
8+
#import "BLocationPickerController.h"
9+
@import Mapbox;
10+
11+
@interface BLocationPickerController() <MGLMapViewDelegate, UIGestureRecognizerDelegate>
12+
13+
@property (nonatomic) MGLMapView * mapView;
14+
@property (nonatomic) MGLPointAnnotation * annotation;
15+
16+
@end
17+
18+
@implementation BLocationPickerController
19+
20+
@synthesize delegate;
21+
22+
- (void)viewDidLoad {
23+
[super viewDidLoad];
24+
25+
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action: @selector(cancel)];
26+
27+
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action: @selector(done)];
28+
self.navigationItem.rightBarButtonItem.enabled = NO;
29+
30+
self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
31+
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
32+
[self.view addSubview:self.mapView];
33+
self.mapView.delegate = self;
34+
35+
self.mapView.showsUserLocation = YES;
36+
[self.mapView setUserTrackingMode:MGLUserTrackingModeFollow animated:YES];
37+
38+
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTab:)];
39+
tapRecognizer.delegate = self;
40+
[self.view addGestureRecognizer:tapRecognizer];
41+
}
42+
43+
- (void)didRecognizeTab:(UITapGestureRecognizer *)sender {
44+
CGPoint point = [sender locationInView:self.mapView];
45+
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
46+
47+
if (self.annotation) {
48+
[self.mapView removeAnnotation:self.annotation];
49+
}
50+
51+
self.annotation = [MGLPointAnnotation alloc];
52+
self.annotation.coordinate = coordinate;
53+
[self.mapView addAnnotation:self.annotation];
54+
55+
self.navigationItem.rightBarButtonItem.enabled = YES;
56+
}
57+
58+
- (void)cancel {
59+
[delegate locationPickerControllerDidCancel:self];
60+
[self dismiss];
61+
}
62+
63+
- (void)done {
64+
CLLocationCoordinate2D coords = self.annotation.coordinate;
65+
CLLocation * location = [[CLLocation alloc] initWithLatitude:coords.latitude longitude:coords.longitude];
66+
[delegate locationPickerController:self didSelectLocation:location];
67+
[self dismiss];
68+
}
69+
70+
- (void)dismiss {
71+
[self dismissViewControllerAnimated:YES completion:nil];
72+
}
73+
74+
#pragma mark - UIGestureRecognizerDelegate
75+
76+
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer {
77+
return YES;
78+
}
79+
80+
@end

0 commit comments

Comments
 (0)