MainViewController.m 17 KB

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