MainViewController.m 16 KB

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