123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- #import "SKMainViewController.h"
- #import "SKMainViewController+banner.h"
- #import "SKMainViewController+watch.h"
- #import "SKMainViewController+server.h"
- #import "SKMainViewController+location.h"
- #import "SKMainViewController+map.h"
- #import "SKMainViewController+realpos.h"
- #import "SKMainViewController+version.h"
- #import "DataManager+address.h"
- #import "SKHistoryPathViewController.h"
- #import "UserDataHelper.h"
- #import <MessageUI/MessageUI.h>
- #import <UMMobClick/MobClick.h>
- #import "SKChatViewController.h"
- #import "ARMacros.h"
- #import "SKBabyDetailInfoViewController.h"
- @interface SKMainViewController()<MFMessageComposeViewControllerDelegate, MKMapViewDelegate, CLLocationManagerDelegate>
- @property(nonatomic,strong) IBOutlet UIButton *showAllBtn;
- @end
- @implementation SKMainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self.navigationController setNavigationBarHidden:YES];
- [self addNavigationView];
-
- @weakify(self);
- self.showAllBtn.hidden = YES;
- self.mIsFirstLoad = YES;
- self.mRealPosMap = [[NSMutableDictionary alloc] init];
- [self initLocationManager];
- [self requestWhenInUseAuthorization:^(BOOL isOK) {
- [weak_self startLocation];
- }];
-
- [self initNetwork];
- [self initMap];
- [self showLastLoaction];
- [self initStep];
- [self initWeather];
- [self initWatchInfo];
- [self initRPStackView];
- [self initPageFlow];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:YES];
-
- [self.mMapView setDelegate:self];
- [self.mMapView setShowsUserLocation:YES];
- [self.locationManager setDelegate:self];
-
- [self checkCurrentChild];
- [self refreshChildAnnotations];
-
- //超过5分钟,定位
- [self autoDealPositionAction];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [self.mMapView setDelegate:nil];
- [self.mMapView setShowsUserLocation:NO];
- [self.locationManager setDelegate:nil];
- }
- - (void)dealloc {
- [self.mRealPosMap removeAllObjects];
- [self stopRPTimer];
- }
- - (void)onRefreshChildsDevices {
- NSInteger index = [[DataManager shared] getSelectChildIndex];
- NSInteger max = [[DataManager shared] getChildCount];
- [self refreshPageFlow:index max:max];
- [self refreshChild:index];
-
- if(self.mIsFirstLoad) {
- [self checkNewAppVersion];
-
- //超过5分钟,定位
- [self autoDealPositionAction];
-
- NSString*type = [[NSUserDefaults standardUserDefaults] objectForKey:@"TABBAR_SELECT_CHAT"];
- if ([type isEqualToString:@"1"]) {
- [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"TABBAR_SELECT_CHAT"];
- [[NSUserDefaults standardUserDefaults] synchronize];
-
- UIViewController* controller = [UIApplication sharedApplication].keyWindow.rootViewController;
- UITabBarController* tabbar = (UITabBarController*)controller;
- if (tabbar) {
- [tabbar setSelectedIndex:1];
- }
- }
- }
-
- self.mIsFirstLoad = NO;
- }
- - (void)onReceiveLocation:(BaseCoordinatesModel*)model {
- BOOL isSelect = [[DataManager shared].mSelectChildModel isChildSelected:model.cid];
- if (isSelect) {
- [self refreshChild:self.mPageFlowView.currentPageIndex];
- }
- [self receiveLocation:model.cid];
- }
- - (void)onReceiveWeather {
- [self refreshView];
- }
- #pragma mark - Change Child
- - (void)onChangeChild:(NSInteger)index {
- [self refreshChild:index];
-
- if (self.mPageFlowView && self.mPageFlowView.currentPageIndex != index) {
- [self.mPageFlowView scrollToPage:index >= 0 ? index:0];
- }
- }
- - (void)refreshChild:(NSInteger)index {
- [self refreshMask:index];
- [self refreshBanner:index];
- [self refreshBannerMatch:index];
-
- //刷新Annotations
- [self refreshChildAnnotations];
- [self refreshAccuracyCircle];
-
- //刷新天气、信号、电量、名称//
- [self refreshView];
-
- [self checkNewWatchVersion];
- }
- - (void)refreshView {
- ChildModel* model = [DataManager shared].mSelectChildModel;
- [self.mNavigationView refreshChildView];
-
- //刷新天气, 信号, 计步
- [self refreshWeather];
- [self refreshSignals];
- [self refreshBattery];
- [self refreshStep];
-
- [self initRPStackViewMatch];
- [self.mWatchInfoView setHidden:![model.device isBind]];
- }
- - (void)getAddressDetail:(NSInteger)index {
- ChildModel* model = [[DataManager shared] getIndexChild:index];
- [[self class] reverseGeo:[model.lastLocation getCoordinates] callback:^(NSArray<CLPlacemark *> * _Nullable placemarks) {
- if (placemarks.count > 0) {
- //Get nearby address
- CLPlacemark *placemark = [placemarks objectAtIndex:0];
- NSString *adressAll = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
- NSString *adressPoint = placemark.name;
- [[DataManager shared] saveAddressDetail:[model.lastLocation getCoordinates] addressPoint:adressPoint addressAll:adressAll];
- [self setAddressBanner:index addressPoint:adressPoint addressAll:adressAll];
- }
- }];
- }
- #pragma mark - Cell Mode Select
- - (void)didSelectCellBtn:(NSInteger)index {
- switch (index) {
- case 0: //电话
- [self showCall];
- break;
- case 1: //私聊
- [self showChat];
- break;
- case 2: //导航
- [self showNavigation];
- break;
- case 3: //监听
- [self showMonitor];
- break;
- case 4: //重新绑定
- [self showBind];
- break;
- case 5: //历史轨迹
- [self showHistoryPath];
- break;
- default:
- break;
- }
- }
- - (void)showCall {
- if([EUtil isInForbiddenTime:[[DataManager shared] getSelectDevice]]) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"In.Forbidden.Time.No.Call", nil) preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Confirm", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- }]];
- [self presentViewController:alert animated:YES completion:nil];
- return;
- }
-
- NSString *phoneNum = [DataManager shared].mSelectChildModel.phoneNumber;
- if (phoneNum == nil || [phoneNum length] <= 0 ) {
- [EasyTextView showInfoText:NSLocalizedString(@"Not.Bind.Mobile.Number", nil)];//@"当前未绑定手机号"];
- return;
- }
-
- //E164
- NSString* nationCode = [DataManager shared].mSelectChildModel.areaCode;
- phoneNum = [EUtil parsePhoneNumber:phoneNum countryCode:nationCode];
-
- phoneNum = [@"tel://" stringByAppendingString:phoneNum];
- if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:phoneNum]]) {
- [EasyTextView showInfoText:NSLocalizedString(@"Call.With.Wrong.Simulator", nil)];//@"呼叫号码不正确或当前设备不支持"];
- } else {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNum] options:@{} completionHandler:^(BOOL success) {
- }];
- }
- }
- //私聊
- - (void)showChat {
- [MobClick event:@"ClickChatPrivate"];
- NSString* cid = [DataManager shared].mSelectChildModel.cid;
- [[DataManager shared] findSessionModel:cid callback:^(SessionModel* model) {
- if (!model)
- return;
- SKChatViewController *controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"SKChatVC"];
- controller.mSessionModel = model;
- [self pushViewController:controller animated:YES];
- }];
- }
- - (void)showNavigation {
- CLLocationCoordinate2D location = [[DataManager shared].mSelectChildModel.lastLocation getCoordinates];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Route.To.Baby.Loc", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Baidu.Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]){
- [EasyTextView showInfoText:[NSString stringWithFormat: NSLocalizedString(@"Not.Install.Hint", nil), NSLocalizedString(@"Baidu.Map", nil)]];
- } else {
- MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
- MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil]];
- [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
- launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
- MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
- }
- }]];
- [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Gaode.Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
- [EasyTextView showInfoText:[NSString stringWithFormat: NSLocalizedString(@"Not.Install.Hint", nil), NSLocalizedString(@"Gaode.Map", nil)]];
- } else {
- NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"SikeyComm",@"SikeyComm",location.latitude, location.longitude];
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
- }];
- }
- }]];
- [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- }]];
- [self presentViewController:alert animated:YES completion:nil];
- }
- - (void)showBind {
- [MobClick event:@"ClickAddBindAgain"];
- [DataManager shared].bindOptions = BO_REBIND;
- SKBabyDetailInfoViewController *controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"BabyDetailInfo"];
- controller.opType = 3;
- controller.childModel = [DataManager shared].mSelectChildModel;
- [self pushViewController:controller animated:YES];
- }
- - (void)showHistoryPath {
- [MobClick event:@"ClickHistoryPathFromMain"];
- SKHistoryPathViewController *mHistoryPathView = [[self storyboard] instantiateViewControllerWithIdentifier:@"HistoryPathVC"];
- [self pushViewController:mHistoryPathView animated:YES];
- }
- - (void)showMonitor {
- DeviceModel* model = [[DataManager shared] getSelectDevice];
- if([EUtil isInForbiddenTime:model]) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"In.Forbidden.Time.No.Monitor", nil) preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Confirm", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- }]];
- [self presentViewController:alert animated:YES completion:nil];
- return;
- }
-
- //如果已经关机
- if(model.startup == 0) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Watch.Turn.Off.Title", nil) message:NSLocalizedString(@"Watch.Turn.Off.Mes", nil) preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Confirm", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- }]];
- [self presentViewController:alert animated:YES completion:nil];
- return;
- }
-
- if(![model isBind]) {
- [EasyTextView showInfoText:NSLocalizedString(@"Not.Bind.Watch", nil)];//@"当前未绑定手表"];
- } else {
- [MobClick event:@"ClickMonitorFromMain"];
- [self sendMonitorRequest];
- }
- }
- - (NSString*)getMonitorMessage:(NSString *)imei {
- NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
- [dateFormatter setDateStyle:NSDateFormatterShortStyle];
- [dateFormatter setDateFormat:@"yyMMddHHmm"];
-
- NSDate *date = [NSDate date];
- NSString *dateStr = [dateFormatter stringFromDate:date];
-
- NSString *ret = [NSString stringWithFormat:@"%@%@", imei, dateStr];
- return ret;
- }
- - (void)sendMonitorRequest {
- NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
- param[@"cid"] = [DataManager shared].mSelectChildModel.cid;
-
- [self ShowHUBWithText:NSLocalizedString(@"Request.Updating", nil)];
- [ERequest httpRequest:param httpURL:URL_MONITOR httpMethod:@"POST" onSuccess:^(NSDictionary *result){
- [self HiddenHUB];
- if([ERequest isSuccessWithResult:result]) {
- [EasyTextView showSuccessText:NSLocalizedString(@"Send.Monitor.Request.Done", nil)];//@"监听已发送"];
- } else {
- [EasyTextView showErrorText:result[@"message"]];
- }
- }onFailure:^(NSError *error) {
- [self HiddenHUB];
- [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
- }];
- }
- - (void)sendMessage:(NSString*)msg toNumber:(NSString*)number {
- BOOL canSendSMS = [MFMessageComposeViewController canSendText];
- if (canSendSMS) {
- MFMessageComposeViewController *msgVC = [[MFMessageComposeViewController alloc] init];
- msgVC.messageComposeDelegate = self;
- msgVC.body = msg;
- msgVC.recipients = [NSArray arrayWithObject:number];
- [self presentViewController:msgVC animated:YES completion:nil];
- }
- }
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
- switch (result) {
- case MessageComposeResultCancelled:
- break;
- case MessageComposeResultSent:
- [EasyTextView showSuccessText:NSLocalizedString(@"Send.SMS.Done", nil)];//@"短信已发送"];
- break;
- case MessageComposeResultFailed:
- [EasyTextView showInfoText:NSLocalizedString(@"Send.SMS.Failed", nil)];//@"短信发送失败!"];
- break;
- default:
- break;
- }
- controller.messageComposeDelegate = nil;
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- @end
|