| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064 |
- //
- // JDFacade.m
- // kneet2
- //
- // Created by Created by Jason Lee on 10/1/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- @import ObjectiveC.runtime;
- #import "JDFacade.h"
- #import "JDUserDefaults.h"
- #import "CypherUtil.h"
- #import "JDUUID.h"
- #import "CustomLoadingView.h"
- #import "CustomImageView.h"
- #import "RequestHandler.h"
- //OpenSource
- #ifndef PRODUCT_MODE
- #import "FLEXManager.h"
- #endif
- #import "UIView+Toast.h"
- #import "JDProgressHUD.h"
- #import "Valet.h"
- #import "VALValet.h"
- #import "LoginViewController.h"
- #import "MainViewController.h"
- #import "DeviceModel.h"
- #import "HomeHubViewController.h"
- #import "MultiHomeHubViewController.h"
- #import "HomeHubViewController.h"
- @interface JDFacade () {
- VALValet *_valet;
- JDProgressHUD *_HUD;
- NSTimer *_homehubBackgroundTimer;
- }
- @end
- @implementation JDFacade
- @synthesize tmpEmailId = _tmpEmailId;
- #pragma mark - Properties
- - (NSString *)deviceUUID {
- if (!_deviceUUID) {
- _deviceUUID = [[JDUUID UUIDHandler] getUUID];
- }
- return _deviceUUID;
- }
- - (NSString *)APNSToken {
- return _APNSToken ? _APNSToken : ksEmptyString;
- }
- - (NSString *)tmpEmailId {
- NSString *enTmpEmailId = [self objectForKeyFromUserDefaults:USDEF_APP_TMP_EMAIL];
- _tmpEmailId = [CypherUtil AES128Decrypt:enTmpEmailId WithKey:[CommonUtil bundleIdentifier]];
-
- return _tmpEmailId ? _tmpEmailId : ksEmptyString;
- }
- - (void)setTmpEmailId:(NSString *)tmpEmailId {
-
- NSString *enTmpEmailId = [CypherUtil AES128Encrypt:tmpEmailId WithKey:[CommonUtil bundleIdentifier]];
- [self storeObjectToUserDefaults:enTmpEmailId forKey:USDEF_APP_TMP_EMAIL];
- }
- - (void)setCurrentOperation:(NSOperation *)currentOperation {
- [CustomLoadingView loadingView].operation = currentOperation;
- }
- - (NSString *)deviceHostName {
- return [self hostnameOfDevice];
- }
- - (void)setLoginUser:(LoginModel *)loginUser {
- _loginUser = loginUser;
-
- if (!_loginUser) {
- if (_homehubBackgroundTimer) {
- [_homehubBackgroundTimer invalidate];
- _homehubBackgroundTimer = nil;
- }
- }
- }
- - (NSString *)hostnameOfDevice {
- char baseHostName[256];
- int success = gethostname(baseHostName, 255);
- if (success != 0) return nil;
- baseHostName[255] = '\0';
-
- #if !TARGET_IPHONE_SIMULATOR
- NSLog(@"%s\n %@, %s", __PRETTY_FUNCTION__, @"!TARGET_IPHONE_SIMULATOR", baseHostName);
-
- NSString *hostname = [NSString stringWithFormat:@"%s", baseHostName];
- if (![hostname hasSuffix:@"local"]) {
- hostname = [NSString stringWithFormat:@"%s.local", baseHostName];
- }
- return hostname;
- #else
- return [NSString stringWithFormat:@"%s", baseHostName];
- #endif
- }
- #pragma mark - View Facade
- - (AppDelegate *)appDelegate {
- return (AppDelegate *)[UIApplication sharedApplication].delegate;
- }
- #pragma mark - 화면 제어
- - (UIViewController *)currentViewController {
- id appDelegate = [UIApplication sharedApplication].delegate;
- UIViewController *vc = (UINavigationController *)[[appDelegate window] rootViewController];
- if (vc.presentedViewController) {
- while (vc.presentedViewController) {
- vc = vc.presentedViewController;
- }
- if ([vc isKindOfClass:[UINavigationController class]]) {
- NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
- vc = [vcArray objectAtIndex:vcArray.count - 1];
- }
- return vc;
- }
- if (vc.presentingViewController && (vc.modalPresentationStyle == UIModalPresentationFullScreen)) {
- vc = vc.presentingViewController;
- }
- if ([vc isKindOfClass:[UINavigationController class]]) {
- NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
- vc = [vcArray objectAtIndex:vcArray.count - 1];
- }
- return vc;
- }
- - (id)viewControllerOnNaviationController:(Class)viewControllerClass {
- UIViewController *targetVc = nil;
- UINavigationController *navigationController = self.currentViewController.navigationController;
- if (!navigationController) {
- navigationController = (UINavigationController *)self.currentViewController.presentingViewController;
- }
- if ([navigationController isKindOfClass:[UINavigationController class]]) {
- for (UIViewController *vc in navigationController.viewControllers) {
- if ([vc isKindOfClass:viewControllerClass]) {
- targetVc = vc;
- break;
- }
- }
- }
- return targetVc;
- }
- - (id)viewControllerOnPresentingViewController:(UIViewController *)pvc viewControllerClass:(Class)viewControllerClass {
- return [self viewControllerOnPresentingViewController:pvc viewControllerClass:viewControllerClass tryTimes:10];
- }
- - (id)viewControllerOnPresentingViewController:(UIViewController *)pvc viewControllerClass:(Class)viewControllerClass tryTimes:(NSInteger)tryTimes {
- UIViewController *vc = pvc.presentingViewController;
-
- if (![vc isKindOfClass:viewControllerClass] && tryTimes > 0) {
- vc = [self viewControllerOnPresentingViewController:vc viewControllerClass:viewControllerClass tryTimes:tryTimes-1];
- }
- return vc;
- }
- - (id)viewControllerOnPresentedViewController:(UIViewController *)pvc viewControllerClass:(Class)viewControllerClass {
- return [self viewControllerOnPresentedViewController:pvc viewControllerClass:viewControllerClass tryTimes:10];
- }
- - (id)viewControllerOnPresentedViewController:(UIViewController *)pvc viewControllerClass:(Class)viewControllerClass tryTimes:(NSInteger)tryTimes {
- UIViewController *vc = pvc.presentedViewController;
-
- if (![vc isKindOfClass:viewControllerClass] && tryTimes > 0) {
- vc = [self viewControllerOnPresentedViewController:vc viewControllerClass:viewControllerClass tryTimes:tryTimes-1];
- }
- return vc;
- }
- #pragma mark - KeychainItem Wrapper
- static NSString *const ksKeychainIdentifier = @"OneCableACCOUNT";
- static NSString *const ksKeychainArchiveData = @"_archiveData";
- - (void)storeObjectToKeychain:(id)object forKey:(NSString *)aKey {
- if (!_valet) {
- _valet = [[VALValet alloc] initWithIdentifier:ksKeychainIdentifier accessibility:VALAccessibilityWhenUnlockedThisDeviceOnly];
- }
-
- NSMutableDictionary *dict = nil;
- NSData *archiveData = [_valet objectForKey:ksKeychainArchiveData];
-
- if (archiveData) {
- dict = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
- }
-
- if (!dict) {
- dict = [[NSMutableDictionary alloc] init];
- }
-
-
- [dict setObject:object forKey:aKey];
-
- archiveData = [NSKeyedArchiver archivedDataWithRootObject:dict];
- [_valet setObject:archiveData forKey:ksKeychainArchiveData];
- }
- - (id)objectForKeyFromKeychain:(NSString *)aKey {
- if (!_valet) {
- _valet = [[VALValet alloc] initWithIdentifier:ksKeychainIdentifier accessibility:VALAccessibilityWhenUnlockedThisDeviceOnly];
- }
-
- NSMutableDictionary *dict = nil;
- NSData *archiveData = [_valet objectForKey:ksKeychainArchiveData];
-
- id obj = nil;
- if (archiveData) {
- dict = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
- obj = [dict objectForKey:aKey];
-
- }
-
- return obj;
- }
- - (void)removeObjectAtKeychain:(NSString *)aKey {
- if (!_valet) {
- _valet = [[VALValet alloc] initWithIdentifier:ksKeychainIdentifier accessibility:VALAccessibilityWhenUnlockedThisDeviceOnly];
- }
-
- NSMutableDictionary *dict = nil;
- NSData *archiveData = [_valet objectForKey:ksKeychainArchiveData];
- if (archiveData) {
- dict = (NSMutableDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
- [dict removeObjectForKey:aKey];
- }
-
- archiveData = [NSKeyedArchiver archivedDataWithRootObject:dict];
- [_valet setObject:archiveData forKey:ksKeychainArchiveData];
- }
- #pragma mark - UserDefautls - Local Store
- - (void)storeObjectToUserDefaults:(id)object forKey:(NSString *)aKey {
- [[JDUserDefaults defaults] syncronizeObject:object forKey:aKey];
- }
- - (id)objectForKeyFromUserDefaults:(NSString *)aKey {
- return [[JDUserDefaults defaults] objectForKey:aKey];
- }
- #pragma mark - Alert & Loading
- - (void)loadIndicator:(BOOL)showIndicator allowUserInteraction:(BOOL)allowUserInteracton {
-
- //로딩시 터치 방지
- if(showIndicator) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [[JDFacade facade].currentViewController.view endEditing:YES];
-
- UIView *topView = [CommonUtil topView];
- [[CustomLoadingView loadingView] showInView:topView];
- });
-
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
- } else {
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[CustomLoadingView loadingView] hide];
- });
-
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
- }
- }
- - (void)showLoadingWhileExecutingBlock:(dispatch_block_t)block completionHandler:(JDFacadeCompletionCallBackHandler)completion {
- [[CustomLoadingView loadingView] showInView:[CommonUtil topView] whileExecutingBlock:block completionHandler:completion];
- }
- #pragma mark - Alert UI
- - (void)alert:(NSString *)message {
- [self alertTitle:@"알림" message:message];
- }
- - (void)alertTitle:(NSString *)title message:(NSString *)message {
- [self.currentViewController.view endEditing:YES];
-
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:title message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
- [alert show];
- }
- - (void)alert:(NSString *)message completionHander:(JDFacadeCompletionCallBackHandler)completion {
- [self alertTitle:@"알림" message:message completionHander:completion];
- }
- - (void)alertTitle:(NSString *)title message:(NSString *)message completionHander:(JDFacadeCompletionCallBackHandler)completion {
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:title message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:nil];
-
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (completion) {
- completion();
- }
- }];
- }
- - (void)confirm:(NSString *)message completion:(CustomAlertViewCallBackHandler)completion {
- [self confirmTitle:@"알림" message:message completion:completion];
- }
- - (void)confirmTitle:(NSString *)title message:(NSString *)message completion:(CustomAlertViewCallBackHandler)completion {
- [self confirmTitle:title message:message btnOKLabel:@"확인" btnCancelLabel:@"취소" completion:completion];
- }
- - (void)confirmTitle:(NSString *)title message:(NSString *)message btnOKLabel:(NSString *)btnOKLabel btnCancelLabel:(NSString *)btnCancelLabel completion:(CustomAlertViewCallBackHandler)completion {
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:title message:message delegate:nil OKButtonTitle:btnOKLabel cancelButtonTitle:btnCancelLabel];
-
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (completion) {
- completion(alertView, buttonIndex);
- }
- }];
- }
- - (void)retryAlert:(NSString *)message target:(id)target selector:(SEL)selector {
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 1) {
- if ([target respondsToSelector:selector]) {
- [target performSelector:selector withObject:nil afterDelay:0.0f];
- }
- }
- }];
- }
- - (void)retryAlert:(NSString *)message target:(id)target selector:(SEL)selector arguments:(id)arguments,... {
- va_list args;
- va_start(args, arguments);
-
- NSInteger i = 0;
- NSMutableArray *argsArray = [[NSMutableArray alloc] init];
-
- [argsArray addObject:arguments];
- while ((arguments = va_arg(args, id)) != nil) {//루프 - 입력된 메시지를 받아옴.
- [argsArray addObject:arguments];
- i++;
- }
-
- va_end(args);
-
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {
- if (target && selector) {
-
- NSMethodSignature *sig = [target methodSignatureForSelector:selector];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
- NSInteger index = 2;
-
- [invocation setTarget:target]; // index 0 (hidden)
- [invocation setSelector:selector]; //index 1 (hidden)
-
- for (id argument in argsArray) {
- [invocation setArgument:(void *)&argument atIndex:index++];
- }
-
- [invocation invoke];
- }
- }
- }];
- }
- - (void)retryAlert:(NSString *)message completionHander:(JDFacadeCompletionCallBackHandler)handler {
-
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"재시도"];
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 1) {//재시도
- if (handler) {
- handler();
- }
- }
- }];
- }
- #pragma mark - ActionSeet UI
- // TODO : Action Sheet 출력 메소드 만들것
- -(void)selectDatas:(id)target listInfo:(DataSelectListModel *)listInfo completion:(JDFacadeSelectCallBackHandler)completion{
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:listInfo.title
- message:nil
- preferredStyle:UIAlertControllerStyleActionSheet];
-
- for (DataSelectModel *info in listInfo.list) {
- UIAlertAction *action = [UIAlertAction actionWithTitle:info.title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- if (completion) {
- completion(info);
- }
- }];
- if ([info getColorCode] != nil) {
- [action setValue:[info getColorCode] forKey:@"titleTextColor"];
- }
- // [action setValue:[UIColor redColor] forKey:@"titleTextColor"];
- [alertController addAction:action];
- }
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"취소" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
- }];
- [alertController addAction:cancelAction];
- // NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:alertController.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:IPHONE_HEIGHT*.5f];
- // [alertController.view addConstraint:constraint];
-
- [alertController.view mas_makeConstraints:^(MASConstraintMaker *make) {
- make.height.lessThanOrEqualTo(@(IPHONE_HEIGHT*.5f));
- }];
- [target presentViewController:alertController animated:YES completion:nil];
- }
- - (void)fireLocalNotification:(NSString *)message {
- NSLog(@"fireLocalNotification %@", message);
- NSDate *now = [NSDate date];
- UILocalNotification *localNotif = [[UILocalNotification alloc] init];
- localNotif.fireDate = now;
- NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
- localNotif.timeZone = timezone;
- localNotif.hasAction = YES;
- localNotif.alertBody = message;
- localNotif.alertAction = @"View";
- localNotif.soundName = UILocalNotificationDefaultSoundName;
- localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
- [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
- }
- - (void)toast:(NSString *)message {
- [[CommonUtil topView] makeToast:message];
- }
- #pragma mark - CheckBox && Radiobutton Status
- - (id)getRadioButtonStatus:(id)object {
- if (!object)
- return @NO;
- return objc_getAssociatedObject(object, ksCustomRadioButtonStatus);
- }
- - (void)setRadioButtonStatus:(id)status object:(id)object {
- if (!object)
- return;
-
- objc_setAssociatedObject(object, ksCustomRadioButtonStatus, status, OBJC_ASSOCIATION_COPY_NONATOMIC); //for radio box
- }
- - (id)getCheckBoxStatus:(id)object {
- if (!object)
- return @NO;
- return objc_getAssociatedObject(object, ksCustomCheckBoxStatus);
- }
- - (void)setCheckBoxStatus:(id)status object:(id)object {
- if (!object)
- return;
- objc_setAssociatedObject(object, ksCustomCheckBoxStatus, status, OBJC_ASSOCIATION_COPY_NONATOMIC); //for check box
- }
- #pragma mark - 화면이동 제어
- - (void)dismissModalStack:(BOOL)animated completion:(JDFacadeCompletionCallBackHandler)completion {
- UIViewController *vc = self.currentViewController;
- while (vc.presentingViewController) {
- vc = vc.presentingViewController;
- }
- CATransition *transition = [CATransition animation];
- transition.duration = kfTransitionRightDur;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- transition.type = kCATransitionMoveIn;
- transition.subtype = kCATransitionFromBottom;
-
- [vc.view.window.layer addAnimation:transition forKey:nil];
- [vc dismissViewControllerAnimated:NO completion:^{
- if (completion) {
- completion();
- }
- }];
- }
- - (void)presentViewControllerByPush:(UIViewController *)vc {
- CATransition *transition = [CATransition animation];
- transition.duration = kfTransitionRightDur;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- transition.type = kCATransitionPush;
- transition.subtype = kCATransitionFromRight;
- [self.currentViewController.view.window.layer addAnimation:transition forKey:nil];
- [self.currentViewController presentViewController:vc animated:NO completion:nil];
- }
- - (void)presentViewControllerByPush:(UIViewController *)vc pvc:(UIViewController *)pvc {
- CATransition *transition = [CATransition animation];
- transition.duration = kfTransitionRightDur;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- transition.type = kCATransitionPush;
- transition.subtype = kCATransitionFromRight;
- [pvc.view.window.layer addAnimation:transition forKey:nil];
- [pvc presentViewController:vc animated:NO completion:nil];
- }
- - (UIViewController *)presentedViewController:(UIViewController *)vc {
- UIViewController *presentedViewController = vc.presentedViewController;
- if (presentedViewController) {
- return [self presentedViewController:presentedViewController];
- }
- return vc;
- }
- - (void)dismissViewControllerByPush:(UIViewController *)vc {
- CATransition *transition = [CATransition animation];
- transition.duration = kfTransitionRightDur;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- transition.type = kCATransitionPush;
- transition.subtype = kCATransitionFromBottom;
- UIViewController *presentedViewController = [self presentedViewController:vc];
- [presentedViewController.view.window.layer addAnimation:transition forKey:nil];
- [vc dismissViewControllerAnimated:NO completion:nil];
- }
- - (void)dismissViewControllerByPush:(UIViewController *)vc completion:(JDFacadeCompletionCallBackHandler)completion {
- CATransition *transition = [CATransition animation];
- transition.duration = kfTransitionRightDur;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- transition.type = kCATransitionPush;
- transition.subtype = kCATransitionFromLeft;
- UIViewController *presentedViewController = [self presentedViewController:vc];
- [presentedViewController.view.window.layer addAnimation:transition forKey:nil];
- [vc dismissViewControllerAnimated:NO completion:^{
- if (completion) {
- completion();
- }
- }];
- }
- - (void)dismissAllViewControllers {
- UIViewController *vc = self.currentViewController;
-
- if (vc.presentingViewController) {
- [vc dismissViewControllerAnimated:NO completion:^{
- [self dismissAllViewControllers];
- }];
- }
- }
- #pragma mark - Biz Logic
- - (void)requestPollingHomeHubStatusInBackground {
- //schedul timer.
- if (!_homehubBackgroundTimer) {
- [self requestPollingHomeHubStatus:^{
- _homehubBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(requestPollingHomeHubStatus) userInfo:nil repeats:YES];
- }];
- }
- }
- - (void)requestPollingHomeHubStatus {
- [self requestPollingHomeHubStatus:nil];
- }
- - (void)requestPollingHomeHubStatus:(JDFacadeCompletionCallBackHandler)completion {
-
- NSString *path = API_GET_DEVICE_HOMEHUB_STATUS;
- dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
- DeviceModel *homehub = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
- modelClass:[DeviceModel class] showLoadingView:NO];
- if (homehub && homehub.onlineState && ![homehub.onlineState isEmptyString]) {
-
- [JDFacade facade].loginUser.homehubOnlineState = homehub.onlineState;
- [JDFacade facade].loginUser.homehubCreateDatetime = homehub.createDatetime;
-
- } else {
-
- [JDFacade facade].loginUser.homehubDeviceId = nil;
- [JDFacade facade].loginUser.homehubOnlineState = nil;
- [JDFacade facade].loginUser.homehubCreateDatetime = nil;
- }
- [_mainViewController updateHomeHubStatusToChildViewController];
-
- //update global - homehub state
- if ([[JDFacade facade].currentViewController isKindOfClass:[HomeHubViewController class]]) {
- HomeHubViewController *vc = (HomeHubViewController *)[JDFacade facade].currentViewController;
- [vc updateHomeHubStatus];
- }
-
- #ifdef DEBUG_MODE
- NSLogInfo(@"==########== homehub state = %@ ==########==", [JDFacade facade].loginUser.homehubOnlineState);
- #endif
- if (completion) {
- completion();
- }
- });
- }
- #pragma mark - UI Flow
- - (void)gotoLoginView {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
- [[JDFacade facade].appDelegate.window setRootViewController:vc];
- }
- //- (void)gotoLoginViewWithExpiring {
- // LoginViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
- // [vc actionAfterLogout];
- //}
- //
- - (void)logout {
-
- LoginViewController *lvc = (LoginViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
- [lvc requestLogout];
- }
- - (void)loadInvitationView {
-
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"InvitationListViewController" storyboardName:@"Main"];
- [self.currentViewController presentViewController:vc animated:YES completion:nil];
- }
- - (void)loadSettingsNumChangeView {
-
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsNumChangeViewController" storyboardName:@"Settings"];
- [self.currentViewController presentViewController:vc animated:YES completion:nil];
- }
- - (void)loadHomeHubView {
-
- //멀티
- UIViewController *viewController = [CommonUtil instantiateViewControllerWithIdentifier:@"MultiHomeHubViewController" storyboardName:@"Main"];;
-
- //단일
- if (![JDFacade facade].loginUser.isMultiHomeHub)
- viewController = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
-
- //NSLog(@"[self alreadyShowHomeHubView] : %d", [self alreadyShowHomeHubView]);
-
- //이미 떠 있는 경우
- if ([self alreadyShowHomeHubView]) {
-
- UIViewController *currentVc = [CommonUtil currentViewController];
- [currentVc.navigationController popToRootViewControllerAnimated:YES completion:nil];
- }
- else {
- UINavigationController *m_navi =[[UINavigationController alloc]initWithRootViewController:viewController];
- [self.currentViewController presentViewController:m_navi animated:YES completion:nil];
- }
- }
- - (BOOL)alreadyShowHomeHubView {
-
- UIViewController *currentVc = [CommonUtil currentViewController];
- NSArray *vcs = currentVc.navigationController.viewControllers;
-
-
- for (UIViewController *vc in vcs) {
-
- if ([vc isKindOfClass:HomeHubViewController.class] || [vc isKindOfClass:MultiHomeHubViewController.class]) {
- return YES;
- }
- }
-
- return NO;
- }
- //- (void)checkDefaultHome {
- // if (!self.loginUser) {//로그인이 안된 경우, 불필요함.
- // return;
- // }
- //
- // [self updateHomegrpListForLoginUser:^{
- // //1. 다른 홈이 있는지 확인 - 정렬순서대로 기본홈을 설정 - currentHome
- // if (self.loginUser.homegrpList && self.loginUser.homegrpList.count) {
- // self.loginHomeGroup = self.loginUser.homegrpList[0];
- // [self gotoWishMenu:KNMenuIdDashboard];
- // return;
- // }
- //
- // //2. 다른 홈이 없는 경우, - 홈만들기로 이동함.
- // CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?", @"현재 참여 중인 홈이 없습니다\n새로운 홈 만들기를 시작할까요?") delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
- // [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- // if (buttonIndex == 0) {//OK
- // [self gotoStartHome:NO];
- // } else {
- // [self logout];
- // }
- // }];
- // }];
- //}
- - (void)gotoStartHome:(BOOL)canGoBack {
- // StartHomeViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"StartHomeViewController" storyboardName:@"SignUp"];
- // vc.canGoBack = canGoBack;
- // [[JDFacade facade] presentViewControllerByPush:vc pvc:self.currentViewController];
- }
- - (void)gotoWishMenu:(KNMenuId)menuId {
- [self gotoWishMenu:menuId completion:nil];
- }
- - (void)gotoWishMenu:(KNMenuId)menuId completion:(JDFacadeCompletionCallBackHandler)completion {
- self.wishMenuId = menuId;
-
- switch (self.wishMenuId) {
- case KNMenuIdDashboard: {
- // [_mainViewController btnCollapseTouched:nil];
-
- [[JDFacade facade] requestPollingHomeHubStatus:^{
- [[JDFacade facade] dismissModalStack:YES completion:nil];
- }];
- }
- break;
- case KNMenuIdThings: {
- [[JDFacade facade] dismissModalStack:YES completion:^{
- [_mainViewController loadThingsViewController];
- }];
- }
- break;
- case KNMenuIdRules: {
- [[JDFacade facade] dismissModalStack:YES completion:^{
- [_mainViewController loadRulesViewController];
- }];
- }
- break;
- case KNMenuIdHomeMember: {
- [[JDFacade facade] dismissModalStack:YES completion:^{
- [_mainViewController loadMembersViewController];
- }];
- }
- break;
- case KNMenuIdManagement: {
- [[JDFacade facade] dismissModalStack:YES completion:^{
- [_mainViewController loadManagementViewController];
- }];
- }
- break;
- default:
- break;
- }
- }
- - (void)gotoHomeHubRegistration {
- // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubStartViewController" storyboardName:@"HomeHub"];
- //
- // vc.providesPresentationContextTransitionStyle = YES;
- // vc.definesPresentationContext = YES;
- //
- // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
- //
- // [self.currentViewController presentViewController:vc animated:NO completion:nil];
- }
- //- (void)updateHomegrpListForLoginUser:(JDFacadeCompletionCallBackHandler)completion {
- //
- // NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID};
- //
- // dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
- // HomeGroupListModel *homegrpList = [[RequestHandler handler] sendSyncGetRequestAPIPath:API_GET_HOMEGROUP parameters:parameter
- // modelClass:[HomeGroupListModel class] showLoadingView:YES];
- //
- //
- // // NSLog(@"%s\n %@", __PRETTY_FUNCTION__, homegrpList);
- // //홈그룹을 설정함.
- // self.loginUser.homegrpList = homegrpList && homegrpList.homegrpList ? homegrpList.homegrpList : nil;
- //
- // //로그인 홈을 지정함.
- // for (HomeGroupModel *homegrp in self.loginUser.homegrpList) {
- // if ([self.loginUser.homegrpId isEqualToString:homegrp.homegrpId]) {
- // self.loginHomeGroup = homegrp;
- // break;
- // }
- // }
- //
- //// [[LocationHandler handler] resetAllMonitoredRegions]; //현재 앱이 구동 중이면, 위치 센서를 다시 등록해야함.
- //// [[LocationHandler handler] registerGeofencingForHomeGroupIndex:0]; //모든 홈 그룹을 다시 모니터링 등록 - 루프
- //
- // if (completion) {
- // completion();
- // }
- // });
- //}
- - (void)updateLoginInfo:(JDFacadeCompletionCallBackHandler)completion {
- NSLog(@"%s\n %@", __PRETTY_FUNCTION__, [JDFacade facade].deviceUUID);
-
- NSDictionary *parameter = @{@"device_sn": [JDFacade facade].deviceUUID,
- @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
- @"os_type": MOBILE_DEVICE_TYPE};
-
- NSString *path = [NSString stringWithFormat:API_GET_SIGN_IN_AUTO];
-
- dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
- LoginModel *loginInfo = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:parameter
- modelClass:[LoginModel class] showLoadingView:YES];
- if (loginInfo) {
- [JDFacade facade].loginUser = loginInfo;
- }
-
- if (completion) {
- completion();
- }
- });
- }
- - (void)updateMainViewController {
- [self.mainViewController updateCurrentViewController];
- }
- - (void)loadURLExternalBrowser:(NSString *)URLString {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString]];
- }
- - (void)alertCameraPermissionDisabled {
- NSString *message = MSG_CAMERA_DISABLE;
-
- CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:message delegate:nil OKButtonTitle:NSLocalizedString(@"설정", @"설정") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
-
- [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- [[UIApplication sharedApplication]openURL:settingsURL];
- }
- }];
- }
- #pragma mark - Debug
- - (void)showFlex {
- #ifndef PRODUCT_MODE
- [[FLEXManager sharedManager] showExplorer];
- #endif
- }
- - (BOOL)redirectNSLog {
- // Create log file
- NSDate *date = [NSDate date];
- NSDateFormatter *df = [CommonUtil dateFormatter];
- [df setDateFormat:@"yyyyMMddHHmmss"];
- NSString *sdate = [df stringFromDate:date];
- NSString *logPath = [NSString stringWithFormat:@"%@/log_%@.txt", NSTemporaryDirectory(), sdate];
- [@"" writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- id fileHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
- if (!fileHandle) {
- NSLog(@"Opening log failed");
- return NO;
- }
- // Redirect stderr
- int err = dup2([fileHandle fileDescriptor], STDERR_FILENO);
- if (!err) {
- NSLog(@"Couldn't redirect stderr");
- return NO;
- }
- return YES;
- }
- - (void)observeKeyboardRect {
-
- if (CGRectEqualToRect(gKeyboardRect, CGRectZero)) {
- [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
- }
- }
- - (void)keyboardWillShow:(NSNotification *)notification {
- NSDictionary *info = [notification userInfo];
- gKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- }
- - (void)keyboardWillHide:(NSNotification*)notification {
- gKeyboardRect = CGRectZero;
- }
- #pragma mark - Singleton
- + (JDFacade *)facade {
- static JDFacade *sharedJSFacade = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedJSFacade = [[self alloc] init];
- [sharedJSFacade observeKeyboardRect];
- });
- return sharedJSFacade;
- }
- - (id)init {
- if (self = [super init]) {
-
- }
- return self;
- }
- -(NSString *)getUrlWithCustID:(NSString *)str aditional:(NSString *)aditional {
- NSString *result = @"";
- if ([aditional isEmptyString]) {
- result = [NSString stringWithFormat:str, _loginUser.custId, aditional];
- }
- else {
- result = [NSString stringWithFormat:str, _loginUser.custId];
- }
-
-
- return result;
- }
- -(NSString *)getUrlWithCustAndGroupID:(NSString *)str aditional:(NSString *)aditional {
- NSString *result = @"";
- if ([aditional isEmptyString]) {
- result = [NSString stringWithFormat:str, _loginUser.custId, _loginUser.ctrtGrpId, aditional];
- }
- else {
- result = [NSString stringWithFormat:str, _loginUser.custId, _loginUser.ctrtGrpId];
- }
-
- return result;
- }
- -(NSString *)getUrlWithCustAndGroupID:(NSString *)str arguments:(NSArray *)arguments
- {
- NSMutableArray *arr = [NSMutableArray array];
- arr[0] = _loginUser.custId;
- arr[1] = _loginUser.ctrtGrpId;
- if (arguments != nil) {
- int i = 2;
- for (NSString *str in arguments) {
- arr[i] = str;
- i++;
- }
- }
- return [self getUrlWithArr:str arguments:arr];
- }
- -(NSString *)getUrlWithArr:(NSString *)str arguments:(NSArray *)arguments {
- // NSRange range = NSMakeRange(0, [arguments count]);
- // NSMutableData* data = [NSMutableData dataWithLength:sizeof(id) * [arguments count]];
- // [arguments getObjects:(__unsafe_unretained id *)data.mutableBytes range:range];
- // NSString* result = [[NSString alloc] initWithFormat:str arguments:data.mutableBytes];
- return [str stringWithFormatWithArr:arguments];
- }
- -(NSString *)getUrlWithCustGroupIDAndMemberID:(NSString *)str {
- return [NSString stringWithFormat:str, _loginUser.custId, _loginUser.ctrtGrpId, _loginUser.memberId];
- }
- -(NSString *)getUrlWithCustAndGroupIDAndInputMemId:(NSString *)str inputId:(NSString *)inputId{
- if ([inputId isEqualToString:@""]) {
- return [NSString stringWithFormat:str, _signUpUser.custId, _signUpUser.ctrtGrpId];
- } else {
- return [NSString stringWithFormat:str, _signUpUser.custId, _signUpUser.ctrtGrpId, inputId];
- }
- }
- //itemsubmodel 에서 sourcesubid로 아이콘 찾기
- - (NSString*)deviceIconFromSourceSubId:(NSString*)source {
-
- NSString *icon = @"" ;
-
- for( NSDictionary *item in DEVICE_SOURCE_ICON ) {
-
- NSString *sourceId = [item objectForKey:@"sourceSubId"] ;
-
- if( [source containsString:sourceId] ) {
-
- icon = [item objectForKey:@"icon"] ;
- break;
- }
-
- }
-
- return icon ;
- }
- //itemmodel 에서 sourcename으로 아이콘 찾기
- - (NSString*)deviceIconFromSourceName:(NSString*)name {
-
- NSString *icon = @"" ;
-
- for( NSDictionary *item in DEVICE_SOURCE_ICON ) {
-
- NSString *sourceName = [item objectForKey:@"sourceName"] ;
-
- if( [name containsString:sourceName] ) {
-
- icon = [item objectForKey:@"icon"] ;
- break;
- }
-
- }
-
- return icon ;
- }
- @end
|