JDFacade.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. //
  2. // JDFacade.m
  3. // kneet2
  4. //
  5. // Created by Created by Jason Lee on 10/1/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. @import ObjectiveC.runtime;
  9. #import "JDFacade.h"
  10. #import "JDUserDefaults.h"
  11. #import "CypherUtil.h"
  12. #import "JDUUID.h"
  13. #import "CustomLoadingView.h"
  14. #import "CustomImageView.h"
  15. #import "RequestHandler.h"
  16. //OpenSource
  17. #import "FLEXManager.h"
  18. #import "UIView+Toast.h"
  19. #import "JDProgressHUD.h"
  20. #import "Valet.h"
  21. #import "VALValet.h"
  22. #import "LoginViewController.h"
  23. #import "MainViewController.h"
  24. #import "DeviceModel.h"
  25. @interface JDFacade () {
  26. VALValet *_valet;
  27. JDProgressHUD *_HUD;
  28. NSTimer *_homehubBackgroundTimer;
  29. }
  30. @end
  31. @implementation JDFacade
  32. @synthesize tmpEmailId = _tmpEmailId;
  33. #pragma mark - Properties
  34. - (NSString *)deviceUUID {
  35. if (!_deviceUUID) {
  36. _deviceUUID = [[JDUUID UUIDHandler] getUUID];
  37. }
  38. return _deviceUUID;
  39. }
  40. - (NSString *)APNSToken {
  41. return _APNSToken ? _APNSToken : ksEmptyString;
  42. }
  43. - (NSString *)tmpEmailId {
  44. //FIXME : 다시 생각해바,
  45. NSString *enTmpEmailId = [self objectForKeyFromUserDefaults:USDEF_APP_TMP_EMAIL];
  46. _tmpEmailId = [CypherUtil AES128Decrypt:enTmpEmailId WithKey:[CommonUtil bundleIdentifier]];
  47. return _tmpEmailId ? _tmpEmailId : ksEmptyString;
  48. }
  49. - (void)setTmpEmailId:(NSString *)tmpEmailId {
  50. NSString *enTmpEmailId = [CypherUtil AES128Encrypt:tmpEmailId WithKey:[CommonUtil bundleIdentifier]];
  51. [self storeObjectToUserDefaults:enTmpEmailId forKey:USDEF_APP_TMP_EMAIL];
  52. }
  53. - (void)setCurrentOperation:(NSOperation *)currentOperation {
  54. [CustomLoadingView loadingView].operation = currentOperation;
  55. }
  56. - (NSString *)deviceHostName {
  57. return [self hostnameOfDevice];
  58. }
  59. - (void)setLoginUser:(LoginModel *)loginUser {
  60. _loginUser = loginUser;
  61. if (!_loginUser) {
  62. if (_homehubBackgroundTimer) {
  63. [_homehubBackgroundTimer invalidate];
  64. _homehubBackgroundTimer = nil;
  65. }
  66. }
  67. }
  68. - (NSString *)hostnameOfDevice {
  69. char baseHostName[256];
  70. int success = gethostname(baseHostName, 255);
  71. if (success != 0) return nil;
  72. baseHostName[255] = '\0';
  73. #if !TARGET_IPHONE_SIMULATOR
  74. return [NSString stringWithFormat:@"%s.local", baseHostName];
  75. #else
  76. return [NSString stringWithFormat:@"%s", baseHostName];
  77. #endif
  78. }
  79. #pragma mark - View Facade
  80. - (AppDelegate *)appDelegate {
  81. return (AppDelegate *)[UIApplication sharedApplication].delegate;
  82. }
  83. #pragma mark - 화면 제어
  84. - (UIViewController *)currentViewController {
  85. id appDelegate = [UIApplication sharedApplication].delegate;
  86. UIViewController *vc = (UINavigationController *)[[appDelegate window] rootViewController];
  87. if (vc.presentedViewController) {
  88. vc = vc.presentedViewController;
  89. if ([vc isKindOfClass:[UINavigationController class]]) {
  90. NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
  91. vc = [vcArray objectAtIndex:vcArray.count - 1];
  92. }
  93. return vc;
  94. }
  95. if (vc.presentingViewController && (vc.modalPresentationStyle == UIModalPresentationFullScreen)) {
  96. vc = vc.presentingViewController;
  97. }
  98. if ([vc isKindOfClass:[UINavigationController class]]) {
  99. NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
  100. vc = [vcArray objectAtIndex:vcArray.count - 1];
  101. }
  102. return vc;
  103. }
  104. - (id)viewControllerOnNaviationController:(Class)viewControllerClass {
  105. UIViewController *targetVc = nil;
  106. UINavigationController *navigationController = self.currentViewController.navigationController;
  107. if (!navigationController) {
  108. navigationController = (UINavigationController *)self.currentViewController.presentingViewController;
  109. }
  110. if ([navigationController isKindOfClass:[UINavigationController class]]) {
  111. for (UIViewController *vc in navigationController.viewControllers) {
  112. if ([vc isKindOfClass:viewControllerClass]) {
  113. targetVc = vc;
  114. break;
  115. }
  116. }
  117. }
  118. return targetVc;
  119. }
  120. - (id)viewControllerOnPresentingViewController:(UIViewController *)pvc viewControllerClass:(Class)viewControllerClass {
  121. UIViewController *vc = pvc.presentingViewController;
  122. if (![vc isKindOfClass:viewControllerClass]) {
  123. vc = [self viewControllerOnPresentingViewController:vc viewControllerClass:viewControllerClass];
  124. }
  125. return vc;
  126. }
  127. #pragma mark - KeychainItem Wrapper
  128. static NSString *const ksKeychainIdentifier = @"KNEET2ACCOUNT";
  129. static NSString *const ksKeychainArchiveData = @"_archiveData";
  130. - (void)storeObjectToKeychain:(id)object forKey:(NSString *)aKey {
  131. if (!_valet) {
  132. _valet = [[VALValet alloc] initWithIdentifier:ksKeychainIdentifier accessibility:VALAccessibilityWhenUnlockedThisDeviceOnly];
  133. }
  134. NSMutableDictionary *dict = nil;
  135. NSData *archiveData = [_valet objectForKey:ksKeychainArchiveData];
  136. if (archiveData) {
  137. dict = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
  138. }
  139. if (!dict) {
  140. dict = [[NSMutableDictionary alloc] init];
  141. }
  142. [dict setObject:object forKey:aKey];
  143. archiveData = [NSKeyedArchiver archivedDataWithRootObject:dict];
  144. [_valet setObject:archiveData forKey:ksKeychainArchiveData];
  145. }
  146. - (id)objectForKeyFromKeychain:(NSString *)aKey {
  147. if (!_valet) {
  148. _valet = [[VALValet alloc] initWithIdentifier:ksKeychainIdentifier accessibility:VALAccessibilityWhenUnlockedThisDeviceOnly];
  149. }
  150. NSMutableDictionary *dict = nil;
  151. NSData *archiveData = [_valet objectForKey:ksKeychainArchiveData];
  152. id obj = nil;
  153. if (archiveData) {
  154. dict = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
  155. obj = [dict objectForKey:aKey];
  156. }
  157. return obj;
  158. }
  159. - (void)removeObjectAtKeychain:(NSString *)aKey {
  160. if (!_valet) {
  161. _valet = [[VALValet alloc] initWithIdentifier:ksKeychainIdentifier accessibility:VALAccessibilityWhenUnlockedThisDeviceOnly];
  162. }
  163. NSMutableDictionary *dict = nil;
  164. NSData *archiveData = [_valet objectForKey:ksKeychainArchiveData];
  165. if (archiveData) {
  166. NSMutableDictionary *dict = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
  167. [dict removeObjectForKey:aKey];
  168. }
  169. archiveData = [NSKeyedArchiver archivedDataWithRootObject:dict];
  170. [_valet setObject:archiveData forKey:ksKeychainArchiveData];
  171. }
  172. #pragma mark - UserDefautls - Local Store
  173. - (void)storeObjectToUserDefaults:(id)object forKey:(NSString *)aKey {
  174. [[JDUserDefaults defaults] syncronizeObject:object forKey:aKey];
  175. }
  176. - (id)objectForKeyFromUserDefaults:(NSString *)aKey {
  177. return [[JDUserDefaults defaults] objectForKey:aKey];
  178. }
  179. #pragma mark - Alert & Loading
  180. - (void)loadIndicator:(BOOL)showIndicator allowUserInteraction:(BOOL)allowUserInteracton {
  181. //로딩시 터치 방지
  182. if(showIndicator) {
  183. dispatch_async(dispatch_get_main_queue(), ^{
  184. [[JDFacade facade].currentViewController.view endEditing:YES];
  185. UIView *topView = [CommonUtil topView];
  186. [[CustomLoadingView loadingView] showInView:topView];
  187. });
  188. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  189. } else {
  190. dispatch_async(dispatch_get_main_queue(), ^{
  191. [[CustomLoadingView loadingView] hide];
  192. });
  193. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  194. }
  195. }
  196. - (void)showLoadingWhileExecutingBlock:(dispatch_block_t)block completionHandler:(JDFacadeCompletionCallBackHandler)completion {
  197. [[CustomLoadingView loadingView] showInView:[CommonUtil topView] whileExecutingBlock:block completionHandler:completion];
  198. }
  199. #pragma mark - Alert UI
  200. - (void)alert:(NSString *)message {
  201. [self alertTitle:@"알림" message:message];
  202. }
  203. - (void)alertTitle:(NSString *)title message:(NSString *)message {
  204. [self.currentViewController.view endEditing:YES];
  205. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:title message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
  206. [alert show];
  207. }
  208. - (void)alert:(NSString *)message completionHander:(JDFacadeCompletionCallBackHandler)completion {
  209. [self alertTitle:@"알림" message:message completionHander:completion];
  210. }
  211. - (void)alertTitle:(NSString *)title message:(NSString *)message completionHander:(JDFacadeCompletionCallBackHandler)completion {
  212. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:title message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
  213. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  214. if (completion) {
  215. completion();
  216. }
  217. }];
  218. }
  219. - (void)confirm:(NSString *)message completion:(CustomAlertViewCallBackHandler)completion {
  220. [self confirmTitle:@"알림" message:message completion:completion];
  221. }
  222. - (void)confirmTitle:(NSString *)title message:(NSString *)message completion:(CustomAlertViewCallBackHandler)completion {
  223. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:title message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"취소"];
  224. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  225. if (completion) {
  226. completion(alertView, buttonIndex);
  227. }
  228. }];
  229. }
  230. - (void)retryAlert:(NSString *)message target:(id)target selector:(SEL)selector {
  231. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
  232. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  233. if (buttonIndex == 1) {
  234. if ([target respondsToSelector:selector]) {
  235. [target performSelector:selector withObject:nil afterDelay:0.0f];
  236. }
  237. }
  238. }];
  239. }
  240. - (void)retryAlert:(NSString *)message target:(id)target selector:(SEL)selector arguments:(id)arguments,... {
  241. va_list args;
  242. va_start(args, arguments);
  243. NSInteger i = 0;
  244. NSMutableArray *argsArray = [[NSMutableArray alloc] init];
  245. [argsArray addObject:arguments];
  246. while ((arguments = va_arg(args, id)) != nil) {//루프 - 입력된 메시지를 받아옴.
  247. [argsArray addObject:arguments];
  248. i++;
  249. }
  250. va_end(args);
  251. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
  252. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  253. if (buttonIndex == 0) {
  254. if (target && selector) {
  255. NSMethodSignature *sig = [target methodSignatureForSelector:selector];
  256. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
  257. NSInteger index = 2;
  258. [invocation setTarget:target]; // index 0 (hidden)
  259. [invocation setSelector:selector]; //index 1 (hidden)
  260. for (id argument in argsArray) {
  261. [invocation setArgument:(void *)&argument atIndex:index++];
  262. }
  263. [invocation invoke];
  264. }
  265. }
  266. }];
  267. }
  268. - (void)retryAlert:(NSString *)message completionHander:(JDFacadeCompletionCallBackHandler)handler {
  269. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
  270. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  271. if (buttonIndex == 1) {//재시도
  272. if (handler) {
  273. handler();
  274. }
  275. }
  276. }];
  277. }
  278. - (void)fireLocalNotification:(NSString *)message {
  279. NSLog(@"fireLocalNotification %@", message);
  280. NSDate *now = [NSDate date];
  281. UILocalNotification *localNotif = [[UILocalNotification alloc] init];
  282. localNotif.fireDate = now;
  283. NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
  284. localNotif.timeZone = timezone;
  285. localNotif.hasAction = YES;
  286. localNotif.alertBody = message;
  287. localNotif.alertAction = @"View";
  288. localNotif.soundName = UILocalNotificationDefaultSoundName;
  289. localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
  290. [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
  291. }
  292. - (void)toast:(NSString *)message {
  293. [[CommonUtil topView] makeToast:message];
  294. }
  295. #pragma mark - CheckBox && Radiobutton Status
  296. - (id)getRadioButtonStatus:(id)object {
  297. return objc_getAssociatedObject(object, ksCustomRadioButtonStatus);
  298. }
  299. - (void)setRadioButtonStatus:(id)status object:(id)object {
  300. objc_setAssociatedObject(object, ksCustomRadioButtonStatus, status, OBJC_ASSOCIATION_COPY_NONATOMIC); //for radio box
  301. }
  302. - (id)getCheckBoxStatus:(id)object {
  303. return objc_getAssociatedObject(object, ksCustomCheckBoxStatus);
  304. }
  305. - (void)setCheckBoxStatus:(id)status object:(id)object {
  306. objc_setAssociatedObject(object, ksCustomCheckBoxStatus, status, OBJC_ASSOCIATION_COPY_NONATOMIC); //for check box
  307. }
  308. #pragma mark - 화면이동 제어
  309. - (void)dismissModalStack:(BOOL)animated completion:(JDFacadeCompletionCallBackHandler)completion {
  310. UIViewController *vc = self.currentViewController;
  311. while (vc.presentingViewController) {
  312. vc = vc.presentingViewController;
  313. }
  314. CATransition *transition = [CATransition animation];
  315. transition.duration = kfTransitionRightDur;
  316. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  317. transition.type = kCATransitionMoveIn;
  318. transition.subtype = kCATransitionFromBottom;
  319. [vc.view.window.layer addAnimation:transition forKey:nil];
  320. [vc dismissViewControllerAnimated:NO completion:^{
  321. if (completion) {
  322. completion();
  323. }
  324. }];
  325. }
  326. - (void)presentViewControllerByPush:(UIViewController *)vc {
  327. CATransition *transition = [CATransition animation];
  328. transition.duration = kfTransitionRightDur;
  329. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  330. transition.type = kCATransitionPush;
  331. transition.subtype = kCATransitionFromRight;
  332. [self.currentViewController.view.window.layer addAnimation:transition forKey:nil];
  333. [self.currentViewController presentViewController:vc animated:NO completion:nil];
  334. }
  335. - (void)presentViewControllerByPush:(UIViewController *)vc pvc:(UIViewController *)pvc {
  336. CATransition *transition = [CATransition animation];
  337. transition.duration = kfTransitionRightDur;
  338. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  339. transition.type = kCATransitionPush;
  340. transition.subtype = kCATransitionFromRight;
  341. [pvc.view.window.layer addAnimation:transition forKey:nil];
  342. [pvc presentViewController:vc animated:NO completion:nil];
  343. }
  344. - (UIViewController *)presentedViewController:(UIViewController *)vc {
  345. UIViewController *presentedViewController = vc.presentedViewController;
  346. if (presentedViewController) {
  347. return [self presentedViewController:presentedViewController];
  348. }
  349. return vc;
  350. }
  351. - (void)dismissViewControllerByPush:(UIViewController *)vc {
  352. CATransition *transition = [CATransition animation];
  353. transition.duration = kfTransitionRightDur;
  354. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  355. transition.type = kCATransitionPush;
  356. transition.subtype = kCATransitionFromBottom;
  357. UIViewController *presentedViewController = [self presentedViewController:vc];
  358. [presentedViewController.view.window.layer addAnimation:transition forKey:nil];
  359. [vc dismissViewControllerAnimated:NO completion:nil];
  360. }
  361. - (void)dismissViewControllerByPush:(UIViewController *)vc completion:(JDFacadeCompletionCallBackHandler)completion {
  362. CATransition *transition = [CATransition animation];
  363. transition.duration = kfTransitionRightDur;
  364. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  365. transition.type = kCATransitionPush;
  366. transition.subtype = kCATransitionFromLeft;
  367. UIViewController *presentedViewController = [self presentedViewController:vc];
  368. [presentedViewController.view.window.layer addAnimation:transition forKey:nil];
  369. [vc dismissViewControllerAnimated:NO completion:^{
  370. if (completion) {
  371. completion();
  372. }
  373. }];
  374. }
  375. - (void)dismissAllViewControllers {
  376. UIViewController *vc = self.currentViewController;
  377. if (vc.presentingViewController) {
  378. [vc dismissViewControllerAnimated:NO completion:^{
  379. [self dismissAllViewControllers];
  380. }];
  381. }
  382. }
  383. #pragma mark - Biz Logic
  384. - (void)requestPollingHomeHubStatusInBackground {
  385. //schedul timer.
  386. if (!_homehubBackgroundTimer) {
  387. _homehubBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(requestPollingHomeHubStatusInBackground) userInfo:nil repeats:YES];
  388. }
  389. NSString *path = API_GET_DEVICE_HOMEHUB_STATUS;
  390. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  391. DeviceModel *homehub = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
  392. modelClass:[DeviceModel class] showLoadingView:YES];
  393. if (homehub) {
  394. //testing
  395. _isHomeHubOnlineState = !_isHomeHubOnlineState;
  396. [JDFacade facade].loginUser.homehubOnlineState = _isHomeHubOnlineState ? @"ON" : @"OFF";
  397. [_mainViewController updateHomeHubStatusToChildViewController];
  398. //testing end
  399. if ([JDFacade facade].loginUser.isHomehubOnline != homehub.isOnline) {//홈허브 상태 변경 체크
  400. return;
  401. // if (![[JDFacade facade].loginUser.homehubOnlineState isEqualToString:homehub.onlineState]) {
  402. //FIXME : real
  403. [JDFacade facade].loginUser.homehubOnlineState = homehub.onlineState;
  404. [_mainViewController updateHomeHubStatusToChildViewController];
  405. }
  406. [JDFacade facade].loginUser.homehubOnlineState = @"ON";//homehub.onlineState;
  407. #ifdef DEBUG_MODE
  408. NSLogInfo(@"==########== homehub state = %@ ==########==", [JDFacade facade].loginUser.homehubOnlineState);
  409. #endif
  410. }
  411. //TODO: update global - homehub state
  412. });
  413. }
  414. #pragma mark - UI Flow
  415. - (void)gotoLoginView {
  416. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  417. [JDFacade facade].appDelegate.window.rootViewController = vc;
  418. }
  419. //- (void)gotoLoginViewWithExpiring {
  420. // LoginViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  421. // [vc actionAfterLogout];
  422. //}
  423. //
  424. - (void)logout {
  425. LoginViewController *lvc = (LoginViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  426. [lvc requestLogout];
  427. }
  428. - (void)loadInvitationView {
  429. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"InvitationListViewController" storyboardName:@"Main"];
  430. [self.currentViewController presentViewController:vc animated:YES completion:nil];
  431. }
  432. //- (void)checkDefaultHome {
  433. // if (!self.loginUser) {//로그인이 안된 경우, 불필요함.
  434. // return;
  435. // }
  436. //
  437. // [self updateHomegrpListForLoginUser:^{
  438. // //1. 다른 홈이 있는지 확인 - 정렬순서대로 기본홈을 설정 - currentHome
  439. // if (self.loginUser.homegrpList && self.loginUser.homegrpList.count) {
  440. // self.loginHomeGroup = self.loginUser.homegrpList[0];
  441. // [self gotoWishMenu:KNMenuIdDashboard];
  442. // return;
  443. // }
  444. //
  445. // //2. 다른 홈이 없는 경우, - 홈만들기로 이동함.
  446. // CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?", @"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?") delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  447. // [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  448. // if (buttonIndex == 0) {//OK
  449. // [self gotoStartHome:NO];
  450. // } else {
  451. // [self logout];
  452. // }
  453. // }];
  454. // }];
  455. //}
  456. - (void)gotoStartHome:(BOOL)canGoBack {
  457. // StartHomeViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"StartHomeViewController" storyboardName:@"SignUp"];
  458. // vc.canGoBack = canGoBack;
  459. // [[JDFacade facade] presentViewControllerByPush:vc pvc:self.currentViewController];
  460. }
  461. - (void)gotoWishMenu:(KNMenuId)menuId {
  462. [self gotoWishMenu:menuId completion:nil];
  463. }
  464. - (void)gotoWishMenu:(KNMenuId)menuId completion:(JDFacadeCompletionCallBackHandler)completion {
  465. self.wishMenuId = menuId;
  466. switch (self.wishMenuId) {
  467. case KNMenuIdThings: {
  468. [_mainViewController loadThingsViewController];
  469. }
  470. break;
  471. case KNMenuIdRules: {
  472. [_mainViewController loadRulesViewController];
  473. }
  474. break;
  475. case KNMenuIdHomeMember: {
  476. [_mainViewController loadMembersViewController];
  477. }
  478. break;
  479. default:
  480. break;
  481. }
  482. }
  483. - (void)gotoHomeHubRegistration {
  484. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubStartViewController" storyboardName:@"HomeHub"];
  485. vc.providesPresentationContextTransitionStyle = YES;
  486. vc.definesPresentationContext = YES;
  487. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  488. [self.currentViewController presentViewController:vc animated:NO completion:nil];
  489. }
  490. //- (void)updateHomegrpListForLoginUser:(JDFacadeCompletionCallBackHandler)completion {
  491. //
  492. // NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID};
  493. //
  494. // dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  495. // HomeGroupListModel *homegrpList = [[RequestHandler handler] sendSyncGetRequestAPIPath:API_GET_HOMEGROUP parameters:parameter
  496. // modelClass:[HomeGroupListModel class] showLoadingView:YES];
  497. //
  498. //
  499. // // NSLog(@"%s\n %@", __PRETTY_FUNCTION__, homegrpList);
  500. // //홈그룹을 설정함.
  501. // self.loginUser.homegrpList = homegrpList && homegrpList.homegrpList ? homegrpList.homegrpList : nil;
  502. //
  503. // //로그인 홈을 지정함.
  504. // for (HomeGroupModel *homegrp in self.loginUser.homegrpList) {
  505. // if ([self.loginUser.homegrpId isEqualToString:homegrp.homegrpId]) {
  506. // self.loginHomeGroup = homegrp;
  507. // break;
  508. // }
  509. // }
  510. //
  511. //// [[LocationHandler handler] resetAllMonitoredRegions]; //현재 앱이 구동 중이면, 위치 센서를 다시 등록해야함.
  512. //// [[LocationHandler handler] registerGeofencingForHomeGroupIndex:0]; //모든 홈 그룹을 다시 모니터링 등록 - 루프
  513. //
  514. // if (completion) {
  515. // completion();
  516. // }
  517. // });
  518. //}
  519. - (void)updateLoginInfo:(JDFacadeCompletionCallBackHandler)completion {
  520. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, [JDFacade facade].deviceUUID);
  521. NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID,
  522. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  523. @"os_type": MOBILE_DEVICE_TYPE};
  524. NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
  525. // dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  526. // LoginModel *loginInfo = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:parameter
  527. // modelClass:[LoginModel class] showLoadingView:YES];
  528. // if (loginInfo) {
  529. // [JDFacade facade].loginUser = loginInfo;
  530. // }
  531. //
  532. // if (completion) {
  533. // completion();
  534. // }
  535. // });
  536. }
  537. - (void)alertLocationServiceDisabled {
  538. NSString *message = MSG_LOCATION_DISABLE;
  539. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소") message:message, nil];
  540. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  541. if (buttonIndex == 0) {//OK
  542. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  543. [[UIApplication sharedApplication]openURL:settingsURL];
  544. }
  545. }];
  546. }
  547. - (void)alertCameraPermissionDisabled {
  548. NSString *message = MSG_CAMERA_DISABLE;
  549. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소") message:message, nil];
  550. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  551. if (buttonIndex == 0) {//OK
  552. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  553. [[UIApplication sharedApplication]openURL:settingsURL];
  554. }
  555. }];
  556. }
  557. #pragma mark - Debug
  558. - (void)showFlex {
  559. [[FLEXManager sharedManager] showExplorer];
  560. }
  561. - (BOOL)redirectNSLog {
  562. // Create log file
  563. NSDate *date = [NSDate date];
  564. NSDateFormatter *df = [CommonUtil dateFormatter];
  565. [df setDateFormat:@"yyyyMMddHHmmss"];
  566. NSString *sdate = [df stringFromDate:date];
  567. NSString *logPath = [NSString stringWithFormat:@"%@/log_%@.txt", NSTemporaryDirectory(), sdate];
  568. [@"" writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  569. id fileHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
  570. if (!fileHandle) {
  571. NSLog(@"Opening log failed");
  572. return NO;
  573. }
  574. // Redirect stderr
  575. int err = dup2([fileHandle fileDescriptor], STDERR_FILENO);
  576. if (!err) {
  577. NSLog(@"Couldn't redirect stderr");
  578. return NO;
  579. }
  580. return YES;
  581. }
  582. #pragma mark - Singleton
  583. + (JDFacade *)facade {
  584. static JDFacade *sharedJSFacade = nil;
  585. static dispatch_once_t onceToken;
  586. dispatch_once(&onceToken, ^{
  587. sharedJSFacade = [[self alloc] init];
  588. });
  589. return sharedJSFacade;
  590. }
  591. - (id)init {
  592. if (self = [super init]) {
  593. }
  594. return self;
  595. }
  596. @end