JDFacade.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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:10 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. if ([JDFacade facade].loginUser.isHomehubOnline != homehub.isOnline) {//홈허브 상태 변경 체크
  395. return;
  396. // if (![[JDFacade facade].loginUser.homehubOnlineState isEqualToString:homehub.onlineState]) {
  397. //FIXME : real
  398. [JDFacade facade].loginUser.homehubOnlineState = homehub.onlineState;
  399. [_mainViewController updateHomeHubStatusToChildViewController];
  400. }
  401. [JDFacade facade].loginUser.homehubOnlineState = @"ON";//homehub.onlineState;
  402. #ifdef DEBUG_MODE
  403. NSLogInfo(@"==########== homehub state = %@ ==########==", [JDFacade facade].loginUser.homehubOnlineState);
  404. #endif
  405. }
  406. //TODO: update global - homehub state
  407. });
  408. }
  409. #pragma mark - UI Flow
  410. - (void)gotoLoginView {
  411. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  412. [JDFacade facade].appDelegate.window.rootViewController = vc;
  413. }
  414. //- (void)gotoLoginViewWithExpiring {
  415. // LoginViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  416. // [vc actionAfterLogout];
  417. //}
  418. //
  419. - (void)logout {
  420. LoginViewController *lvc = (LoginViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  421. [lvc requestLogout];
  422. }
  423. - (void)loadInvitationView {
  424. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"InvitationListViewController" storyboardName:@"Main"];
  425. [self.currentViewController presentViewController:vc animated:YES completion:nil];
  426. }
  427. //- (void)checkDefaultHome {
  428. // if (!self.loginUser) {//로그인이 안된 경우, 불필요함.
  429. // return;
  430. // }
  431. //
  432. // [self updateHomegrpListForLoginUser:^{
  433. // //1. 다른 홈이 있는지 확인 - 정렬순서대로 기본홈을 설정 - currentHome
  434. // if (self.loginUser.homegrpList && self.loginUser.homegrpList.count) {
  435. // self.loginHomeGroup = self.loginUser.homegrpList[0];
  436. // [self gotoWishMenu:KNMenuIdDashboard];
  437. // return;
  438. // }
  439. //
  440. // //2. 다른 홈이 없는 경우, - 홈만들기로 이동함.
  441. // CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?", @"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?") delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  442. // [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  443. // if (buttonIndex == 0) {//OK
  444. // [self gotoStartHome:NO];
  445. // } else {
  446. // [self logout];
  447. // }
  448. // }];
  449. // }];
  450. //}
  451. - (void)gotoStartHome:(BOOL)canGoBack {
  452. // StartHomeViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"StartHomeViewController" storyboardName:@"SignUp"];
  453. // vc.canGoBack = canGoBack;
  454. // [[JDFacade facade] presentViewControllerByPush:vc pvc:self.currentViewController];
  455. }
  456. - (void)gotoWishMenu:(KNMenuId)menuId {
  457. [self gotoWishMenu:menuId completion:nil];
  458. }
  459. - (void)gotoWishMenu:(KNMenuId)menuId completion:(JDFacadeCompletionCallBackHandler)completion {
  460. self.wishMenuId = menuId;
  461. switch (self.wishMenuId) {
  462. case KNMenuIdThings: {
  463. [_mainViewController loadThingsViewController];
  464. }
  465. break;
  466. case KNMenuIdRules: {
  467. [_mainViewController loadRulesViewController];
  468. }
  469. break;
  470. case KNMenuIdHomeMember: {
  471. [_mainViewController loadMembersViewController];
  472. }
  473. break;
  474. default:
  475. break;
  476. }
  477. }
  478. - (void)gotoHomeHubRegistration {
  479. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubStartViewController" storyboardName:@"HomeHub"];
  480. vc.providesPresentationContextTransitionStyle = YES;
  481. vc.definesPresentationContext = YES;
  482. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  483. [self.currentViewController presentViewController:vc animated:NO completion:nil];
  484. }
  485. //- (void)updateHomegrpListForLoginUser:(JDFacadeCompletionCallBackHandler)completion {
  486. //
  487. // NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID};
  488. //
  489. // dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  490. // HomeGroupListModel *homegrpList = [[RequestHandler handler] sendSyncGetRequestAPIPath:API_GET_HOMEGROUP parameters:parameter
  491. // modelClass:[HomeGroupListModel class] showLoadingView:YES];
  492. //
  493. //
  494. // // NSLog(@"%s\n %@", __PRETTY_FUNCTION__, homegrpList);
  495. // //홈그룹을 설정함.
  496. // self.loginUser.homegrpList = homegrpList && homegrpList.homegrpList ? homegrpList.homegrpList : nil;
  497. //
  498. // //로그인 홈을 지정함.
  499. // for (HomeGroupModel *homegrp in self.loginUser.homegrpList) {
  500. // if ([self.loginUser.homegrpId isEqualToString:homegrp.homegrpId]) {
  501. // self.loginHomeGroup = homegrp;
  502. // break;
  503. // }
  504. // }
  505. //
  506. //// [[LocationHandler handler] resetAllMonitoredRegions]; //현재 앱이 구동 중이면, 위치 센서를 다시 등록해야함.
  507. //// [[LocationHandler handler] registerGeofencingForHomeGroupIndex:0]; //모든 홈 그룹을 다시 모니터링 등록 - 루프
  508. //
  509. // if (completion) {
  510. // completion();
  511. // }
  512. // });
  513. //}
  514. - (void)updateLoginInfo:(JDFacadeCompletionCallBackHandler)completion {
  515. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, [JDFacade facade].deviceUUID);
  516. NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID,
  517. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  518. @"os_type": MOBILE_DEVICE_TYPE};
  519. NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
  520. // dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  521. // LoginModel *loginInfo = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:parameter
  522. // modelClass:[LoginModel class] showLoadingView:YES];
  523. // if (loginInfo) {
  524. // [JDFacade facade].loginUser = loginInfo;
  525. // }
  526. //
  527. // if (completion) {
  528. // completion();
  529. // }
  530. // });
  531. }
  532. - (void)alertLocationServiceDisabled {
  533. NSString *message = MSG_LOCATION_DISABLE;
  534. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소") message:message, nil];
  535. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  536. if (buttonIndex == 0) {//OK
  537. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  538. [[UIApplication sharedApplication]openURL:settingsURL];
  539. }
  540. }];
  541. }
  542. - (void)alertCameraPermissionDisabled {
  543. NSString *message = MSG_CAMERA_DISABLE;
  544. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소") message:message, nil];
  545. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  546. if (buttonIndex == 0) {//OK
  547. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  548. [[UIApplication sharedApplication]openURL:settingsURL];
  549. }
  550. }];
  551. }
  552. #pragma mark - Debug
  553. - (void)showFlex {
  554. [[FLEXManager sharedManager] showExplorer];
  555. }
  556. - (BOOL)redirectNSLog {
  557. // Create log file
  558. NSDate *date = [NSDate date];
  559. NSDateFormatter *df = [CommonUtil dateFormatter];
  560. [df setDateFormat:@"yyyyMMddHHmmss"];
  561. NSString *sdate = [df stringFromDate:date];
  562. NSString *logPath = [NSString stringWithFormat:@"%@/log_%@.txt", NSTemporaryDirectory(), sdate];
  563. [@"" writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  564. id fileHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
  565. if (!fileHandle) {
  566. NSLog(@"Opening log failed");
  567. return NO;
  568. }
  569. // Redirect stderr
  570. int err = dup2([fileHandle fileDescriptor], STDERR_FILENO);
  571. if (!err) {
  572. NSLog(@"Couldn't redirect stderr");
  573. return NO;
  574. }
  575. return YES;
  576. }
  577. #pragma mark - Singleton
  578. + (JDFacade *)facade {
  579. static JDFacade *sharedJSFacade = nil;
  580. static dispatch_once_t onceToken;
  581. dispatch_once(&onceToken, ^{
  582. sharedJSFacade = [[self alloc] init];
  583. });
  584. return sharedJSFacade;
  585. }
  586. - (id)init {
  587. if (self = [super init]) {
  588. }
  589. return self;
  590. }
  591. @end