MainViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. @interface MainViewController () <ImageUtilDelegate> {
  20. ThingsViewController *_tvc;
  21. RulesViewController *_rvc;
  22. HomeMemberViewController *_mvc;
  23. ImageUtil *_imageUtil;
  24. }
  25. @property (strong, nonatomic) UIView *thingsContainerView;
  26. @property (strong, nonatomic) UIView *rulesContainerView;
  27. @property (strong, nonatomic) UIView *membersContainerView;
  28. @end
  29. #pragma mark - Class Definition
  30. @implementation MainViewController
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view.
  34. [self initUI];
  35. [self prepareViewDidLoad];
  36. }
  37. - (void)initUI {
  38. //1. init tabBar
  39. //1-1. scroll top bottom
  40. //2. init scrollView
  41. CGFloat height = IPHONE_HEIGHT - _tabBar.height;
  42. [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, height)];
  43. _scrollView.pagingEnabled = YES;
  44. _scrollView.scrollEnabled = NO;
  45. CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, height);
  46. //init child view for childViewController
  47. _thingsContainerView = [[UIView alloc] initWithFrame:sr];
  48. _rulesContainerView = [[UIView alloc] initWithFrame:sr];
  49. _membersContainerView = [[UIView alloc] initWithFrame:sr];
  50. [_scrollView addSubview:_thingsContainerView];
  51. _rulesContainerView.x = IPHONE_WIDTH;
  52. [_scrollView addSubview:_rulesContainerView];
  53. _membersContainerView.x = IPHONE_WIDTH * 2;
  54. [_scrollView addSubview:_membersContainerView];
  55. //when first loading this view.
  56. [self initThingsViewController];
  57. }
  58. - (void)initThingsViewController {
  59. if (!_tvc) {
  60. _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
  61. }
  62. [self addChildViewController:_tvc];
  63. [_tvc didMoveToParentViewController:self];
  64. if ([self isViewLoaded]) {
  65. [_tvc beginAppearanceTransition:YES animated:NO];
  66. [_thingsContainerView addSubview:_tvc.view];
  67. [_tvc endAppearanceTransition];
  68. }
  69. }
  70. - (void)initRulesViewController {
  71. if (!_rvc) {
  72. _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
  73. }
  74. [self addChildViewController:_rvc];
  75. [_rvc didMoveToParentViewController:self];
  76. if ([self isViewLoaded]) {
  77. [_rvc beginAppearanceTransition:YES animated:NO];
  78. [_rulesContainerView addSubview:_rvc.view];
  79. [_rvc endAppearanceTransition];
  80. }
  81. }
  82. - (void)initMembersViewController {
  83. if (!_mvc) {
  84. _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
  85. }
  86. [self addChildViewController:_mvc];
  87. [_mvc didMoveToParentViewController:self];
  88. if ([self isViewLoaded]) {
  89. [_mvc beginAppearanceTransition:YES animated:NO];
  90. [_membersContainerView addSubview:_mvc.view];
  91. [_mvc endAppearanceTransition];
  92. }
  93. }
  94. - (void)prepareViewDidLoad {
  95. }
  96. #pragma mark - Main Logic
  97. - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
  98. //parameters
  99. NSDictionary *parameter = @{@"image_file": profileImage};
  100. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
  101. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  102. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  103. return;
  104. }
  105. LoginModel *response = (LoginModel *)responseObject;
  106. if (response) {//API 성공 ,
  107. [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  108. [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
  109. }
  110. } failure:^(id errorObject) {
  111. JDErrorModel *error = (JDErrorModel *)errorObject;
  112. [[JDFacade facade] alert:error.errorMessage];
  113. }];
  114. }
  115. #pragma mark - ImageUtil delegate
  116. - (void)didFinishPickingImage:(UIImage *)image {
  117. [self requestUpdateHomeGroupImage:image];
  118. }
  119. #pragma mark - UI Events
  120. - (IBAction)btnThingsTouched:(id)sender {
  121. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  122. if (!_tvc) {
  123. dispatch_async(dispatch_get_main_queue(), ^{
  124. [self initThingsViewController];
  125. });
  126. }
  127. } completionHandler:^{
  128. CGPoint cpoint = CGPointMake(0, 0);
  129. [_scrollView setContentOffset:cpoint animated:NO];
  130. }];
  131. }
  132. - (IBAction)btnRuleTouched:(id)sender {
  133. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  134. if (!_rvc) {
  135. dispatch_async(dispatch_get_main_queue(), ^{
  136. [self initRulesViewController];
  137. });
  138. }
  139. } completionHandler:^{
  140. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  141. [_scrollView setContentOffset:cpoint animated:NO];
  142. }];
  143. }
  144. - (IBAction)btnMemberTouched:(id)sender {
  145. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  146. if (!_mvc) {
  147. dispatch_async(dispatch_get_main_queue(), ^{
  148. [self initMembersViewController];
  149. });
  150. }
  151. } completionHandler:^{
  152. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
  153. [_scrollView setContentOffset:cpoint animated:NO];
  154. }];
  155. }
  156. - (IBAction)btnExtendTouched:(id)sender {
  157. _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  158. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  159. [self.view layoutIfNeeded];
  160. }];
  161. }
  162. - (IBAction)btnCollapseTouched:(id)sender {
  163. _constraintExpandViewTop.constant = 0;
  164. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  165. [self.view layoutIfNeeded];
  166. }];
  167. }
  168. - (IBAction)btnProfileTouched:(id)sender {
  169. if (!_imageUtil) {
  170. _imageUtil = [[ImageUtil alloc] init];
  171. _imageUtil.delegate = self;
  172. }
  173. [_imageUtil prepareImagePicker];
  174. }
  175. - (IBAction)btnNoticeTouched:(id)sender {
  176. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NoticeViewController" storyboardName:@"Main"];
  177. [self presentViewController:vc animated:YES completion:nil];
  178. }
  179. - (void)btnMessageBoxTouched:(id)sender {
  180. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MessageBoxViewController" storyboardName:@"Main"];
  181. [self presentViewController:vc animated:YES completion:nil];
  182. }
  183. - (void)btnHomeHubTouched:(id)sender {
  184. }
  185. - (IBAction)btnSettingsTouched:(id)sender {
  186. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsViewController" storyboardName:@"Settings"];
  187. [self presentViewController:vc animated:YES completion:nil];
  188. }
  189. #pragma mark - MemoryWarning
  190. - (void)didReceiveMemoryWarning
  191. {
  192. [super didReceiveMemoryWarning];
  193. // Dispose of any resources that can be recreated.
  194. }
  195. @end