| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- //
- // MainViewController.m
- // kneet2
- //
- // StoryBoard - page 8.
- //
- // Created by Jason Lee on 10/2/15.
- // Copyright © 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "CustomButton.h"
- #import "UIButton+WebCache.h"
- #import "ImageUtil.h"
- #import "ThingsViewController.h"
- #import "RulesViewController.h"
- #import "HomeMemberViewController.h"
- #import "MainViewController.h"
- #import "HomeModeUpdateViewController.h"
- @interface MainViewController () <ImageUtilDelegate> {
- ThingsViewController *_tvc;
- RulesViewController *_rvc;
- HomeMemberViewController *_mvc;
-
- ImageUtil *_imageUtil;
-
- NSDate *_readDateTime;
-
- DashboardModel *_dashboard;
- }
- @property (strong, nonatomic) UIView *thingsContainerView;
- @property (strong, nonatomic) UIView *rulesContainerView;
- @property (strong, nonatomic) UIView *membersContainerView;
- @end
- #pragma mark - Class Definition
- @implementation MainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
- //1. init tabBar
- //1-1. scroll top bottom
-
- //2. init scrollView
- CGFloat height = IPHONE_HEIGHT - _tabBar.height;
- [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, height)];
- _scrollView.pagingEnabled = YES;
- _scrollView.scrollEnabled = NO;
-
-
- CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, height);
-
- //init child view for childViewController
- _thingsContainerView = [[UIView alloc] initWithFrame:sr];
- _rulesContainerView = [[UIView alloc] initWithFrame:sr];
- _membersContainerView = [[UIView alloc] initWithFrame:sr];
-
- [_scrollView addSubview:_thingsContainerView];
-
- _rulesContainerView.x = IPHONE_WIDTH;
- [_scrollView addSubview:_rulesContainerView];
-
- _membersContainerView.x = IPHONE_WIDTH * 2;
- [_scrollView addSubview:_membersContainerView];
-
- //when first loading this view.
- [self initThingsViewController];
-
- _btnHomeHub.enabled = (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]);
- }
- - (void)initThingsViewController {
-
- if (!_tvc) {
- _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
- }
-
- [self addChildViewController:_tvc];
- [_tvc didMoveToParentViewController:self];
-
- if ([self isViewLoaded]) {
- [_tvc beginAppearanceTransition:YES animated:NO];
- [_thingsContainerView addSubview:_tvc.view];
- [_tvc endAppearanceTransition];
- }
- }
- - (void)initRulesViewController {
-
- if (!_rvc) {
- _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
- }
-
- [self addChildViewController:_rvc];
- [_rvc didMoveToParentViewController:self];
-
- if ([self isViewLoaded]) {
- [_rvc beginAppearanceTransition:YES animated:NO];
- [_rulesContainerView addSubview:_rvc.view];
- [_rvc endAppearanceTransition];
- }
- }
- - (void)initMembersViewController {
- if (!_mvc) {
- _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
- }
-
- [self addChildViewController:_mvc];
- [_mvc didMoveToParentViewController:self];
-
- if ([self isViewLoaded]) {
- [_mvc beginAppearanceTransition:YES animated:NO];
- [_membersContainerView addSubview:_mvc.view];
- [_mvc endAppearanceTransition];
- }
- }
- - (void)prepareViewDidLoad {
- [self requestDashboard];
- }
- #pragma mark - Main Logic
- - (void)requestDashboard {
- //parameters
- NSDictionary *parameter = @{@"read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString};
-
- NSString *path = [NSString stringWithFormat:API_GET_DASHBOARD];
-
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DashboardModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- DashboardModel *dashboard = (DashboardModel *) responseObject;
-
- if (dashboard) {//API 성공 ,
- _dashboard = dashboard;
- [self setContents];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)requestUpdateHomeGroupImage:(UIImage *)profileImage {
-
- //parameters
- NSDictionary *parameter = @{@"image_file": profileImage};
-
- NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_UPDATE_IMAGE];
-
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- LoginModel *response = (LoginModel *)responseObject;
-
- if (response) {//API 성공 ,
-
- _dashboard.imageFileName = response.imageFileName;
- [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
- [[JDFacade facade] toast:@"홈 이미지를 변경했습니다"];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)setContents {
-
- [_btnHomegrpImage sd_setImageWithURL:[NSURL URLWithString:_dashboard.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
- }
- #pragma mark - ImageUtil delegate
- - (void)didFinishPickingImage:(UIImage *)image {
- [self requestUpdateHomeGroupImage:image];
- }
- #pragma mark - UI Control
- - (void)loadThingsViewController {
- [[JDFacade facade] showLoadingWhileExecutingBlock:^{
- if (!_tvc) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self initThingsViewController];
- });
- }
- } completionHandler:^{
- CGPoint cpoint = CGPointMake(0, 0);
- [_scrollView setContentOffset:cpoint animated:NO];
-
- [_tvc prepareViewDidLoad];
- }];
- }
- - (void)loadRulesViewController {
- [[JDFacade facade] showLoadingWhileExecutingBlock:^{
- if (!_rvc) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self initRulesViewController];
- });
- }
- } completionHandler:^{
- CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
- [_scrollView setContentOffset:cpoint animated:NO];
- }];
- }
- - (void)loadMembersViewController {
- [[JDFacade facade] showLoadingWhileExecutingBlock:^{
- if (!_mvc) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self initMembersViewController];
- });
- }
- } completionHandler:^{
- CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
- [_scrollView setContentOffset:cpoint animated:NO];
- }];
- }
- #pragma mark - UI Events
- - (IBAction)btnThingsTouched:(id)sender {
- [self loadThingsViewController];
- }
- - (IBAction)btnRuleTouched:(id)sender {
- [self loadRulesViewController];
- }
- - (IBAction)btnMemberTouched:(id)sender {
- [self loadMembersViewController];
- }
- - (IBAction)btnExtendTouched:(id)sender {
-
- _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
-
- [UIView animateWithDuration:kfAnimationDur animations:^{
- [self.view layoutIfNeeded];
- }];
- }
- - (IBAction)btnCollapseTouched:(id)sender {
-
- _constraintExpandViewTop.constant = 0;
-
- [UIView animateWithDuration:kfAnimationDur animations:^{
- [self.view layoutIfNeeded];
- }];
- }
- - (IBAction)btnProfileTouched:(id)sender {
- if (!_imageUtil) {
- _imageUtil = [[ImageUtil alloc] init];
- _imageUtil.delegate = self;
- }
-
- [_imageUtil prepareImagePicker];
- }
- - (IBAction)btnNoticeTouched:(id)sender {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"NoticeViewController" storyboardName:@"Main"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (void)btnMessageBoxTouched:(id)sender {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MessageBoxViewController" storyboardName:@"Main"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (void)btnHomeHubTouched:(id)sender {
-
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (IBAction)btnSettingsTouched:(id)sender {
-
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsViewController" storyboardName:@"Settings"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (IBAction)btnModeHomeTouched:(id)sender {
- [self doChangeHomeMode:HOME_MODE_HOME];
- }
- - (IBAction)btnModeAwayTouched:(id)sender {
- [self doChangeHomeMode:HOME_MODE_AWAY];
- }
- - (IBAction)btnModeMorningTouched:(id)sender {
- [self doChangeHomeMode:HOME_MODE_MORNING];
- }
- - (IBAction)btnModeNightTouched:(id)sender {
- [self doChangeHomeMode:HOME_MODE_NIGHT];
- }
- - (void)doChangeHomeMode:(NSString *)modeCode {
-
- if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
- [[JDFacade facade] toast:@"홈허브가 온라인 상태로\n연결되어 있을 때 시도해주세요"];
- return;
- }
-
- NSUInteger index = [_dashboard.modeList indexOfObjectPassingTest:^BOOL(ModeModel *pmode, NSUInteger idx, BOOL * _Nonnull stop) {
- NSUInteger findex = NSNotFound;
- if ([pmode.basicModeCode isEqualToString:modeCode]) {
- findex = index;
- }
- return findex;
- }];
- ModeModel *mode = [_dashboard.modeList objectAtIndex:index];
-
- HomeModeUpdateViewController *vc = (HomeModeUpdateViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeUpdateViewController"
- storyboardName:@"Main"];
- vc.mode = mode;
- [self presentViewController:vc animated:YES completion:nil];
-
- //
- // if (!_dashboard.modeList || !_dashboard.modeList.count) {
- // return;
- // }
- //
- //
- // //parameters
- // NSDictionary *parameter = @{@"mode_id": mode.modeId};
- //
- // NSString *path = [NSString stringWithFormat:API_POST_DASHBOARD_MODE_CHANGE];
- //
- // [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
- // [JDFacade facade]
- // } failure:^(id errorObject) {
- // JDErrorModel *error = (JDErrorModel *)errorObject;
- // [[JDFacade facade] alert:error.errorMessage];
- // }];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|