MainViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. //
  2. // MainViewController.m
  3. // kneet2
  4. //
  5. // StoryBoard - page 8.
  6. //
  7. // Created by Jason Lee on 10/2/15.
  8. // Copyright © 2015 ntels. All rights reserved.
  9. //
  10. #import "JDObject.h"
  11. #import "RequestHandler.h"
  12. #import "CustomButton.h"
  13. #import "UIButton+WebCache.h"
  14. #import "ImageUtil.h"
  15. #import "ThingsViewController.h"
  16. #import "RulesViewController.h"
  17. #import "HomeMemberViewController.h"
  18. #import "MainViewController.h"
  19. #import "HomeModeUpdateViewController.h"
  20. #import "HomeModeSettingsViewController.h"
  21. #import "ManagementViewController.h"
  22. #import "CustomLabelButton.h"
  23. #import "CustomImageView.h"
  24. #import "PwdPopupView.h"
  25. @interface MainViewController () <ImageUtilDelegate> {
  26. RulesViewController *_rvc;
  27. HomeMemberViewController *_mvc;
  28. ManagementViewController *_manageVc;
  29. ImageUtil *_imageUtil;
  30. NSDate *_readDateTime;
  31. DashboardModel *_dashboard;
  32. BOOL _isNotFirstLoading;
  33. }
  34. @property (strong, nonatomic) JDViewController *currentChildViewController;
  35. @property (strong, nonatomic) UIView *thingsContainerView;
  36. @property (strong, nonatomic) UIView *rulesContainerView;
  37. @property (strong, nonatomic) UIView *membersContainerView;
  38. @property (strong, nonatomic) UIView *manageContainerVIew;
  39. @end
  40. #pragma mark - Class Definition
  41. @implementation MainViewController
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // Do any additional setup after loading the view.
  45. [self initUI];
  46. [self prepareViewDidLoad];
  47. }
  48. - (void)initUI {
  49. //1. init tabBar
  50. //1-1. scroll top bottom
  51. //2. init scrollView
  52. // CGFloat height = ViewHeight(_scrollView); //IPHONE_HEIGHT - kfMainTabBarHeight;
  53. [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 4, IPHONE_HEIGHT)];
  54. _scrollView.pagingEnabled = YES;
  55. _scrollView.scrollEnabled = NO;
  56. CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT);
  57. //init child view for childViewController
  58. _thingsContainerView = [[UIView alloc] initWithFrame:sr];
  59. _rulesContainerView = [[UIView alloc] initWithFrame:sr];
  60. _membersContainerView = [[UIView alloc] initWithFrame:sr];
  61. _manageContainerVIew = [[UIView alloc] initWithFrame:sr];
  62. [_scrollView addSubview:_thingsContainerView];
  63. _rulesContainerView.x = IPHONE_WIDTH;
  64. [_scrollView addSubview:_rulesContainerView];
  65. _membersContainerView.x = IPHONE_WIDTH * 2;
  66. [_scrollView addSubview:_membersContainerView];
  67. _manageContainerVIew.x = IPHONE_WIDTH * 3;
  68. [_scrollView addSubview:_manageContainerVIew];
  69. _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  70. _constraintExpandViewHeight.constant = IPHONE_HEIGHT;
  71. [self.view layoutIfNeeded];
  72. }
  73. - (void)initThingsViewController {
  74. if (!_tvc) {
  75. _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
  76. }
  77. [self addChildViewController:_tvc];
  78. [_tvc didMoveToParentViewController:self];
  79. if ([self isViewLoaded]) {
  80. [_tvc beginAppearanceTransition:YES animated:NO];
  81. [_thingsContainerView addSubview:_tvc.view];
  82. [_tvc endAppearanceTransition];
  83. }
  84. }
  85. - (void)initRulesViewController {
  86. if (!_rvc) {
  87. _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
  88. }
  89. [self addChildViewController:_rvc];
  90. [_rvc didMoveToParentViewController:self];
  91. if ([self isViewLoaded]) {
  92. [_rvc beginAppearanceTransition:YES animated:NO];
  93. [_rulesContainerView addSubview:_rvc.view];
  94. [_rvc endAppearanceTransition];
  95. }
  96. }
  97. - (void)initMembersViewController {
  98. if (!_mvc) {
  99. _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
  100. }
  101. [self addChildViewController:_mvc];
  102. [_mvc didMoveToParentViewController:self];
  103. if ([self isViewLoaded]) {
  104. [_mvc beginAppearanceTransition:YES animated:NO];
  105. [_membersContainerView addSubview:_mvc.view];
  106. [_mvc endAppearanceTransition];
  107. }
  108. }
  109. - (void)initManagementViewController {
  110. if (!_manageVc) {
  111. _manageVc = (ManagementViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ManagementViewController" storyboardName:@"Main"];
  112. }
  113. [self addChildViewController:_manageVc];
  114. [_manageVc didMoveToParentViewController:self];
  115. if ([self isViewLoaded]) {
  116. [_manageVc beginAppearanceTransition:YES animated:NO];
  117. [_manageContainerVIew addSubview:_manageVc.view];
  118. [_manageVc endAppearanceTransition];
  119. }
  120. }
  121. - (void)viewWillAppear:(BOOL)animated {
  122. [self requestDashboard:nil];
  123. }
  124. - (void)prepareViewDidLoad {
  125. // [self requestDashboard:nil];
  126. // [[JDFacade facade] requestPollingHomeHubStatusInBackground];
  127. // TODO: 웹소켓 연결해서 정보 받아오는 형식으로 전환할것
  128. }
  129. - (void)setCurrentChildViewController:(JDViewController *)currentChildViewController {
  130. _currentChildViewController = currentChildViewController;
  131. if (![_currentChildViewController isEqual:_tvc]) {
  132. [_tvc releaseDevicesTimer];
  133. }
  134. }
  135. #pragma mark - Main Logic
  136. - (void)updateHomeHubStatusToChildViewController {
  137. // [self setContents];
  138. //[self checkTempPassword];
  139. if (_tvc) {
  140. [_tvc updateHomeHubStatusToDevices];
  141. }
  142. if (_rvc) {
  143. [_rvc updateHomeHubStatusToRules];
  144. }
  145. if (_mvc) {
  146. [_mvc updateHomeHubStatusToMembers];
  147. }
  148. if (_manageVc) {
  149. [_manageVc updateHomeHubStatusToManage];
  150. }
  151. }
  152. - (void)updateCurrentViewController {
  153. if ([_currentChildViewController isEqual:_tvc]) {
  154. [_tvc prepareViewDidLoadRefreshPopup];
  155. } else if ([_currentChildViewController isEqual:_rvc]) {
  156. [_rvc prepareViewDidLoad];
  157. } else if ([_currentChildViewController isEqual:_mvc]) {
  158. [_mvc prepareViewDidLoad];
  159. } else if([_currentChildViewController isEqual:_manageVc]) {
  160. [_manageVc prepareViewDidLoad];
  161. }
  162. }
  163. - (void)requestDashboard:(JDFacadeCompletionCallBackHandler)completion {
  164. //parameters
  165. NSDictionary *parameter = @{@"member_id": [JDFacade facade].loginUser.memberId,
  166. @"notice_read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString,
  167. @"message_read_datetime": [JDFacade facade].loginUser.personalNoticeReadTime ? [JDFacade facade].loginUser.personalNoticeReadTime : ksEmptyString};
  168. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_POST_DASHBOARD arguments:nil];
  169. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[DashboardModel class] completion:^(id responseObject) {
  170. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  171. return;
  172. }
  173. DashboardModel *dashboard = (DashboardModel *) responseObject;
  174. NSLog(@"DashBoard Info : %@", responseObject);
  175. if (dashboard) {//API 성공 ,
  176. _dashboard = dashboard;
  177. if (!_isNotFirstLoading) {
  178. _isNotFirstLoading = YES;
  179. // FIXME : 이부분이 어떤 내용인지 파악해야 한다.
  180. // [JDFacade facade].loginUser.homehubDeviceId = _dashboard.deviceId; //초대에서 넘어오는 경우,
  181. //when first loading this view.
  182. [self initThingsViewController];
  183. self.currentChildViewController = _tvc;
  184. [_btnThings setHighlighted:YES];
  185. }
  186. // [self setContents];
  187. [self checkTempPassword];
  188. }
  189. if (completion) {
  190. completion();
  191. }
  192. } failure:^(id errorObject) {
  193. JDErrorModel *error = (JDErrorModel *)errorObject;
  194. [[JDFacade facade] alert:error.errorMessage];
  195. }];
  196. }
  197. - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
  198. //parameters
  199. NSDictionary *parameter = @{@"image_file": profileImage};
  200. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
  201. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  202. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  203. return;
  204. }
  205. LoginModel *response = (LoginModel *)responseObject;
  206. if (response) {//API 성공 ,
  207. _dashboard.imageFileName = response.profileImage;
  208. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.profileImage] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  209. [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
  210. }
  211. } failure:^(id errorObject) {
  212. JDErrorModel *error = (JDErrorModel *)errorObject;
  213. [[JDFacade facade] alert:error.errorMessage];
  214. }];
  215. }
  216. ////extendView 컨텐츠 설정
  217. //- (void)setContents {
  218. //
  219. // _lblNoticeCount.text = _dashboard.noticeCount;
  220. // _lblPushCount.text = _dashboard.pushCount;
  221. // _imgvHomehubOff.hidden = [JDFacade facade].loginUser.isHomehubOnline;
  222. //
  223. //
  224. // _btnHomeHub.enabled = [JDFacade facade].loginUser.hasHomeHub;
  225. // _btnHomeHub.alpha = _btnHomeHub.enabled ? 1.0f : 0.4f;
  226. //
  227. ////don't delete - FOR KNEET2.0
  228. //// [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:_dashboard.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  229. //
  230. // NSInteger i = 0;
  231. // for (ModeModel *mode in _dashboard.modeList) {//모드버튼 설정
  232. // CustomLabelButton *btn = _btnModes[i];
  233. //
  234. // [btn setTitle:mode.modeName];
  235. //
  236. // if (![JDFacade facade].loginUser.hasHomeHub) {//비활성모드 : 홈허브가 없을 경우, 또는 삭제된 경우,
  237. // [btn sd_setImageWithURL:[NSURL URLWithString:mode.imageFileDisableName] forState:UIControlStateNormal];
  238. // btn.label.textColor = kUITextColor04;
  239. // btn.label.alpha = 0.4f;
  240. // btn.enabled = NO;
  241. //
  242. // } else {
  243. //
  244. // if ([mode.useYn boolValue]) {//활성모드
  245. // [btn sd_setImageWithURL:[NSURL URLWithString:mode.imageFileActiveName] forState:UIControlStateNormal];
  246. // btn.label.textColor = kUITextColor01;
  247. // btn.label.alpha = 1.0f;
  248. // btn.enabled = YES;
  249. //
  250. // } else {//일반모드
  251. // [btn sd_setImageWithURL:[NSURL URLWithString:mode.imageFileName] forState:UIControlStateNormal];
  252. // btn.label.textColor = kUITextColor04;
  253. // btn.label.alpha = 1.0f;
  254. // btn.enabled = YES;
  255. // }
  256. // }
  257. //
  258. // i++;
  259. // }
  260. //
  261. // [self checkTempPassword];
  262. //}
  263. // TODO : 임시 비밀번호 변경관련 작업 할것.
  264. - (void)checkTempPassword {
  265. if ([JDFacade facade].loginUser.tempPasswordYn && [[JDFacade facade].loginUser.tempPasswordYn boolValue]) {
  266. [[JDFacade facade] confirm:@"임시 비밀번호를 변경해주세요" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  267. if (buttonIndex == 0) {//OK
  268. PwdPopupView *popup = [[PwdPopupView alloc] initFromNib];
  269. [popup show];
  270. }
  271. }];
  272. }
  273. }
  274. - (void)updateRulesListRecently {
  275. [_rvc prepareViewDidLoad];
  276. }
  277. #pragma mark - ImageUtil delegate
  278. - (void)didFinishPickingImage:(UIImage *)image {
  279. [self requestUpdateHomeGroupImage:image];
  280. }
  281. #pragma mark - SocketServiceHandler delegate
  282. -(void)socketConnectComplete
  283. {
  284. // NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
  285. //
  286. // data[@"message_type"] = @"auth";
  287. // data[@"member_id"] = @"100";
  288. // data[@"password"] = @"1234";
  289. // data[@"os_type"] = @"MA";
  290. // data[@"cust_id"] = @"1002";
  291. // data[@"ctrt_grp_id"] = @"1002";
  292. //
  293. //
  294. // [[SocketServiceHandler sharedManager] sendData:data];
  295. }
  296. #pragma mark - UI Control
  297. - (void)loadThingsViewController {
  298. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  299. if (!_tvc) {
  300. dispatch_async(dispatch_get_main_queue(), ^{
  301. [self initThingsViewController];
  302. });
  303. }
  304. } completionHandler:^{
  305. CGPoint cpoint = CGPointMake(0, 0);
  306. [_scrollView setContentOffset:cpoint animated:NO];
  307. [_btnThings setHighlighted:YES];
  308. [_btnRules setHighlighted:NO];
  309. [_btnMembers setHighlighted:NO];
  310. [_btnExtend setHighlighted:NO];
  311. [_tvc prepareViewDidLoadRefreshPopup];
  312. self.currentChildViewController = _tvc;
  313. }];
  314. }
  315. - (void)loadRulesViewController {
  316. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  317. if (!_rvc) {
  318. dispatch_async(dispatch_get_main_queue(), ^{
  319. [self initRulesViewController];
  320. });
  321. }
  322. } completionHandler:^{
  323. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  324. [_scrollView setContentOffset:cpoint animated:NO];
  325. [_btnThings setHighlighted:NO];
  326. [_btnRules setHighlighted:YES];
  327. [_btnMembers setHighlighted:NO];
  328. [_btnExtend setHighlighted:NO];
  329. [_rvc prepareViewDidLoad];
  330. self.currentChildViewController = _rvc;
  331. }];
  332. }
  333. - (void)loadMembersViewController {
  334. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  335. if (!_mvc) {
  336. dispatch_async(dispatch_get_main_queue(), ^{
  337. [self initMembersViewController];
  338. });
  339. }
  340. } completionHandler:^{
  341. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
  342. [_scrollView setContentOffset:cpoint animated:NO];
  343. [_btnThings setHighlighted:NO];
  344. [_btnRules setHighlighted:NO];
  345. [_btnMembers setHighlighted:YES];
  346. [_btnExtend setHighlighted:NO];
  347. [_mvc prepareViewDidLoad];
  348. self.currentChildViewController = _mvc;
  349. }];
  350. }
  351. - (void)loadManagementViewController {
  352. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  353. if (!_manageVc) {
  354. dispatch_async(dispatch_get_main_queue(), ^{
  355. [self initManagementViewController];
  356. });
  357. }
  358. } completionHandler:^{
  359. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 3, 0);
  360. [_scrollView setContentOffset:cpoint animated:NO];
  361. [_btnThings setHighlighted:NO];
  362. [_btnRules setHighlighted:NO];
  363. [_btnMembers setHighlighted:NO];
  364. [_btnExtend setHighlighted:YES];
  365. [_manageVc prepareViewDidLoad];
  366. self.currentChildViewController = _manageVc;
  367. }];
  368. }
  369. #pragma mark - UI Events
  370. - (IBAction)btnThingsTouched:(id)sender {
  371. [self loadThingsViewController];
  372. }
  373. - (IBAction)btnRuleTouched:(id)sender {
  374. [self loadRulesViewController];
  375. }
  376. - (IBAction)btnMemberTouched:(id)sender {
  377. [self loadMembersViewController];
  378. }
  379. - (IBAction)btnExtendTouched:(id)sender {
  380. [self loadManagementViewController];
  381. // _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  382. //
  383. // [UIView animateWithDuration:kfAnimationDur animations:^{
  384. // [self.view layoutIfNeeded];
  385. // } completion:^(BOOL finished) {
  386. // [self requestDashboard:nil];
  387. // }];
  388. }
  389. //- (IBAction)btnCollapseTouched:(id)sender {
  390. //
  391. // _constraintExpandViewTop.constant = 0;
  392. //
  393. // [UIView animateWithDuration:kfAnimationDur animations:^{
  394. // [self.view layoutIfNeeded];
  395. // } completion:^(BOOL finished) {
  396. //
  397. // if ([_currentChildViewController isEqual:_tvc]) {
  398. // [_tvc prepareViewDidLoad];
  399. // }
  400. // }];
  401. //}
  402. //- (IBAction)btnProfileTouched:(id)sender {
  403. // if (!_imageUtil) {
  404. // _imageUtil = [[ImageUtil alloc] init];
  405. // _imageUtil.delegate = self;
  406. // }
  407. //
  408. // [_imageUtil prepareImagePicker:self];
  409. //}
  410. //- (IBAction)btnNoticeTouched:(id)sender {
  411. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NoticeViewController" storyboardName:@"Main"];
  412. // [self presentViewController:vc animated:YES completion:nil];
  413. //}
  414. //
  415. //- (void)btnMessageBoxTouched:(id)sender {
  416. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MessageBoxViewController" storyboardName:@"Main"];
  417. // [self presentViewController:vc animated:YES completion:nil];
  418. //}
  419. //
  420. //- (void)btnHomeHubTouched:(id)sender {
  421. //
  422. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
  423. // [self presentViewController:vc animated:YES completion:nil];
  424. //}
  425. //
  426. //- (IBAction)btnSettingsTouched:(id)sender {
  427. //
  428. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsViewController" storyboardName:@"Settings"];
  429. // [self presentViewController:vc animated:YES completion:nil];
  430. //}
  431. //
  432. //- (IBAction)btnModeHomeTouched:(id)sender {
  433. // [self doChangeHomeMode:HOME_MODE_HOME];
  434. //}
  435. //
  436. //- (IBAction)btnModeAwayTouched:(id)sender {
  437. // [self doChangeHomeMode:HOME_MODE_AWAY];
  438. //}
  439. //
  440. //- (IBAction)btnModeMorningTouched:(id)sender {
  441. // [self doChangeHomeMode:HOME_MODE_MORNING];
  442. //}
  443. //
  444. //- (IBAction)btnModeNightTouched:(id)sender {
  445. // [self doChangeHomeMode:HOME_MODE_NIGHT];
  446. //}
  447. //
  448. //- (void)doChangeHomeMode:(NSString *)modeCode {
  449. //
  450. // if (![JDFacade facade].loginUser.isHomehubOnline || ![JDFacade facade].loginUser.hasHomeHub) {
  451. // [[JDFacade facade] toast:@"홈허브가 온라인 상태로\n연결되어 있을 때 시도해주세요"];
  452. // return;
  453. // }
  454. //
  455. // ModeModel *mode = [_dashboard.modeList objectKey:@"basicModeCode" eqaulToString:modeCode];
  456. ////로직 변경
  457. //// if (!mode.homegrpSceneId || [mode.homegrpSceneId isEmptyString]) {
  458. ////
  459. //// if ([JDFacade facade].loginUser.level < 90) {//일반 멤버
  460. //// [[JDFacade facade] toast:@"아직 선택된 장치가 없습니다.\r장치선택은 마스터만 할 수 있습니다."];
  461. //// } else {
  462. //// HomeModeSettingsViewController *vc = (HomeModeSettingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeSettingsViewController" storyboardName:@"Main"];
  463. //// vc.mode = mode;
  464. ////
  465. //// [self presentViewController:vc animated:YES completion:nil];
  466. //// }
  467. //// return;
  468. //// }
  469. //
  470. // HomeModeUpdateViewController *vc = (HomeModeUpdateViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeUpdateViewController"
  471. // storyboardName:@"Main"];
  472. // vc.mode = mode;
  473. // [self presentViewController:vc animated:YES completion:nil];
  474. //}
  475. //
  476. //- (IBAction)btnDashboardRefresh:(id)sender {
  477. // [self requestDashboard:nil];
  478. //}
  479. #pragma mark - MemoryWarning
  480. - (void)didReceiveMemoryWarning
  481. {
  482. [super didReceiveMemoryWarning];
  483. // Dispose of any resources that can be recreated.
  484. }
  485. @end