MainViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // MainViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 10/2/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "ThingsViewController.h"
  11. #import "RulesViewController.h"
  12. #import "HomeMemberViewController.h"
  13. #import "MainViewController.h"
  14. @interface MainViewController () {
  15. ThingsViewController *_tvc;
  16. RulesViewController *_rvc;
  17. HomeMemberViewController *_mvc;
  18. }
  19. @property (strong, nonatomic) UIView *thingsContainerView;
  20. @property (strong, nonatomic) UIView *rulesContainerView;
  21. @property (strong, nonatomic) UIView *membersContainerView;
  22. @end
  23. #pragma mark - Class Definition
  24. @implementation MainViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view.
  28. [self initUI];
  29. [self prepareViewDidLoad];
  30. }
  31. - (void)initUI {
  32. //1. init tabBar
  33. //1-1. scroll top bottom
  34. //2. init scrollView
  35. CGFloat height = IPHONE_HEIGHT - _tabBar.height;
  36. [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, height)];
  37. _scrollView.pagingEnabled = YES;
  38. _scrollView.scrollEnabled = NO;
  39. CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, height);
  40. //init child view for childViewController
  41. _thingsContainerView = [[UIView alloc] initWithFrame:sr];
  42. _rulesContainerView = [[UIView alloc] initWithFrame:sr];
  43. _membersContainerView = [[UIView alloc] initWithFrame:sr];
  44. [_scrollView addSubview:_thingsContainerView];
  45. _rulesContainerView.x = IPHONE_WIDTH;
  46. [_scrollView addSubview:_rulesContainerView];
  47. _membersContainerView.x = IPHONE_WIDTH * 2;
  48. [_scrollView addSubview:_membersContainerView];
  49. //when first loading this view.
  50. [self initThingsViewController];
  51. }
  52. - (void)initThingsViewController {
  53. if (!_tvc) {
  54. _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
  55. }
  56. [self addChildViewController:_tvc];
  57. [_tvc didMoveToParentViewController:self];
  58. if ([self isViewLoaded]) {
  59. [_tvc beginAppearanceTransition:YES animated:NO];
  60. [_thingsContainerView addSubview:_tvc.view];
  61. [_tvc endAppearanceTransition];
  62. }
  63. }
  64. - (void)initRulesViewController {
  65. if (!_rvc) {
  66. _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
  67. }
  68. [self addChildViewController:_rvc];
  69. [_rvc didMoveToParentViewController:self];
  70. if ([self isViewLoaded]) {
  71. [_rvc beginAppearanceTransition:YES animated:NO];
  72. [_rulesContainerView addSubview:_rvc.view];
  73. [_rvc endAppearanceTransition];
  74. }
  75. }
  76. - (void)initMembersViewController {
  77. if (!_mvc) {
  78. _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
  79. }
  80. [self addChildViewController:_mvc];
  81. [_mvc didMoveToParentViewController:self];
  82. if ([self isViewLoaded]) {
  83. [_mvc beginAppearanceTransition:YES animated:NO];
  84. [_membersContainerView addSubview:_mvc.view];
  85. [_mvc endAppearanceTransition];
  86. }
  87. }
  88. - (void)prepareViewDidLoad {
  89. }
  90. #pragma mark - Main Logic
  91. #pragma mark - UI Events
  92. - (IBAction)btnThingsTouched:(id)sender {
  93. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  94. if (!_tvc) {
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. [self initThingsViewController];
  97. });
  98. }
  99. } completionHandler:^{
  100. CGPoint cpoint = CGPointMake(0, 0);
  101. [_scrollView setContentOffset:cpoint animated:NO];
  102. }];
  103. }
  104. - (IBAction)btnRuleTouched:(id)sender {
  105. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  106. if (!_rvc) {
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. [self initRulesViewController];
  109. });
  110. }
  111. } completionHandler:^{
  112. CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
  113. [_scrollView setContentOffset:cpoint animated:NO];
  114. }];
  115. }
  116. - (IBAction)btnMemberTouched:(id)sender {
  117. [[JDFacade facade] showLoadingWhileExecutingBlock:^{
  118. if (!_mvc) {
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. [self initMembersViewController];
  121. });
  122. }
  123. } completionHandler:^{
  124. CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
  125. [_scrollView setContentOffset:cpoint animated:NO];
  126. }];
  127. }
  128. - (IBAction)btnExtendTouched:(id)sender {
  129. _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
  130. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  131. [self.view layoutIfNeeded];
  132. }];
  133. }
  134. - (IBAction)btnCollapseTouched:(id)sender {
  135. _constraintExpandViewTop.constant = 0;
  136. [UIView animateWithDuration:ANIMATION_DUR animations:^{
  137. [self.view layoutIfNeeded];
  138. }];
  139. }
  140. #pragma mark - MemoryWarning
  141. - (void)didReceiveMemoryWarning
  142. {
  143. [super didReceiveMemoryWarning];
  144. // Dispose of any resources that can be recreated.
  145. }
  146. @end