MainViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. @interface MainViewController () <ImageUtilDelegate> {
  22. RulesViewController *_rvc;
  23. HomeMemberViewController *_mvc;
  24. ImageUtil *_imageUtil;
  25. NSDate *_readDateTime;
  26. DashboardModel *_dashboard;
  27. }
  28. @property (strong, nonatomic) UIView *thingsContainerView;
  29. @property (strong, nonatomic) UIView *rulesContainerView;
  30. @property (strong, nonatomic) UIView *membersContainerView;
  31. @end
  32. #pragma mark - Class Definition
  33. @implementation MainViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. [self initUI];
  38. [self prepareViewDidLoad];
  39. }
  40. - (void)initUI {
  41. //1. init tabBar
  42. //1-1. scroll top bottom
  43. //2. init scrollView
  44. CGFloat height = IPHONE_HEIGHT - kfMainTabBarHeight;
  45. [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, height)];
  46. _scrollView.pagingEnabled = YES;
  47. _scrollView.scrollEnabled = NO;
  48. CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, height);
  49. //init child view for childViewController
  50. _thingsContainerView = [[UIView alloc] initWithFrame:sr];
  51. _rulesContainerView = [[UIView alloc] initWithFrame:sr];
  52. _membersContainerView = [[UIView alloc] initWithFrame:sr];
  53. [_scrollView addSubview:_thingsContainerView];
  54. _rulesContainerView.x = IPHONE_WIDTH;
  55. [_scrollView addSubview:_rulesContainerView];
  56. _membersContainerView.x = IPHONE_WIDTH * 2;
  57. [_scrollView addSubview:_membersContainerView];
  58. //when first loading this view.
  59. [self initThingsViewController];
  60. _btnHomeHub.enabled = !(![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]);
  61. }
  62. - (void)initThingsViewController {
  63. if (!_tvc) {
  64. _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
  65. }
  66. [self addChildViewController:_tvc];
  67. [_tvc didMoveToParentViewController:self];
  68. if ([self isViewLoaded]) {
  69. [_tvc beginAppearanceTransition:YES animated:NO];
  70. [_thingsContainerView addSubview:_tvc.view];
  71. [_tvc endAppearanceTransition];
  72. }
  73. }
  74. - (void)initRulesViewController {
  75. if (!_rvc) {
  76. _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
  77. }
  78. [self addChildViewController:_rvc];
  79. [_rvc didMoveToParentViewController:self];
  80. if ([self isViewLoaded]) {
  81. [_rvc beginAppearanceTransition:YES animated:NO];
  82. [_rulesContainerView addSubview:_rvc.view];
  83. [_rvc endAppearanceTransition];
  84. }
  85. }
  86. - (void)initMembersViewController {
  87. if (!_mvc) {
  88. _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
  89. }
  90. [self addChildViewController:_mvc];
  91. [_mvc didMoveToParentViewController:self];
  92. if ([self isViewLoaded]) {
  93. [_mvc beginAppearanceTransition:YES animated:NO];
  94. [_membersContainerView addSubview:_mvc.view];
  95. [_mvc endAppearanceTransition];
  96. }
  97. }
  98. - (void)prepareViewDidLoad {
  99. [self requestDashboard];
  100. [[JDFacade facade] requestPollingHomeHubStatusInBackground];
  101. }
  102. #pragma mark - Main Logic
  103. - (void)updateHomeHubStatusToChildViewController {
  104. if (_tvc) {
  105. [_tvc updateHomeHubStatusToDevices];
  106. }
  107. if (_rvc) {
  108. [_rvc updateHomeHubStatusToRules];
  109. }
  110. if (_mvc) {
  111. [_mvc updateHomeHubStatusToMembers];
  112. }
  113. }
  114. - (void)requestDashboard {
  115. //parameters
  116. NSDictionary *parameter = @{@"read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString};
  117. NSString *path = [NSString stringWithFormat:API_GET_DASHBOARD];
  118. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DashboardModel class] completion:^(id responseObject) {
  119. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  120. return;
  121. }
  122. DashboardModel *dashboard = (DashboardModel *) responseObject;
  123. if (dashboard) {//API 성공 ,
  124. _dashboard = dashboard;
  125. [self setContents];
  126. }
  127. } failure:^(id errorObject) {
  128. JDErrorModel *error = (JDErrorModel *)errorObject;
  129. [[JDFacade facade] alert:error.errorMessage];
  130. }];
  131. }
  132. - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
  133. //parameters
  134. NSDictionary *parameter = @{@"image_file": profileImage};
  135. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
  136. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  137. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  138. return;
  139. }
  140. LoginModel *response = (LoginModel *)responseObject;
  141. if (response) {//API 성공 ,
  142. _dashboard.imageFileName = response.imageFileName;
  143. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  144. [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
  145. }
  146. } failure:^(id errorObject) {
  147. JDErrorModel *error = (JDErrorModel *)errorObject;
  148. [[JDFacade facade] alert:error.errorMessage];
  149. }];
  150. }
  151. - (void)setContents {
  152. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:_dashboard.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  153. }
  154. #pragma mark - ImageUtil delegate
  155. - (void)didFinishPickingImage:(UIImage *)image {
  156. [self requestUpdateHomeGroupImage:image];
  157. }
  158. #pragma mark - UI Control
  159. - (void)loadThingsViewController {
  160. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  161. if (!_tvc) {
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. [self initThingsViewController];
  164. });
  165. }
  166. } completionHandler:^{
  167. CGPoint cpoint = CGPointMake(0, 0);
  168. [_scrollView setContentOffset:cpoint animated:NO];
  169. [_tvc prepareViewDidLoad];
  170. }];
  171. }
  172. - (void)loadRulesViewController {
  173. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  174. if (!_rvc) {
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. [self initRulesViewController];
  177. });
  178. }
  179. } completionHandler:^{
  180. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  181. [_scrollView setContentOffset:cpoint animated:NO];
  182. // [_rvc prepareViewDidLoad];
  183. }];
  184. }
  185. - (void)loadMembersViewController {
  186. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  187. if (!_mvc) {
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. [self initMembersViewController];
  190. });
  191. }
  192. } completionHandler:^{
  193. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
  194. [_scrollView setContentOffset:cpoint animated:NO];
  195. }];
  196. }
  197. #pragma mark - UI Events
  198. - (IBAction)btnThingsTouched:(id)sender {
  199. [self loadThingsViewController];
  200. }
  201. - (IBAction)btnRuleTouched:(id)sender {
  202. [self loadRulesViewController];
  203. }
  204. - (IBAction)btnMemberTouched:(id)sender {
  205. [self loadMembersViewController];
  206. }
  207. - (IBAction)btnExtendTouched:(id)sender {
  208. _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  209. [UIView animateWithDuration:kfAnimationDur animations:^{
  210. [self.view layoutIfNeeded];
  211. }];
  212. }
  213. - (IBAction)btnCollapseTouched:(id)sender {
  214. _constraintExpandViewTop.constant = 0;
  215. [UIView animateWithDuration:kfAnimationDur animations:^{
  216. [self.view layoutIfNeeded];
  217. }];
  218. }
  219. - (IBAction)btnProfileTouched:(id)sender {
  220. if (!_imageUtil) {
  221. _imageUtil = [[ImageUtil alloc] init];
  222. _imageUtil.delegate = self;
  223. }
  224. [_imageUtil prepareImagePicker];
  225. }
  226. - (IBAction)btnNoticeTouched:(id)sender {
  227. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NoticeViewController" storyboardName:@"Main"];
  228. [self presentViewController:vc animated:YES completion:nil];
  229. }
  230. - (void)btnMessageBoxTouched:(id)sender {
  231. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MessageBoxViewController" storyboardName:@"Main"];
  232. [self presentViewController:vc animated:YES completion:nil];
  233. }
  234. - (void)btnHomeHubTouched:(id)sender {
  235. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
  236. [self presentViewController:vc animated:YES completion:nil];
  237. }
  238. - (IBAction)btnSettingsTouched:(id)sender {
  239. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsViewController" storyboardName:@"Settings"];
  240. [self presentViewController:vc animated:YES completion:nil];
  241. }
  242. - (IBAction)btnModeHomeTouched:(id)sender {
  243. [self doChangeHomeMode:HOME_MODE_HOME];
  244. }
  245. - (IBAction)btnModeAwayTouched:(id)sender {
  246. [self doChangeHomeMode:HOME_MODE_AWAY];
  247. }
  248. - (IBAction)btnModeMorningTouched:(id)sender {
  249. [self doChangeHomeMode:HOME_MODE_MORNING];
  250. }
  251. - (IBAction)btnModeNightTouched:(id)sender {
  252. [self doChangeHomeMode:HOME_MODE_NIGHT];
  253. }
  254. - (void)doChangeHomeMode:(NSString *)modeCode {
  255. if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
  256. [[JDFacade facade] toast:@"홈허브가 온라인 상태로\n연결되어 있을 때 시도해주세요"];
  257. return;
  258. }
  259. ModeModel *mode = [_dashboard.modeList objectKey:@"basicModeCode" eqaulToString:modeCode];
  260. //로직 변경
  261. // if (!mode.homegrpSceneId || [mode.homegrpSceneId isEmptyString]) {
  262. //
  263. // if ([JDFacade facade].loginUser.level < 90) {//일반 멤버
  264. // [[JDFacade facade] toast:@"아직 선택된 장치가 없습니다.\r장치선택은 마스터만 할 수 있습니다."];
  265. // } else {
  266. // HomeModeSettingsViewController *vc = (HomeModeSettingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeSettingsViewController" storyboardName:@"Main"];
  267. // vc.mode = mode;
  268. //
  269. // [self presentViewController:vc animated:YES completion:nil];
  270. // }
  271. // return;
  272. // }
  273. HomeModeUpdateViewController *vc = (HomeModeUpdateViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeUpdateViewController"
  274. storyboardName:@"Main"];
  275. vc.mode = mode;
  276. [self presentViewController:vc animated:YES completion:nil];
  277. }
  278. #pragma mark - MemoryWarning
  279. - (void)didReceiveMemoryWarning
  280. {
  281. [super didReceiveMemoryWarning];
  282. // Dispose of any resources that can be recreated.
  283. }
  284. - (void)updateRulesListRecently {
  285. [_rvc requestRuleListRecently];
  286. }
  287. @end