Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Location Picker #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions ChatSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,66 @@ Pod::Spec.new do |s|

s.platform = :ios, '8.0'
s.requires_arc = true

s.default_subspec = 'UI'

s.subspec 'Core' do |core|

core.source_files = ['ChatSDKCore/Classes/**/*']
core.resource_bundles = {
'ChatCore' => ['ChatSDKCore/Assets/**/*']
}
core.source_files = ['ChatSDKCore/Classes/**/*']
core.resource_bundles = {
'ChatCore' => ['ChatSDKCore/Assets/**/*']
}

core.dependency 'RXPromise', '~> 1.0'
# core.dependency 'PromiseKit'
core.dependency 'Reachability', '~> 3.0'
core.dependency 'AFNetworking', '~>3.1.0'

core.dependency 'RXPromise', '~> 1.0'
# core.dependency 'PromiseKit'
core.dependency 'Reachability', '~> 3.0'
core.dependency 'AFNetworking', '~>3.1.0'
# core.frameworks = 'CoreLocation'
core.frameworks = 'SafariServices'

# core.frameworks = 'CoreLocation'
core.frameworks = 'SafariServices'

end

s.subspec 'CoreData' do |cd|
cd.source_files = ['ChatSDKCoreData/Classes/**/*']

cd.source_files = ['ChatSDKCoreData/Classes/**/*']
cd.resource_bundles = {
'ChatCoreData' => ['ChatSDKCoreData/Assets/**/*']
}
}

cd.frameworks = 'UIKit', 'CoreData'
cd.dependency 'ChatSDK/Core'

end

s.subspec 'UI' do |ui|

ui.source_files = ['ChatSDKUI/Classes/**/*']
ui.resource_bundles = {
'ChatUI' => ['ChatSDKUI/Assets/**/*', 'ChatSDKUI/Interface/**/*']
}

ui.dependency 'MBProgressHUD', '~> 1.0'
ui.dependency 'VENTokenField', '~> 2.0'
ui.dependency 'SDWebImage', '~> 4.0'
ui.dependency 'StaticDataTableViewController', '~> 2.0'
ui.dependency 'CountryPicker', '~> 1.0'
ui.dependency 'DateTools', '~> 1.0'
ui.dependency 'TOCropViewController', '~> 2.0'
ui.dependency 'Hakawai', '~> 5.0.0'
ui.dependency 'ChatSDKKeepLayout'
ui.dependency 'Toast', '~>4.0.0'

ui.dependency 'ChatSDK/CoreData'

ui.frameworks = 'CoreLocation'

s.subspec 'UI' do |ui|

ui.source_files = ['ChatSDKUI/Classes/**/*']
ui.resource_bundles = {
'ChatUI' => ['ChatSDKUI/Assets/**/*', 'ChatSDKUI/Interface/**/*']
}

ui.dependency 'MBProgressHUD', '~> 1.0'
ui.dependency 'VENTokenField', '~> 2.0'
ui.dependency 'SDWebImage', '~> 4.0'
ui.dependency 'StaticDataTableViewController', '~> 2.0'
ui.dependency 'CountryPicker', '~> 1.0'
ui.dependency 'DateTools', '~> 1.0'
ui.dependency 'TOCropViewController', '~> 2.0'
ui.dependency 'Hakawai', '~> 5.0.0'
ui.dependency 'ChatSDKKeepLayout'
ui.dependency 'Toast', '~>4.0.0'
ui.dependency 'Mapbox-iOS-SDK', '~> 3.7'

ui.dependency 'ChatSDK/CoreData'

ui.frameworks = 'CoreLocation'

end

# s.subspec 'FirebaseAdapter' do |fb|
#
# fb.source_files = ['ChatSDKFirebase/FirebaseNetworkAdapter/Classes/**/*']
# fb.source_files = ['ChatSDKFirebase/FirebaseNetworkAdapter/Classes/**/*']
#
# fb.dependency 'Firebase/Core'
# fb.dependency 'Firebase/Auth'
Expand All @@ -78,7 +79,7 @@ Pod::Spec.new do |s|
#
# # fb.vendored_frameworks = "ChatSDKFirebaseAdapter/Frameworks/FirebaseAdapter.framework"
#
# fb.dependency 'ChatSDK/Core'
# fb.dependency 'ChatSDK/Core'
#
# end

Expand Down
10 changes: 5 additions & 5 deletions ChatSDKUI/Classes/Actions/BSelectLocationAction.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//
// BSelectLocationAction.h
// AFNetworking
// ChatSDK
//
// Created by Ben on 12/11/17.
//

#import <Foundation/Foundation.h>
#import <ChatSDK/PAction.h>
#import <CoreLocation/CoreLocation.h>
#import <ChatSDK/BLocationPickerController.h>

@protocol CLLocationManagerDelegate;

@interface BSelectLocationAction : NSObject<PAction, CLLocationManagerDelegate> {
CLLocationManager * _locationManager;
RXPromise * _promise;
}
@interface BSelectLocationAction : NSObject<PAction, BLocationPickerControllerDelegate>

- (instancetype)initWithViewController:(UIViewController *)controller;

