JDFacade.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 "KeychainItemWrapper.h"
  13. #import "JDUUID.h"
  14. #import "CustomLoadingView.h"
  15. #import "CustomImageView.h"
  16. #import "CustomAlertView.h"
  17. #import "RequestHandler.h"
  18. //OpenSource
  19. #import "FLEXManager.h"
  20. #import "UIView+Toast.h"
  21. @interface JDFacade () {
  22. KeychainItemWrapper *_keychainItemWrapper;
  23. }
  24. @end
  25. @implementation JDFacade
  26. #pragma mark - Properties
  27. - (NSString *)deviceUUID {
  28. if (!_deviceUUID) {
  29. _deviceUUID = [[JDUUID UUIDHandler] getUUID];
  30. }
  31. return _deviceUUID;
  32. }
  33. - (NSString *)APNSToken {
  34. return _APNSToken ? _APNSToken : ksEmptyString;
  35. }
  36. #pragma mark - View Facade
  37. - (AppDelegate *)appDelegate {
  38. return (AppDelegate *)[UIApplication sharedApplication].delegate;
  39. }
  40. #pragma mark - 화면 제어
  41. - (UIViewController *)currentViewController {
  42. id appDelegate = [UIApplication sharedApplication].delegate;
  43. UIViewController *vc = (UINavigationController *)[[appDelegate window] rootViewController];
  44. if (vc.presentedViewController) {
  45. vc = vc.presentedViewController;
  46. if ([vc isKindOfClass:[UINavigationController class]]) {
  47. NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
  48. vc = [vcArray objectAtIndex:vcArray.count - 1];
  49. }
  50. return vc;
  51. }
  52. if (vc.presentingViewController && (vc.modalPresentationStyle == UIModalPresentationFullScreen)) {
  53. vc = vc.presentingViewController;
  54. }
  55. if ([vc isKindOfClass:[UINavigationController class]]) {
  56. NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
  57. vc = [vcArray objectAtIndex:vcArray.count - 1];
  58. }
  59. return vc;
  60. }
  61. - (id)viewControllerOnNaviationController:(Class)viewControllerClass {
  62. UIViewController *targetVc = nil;
  63. UINavigationController *navigationController = self.currentViewController.navigationController;
  64. if (!navigationController) {
  65. navigationController = (UINavigationController *)self.currentViewController.presentingViewController;
  66. }
  67. if ([navigationController isKindOfClass:[UINavigationController class]]) {
  68. for (UIViewController *vc in navigationController.viewControllers) {
  69. if ([vc isKindOfClass:viewControllerClass]) {
  70. targetVc = vc;
  71. break;
  72. }
  73. }
  74. }
  75. return targetVc;
  76. }
  77. - (id)viewControllerOnPresentingViewController:(UIViewController *)pvc viewControllerClass:(Class)viewControllerClass {
  78. UIViewController *vc = pvc.presentingViewController;
  79. if (![vc isKindOfClass:viewControllerClass]) {
  80. vc = [self viewControllerOnPresentingViewController:vc viewControllerClass:viewControllerClass];
  81. }
  82. return vc;
  83. }
  84. #pragma mark - UserDefautls - Local Store
  85. - (void)storeObjectToUserDefaults:(id)object forKey:(NSString *)aKey {
  86. [[JDUserDefaults defaults] syncronizeObject:object forKey:aKey];
  87. }
  88. - (id)objectForKeyFromUserDefaults:(NSString *)aKey {
  89. return [[JDUserDefaults defaults] objectForKey:aKey];
  90. }
  91. #pragma mark - Alert & Loading
  92. - (void)loadIndicator:(BOOL)showIndicator allowUserInteraction:(BOOL)allowUserInteracton {
  93. UIWindow *mainWindow = [self appDelegate].window;
  94. //로딩시 터치 방지
  95. if(showIndicator) {
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. [[JDFacade facade].currentViewController.view endEditing:YES];
  98. UIView *topView = [[mainWindow subviews] lastObject];
  99. [CustomLoadingView showWithView:topView];
  100. mainWindow.userInteractionEnabled = allowUserInteracton;
  101. });
  102. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  103. } else {
  104. dispatch_async(dispatch_get_main_queue(), ^{
  105. [CustomLoadingView hide];
  106. mainWindow.userInteractionEnabled = allowUserInteracton;
  107. });
  108. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  109. }
  110. }
  111. - (void)alert:(NSString *)message {
  112. [self.currentViewController.view endEditing:YES];
  113. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
  114. [alert show];
  115. }
  116. - (void)retryAlert:(NSString *)message target:(id)target selector:(SEL)selector {
  117. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
  118. [alert show];
  119. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  120. if (buttonIndex == 1) {
  121. if ([target respondsToSelector:selector]) {
  122. [target performSelector:selector withObject:nil afterDelay:0.0f];
  123. }
  124. }
  125. }];
  126. }
  127. - (void)alert:(NSString *)message completionHander:(completionCallBackHandler)handler {
  128. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
  129. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  130. if (handler) {
  131. handler();
  132. }
  133. }];
  134. }
  135. - (void)fireLocalNotification:(NSString *)message {
  136. NSLog(@"fireLocalNotification %@", message);
  137. NSDate *now = [NSDate date];
  138. UILocalNotification *localNotif = [[UILocalNotification alloc] init];
  139. localNotif.fireDate = now;
  140. NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
  141. localNotif.timeZone = timezone;
  142. localNotif.hasAction = YES;
  143. localNotif.alertBody = message;
  144. localNotif.alertAction = @"View";
  145. localNotif.soundName = UILocalNotificationDefaultSoundName;
  146. localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
  147. [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
  148. }
  149. - (void)toast:(NSString *)message {
  150. [[CommonUtil topView] makeToast:message];
  151. }
  152. #pragma mark - 화면이동 제어
  153. - (id)getRadioButtonStatus:(id)object {
  154. return objc_getAssociatedObject(object, ksCustomRadioButtonStatus);
  155. }
  156. - (void)setRadioButtonStatus:(id)status object:(id)object {
  157. objc_setAssociatedObject(object, ksCustomRadioButtonStatus, status, OBJC_ASSOCIATION_COPY_NONATOMIC); //for radio box
  158. }
  159. - (id)getCheckBoxStatus:(id)object {
  160. return objc_getAssociatedObject(object, ksCustomCheckBoxStatus);
  161. }
  162. - (void)setCheckBoxStatus:(id)status object:(id)object {
  163. objc_setAssociatedObject(object, ksCustomCheckBoxStatus, status, OBJC_ASSOCIATION_COPY_NONATOMIC); //for check box
  164. }
  165. - (void)presentViewControllerByPush:(UIViewController *)vc {
  166. CATransition *transition = [CATransition animation];
  167. transition.duration = kfTransitionRightDur;
  168. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  169. transition.type = kCATransitionPush;
  170. transition.subtype = kCATransitionFromRight;
  171. [self.currentViewController.view.window.layer addAnimation:transition forKey:nil];
  172. [self.currentViewController presentViewController:vc animated:NO completion:nil];
  173. }
  174. - (void)presentViewControllerByPush:(UIViewController *)vc pvc:(UIViewController *)pvc {
  175. CATransition *transition = [CATransition animation];
  176. transition.duration = kfTransitionRightDur;
  177. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  178. transition.type = kCATransitionPush;
  179. transition.subtype = kCATransitionFromRight;
  180. [pvc.view.window.layer addAnimation:transition forKey:nil];
  181. [pvc presentViewController:vc animated:NO completion:nil];
  182. }
  183. - (UIViewController *)presentedViewController:(UIViewController *)vc {
  184. UIViewController *presentedViewController = vc.presentedViewController;
  185. if (presentedViewController) {
  186. return [self presentedViewController:presentedViewController];
  187. }
  188. return vc;
  189. }
  190. - (void)dismissViewControllerByPush:(UIViewController *)vc {
  191. CATransition *transition = [CATransition animation];
  192. transition.duration = kfTransitionRightDur;
  193. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  194. transition.type = kCATransitionPush;
  195. transition.subtype = kCATransitionFromLeft;
  196. UIViewController *presentedViewController = [self presentedViewController:vc];
  197. [presentedViewController.view.window.layer addAnimation:transition forKey:nil];
  198. [vc dismissViewControllerAnimated:NO completion:nil];
  199. }
  200. - (void)dismissViewControllerByPush:(UIViewController *)vc completion:(completionCallBackHandler)completion {
  201. CATransition *transition = [CATransition animation];
  202. transition.duration = kfTransitionRightDur;
  203. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  204. transition.type = kCATransitionPush;
  205. transition.subtype = kCATransitionFromLeft;
  206. UIViewController *presentedViewController = [self presentedViewController:vc];
  207. [presentedViewController.view.window.layer addAnimation:transition forKey:nil];
  208. [vc dismissViewControllerAnimated:NO completion:^{
  209. if (completion) {
  210. completion();
  211. }
  212. }];
  213. }
  214. - (void)dismissAllViewControllers {
  215. UIViewController *vc = self.currentViewController;
  216. if (vc.presentingViewController) {
  217. [vc dismissViewControllerAnimated:NO completion:^{
  218. [self dismissAllViewControllers];
  219. }];
  220. }
  221. }
  222. #pragma mark - UI Flow
  223. - (void)gotoLoginView {
  224. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  225. [JDFacade facade].appDelegate.window.rootViewController = vc;
  226. }
  227. //- (void)gotoLoginViewWithExpiring {
  228. // LoginViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  229. // [vc actionAfterLogout];
  230. //}
  231. //
  232. //- (void)logout {
  233. // LoginViewController *lvc = (LoginViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  234. // [lvc requestLogout];
  235. //}
  236. //
  237. //- (void)loadInvitationView {
  238. // UIViewController *pvc = [JDFacade facade].currentViewController.presentedViewController;
  239. // if (!pvc) {
  240. // pvc = [JDFacade facade].currentViewController;
  241. // }
  242. //
  243. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"InvitationListViewController" storyboardName:@"Main"];
  244. // [[JDFacade facade] presentViewControllerByPush:vc pvc:pvc];
  245. //}
  246. //
  247. //- (void)checkDefaultHome {
  248. // if (!self.loginUser) {//로그인이 안된 경우, 불필요함.
  249. // return;
  250. // }
  251. //
  252. // [self updateHomegrpListForLoginUser:^{
  253. // //1. 다른 홈이 있는지 확인 - 정렬순서대로 기본홈을 설정 - currentHome
  254. // if (self.loginUser.homegrpList && self.loginUser.homegrpList.count) {
  255. // self.loginHomeGroup = self.loginUser.homegrpList[0];
  256. // [self gotoWishMenu:KNMenuIdDashboard];
  257. // return;
  258. // }
  259. //
  260. // //2. 다른 홈이 없는 경우, - 홈만들기로 이동함.
  261. // CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?", @"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?") delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  262. // [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  263. // if (buttonIndex == 0) {//OK
  264. // [self gotoStartHome:NO];
  265. // } else {
  266. // [self logout];
  267. // }
  268. // }];
  269. // }];
  270. //}
  271. - (void)gotoStartHome:(BOOL)canGoBack {
  272. // StartHomeViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"StartHomeViewController" storyboardName:@"SignUp"];
  273. // vc.canGoBack = canGoBack;
  274. // [[JDFacade facade] presentViewControllerByPush:vc pvc:self.currentViewController];
  275. }
  276. - (void)gotoWishMenu:(KNMenuId)menuId {
  277. [self gotoWishMenu:menuId completion:nil];
  278. }
  279. - (void)gotoWishMenu:(KNMenuId)menuId completion:(completionCallBackHandler)completion {
  280. self.wishMenuId = menuId;
  281. UIViewController *vc = self.appDelegate.window.rootViewController;
  282. if (vc.presentedViewController || (vc.presentingViewController && (vc.modalPresentationStyle == UIModalPresentationFullScreen))){
  283. [vc dismissViewControllerAnimated:YES completion:nil];
  284. }
  285. // MenuViewController *mvc = [JDFacade facade].menuViewController;
  286. // [mvc gotoWishMenu:self.wishMenuId completion:completion];
  287. }
  288. - (void)updateHomegrpListForLoginUser:(completionCallBackHandler)completion {
  289. NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID};
  290. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  291. HomeGroupListModel *homegrpList = [[RequestHandler handler] sendSyncGetRequestAPIPath:API_GET_HOMEGROUP parameters:parameter
  292. modelClass:[HomeGroupListModel class] showLoadingView:YES];
  293. // NSLog(@"%s\n %@", __PRETTY_FUNCTION__, homegrpList);
  294. //홈그룹을 설정함.
  295. self.loginUser.homegrpList = homegrpList && homegrpList.homegrpList ? homegrpList.homegrpList : nil;
  296. //로그인 홈을 지정함.
  297. for (HomeGroupModel *homegrp in self.loginUser.homegrpList) {
  298. if ([self.loginUser.homegrpId isEqualToString:homegrp.homegrpId]) {
  299. self.loginHomeGroup = homegrp;
  300. break;
  301. }
  302. }
  303. // [[LocationHandler handler] resetAllMonitoredRegions]; //현재 앱이 구동 중이면, 위치 센서를 다시 등록해야함.
  304. // [[LocationHandler handler] registerGeofencingForHomeGroupIndex:0]; //모든 홈 그룹을 다시 모니터링 등록 - 루프
  305. if (completion) {
  306. completion();
  307. }
  308. });
  309. }
  310. - (void)updateLoginInfo:(completionCallBackHandler)completion {
  311. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, [JDFacade facade].deviceUUID);
  312. NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID,
  313. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  314. @"os_type": MOBILE_DEVICE_TYPE};
  315. NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
  316. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  317. LoginModel *loginInfo = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:parameter
  318. modelClass:[LoginModel class] showLoadingView:YES];
  319. if (loginInfo) {
  320. [JDFacade facade].loginUser = loginInfo;
  321. }
  322. if (completion) {
  323. completion();
  324. }
  325. });
  326. }
  327. - (void)alertLocationServiceDisabled {
  328. NSString *message = MSG_LOCATION_DISABLE;
  329. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소") message:message, nil];
  330. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  331. if (buttonIndex == 0) {//OK
  332. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  333. [[UIApplication sharedApplication]openURL:settingsURL];
  334. }
  335. }];
  336. }
  337. - (void)alertCameraPermissionDisabled {
  338. NSString *message = MSG_CAMERA_DISABLE;
  339. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소") message:message, nil];
  340. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  341. if (buttonIndex == 0) {//OK
  342. NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  343. [[UIApplication sharedApplication]openURL:settingsURL];
  344. }
  345. }];
  346. }
  347. #pragma mark - Debug
  348. - (void)showFlex {
  349. [[FLEXManager sharedManager] showExplorer];
  350. }
  351. - (BOOL)redirectNSLog {
  352. // Create log file
  353. NSDate *date = [NSDate date];
  354. NSDateFormatter *df = [CommonUtil dateFormatter];
  355. [df setDateFormat:@"yyyyMMddHHmmss"];
  356. NSString *sdate = [df stringFromDate:date];
  357. NSString *logPath = [NSString stringWithFormat:@"%@/log_%@.txt", NSTemporaryDirectory(), sdate];
  358. [@"" writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  359. id fileHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
  360. if (!fileHandle) {
  361. NSLog(@"Opening log failed");
  362. return NO;
  363. }
  364. // Redirect stderr
  365. int err = dup2([fileHandle fileDescriptor], STDERR_FILENO);
  366. if (!err) {
  367. NSLog(@"Couldn't redirect stderr");
  368. return NO;
  369. }
  370. return YES;
  371. }
  372. #pragma mark - Singleton
  373. + (JDFacade *)facade {
  374. static JDFacade *sharedJSFacade = nil;
  375. static dispatch_once_t onceToken;
  376. dispatch_once(&onceToken, ^{
  377. sharedJSFacade = [[self alloc] init];
  378. });
  379. return sharedJSFacade;
  380. }
  381. - (id)init {
  382. if (self = [super init]) {
  383. }
  384. return self;
  385. }
  386. @end