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