MainViewController.m 17 KB

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