@end
67 changes: 33 additions & 34 deletions ChatSDKUI/Classes/Actions/BSelectLocationAction.m
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
//
// BSelectLocationAction.m
// AFNetworking
// ChatSDK
//
// Created by Ben on 12/11/17.
//

#import "BSelectLocationAction.h"
#import <ChatSDK/ChatCore.h>

@interface BSelectLocationAction() {
RXPromise * _promise;
__weak UIViewController * _controller;
UINavigationController * _navController;
BLocationPickerController * _picker;
}
@end

@implementation BSelectLocationAction

-(RXPromise *) execute {

if(_promise) {
return _promise;
}
else {
_promise = [RXPromise new];
- (instancetype)initWithViewController:(UIViewController *)controller {
if ((self = [self init])) {
_controller = controller;
}

if(!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
[_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}

_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
[_locationManager startUpdatingLocation];

return _promise;
return self;
}

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

[manager stopUpdatingLocation];
if(_locationManager) {
_locationManager = nil;

CLLocation * location = locations.lastObject;
[_promise resolveWithResult:location];
if (!_picker) {
_picker = [[BLocationPickerController alloc] init];
_picker.delegate = self;
}
if(_promise) {
[_promise resolveWithResult: Nil];

if (!_navController) {
_navController = [[UINavigationController alloc] initWithRootViewController:_picker];
}
_promise = Nil;

[_controller presentViewController:_navController animated:YES completion:nil];

return _promise;
}

- (void)locationPickerController:(id)locationPicker didSelectLocation:(CLLocation *)location {
[_promise resolveWithResult:location];
}

- (void)locationPickerControllerDidCancel:(id)locationPicker {
[_promise rejectWithReason:nil];
}

@end
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
//
// BLocationOption.h
// Pods
// ChatSDK
//
// Created by Benjamin Smiley-andrews on 17/12/2016.
//
//

#import <ChatSDK/BChatOption.h>

@class RXPromise;
@class BSelectLocationAction;

@interface BLocationChatOption : BChatOption {
BSelectLocationAction * _action;
}
@interface BLocationChatOption : BChatOption

@end
16 changes: 12 additions & 4 deletions ChatSDKUI/Classes/Components/ChatOptions/BLocationChatOption.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// BLocationOption.m
// Pods
// ChatSDK
//
// Created by Benjamin Smiley-andrews on 17/12/2016.
//
Expand All @@ -10,6 +10,13 @@
#import <ChatSDK/ChatCore.h>
#import <ChatSDK/ChatUI.h>

@class BLocationPickerControllerDelegate;

@interface BLocationChatOption() {
BSelectLocationAction * action;
}
@end

@implementation BLocationChatOption

@synthesize parent;
Expand All @@ -23,10 +30,11 @@ -(NSString *) title {
}

-(RXPromise *) execute {
if(_action == Nil) {
_action = [[BSelectLocationAction alloc] init];
if (!action) {
action = [[BSelectLocationAction alloc] initWithViewController:self.parent.delegate.currentViewController];
}
return [_action execute].thenOnMain(^id(id location) {

return [action execute].thenOnMain(^id(CLLocation * location) {
return [self.parent.delegate sendLocationMessage:location];
}, Nil);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// BLocationPickerController.h
// ChatSDK
//
// Created by Pepe Becker on 24.04.18.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@protocol BLocationPickerControllerDelegate <NSObject>

- (void)locationPickerController:(id)locationPicker didSelectLocation:(CLLocation *)location;
- (void)locationPickerControllerDidCancel:(id)locationPicker;

@end

@interface BLocationPickerController : UIViewController

@property (nonatomic, weak) id <BLocationPickerControllerDelegate> delegate;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// BLocationPickerController.m
// ChatSDK
//
// Created by Pepe Becker on 24.04.18.
//

#import "BLocationPickerController.h"
@import Mapbox;

@interface BLocationPickerController() <MGLMapViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic) MGLMapView * mapView;
@property (nonatomic) MGLPointAnnotation * annotation;

@end

@implementation BLocationPickerController

@synthesize delegate;

- (void)viewDidLoad {
[super viewDidLoad];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action: @selector(cancel)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action: @selector(done)];
self.navigationItem.rightBarButtonItem.enabled = NO;

self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.mapView];
self.mapView.delegate = self;

self.mapView.showsUserLocation = YES;
[self.mapView setUserTrackingMode:MGLUserTrackingModeFollow animated:YES];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTab:)];
tapRecognizer.delegate = self;
[self.view addGestureRecognizer:tapRecognizer];
}

- (void)didRecognizeTab:(UITapGestureRecognizer *)sender {
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

if (self.annotation) {
[self.mapView removeAnnotation:self.annotation];
}

self.annotation = [MGLPointAnnotation alloc];
self.annotation.coordinate = coordinate;
[self.mapView addAnnotation:self.annotation];

self.navigationItem.rightBarButtonItem.enabled = YES;
}

- (void)cancel {
[delegate locationPickerControllerDidCancel:self];
[self dismiss];
}

- (void)done {
CLLocationCoordinate2D coords = self.annotation.coordinate;
CLLocation * location = [[CLLocation alloc] initWithLatitude:coords.latitude longitude:coords.longitude];
[delegate locationPickerController:self didSelectLocation:location];
[self dismiss];
}

- (void)dismiss {
[self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

@end
Loading