SKMainViewController.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #import "SKMainViewController.h"
  2. #import "SKMainViewController+banner.h"
  3. #import "SKMainViewController+watch.h"
  4. #import "SKMainViewController+server.h"
  5. #import "SKMainViewController+location.h"
  6. #import "SKMainViewController+map.h"
  7. #import "SKMainViewController+realpos.h"
  8. #import "SKMainViewController+version.h"
  9. #import "DataManager+address.h"
  10. #import "SKHistoryPathViewController.h"
  11. #import "UserDataHelper.h"
  12. #import <MessageUI/MessageUI.h>
  13. #import <UMMobClick/MobClick.h>
  14. #import "SKChatViewController.h"
  15. #import "ARMacros.h"
  16. #import "SKBabyDetailInfoViewController.h"
  17. @interface SKMainViewController()<MFMessageComposeViewControllerDelegate, MKMapViewDelegate, CLLocationManagerDelegate>
  18. @property(nonatomic,strong) IBOutlet UIButton *showAllBtn;
  19. @end
  20. @implementation SKMainViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self.navigationController setNavigationBarHidden:YES];
  24. [self addNavigationView];
  25. @weakify(self);
  26. self.showAllBtn.hidden = YES;
  27. self.mIsFirstLoad = YES;
  28. self.mRealPosMap = [[NSMutableDictionary alloc] init];
  29. [self initLocationManager];
  30. [self requestWhenInUseAuthorization:^(BOOL isOK) {
  31. [weak_self startLocation];
  32. }];
  33. [self initNetwork];
  34. [self initMap];
  35. [self showLastLoaction];
  36. [self initStep];
  37. [self initWeather];
  38. [self initWatchInfo];
  39. [self initRPStackView];
  40. [self initPageFlow];
  41. }
  42. - (void)viewWillAppear:(BOOL)animated {
  43. [super viewWillAppear:animated];
  44. [self.navigationController setNavigationBarHidden:YES];
  45. [self.mMapView setDelegate:self];
  46. [self.mMapView setShowsUserLocation:YES];
  47. [self.locationManager setDelegate:self];
  48. [self checkCurrentChild];
  49. [self refreshChildAnnotations];
  50. //超过5分钟,定位
  51. [self autoDealPositionAction];
  52. }
  53. - (void)viewWillDisappear:(BOOL)animated {
  54. [super viewWillDisappear:animated];
  55. [self.mMapView setDelegate:nil];
  56. [self.mMapView setShowsUserLocation:NO];
  57. [self.locationManager setDelegate:nil];
  58. }
  59. - (void)dealloc {
  60. [self.mRealPosMap removeAllObjects];
  61. [self stopRPTimer];
  62. }
  63. - (void)onRefreshChildsDevices {
  64. NSInteger index = [[DataManager shared] getSelectChildIndex];
  65. NSInteger max = [[DataManager shared] getChildCount];
  66. [self refreshPageFlow:index max:max];
  67. [self refreshChild:index];
  68. if(self.mIsFirstLoad) {
  69. [self checkNewAppVersion];
  70. //超过5分钟,定位
  71. [self autoDealPositionAction];
  72. NSString*type = [[NSUserDefaults standardUserDefaults] objectForKey:@"TABBAR_SELECT_CHAT"];
  73. if ([type isEqualToString:@"1"]) {
  74. [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"TABBAR_SELECT_CHAT"];
  75. [[NSUserDefaults standardUserDefaults] synchronize];
  76. UIViewController* controller = [UIApplication sharedApplication].keyWindow.rootViewController;
  77. UITabBarController* tabbar = (UITabBarController*)controller;
  78. if (tabbar) {
  79. [tabbar setSelectedIndex:1];
  80. }
  81. }
  82. }
  83. self.mIsFirstLoad = NO;
  84. }
  85. - (void)onReceiveLocation:(BaseCoordinatesModel*)model {
  86. BOOL isSelect = [[DataManager shared].mSelectChildModel isChildSelected:model.cid];
  87. if (isSelect) {
  88. [self refreshChild:self.mPageFlowView.currentPageIndex];
  89. }
  90. [self receiveLocation:model.cid];
  91. }
  92. - (void)onReceiveWeather {
  93. [self refreshView];
  94. }
  95. #pragma mark - Change Child
  96. - (void)onChangeChild:(NSInteger)index {
  97. [self refreshChild:index];
  98. if (self.mPageFlowView && self.mPageFlowView.currentPageIndex != index) {
  99. [self.mPageFlowView scrollToPage:index >= 0 ? index:0];
  100. }
  101. }
  102. - (void)refreshChild:(NSInteger)index {
  103. [self refreshMask:index];
  104. [self refreshBanner:index];
  105. [self refreshBannerMatch:index];
  106. //刷新Annotations
  107. [self refreshChildAnnotations];
  108. [self refreshAccuracyCircle];
  109. //刷新天气、信号、电量、名称//
  110. [self refreshView];
  111. [self checkNewWatchVersion];
  112. }
  113. - (void)refreshView {
  114. ChildModel* model = [DataManager shared].mSelectChildModel;
  115. [self.mNavigationView refreshChildView];
  116. //刷新天气, 信号, 计步
  117. [self refreshWeather];
  118. [self refreshSignals];
  119. [self refreshBattery];
  120. [self refreshStep];
  121. [self initRPStackViewMatch];
  122. [self.mWatchInfoView setHidden:![model.device isBind]];
  123. }
  124. - (void)getAddressDetail:(NSInteger)index {
  125. ChildModel* model = [[DataManager shared] getIndexChild:index];
  126. [[self class] reverseGeo:[model.lastLocation getCoordinates] callback:^(NSArray<CLPlacemark *> * _Nullable placemarks) {
  127. if (placemarks.count > 0) {
  128. //Get nearby address
  129. CLPlacemark *placemark = [placemarks objectAtIndex:0];
  130. NSString *adressAll = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
  131. NSString *adressPoint = placemark.name;
  132. [[DataManager shared] saveAddressDetail:[model.lastLocation getCoordinates] addressPoint:adressPoint addressAll:adressAll];
  133. [self setAddressBanner:index addressPoint:adressPoint addressAll:adressAll];
  134. }
  135. }];
  136. }
  137. #pragma mark - Cell Mode Select
  138. - (void)didSelectCellBtn:(NSInteger)index {
  139. switch (index) {
  140. case 0: //电话
  141. [self showCall];
  142. break;
  143. case 1: //私聊
  144. [self showChat];
  145. break;
  146. case 2: //导航
  147. [self showNavigation];
  148. break;
  149. case 3: //监听
  150. [self showMonitor];
  151. break;
  152. case 4: //重新绑定
  153. [self showBind];
  154. break;
  155. case 5: //历史轨迹
  156. [self showHistoryPath];
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. - (void)showCall {
  163. if([EUtil isInForbiddenTime:[[DataManager shared] getSelectDevice]]) {
  164. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"In.Forbidden.Time.No.Call", nil) preferredStyle:UIAlertControllerStyleAlert];
  165. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Confirm", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  166. }]];
  167. [self presentViewController:alert animated:YES completion:nil];
  168. return;
  169. }
  170. NSString *phoneNum = [DataManager shared].mSelectChildModel.phoneNumber;
  171. if (phoneNum == nil || [phoneNum length] <= 0 ) {
  172. [EasyTextView showInfoText:NSLocalizedString(@"Not.Bind.Mobile.Number", nil)];//@"当前未绑定手机号"];
  173. return;
  174. }
  175. //E164
  176. NSString* nationCode = [DataManager shared].mSelectChildModel.areaCode;
  177. phoneNum = [EUtil parsePhoneNumber:phoneNum countryCode:nationCode];
  178. phoneNum = [@"tel://" stringByAppendingString:phoneNum];
  179. if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:phoneNum]]) {
  180. [EasyTextView showInfoText:NSLocalizedString(@"Call.With.Wrong.Simulator", nil)];//@"呼叫号码不正确或当前设备不支持"];
  181. } else {
  182. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNum] options:@{} completionHandler:^(BOOL success) {
  183. }];
  184. }
  185. }
  186. //私聊
  187. - (void)showChat {
  188. [MobClick event:@"ClickChatPrivate"];
  189. NSString* cid = [DataManager shared].mSelectChildModel.cid;
  190. [[DataManager shared] findSessionModel:cid callback:^(SessionModel* model) {
  191. if (!model)
  192. return;
  193. SKChatViewController *controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"SKChatVC"];
  194. controller.mSessionModel = model;
  195. [self pushViewController:controller animated:YES];
  196. }];
  197. }
  198. - (void)showNavigation {
  199. CLLocationCoordinate2D location = [[DataManager shared].mSelectChildModel.lastLocation getCoordinates];
  200. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Route.To.Baby.Loc", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  201. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Baidu.Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  202. if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]){
  203. [EasyTextView showInfoText:[NSString stringWithFormat: NSLocalizedString(@"Not.Install.Hint", nil), NSLocalizedString(@"Baidu.Map", nil)]];
  204. } else {
  205. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  206. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil]];
  207. [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
  208. launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
  209. MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
  210. }
  211. }]];
  212. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Gaode.Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  213. if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
  214. [EasyTextView showInfoText:[NSString stringWithFormat: NSLocalizedString(@"Not.Install.Hint", nil), NSLocalizedString(@"Gaode.Map", nil)]];
  215. } else {
  216. NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"SikeyComm",@"SikeyComm",location.latitude, location.longitude];
  217. urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  218. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
  219. }];
  220. }
  221. }]];
  222. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  223. }]];
  224. [self presentViewController:alert animated:YES completion:nil];
  225. }
  226. - (void)showBind {
  227. [MobClick event:@"ClickAddBindAgain"];
  228. [DataManager shared].bindOptions = BO_REBIND;
  229. SKBabyDetailInfoViewController *controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"BabyDetailInfo"];
  230. controller.opType = 3;
  231. controller.childModel = [DataManager shared].mSelectChildModel;
  232. [self pushViewController:controller animated:YES];
  233. }
  234. - (void)showHistoryPath {
  235. [MobClick event:@"ClickHistoryPathFromMain"];
  236. SKHistoryPathViewController *mHistoryPathView = [[self storyboard] instantiateViewControllerWithIdentifier:@"HistoryPathVC"];
  237. [self pushViewController:mHistoryPathView animated:YES];
  238. }
  239. - (void)showMonitor {
  240. DeviceModel* model = [[DataManager shared] getSelectDevice];
  241. if([EUtil isInForbiddenTime:model]) {
  242. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"In.Forbidden.Time.No.Monitor", nil) preferredStyle:UIAlertControllerStyleAlert];
  243. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Confirm", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  244. }]];
  245. [self presentViewController:alert animated:YES completion:nil];
  246. return;
  247. }
  248. //如果已经关机
  249. if(model.startup == 0) {
  250. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Watch.Turn.Off.Title", nil) message:NSLocalizedString(@"Watch.Turn.Off.Mes", nil) preferredStyle:UIAlertControllerStyleAlert];
  251. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"SK.Confirm", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  252. }]];
  253. [self presentViewController:alert animated:YES completion:nil];
  254. return;
  255. }
  256. if(![model isBind]) {
  257. [EasyTextView showInfoText:NSLocalizedString(@"Not.Bind.Watch", nil)];//@"当前未绑定手表"];
  258. } else {
  259. [MobClick event:@"ClickMonitorFromMain"];
  260. [self sendMonitorRequest];
  261. }
  262. }
  263. - (NSString*)getMonitorMessage:(NSString *)imei {
  264. NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
  265. [dateFormatter setDateStyle:NSDateFormatterShortStyle];
  266. [dateFormatter setDateFormat:@"yyMMddHHmm"];
  267. NSDate *date = [NSDate date];
  268. NSString *dateStr = [dateFormatter stringFromDate:date];
  269. NSString *ret = [NSString stringWithFormat:@"%@%@", imei, dateStr];
  270. return ret;
  271. }
  272. - (void)sendMonitorRequest {
  273. NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
  274. param[@"cid"] = [DataManager shared].mSelectChildModel.cid;
  275. [self ShowHUBWithText:NSLocalizedString(@"Request.Updating", nil)];
  276. [ERequest httpRequest:param httpURL:URL_MONITOR httpMethod:@"POST" onSuccess:^(NSDictionary *result){
  277. [self HiddenHUB];
  278. if([ERequest isSuccessWithResult:result]) {
  279. [EasyTextView showSuccessText:NSLocalizedString(@"Send.Monitor.Request.Done", nil)];//@"监听已发送"];
  280. } else {
  281. [EasyTextView showErrorText:result[@"message"]];
  282. }
  283. }onFailure:^(NSError *error) {
  284. [self HiddenHUB];
  285. [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
  286. }];
  287. }
  288. - (void)sendMessage:(NSString*)msg toNumber:(NSString*)number {
  289. BOOL canSendSMS = [MFMessageComposeViewController canSendText];
  290. if (canSendSMS) {
  291. MFMessageComposeViewController *msgVC = [[MFMessageComposeViewController alloc] init];
  292. msgVC.messageComposeDelegate = self;
  293. msgVC.body = msg;
  294. msgVC.recipients = [NSArray arrayWithObject:number];
  295. [self presentViewController:msgVC animated:YES completion:nil];
  296. }
  297. }
  298. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
  299. switch (result) {
  300. case MessageComposeResultCancelled:
  301. break;
  302. case MessageComposeResultSent:
  303. [EasyTextView showSuccessText:NSLocalizedString(@"Send.SMS.Done", nil)];//@"短信已发送"];
  304. break;
  305. case MessageComposeResultFailed:
  306. [EasyTextView showInfoText:NSLocalizedString(@"Send.SMS.Failed", nil)];//@"短信发送失败!"];
  307. break;
  308. default:
  309. break;
  310. }
  311. controller.messageComposeDelegate = nil;
  312. [self dismissViewControllerAnimated:YES completion:nil];
  313. }
  314. @end