MainViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 = ViewHeight(_scrollView); //IPHONE_HEIGHT - kfMainTabBarHeight;
  50. [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, IPHONE_HEIGHT)];
  51. _scrollView.pagingEnabled = YES;
  52. _scrollView.scrollEnabled = NO;
  53. CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_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. }
  67. - (void)initThingsViewController {
  68. if (!_tvc) {
  69. _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
  70. }
  71. [self addChildViewController:_tvc];
  72. [_tvc didMoveToParentViewController:self];
  73. if ([self isViewLoaded]) {
  74. [_tvc beginAppearanceTransition:YES animated:NO];
  75. [_thingsContainerView addSubview:_tvc.view];
  76. [_tvc endAppearanceTransition];
  77. }
  78. }
  79. - (void)initRulesViewController {
  80. if (!_rvc) {
  81. _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
  82. }
  83. [self addChildViewController:_rvc];
  84. [_rvc didMoveToParentViewController:self];
  85. if ([self isViewLoaded]) {
  86. [_rvc beginAppearanceTransition:YES animated:NO];
  87. [_rulesContainerView addSubview:_rvc.view];
  88. [_rvc endAppearanceTransition];
  89. }
  90. }
  91. - (void)initMembersViewController {
  92. if (!_mvc) {
  93. _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
  94. }
  95. [self addChildViewController:_mvc];
  96. [_mvc didMoveToParentViewController:self];
  97. if ([self isViewLoaded]) {
  98. [_mvc beginAppearanceTransition:YES animated:NO];
  99. [_membersContainerView addSubview:_mvc.view];
  100. [_mvc endAppearanceTransition];
  101. }
  102. }
  103. - (void)viewWillAppear:(BOOL)animated {
  104. [self requestDashboard:nil];
  105. }
  106. - (void)prepareViewDidLoad {
  107. // [self requestDashboard:nil];
  108. // [[JDFacade facade] requestPollingHomeHubStatusInBackground];
  109. // TODO: 웹소켓 연결해서 정보 받아오는 형식으로 전환할것
  110. }
  111. - (void)setCurrentChildViewController:(JDViewController *)currentChildViewController {
  112. _currentChildViewController = currentChildViewController;
  113. if (![_currentChildViewController isEqual:_tvc]) {
  114. [_tvc releaseDevicesTimer];
  115. }
  116. }
  117. #pragma mark - Main Logic
  118. - (void)updateHomeHubStatusToChildViewController {
  119. [self setContents];
  120. if (_tvc) {
  121. [_tvc updateHomeHubStatusToDevices];
  122. }
  123. if (_rvc) {
  124. [_rvc updateHomeHubStatusToRules];
  125. }
  126. if (_mvc) {
  127. [_mvc updateHomeHubStatusToMembers];
  128. }
  129. }
  130. - (void)updateCurrentViewController {
  131. if ([_currentChildViewController isEqual:_tvc]) {
  132. [_tvc prepareViewDidLoad];
  133. } else if ([_currentChildViewController isEqual:_rvc]) {
  134. [_rvc prepareViewDidLoad];
  135. } else if ([_currentChildViewController isEqual:_mvc]) {
  136. [_mvc prepareViewDidLoad];
  137. }
  138. }
  139. - (void)requestDashboard:(JDFacadeCompletionCallBackHandler)completion {
  140. //parameters
  141. NSDictionary *parameter = @{@"member_id": [JDFacade facade].loginUser.memberId,
  142. @"notice_read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString,
  143. @"message_read_datetime": [JDFacade facade].loginUser.personalNoticeReadTime ? [JDFacade facade].loginUser.personalNoticeReadTime : ksEmptyString};
  144. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_DASHBOARD arguments:nil];
  145. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DashboardModel class] completion:^(id responseObject) {
  146. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  147. return;
  148. }
  149. DashboardModel *dashboard = (DashboardModel *) responseObject;
  150. NSLog(@"DashBoard Info : %@", responseObject);
  151. if (dashboard) {//API 성공 ,
  152. _dashboard = dashboard;
  153. if (!_isNotFirstLoading) {
  154. _isNotFirstLoading = YES;
  155. // FIXME : 이부분이 어떤 내용인지 파악해야 한다.
  156. // [JDFacade facade].loginUser.homehubDeviceId = _dashboard.deviceId; //초대에서 넘어오는 경우,
  157. //when first loading this view.
  158. [self initThingsViewController];
  159. self.currentChildViewController = _tvc;
  160. [_btnThings setHighlighted:YES];
  161. }
  162. [self setContents];
  163. }
  164. if (completion) {
  165. completion();
  166. }
  167. } failure:^(id errorObject) {
  168. JDErrorModel *error = (JDErrorModel *)errorObject;
  169. [[JDFacade facade] alert:error.errorMessage];
  170. }];
  171. }
  172. - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
  173. //parameters
  174. NSDictionary *parameter = @{@"image_file": profileImage};
  175. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
  176. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  177. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  178. return;
  179. }
  180. LoginModel *response = (LoginModel *)responseObject;
  181. if (response) {//API 성공 ,
  182. _dashboard.imageFileName = response.profileImage;
  183. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.profileImage] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  184. [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
  185. }
  186. } failure:^(id errorObject) {
  187. JDErrorModel *error = (JDErrorModel *)errorObject;
  188. [[JDFacade facade] alert:error.errorMessage];
  189. }];
  190. }
  191. //extendView 컨텐츠 설정
  192. - (void)setContents {
  193. _lblNoticeCount.text = _dashboard.noticeCount;
  194. _lblPushCount.text = _dashboard.pushCount;
  195. _imgvHomehubOff.hidden = [JDFacade facade].loginUser.isHomehubOnline;
  196. _btnHomeHub.enabled = [JDFacade facade].loginUser.hasHomeHub;
  197. _btnHomeHub.alpha = _btnHomeHub.enabled ? 1.0f : 0.4f;
  198. //don't delete - FOR KNEET2.0
  199. // [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:_dashboard.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  200. NSInteger i = 0;
  201. for (ModeModel *mode in _dashboard.modeList) {//모드버튼 설정
  202. CustomLabelButton *btn = _btnModes[i];
  203. [btn setTitle:mode.modeName];
  204. if (![JDFacade facade].loginUser.hasHomeHub) {//비활성모드 : 홈허브가 없을 경우, 또는 삭제된 경우,
  205. [btn sd_setImageWithURL:[NSURL URLWithString:mode.imageFileDisableName] forState:UIControlStateNormal];
  206. btn.label.textColor = kUITextColor04;
  207. btn.label.alpha = 0.4f;
  208. btn.enabled = NO;
  209. } else {
  210. if ([mode.useYn boolValue]) {//활성모드
  211. [btn sd_setImageWithURL:[NSURL URLWithString:mode.imageFileActiveName] forState:UIControlStateNormal];
  212. btn.label.textColor = kUITextColor01;
  213. btn.label.alpha = 1.0f;
  214. btn.enabled = YES;
  215. } else {//일반모드
  216. [btn sd_setImageWithURL:[NSURL URLWithString:mode.imageFileName] forState:UIControlStateNormal];
  217. btn.label.textColor = kUITextColor04;
  218. btn.label.alpha = 1.0f;
  219. btn.enabled = YES;
  220. }
  221. }
  222. i++;
  223. }
  224. [self checkTempPassword];
  225. }
  226. // TODO : 임시 비밀번호 변경관련 작업 할것.
  227. - (void)checkTempPassword {
  228. if ([JDFacade facade].loginUser.tempPasswordYn && [[JDFacade facade].loginUser.tempPasswordYn boolValue]) {
  229. [[JDFacade facade] confirm:@"임시 비밀번호를 변경해주세요" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  230. if (buttonIndex == 0) {//OK
  231. PwdPopupView *popup = [[PwdPopupView alloc] initFromNib];
  232. [popup show];
  233. }
  234. }];
  235. }
  236. }
  237. - (void)updateRulesListRecently {
  238. [_rvc prepareViewDidLoad];
  239. }
  240. #pragma mark - ImageUtil delegate
  241. - (void)didFinishPickingImage:(UIImage *)image {
  242. [self requestUpdateHomeGroupImage:image];
  243. }
  244. #pragma mark - SocketServiceHandler delegate
  245. -(void)socketConnectComplete
  246. {
  247. // NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
  248. //
  249. // data[@"message_type"] = @"auth";
  250. // data[@"member_id"] = @"100";
  251. // data[@"password"] = @"1234";
  252. // data[@"os_type"] = @"MA";
  253. // data[@"cust_id"] = @"1002";
  254. // data[@"ctrt_grp_id"] = @"1002";
  255. //
  256. //
  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