// // HomepageViewController.m // FirstLink // // Created by ascii on 15/5/28. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "HomeViewController.h" #import "FKHomeViewModel.h" #import "FKNaviBarControl.h" #import "FKHomePersonInfoCell.h" #import "FKHomeFollowInfoCell.h" #import "FKHomeOrderHeaderCell.h" #import "FKHomeOrderEntranceCell.h" #import "FKHomeImageTextCell.h" #import "FLControllerHelper.h" #import "FirstLinkAppDelegate.h" #import "SessionListController.h" #import "SettingViewController.h" #import "AvatarViewController.h" #import "NicknameViewController.h" #import "FKPointController.h" #import "FKSignDoneView.h" #import "FKPriceWarnListController.h" #import "CollectViewController.h" #import "FKMyBookController.h" #import "FKOrderSegmentController.h" #import "FKMyVipController.h" #import "FKCouponController.h" #import "FKPushSettingController.h" #import "WebViewController.h" #import "UIImageView+FLAddition.h" #import "FKPersonOrderReform.h" #import "FKPersonOrderRequest.h" #import "UserDefaultManager.h" #import "FKHomeRequest.h" #import "FKHomeReform.h" #import "FKPointRequest.h" #import "FKPointReform.h" #import "FKWantBuyController.h" #import "FKWantBuyRequest.h" #import "FKWantBuyReform.h" #import "GuideView.h" const int FKOrderBadgeRequest = 1001; const int FKCouponCountRequest = 1002; const int FKSignDetailRequest = 1003; const int FKSignTodayRequest = 1004; const int FKHomeCountRequest = 1005; static NSString *WANT_BUY_IS_SHOW_FIRST_KEY = @"WANT_BUY_IS_SHOW_FIRST_KEY"; static NSString *GUIDE_NOTIFICATION_IMAGENAME = @"guide_notification"; static NSString *GUIDE_INTEGRAL_IMAGENAME = @"guide_integral"; static NSString *GUIDE_WANGBUY_IMAGENAME = @"guide_want_buy"; @interface HomeViewController () @property (nonatomic, strong) FKHomeViewModel *viewModel; @property (nonatomic, strong) FKNaviBarControl *leftControl; @property (nonatomic, strong) FKNaviBarControl *rightControl; @property (nonatomic, strong) UILabel *tabbarBadgeView; @property (nonatomic, strong) FKHomePersonInfoCell *personCell; @property (nonatomic, assign) NSInteger wantBuyTotal; @end @implementation HomeViewController - (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self configTabbarBadgeView]; [self configUnreadRefreshCallBackBeforeViewDidLoad]; [[FKMessageManager sharedInstance] refreshMessageUnreadMessage]; } return self; } - (void)loadView { [super loadView]; self.navigationItem.title = @"我的"; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; } - (void)dealloc { [self.tabbarBadgeView removeFromSuperview]; } - (void)viewDidLoad { [super viewDidLoad]; [self.tabbarBadgeView removeFromSuperview]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [UIApplication sharedApplication].statusBarHidden = NO; [self.navigationController setNavigationBarHidden:NO animated:NO]; [self configLeftNaviItem]; [self configRightNaviItem]; [self configMessageUnreadBadgeNumber]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { self.viewModel.badgeItem = nil; self.tabbarBadgeView.hidden = YES; } else { [self requestBadgeInfo]; [self requstCouponCountData]; [self requestSignDetailData]; [self requestHomeCountData]; } [self configUnreadRefreshCallBackAfterViewDidLoad]; [self.tableView reloadData]; [self showGuideView]; } #pragma mark - Request - (void)requestBadgeInfo { [FKPersonOrderRequest requestOrderBadge:FKOrderBadgeRequest deleagate:self]; } -(void)requstCouponCountData { [FKHomeRequest requestItems:FKCouponCountRequest deleagate:self]; } - (void)requestSignDetailData { [FKPointRequest reqPointDataWithIdentify:FKSignDetailRequest delegate:self]; } - (void)requestHomeCountData { [FKHomeRequest requestCount:FKHomeCountRequest deleagate:self]; } - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { if (RefreshDirectionTop == direction) { [self requestBadgeInfo]; [self requstCouponCountData]; [self requestSignDetailData]; [self requestHomeCountData]; } } #pragma mark - response - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header { [self.refreshControl finishRefreshingDirection:RefreshDirectionTop]; [self.hudView hide:NO]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { if (identify == FKOrderBadgeRequest) { self.viewModel.badgeItem = [FKPersonOrderReform parseOrderBadge:response]; } else if (identify == FKCouponCountRequest) { self.viewModel.couponCount = [FKHomeReform parseCouponCount:response]; self.viewModel.serverTime = (self.viewModel.couponCount > 0 ? [FKHomeReform parseServerTime:response] : nil); } else if (identify == FKSignDetailRequest) { self.viewModel.signModel = [FKPointReform parserDataModelWithDict:response]; } else if (identify == FKSignTodayRequest) { self.viewModel.signModel.signToday = YES; [self.personCell setShowSignedBtn]; FKPointSignItem *item = [FKPointReform parserSignInfoWithDict:response]; [FKSignDoneView showInView:self.view.window withNextDay:item.nextDayScore score:item.score bgImage:nil completion:^(BOOL finished) { [self.personCell animationHideSignButtonCompletion:^(BOOL finished) { [self requestHomeCountData]; }]; }]; return; } else if (identify == FKHomeCountRequest) { FKHomeCountModel *model = [FKHomeCountModel new]; [model mts_setValuesForKeysWithDictionary:response[@"data"]]; self.viewModel.countModel = model; } [self.tableView reloadData]; } } - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{ [self.refreshControl finishRefreshingDirection:RefreshDirectionTop]; [self.hudView hide:NO]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } #pragma mark - TableView Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.viewModel numberOfSectionsInTableView]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.viewModel numberOfRowsInSection:section]; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.viewModel heightForRowAtIndexPath:indexPath]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { kHomeCellType cellType = [self.viewModel cellTypeForIndexPath:indexPath]; switch (cellType) { case kHomeCellTypePersonInfo: { FKHomePersonInfoCell *infoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKHomePersonInfoCell class])]; [self configPersonInfoCellWithAction:infoCell]; self.personCell = infoCell; [infoCell fk_configWithViewModel:self.viewModel indexPath:indexPath]; return infoCell; break; } case kHomeCellTypeFollow: { FKHomeFollowInfoCell *followCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKHomeFollowInfoCell class])]; followCell.delegate = self; [followCell fk_configWithViewModel:self.viewModel indexPath:indexPath]; return followCell; break; } case kHomeCellTypeOrder: { FKHomeOrderEntranceCell *orderCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKHomeOrderEntranceCell class])]; orderCell.waitPayStr = self.viewModel.badgeItem.nonpaymentOrderBadge; WeakSelf(weakSelf); orderCell.enterOrderAction = ^(FKPersonOrderClassify type){ [weakSelf pushOrderControllerWithType:type]; }; return orderCell; } case kHomeCellTypeVIP: case kHomeCellTypeCoupon: case kHomeCellTypeHelpBuy: case kHomeCellTypePush: case kHomeCellTypeConsult: { FKHomeImageTextCell *imageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKHomeImageTextCell class])]; [imageCell fk_configWithViewModel:self.viewModel indexPath:indexPath]; return imageCell; break; } default: break; } return [[UITableViewCell alloc] init]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { kHomeCellType cellType = [self.viewModel cellTypeForIndexPath:indexPath]; switch (cellType) { case kHomeCellTypeVIP: case kHomeCellTypeCoupon: case kHomeCellTypeHelpBuy: case kHomeCellTypePush: { if (cellType == kHomeCellTypeVIP) { [self.viewModel shownVipRedpointGuide]; [self.tableView reloadData]; } if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) { [self showLoginActionMenu]; } else { [self pushControllerWithCellType:cellType]; } break; } case kHomeCellTypeConsult: { [self pushWebController]; break; } default: break; } } #pragma mark - Push Controller - (void)pushControllerWithCellType:(kHomeCellType)cellType { UIViewController *controller; switch (cellType) { case kHomeCellTypeOrderHeader: { controller = [FKOrderSegmentController new]; break; } case kHomeCellTypeVIP: { controller = [FKMyVipController new]; break; } case kHomeCellTypeCoupon: { if ([self.viewModel needShowInvalidCouponMsg]){ [self.viewModel saveLastShowInvalidCouponMsgTime]; } controller = [FKCouponController new]; break; } case kHomeCellTypeHelpBuy: { controller = [FKWantBuyController new]; break; } case kHomeCellTypePush: { controller = [FKPushSettingController new]; break; } default: break; } if (controller) { controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } } - (void)pushController:(UIViewController *)controller shouldLogin:(BOOL)shouldLogin { if (shouldLogin) { if (![FKUserManager isUserLogin]) { [self showLoginActionMenu]; return; } } if (controller) { controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } } - (IBAction)pushMessageController { [self pushController:[[SessionListController alloc] init] shouldLogin:YES]; } - (IBAction)pushSettingController:(id)sender { [self pushController:[[SettingViewController alloc] init] shouldLogin:YES]; } - (IBAction)clickPersonNicknameAction:(id)sender { if (![FKUserManager isUserLogin]) { [FLControllerHelper presentLoginViewController]; } else { NicknameViewController *controller = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"NicknameViewController"]; controller.type = NicknameControllerTypeModify; [self pushController:controller shouldLogin:YES]; } } - (IBAction)clickPersonAvatarAction:(id)sender { if (![FKUserManager isUserLogin]) { [FLControllerHelper presentLoginViewController]; } else { AvatarViewController *controller = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"AvatarViewController"]; controller.type = AvatarControllerTypeModify; [self pushController:controller shouldLogin:YES]; } } - (IBAction)clickPersonPointAction:(id)sender { FKPointController *controller = [FKPointController new]; [self pushController:controller shouldLogin:YES]; } - (IBAction)clickPersonSignAction:(id)sender { if (![FKUserManager isUserLogin]) { [self showLoginActionMenu]; } else if (self.viewModel.signModel.signToday == NO) { [FKPointRequest requestSignState:FKSignTodayRequest deleagate:self]; } } #pragma mark - Push Controller - FKHomeFollowCellDelegate - (void)followClickCell:(id)cell index:(NSInteger)index { if (index == 0) { [self pushController:[FKPriceWarnListController new] shouldLogin:YES]; } else if (index == 1) { [self pushController:[CollectViewController new]shouldLogin:YES]; } else if (index == 2) { [self pushController:[FKMyBookController new]shouldLogin:YES]; } } #pragma mark - Push Controller - (void)pushOrderControllerWithType:(FKPersonOrderClassify)type { FKOrderSegmentController *controller = [[FKOrderSegmentController alloc] init]; controller.selectedType = type; [self pushController:controller shouldLogin:YES]; } - (void)pushWebController { NSString *url = [NSString stringWithFormat:@"%@/support/index.html", [[FKServerUtil sharedInstance] webServer]]; WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"]; newViewController.url = url; newViewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:newViewController animated:YES]; } #pragma mark - Method - (void)showGuideView{ if ([FKUserManager isUserLogin] && !IS_IPHONE_4) { CGPoint integralPoint = [self.personCell convertPoint:self.personCell.pointButton.frame.origin toView:nil]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3]; CGPoint wantBuyCellPoint = [self.tableView convertPoint:[self.tableView cellForRowAtIndexPath:indexPath].frame.origin toView:nil]; BOOL res = [[[NSUserDefaults standardUserDefaults] objectForKey:WANT_BUY_IS_SHOW_FIRST_KEY] boolValue]; if (!res) { GuideView *guideView = [[GuideView alloc] init]; [guideView addMaskViewWithType:NEW_FEATURE_TYPE_ENUM_IMG image:@"guide_notification_img" andRect:CGRectMake(7, IS_IPHONE_X ? 50 : 26, 175, 61)]; [guideView addMaskViewWithType:NEW_FEATURE_TYPE_ENUM_IMG image:@"guide_integral_img" andRect:CGRectMake(integralPoint.x, integralPoint.y, 200, 51)]; [guideView addMaskView:@"guide_want_buy_img" withImageRect:CGRectMake(wantBuyCellPoint.x, wantBuyCellPoint.y-140, UISCREENWIDTH, 200) andBtnRect:CGRectMake(UISCREENWIDTH/2-50, wantBuyCellPoint.y-130, 120, 40)]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:WANT_BUY_IS_SHOW_FIRST_KEY]; [[NSUserDefaults standardUserDefaults] synchronize]; } } } - (void)configLeftNaviItem { self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftControl]; } - (void)configRightNaviItem { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightControl]; } - (void)configTabbarBadgeView { FirstLinkAppDelegate *delegate = (FirstLinkAppDelegate *)[[UIApplication sharedApplication] delegate]; UITabBarController *tabbarController = (UITabBarController*)delegate.window.rootViewController; if ([tabbarController isKindOfClass:[UITabBarController class]]) { // 只需要夹在一次角标提示 if (!_tabbarBadgeView) { [tabbarController.tabBar addSubview:self.tabbarBadgeView]; } BOOL hasShown = [[[NSUserDefaults standardUserDefaults] objectForKey:kVipRedpointGuideKey] boolValue]; if (!hasShown) { self.tabbarBadgeView.hidden = NO; } } } - (void)configTabbarUnreadBadge { __weak FKMessageManager *messageManage = [FKMessageManager sharedInstance]; NSInteger localBadge = [SessionListController localUnreadMessageCount]; NSInteger remoteBadge = messageManage.remoteMessageBadge.totalBadge; NSInteger orderBadge = messageManage.orderBadge.nonpaymentOrderBadge.integerValue; NSInteger expiredCount = messageManage.expireCouponCount; self.tabbarBadgeView.hidden = ((localBadge + remoteBadge + orderBadge + expiredCount) > 0 ? NO : YES); BOOL hasShown = [[[NSUserDefaults standardUserDefaults] objectForKey:kVipRedpointGuideKey] boolValue]; if (!hasShown) { self.tabbarBadgeView.hidden = NO; } } - (void)configUnreadRefreshCallBackBeforeViewDidLoad { WeakSelf(weakSelf); [FKMessageManager sharedInstance].unreadRefreshCallBackBeforeViewDidLoad = ^() { [weakSelf configTabbarUnreadBadge]; }; } - (void)configUnreadRefreshCallBackAfterViewDidLoad { WeakSelf(weakSelf); [FKMessageManager sharedInstance].unreadRefreshCallBackAfterViewDidLoad = ^() { [weakSelf configMessageUnreadBadgeNumber]; [weakSelf configTabbarUnreadBadge]; }; } - (void)configMessageUnreadBadgeNumber { FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance]; NSInteger localBadge = [SessionListController localUnreadMessageCount]; NSInteger totalBadge = (localBadge + weakMessageManage.remoteMessageBadge.totalBadge); self.leftControl.countLabel.text = [NSString stringWithFormat:@"%ld", (long)totalBadge]; self.leftControl.countLabel.hidden = (totalBadge > 0 ? NO : YES); } - (void)configPersonInfoCellWithAction:(FKHomePersonInfoCell *)cell { [cell.nickLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPersonNicknameAction:)]]; [cell.headView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPersonAvatarAction:)]]; [cell.pointButton addTarget:self action:@selector(clickPersonPointAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.signButton addTarget:self action:@selector(clickPersonSignAction:) forControlEvents:UIControlEventTouchUpInside]; } #pragma mark - Property - (FKHomeViewModel *)viewModel { if (!_viewModel) { _viewModel = [FKHomeViewModel new]; } return _viewModel; } - (FKNaviBarControl *)leftControl { if (!_leftControl) { _leftControl = [[FKNaviBarControl alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _leftControl.countLabel.hidden = YES; _leftControl.imageView.image = [UIImage imageNamed:@"homeLeftIcon"]; [_leftControl addTarget:self action:@selector(pushMessageController) forControlEvents:UIControlEventTouchUpInside]; [[_leftControl.widthAnchor constraintEqualToConstant:44] setActive:YES]; [[_leftControl.heightAnchor constraintEqualToConstant:44] setActive:YES]; [_leftControl remakeConstraints:NaviBarControlLayoutLeft]; } return _leftControl; } - (FKNaviBarControl *)rightControl { if (!_rightControl) { _rightControl = [[FKNaviBarControl alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _rightControl.imageView.image = [UIImage imageNamed:@"homeRightIcon"]; [_rightControl addTarget:self action:@selector(pushSettingController:) forControlEvents:UIControlEventTouchUpInside]; [[_rightControl.widthAnchor constraintEqualToConstant:44] setActive:YES]; [[_rightControl.heightAnchor constraintEqualToConstant:44] setActive:YES]; [_rightControl remakeConstraints:NaviBarControlLayoutRight]; } return _rightControl; } - (UILabel*)tabbarBadgeView { if (!_tabbarBadgeView) { _tabbarBadgeView = [[UILabel alloc]init]; _tabbarBadgeView.textAlignment = NSTextAlignmentCenter; _tabbarBadgeView.layer.cornerRadius = 4; _tabbarBadgeView.textColor = [UIColor whiteColor]; _tabbarBadgeView.backgroundColor = UIColorFromRGB(0xff676b); _tabbarBadgeView.font = [UIFont systemFontOfSize:11]; _tabbarBadgeView.clipsToBounds = YES; _tabbarBadgeView.hidden = YES; } _tabbarBadgeView.frame = CGRectMake(UISCREENWIDTH - (IS_IPHONE_6P ? 28 : 24), 6, 8, 8); return _tabbarBadgeView; } - (UITableView*)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.backgroundColor = COMMON_TABLEVIEW_BACKGROUND_COLOR; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.sectionHeaderHeight = 0.0f; [self initRefreshControlWithTableView:_tableView]; self.refreshControl.bottomEnabled = NO; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [_tableView registerClass:[FKHomePersonInfoCell class] forCellReuseIdentifier:NSStringFromClass([FKHomePersonInfoCell class])]; [_tableView registerClass:[FKHomeFollowInfoCell class] forCellReuseIdentifier:NSStringFromClass([FKHomeFollowInfoCell class])]; [_tableView registerClass:[FKHomeOrderHeaderCell class] forCellReuseIdentifier:NSStringFromClass([FKHomeOrderHeaderCell class])]; [_tableView registerClass:[FKHomeOrderEntranceCell class] forCellReuseIdentifier:NSStringFromClass([FKHomeOrderEntranceCell class])]; [_tableView registerClass:[FKHomeImageTextCell class] forCellReuseIdentifier:NSStringFromClass([FKHomeImageTextCell class])]; } return _tableView; } @end