MainViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. [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 = @{@"device_id": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString};
  150. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_DASHBOARD aditional:nil];
  151. // NSString *path = [NSString stringWithFormat:API_GET_DASHBOARD, [[JDFacade facade].loginUser custId], [[JDFacade facade].loginUser ctrtGrpId]];
  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. // TODO : 임시 비밀번호 변경관련 작업 할것.
  234. - (void)checkTempPassword {
  235. if ([JDFacade facade].loginUser.tempPasswordYn && [[JDFacade facade].loginUser.tempPasswordYn boolValue]) {
  236. [[JDFacade facade] confirm:@"임시 비밀번호를 변경해주세요" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  237. if (buttonIndex == 0) {//OK
  238. PwdPopupView *popup = [[PwdPopupView alloc] initFromNib];
  239. [popup show];
  240. }
  241. }];
  242. }
  243. }
  244. - (void)updateRulesListRecently {
  245. [_rvc prepareViewDidLoad];
  246. }
  247. #pragma mark - ImageUtil delegate
  248. - (void)didFinishPickingImage:(UIImage *)image {
  249. [self requestUpdateHomeGroupImage:image];
  250. }
  251. #pragma mark - SocketServiceHandler delegate
  252. -(void)socketConnectComplete
  253. {
  254. // NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
  255. //
  256. // data[@"message_type"] = @"auth";
  257. // data[@"member_id"] = @"100";
  258. // data[@"password"] = @"1234";
  259. // data[@"os_type"] = @"MA";
  260. // data[@"cust_id"] = @"1002";
  261. // data[@"ctrt_grp_id"] = @"1002";
  262. //
  263. //
  264. // [[SocketServiceHandler sharedManager] sendData:data];
  265. }
  266. #pragma mark - UI Control
  267. - (void)loadThingsViewController {
  268. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  269. if (!_tvc) {
  270. dispatch_async(dispatch_get_main_queue(), ^{
  271. [self initThingsViewController];
  272. });
  273. }
  274. } completionHandler:^{
  275. CGPoint cpoint = CGPointMake(0, 0);
  276. [_scrollView setContentOffset:cpoint animated:NO];
  277. [_btnThings setHighlighted:YES];
  278. [_btnRules setHighlighted:NO];
  279. [_btnMembers setHighlighted:NO];
  280. [_tvc prepareViewDidLoad];
  281. self.currentChildViewController = _tvc;
  282. }];
  283. }
  284. - (void)loadRulesViewController {
  285. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  286. if (!_rvc) {
  287. dispatch_async(dispatch_get_main_queue(), ^{
  288. [self initRulesViewController];
  289. });
  290. }
  291. } completionHandler:^{
  292. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  293. [_scrollView setContentOffset:cpoint animated:NO];
  294. [_btnThings setHighlighted:NO];
  295. [_btnRules setHighlighted:YES];
  296. [_btnMembers setHighlighted:NO];
  297. [_rvc prepareViewDidLoad];
  298. self.currentChildViewController = _rvc;
  299. }];
  300. }
  301. - (void)loadMembersViewController {
  302. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  303. if (!_mvc) {
  304. dispatch_async(dispatch_get_main_queue(), ^{
  305. [self initMembersViewController];
  306. });
  307. }
  308. } completionHandler:^{
  309. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
  310. [_scrollView setContentOffset:cpoint animated:NO];
  311. [_btnThings setHighlighted:NO];
  312. [_btnRules setHighlighted:NO];
  313. [_btnMembers setHighlighted:YES];
  314. [_mvc prepareViewDidLoad];
  315. self.currentChildViewController = _mvc;
  316. }];
  317. }
  318. #pragma mark - UI Events
  319. - (IBAction)btnThingsTouched:(id)sender {
  320. [self loadThingsViewController];
  321. }
  322. - (IBAction)btnRuleTouched:(id)sender {
  323. [self loadRulesViewController];
  324. }
  325. - (IBAction)btnMemberTouched:(id)sender {
  326. [self loadMembersViewController];
  327. }
  328. - (IBAction)btnExtendTouched:(id)sender {
  329. _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  330. [UIView animateWithDuration:kfAnimationDur animations:^{
  331. [self.view layoutIfNeeded];
  332. } completion:^(BOOL finished) {
  333. [self requestDashboard:nil];
  334. }];
  335. }
  336. - (IBAction)btnCollapseTouched:(id)sender {
  337. _constraintExpandViewTop.constant = 0;
  338. [UIView animateWithDuration:kfAnimationDur animations:^{
  339. [self.view layoutIfNeeded];
  340. } completion:^(BOOL finished) {
  341. if ([_currentChildViewController isEqual:_tvc]) {
  342. [_tvc prepareViewDidLoad];
  343. }
  344. }];
  345. }
  346. - (IBAction)btnProfileTouched:(id)sender {
  347. if (!_imageUtil) {
  348. _imageUtil = [[ImageUtil alloc] init];
  349. _imageUtil.delegate = self;
  350. }
  351. [_imageUtil prepareImagePicker:self];
  352. }
  353. - (IBAction)btnNoticeTouched:(id)sender {
  354. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NoticeViewController" storyboardName:@"Main"];
  355. [self presentViewController:vc animated:YES completion:nil];
  356. }
  357. - (void)btnMessageBoxTouched:(id)sender {
  358. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MessageBoxViewController" storyboardName:@"Main"];
  359. [self presentViewController:vc animated:YES completion:nil];
  360. }
  361. - (void)btnHomeHubTouched:(id)sender {
  362. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
  363. [self presentViewController:vc animated:YES completion:nil];
  364. }
  365. - (IBAction)btnSettingsTouched:(id)sender {
  366. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsViewController" storyboardName:@"Settings"];
  367. [self presentViewController:vc animated:YES completion:nil];
  368. }
  369. - (IBAction)btnModeHomeTouched:(id)sender {
  370. [self doChangeHomeMode:HOME_MODE_HOME];
  371. }
  372. - (IBAction)btnModeAwayTouched:(id)sender {
  373. [self doChangeHomeMode:HOME_MODE_AWAY];
  374. }
  375. - (IBAction)btnModeMorningTouched:(id)sender {
  376. [self doChangeHomeMode:HOME_MODE_MORNING];
  377. }
  378. - (IBAction)btnModeNightTouched:(id)sender {
  379. [self doChangeHomeMode:HOME_MODE_NIGHT];
  380. }
  381. - (void)doChangeHomeMode:(NSString *)modeCode {
  382. if (![JDFacade facade].loginUser.isHomehubOnline || ![JDFacade facade].loginUser.hasHomeHub) {
  383. [[JDFacade facade] toast:@"홈허브가 온라인 상태로\n연결되어 있을 때 시도해주세요"];
  384. return;
  385. }
  386. ModeModel *mode = [_dashboard.modeList objectKey:@"basicModeCode" eqaulToString:modeCode];
  387. //로직 변경
  388. // if (!mode.homegrpSceneId || [mode.homegrpSceneId isEmptyString]) {
  389. //
  390. // if ([JDFacade facade].loginUser.level < 90) {//일반 멤버
  391. // [[JDFacade facade] toast:@"아직 선택된 장치가 없습니다.\r장치선택은 마스터만 할 수 있습니다."];
  392. // } else {
  393. // HomeModeSettingsViewController *vc = (HomeModeSettingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeSettingsViewController" storyboardName:@"Main"];
  394. // vc.mode = mode;
  395. //
  396. // [self presentViewController:vc animated:YES completion:nil];
  397. // }
  398. // return;
  399. // }
  400. HomeModeUpdateViewController *vc = (HomeModeUpdateViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeUpdateViewController"
  401. storyboardName:@"Main"];
  402. vc.mode = mode;
  403. [self presentViewController:vc animated:YES completion:nil];
  404. }
  405. - (IBAction)btnDashboardRefresh:(id)sender {
  406. [self requestDashboard:nil];
  407. }
  408. #pragma mark - MemoryWarning
  409. - (void)didReceiveMemoryWarning
  410. {
  411. [super didReceiveMemoryWarning];
  412. // Dispose of any resources that can be recreated.
  413. }
  414. @end