MainViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. ThingsViewController *_tvc;
  23. RulesViewController *_rvc;
  24. HomeMemberViewController *_mvc;
  25. ImageUtil *_imageUtil;
  26. NSDate *_readDateTime;
  27. DashboardModel *_dashboard;
  28. }
  29. @property (strong, nonatomic) UIView *thingsContainerView;
  30. @property (strong, nonatomic) UIView *rulesContainerView;
  31. @property (strong, nonatomic) UIView *membersContainerView;
  32. @end
  33. #pragma mark - Class Definition
  34. @implementation MainViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. [self initUI];
  39. [self prepareViewDidLoad];
  40. }
  41. - (void)initUI {
  42. //1. init tabBar
  43. //1-1. scroll top bottom
  44. //2. init scrollView
  45. CGFloat height = IPHONE_HEIGHT - _tabBar.height;
  46. [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, height)];
  47. _scrollView.pagingEnabled = YES;
  48. _scrollView.scrollEnabled = NO;
  49. CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, height);
  50. //init child view for childViewController
  51. _thingsContainerView = [[UIView alloc] initWithFrame:sr];
  52. _rulesContainerView = [[UIView alloc] initWithFrame:sr];
  53. _membersContainerView = [[UIView alloc] initWithFrame:sr];
  54. [_scrollView addSubview:_thingsContainerView];
  55. _rulesContainerView.x = IPHONE_WIDTH;
  56. [_scrollView addSubview:_rulesContainerView];
  57. _membersContainerView.x = IPHONE_WIDTH * 2;
  58. [_scrollView addSubview:_membersContainerView];
  59. //when first loading this view.
  60. [self initThingsViewController];
  61. _btnHomeHub.enabled = !(![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]);
  62. }
  63. - (void)initThingsViewController {
  64. if (!_tvc) {
  65. _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
  66. }
  67. [self addChildViewController:_tvc];
  68. [_tvc didMoveToParentViewController:self];
  69. if ([self isViewLoaded]) {
  70. [_tvc beginAppearanceTransition:YES animated:NO];
  71. [_thingsContainerView addSubview:_tvc.view];
  72. [_tvc endAppearanceTransition];
  73. }
  74. }
  75. - (void)initRulesViewController {
  76. if (!_rvc) {
  77. _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
  78. }
  79. [self addChildViewController:_rvc];
  80. [_rvc didMoveToParentViewController:self];
  81. if ([self isViewLoaded]) {
  82. [_rvc beginAppearanceTransition:YES animated:NO];
  83. [_rulesContainerView addSubview:_rvc.view];
  84. [_rvc endAppearanceTransition];
  85. }
  86. }
  87. - (void)initMembersViewController {
  88. if (!_mvc) {
  89. _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
  90. }
  91. [self addChildViewController:_mvc];
  92. [_mvc didMoveToParentViewController:self];
  93. if ([self isViewLoaded]) {
  94. [_mvc beginAppearanceTransition:YES animated:NO];
  95. [_membersContainerView addSubview:_mvc.view];
  96. [_mvc endAppearanceTransition];
  97. }
  98. }
  99. - (void)prepareViewDidLoad {
  100. [self requestDashboard];
  101. [[JDFacade facade] checkHomeHubStatus];
  102. }
  103. #pragma mark - Main Logic
  104. - (void)updateHomeHubStatusToChildViewController {
  105. if (_tvc) {
  106. [_tvc updateHomeHubStatusToDevices];
  107. }
  108. if (_rvc) {
  109. [_rvc updateHomeHubStatusToRules];
  110. }
  111. if (_mvc) {
  112. [_mvc updateHomeHubStatusToMembers];
  113. }
  114. }
  115. - (void)requestDashboard {
  116. //parameters
  117. NSDictionary *parameter = @{@"read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString};
  118. NSString *path = [NSString stringWithFormat:API_GET_DASHBOARD];
  119. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DashboardModel class] completion:^(id responseObject) {
  120. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  121. return;
  122. }
  123. DashboardModel *dashboard = (DashboardModel *) responseObject;
  124. if (dashboard) {//API 성공 ,
  125. _dashboard = dashboard;
  126. [self setContents];
  127. }
  128. } failure:^(id errorObject) {
  129. JDErrorModel *error = (JDErrorModel *)errorObject;
  130. [[JDFacade facade] alert:error.errorMessage];
  131. }];
  132. }
  133. - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
  134. //parameters
  135. NSDictionary *parameter = @{@"image_file": profileImage};
  136. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
  137. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  138. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  139. return;
  140. }
  141. LoginModel *response = (LoginModel *)responseObject;
  142. if (response) {//API 성공 ,
  143. _dashboard.imageFileName = response.imageFileName;
  144. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  145. [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
  146. }
  147. } failure:^(id errorObject) {
  148. JDErrorModel *error = (JDErrorModel *)errorObject;
  149. [[JDFacade facade] alert:error.errorMessage];
  150. }];
  151. }
  152. - (void)setContents {
  153. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:_dashboard.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  154. }
  155. #pragma mark - ImageUtil delegate
  156. - (void)didFinishPickingImage:(UIImage *)image {
  157. [self requestUpdateHomeGroupImage:image];
  158. }
  159. #pragma mark - UI Control
  160. - (void)loadThingsViewController {
  161. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  162. if (!_tvc) {
  163. dispatch_async(dispatch_get_main_queue(), ^{
  164. [self initThingsViewController];
  165. });
  166. }
  167. } completionHandler:^{
  168. CGPoint cpoint = CGPointMake(0, 0);
  169. [_scrollView setContentOffset:cpoint animated:NO];
  170. [_tvc prepareViewDidLoad];
  171. }];
  172. }
  173. - (void)loadRulesViewController {
  174. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  175. if (!_rvc) {
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. [self initRulesViewController];
  178. });
  179. }
  180. } completionHandler:^{
  181. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  182. [_scrollView setContentOffset:cpoint animated:NO];
  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. if (!mode.homegrpSceneId || [mode.homegrpSceneId isEmptyString]) {
  261. if ([JDFacade facade].loginUser.level < 90) {//일반 멤버
  262. [[JDFacade facade] toast:@"아직 선택된 장치가 없습니다.\r장치선택은 마스터만 할 수 있습니다."];
  263. } else {
  264. HomeModeSettingsViewController *vc = (HomeModeSettingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeSettingsViewController" storyboardName:@"Main"];
  265. vc.mode = mode;
  266. [self presentViewController:vc animated:YES completion:nil];
  267. }
  268. return;
  269. }
  270. HomeModeUpdateViewController *vc = (HomeModeUpdateViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeUpdateViewController"
  271. storyboardName:@"Main"];
  272. vc.mode = mode;
  273. [self presentViewController:vc animated:YES completion:nil];
  274. }
  275. #pragma mark - MemoryWarning
  276. - (void)didReceiveMemoryWarning
  277. {
  278. [super didReceiveMemoryWarning];
  279. // Dispose of any resources that can be recreated.
  280. }
  281. @end