MainViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. @interface MainViewController () <ImageUtilDelegate> {
  21. ThingsViewController *_tvc;
  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 - _tabBar.height;
  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. }
  101. #pragma mark - Main Logic
  102. - (void)requestDashboard {
  103. //parameters
  104. NSDictionary *parameter = @{@"read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString};
  105. NSString *path = [NSString stringWithFormat:API_GET_DASHBOARD];
  106. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DashboardModel class] completion:^(id responseObject) {
  107. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  108. return;
  109. }
  110. DashboardModel *dashboard = (DashboardModel *) responseObject;
  111. if (dashboard) {//API 성공 ,
  112. _dashboard = dashboard;
  113. [self setContents];
  114. }
  115. } failure:^(id errorObject) {
  116. JDErrorModel *error = (JDErrorModel *)errorObject;
  117. [[JDFacade facade] alert:error.errorMessage];
  118. }];
  119. }
  120. - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
  121. //parameters
  122. NSDictionary *parameter = @{@"image_file": profileImage};
  123. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
  124. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  125. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  126. return;
  127. }
  128. LoginModel *response = (LoginModel *)responseObject;
  129. if (response) {//API 성공 ,
  130. _dashboard.imageFileName = response.imageFileName;
  131. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  132. [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
  133. }
  134. } failure:^(id errorObject) {
  135. JDErrorModel *error = (JDErrorModel *)errorObject;
  136. [[JDFacade facade] alert:error.errorMessage];
  137. }];
  138. }
  139. - (void)setContents {
  140. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:_dashboard.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  141. }
  142. #pragma mark - ImageUtil delegate
  143. - (void)didFinishPickingImage:(UIImage *)image {
  144. [self requestUpdateHomeGroupImage:image];
  145. }
  146. #pragma mark - UI Control
  147. - (void)loadThingsViewController {
  148. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  149. if (!_tvc) {
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. [self initThingsViewController];
  152. });
  153. }
  154. } completionHandler:^{
  155. CGPoint cpoint = CGPointMake(0, 0);
  156. [_scrollView setContentOffset:cpoint animated:NO];
  157. [_tvc prepareViewDidLoad];
  158. }];
  159. }
  160. - (void)loadRulesViewController {
  161. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  162. if (!_rvc) {
  163. dispatch_async(dispatch_get_main_queue(), ^{
  164. [self initRulesViewController];
  165. });
  166. }
  167. } completionHandler:^{
  168. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  169. [_scrollView setContentOffset:cpoint animated:NO];
  170. }];
  171. }
  172. - (void)loadMembersViewController {
  173. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  174. if (!_mvc) {
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. [self initMembersViewController];
  177. });
  178. }
  179. } completionHandler:^{
  180. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
  181. [_scrollView setContentOffset:cpoint animated:NO];
  182. }];
  183. }
  184. #pragma mark - UI Events
  185. - (IBAction)btnThingsTouched:(id)sender {
  186. [self loadThingsViewController];
  187. }
  188. - (IBAction)btnRuleTouched:(id)sender {
  189. [self loadRulesViewController];
  190. }
  191. - (IBAction)btnMemberTouched:(id)sender {
  192. [self loadMembersViewController];
  193. }
  194. - (IBAction)btnExtendTouched:(id)sender {
  195. _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  196. [UIView animateWithDuration:kfAnimationDur animations:^{
  197. [self.view layoutIfNeeded];
  198. }];
  199. }
  200. - (IBAction)btnCollapseTouched:(id)sender {
  201. _constraintExpandViewTop.constant = 0;
  202. [UIView animateWithDuration:kfAnimationDur animations:^{
  203. [self.view layoutIfNeeded];
  204. }];
  205. }
  206. - (IBAction)btnProfileTouched:(id)sender {
  207. if (!_imageUtil) {
  208. _imageUtil = [[ImageUtil alloc] init];
  209. _imageUtil.delegate = self;
  210. }
  211. [_imageUtil prepareImagePicker];
  212. }
  213. - (IBAction)btnNoticeTouched:(id)sender {
  214. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NoticeViewController" storyboardName:@"Main"];
  215. [self presentViewController:vc animated:YES completion:nil];
  216. }
  217. - (void)btnMessageBoxTouched:(id)sender {
  218. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MessageBoxViewController" storyboardName:@"Main"];
  219. [self presentViewController:vc animated:YES completion:nil];
  220. }
  221. - (void)btnHomeHubTouched:(id)sender {
  222. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
  223. [self presentViewController:vc animated:YES completion:nil];
  224. }
  225. - (IBAction)btnSettingsTouched:(id)sender {
  226. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsViewController" storyboardName:@"Settings"];
  227. [self presentViewController:vc animated:YES completion:nil];
  228. }
  229. - (IBAction)btnModeHomeTouched:(id)sender {
  230. [self doChangeHomeMode:HOME_MODE_HOME];
  231. }
  232. - (IBAction)btnModeAwayTouched:(id)sender {
  233. [self doChangeHomeMode:HOME_MODE_AWAY];
  234. }
  235. - (IBAction)btnModeMorningTouched:(id)sender {
  236. [self doChangeHomeMode:HOME_MODE_MORNING];
  237. }
  238. - (IBAction)btnModeNightTouched:(id)sender {
  239. [self doChangeHomeMode:HOME_MODE_NIGHT];
  240. }
  241. - (void)doChangeHomeMode:(NSString *)modeCode {
  242. if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
  243. [[JDFacade facade] toast:@"홈허브가 온라인 상태로\n연결되어 있을 때 시도해주세요"];
  244. return;
  245. }
  246. NSUInteger index = [_dashboard.modeList indexOfObjectPassingTest:^BOOL(ModeModel *pmode, NSUInteger idx, BOOL * _Nonnull stop) {
  247. NSUInteger findex = NSNotFound;
  248. if ([pmode.basicModeCode isEqualToString:modeCode]) {
  249. findex = index;
  250. }
  251. return findex;
  252. }];
  253. ModeModel *mode = [_dashboard.modeList objectAtIndex:index];
  254. HomeModeUpdateViewController *vc = (HomeModeUpdateViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeUpdateViewController"
  255. storyboardName:@"Main"];
  256. vc.mode = mode;
  257. [self presentViewController:vc animated:YES completion:nil];
  258. //
  259. // if (!_dashboard.modeList || !_dashboard.modeList.count) {
  260. // return;
  261. // }
  262. //
  263. //
  264. // //parameters
  265. // NSDictionary *parameter = @{@"mode_id": mode.modeId};
  266. //
  267. // NSString *path = [NSString stringWithFormat:API_POST_DASHBOARD_MODE_CHANGE];
  268. //
  269. // [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  270. // [JDFacade facade]
  271. // } failure:^(id errorObject) {
  272. // JDErrorModel *error = (JDErrorModel *)errorObject;
  273. // [[JDFacade facade] alert:error.errorMessage];
  274. // }];
  275. }
  276. #pragma mark - MemoryWarning
  277. - (void)didReceiveMemoryWarning
  278. {
  279. [super didReceiveMemoryWarning];
  280. // Dispose of any resources that can be recreated.
  281. }
  282. @end