// // DRMessageListController.m // YouHuiProject // // Created by 小花 on 2018/5/21. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRMessageListController.h" #import "DRMessageListCell.h" #import "DRMessageModel.h" #import "DRGoodDetailViewController.h" #import "DRCommissionMainViewController.h" #import "DRMyFansViewController.h" #import "DRCollectionMainViewController.h" #import "DRChildAccountViewController.h" #import "DRGoodListViewController.h" @interface DRMessageListController () { NSInteger _page; UIButton *rightBtn; } @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArr; @property(nonatomic,strong)UIView *notiView; @end @implementation DRMessageListController - (void)viewDidLoad { [super viewDidLoad]; //监听,是否开启通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isshowOpenNot) name:@"setMessage" object:nil]; [self configNavigationBar]; [self configTableView]; [self requestData]; } -(void)isshowOpenNot { if (![self isUserNotificationEnable]) {//是否开启通知 self.notiView.hidden=NO; self.tableView.frame=CGRectMake(0, NavBarHeight+FITSIZE(50), SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-FITSIZE(50)); }else{ self.notiView.hidden=YES; self.tableView.frame=CGRectMake(0, NavBarHeight+FITSIZE(10), SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-FITSIZE(10)); } } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [self isshowOpenNot]; } -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; [LoadingView dismiss]; } - (void)configTableView { _page = 1; [self.view addSubview:self.tableView]; [self.view addSubview:self.notiView]; [LoadingView show]; } //判断是否开启通知 - (BOOL)isUserNotificationEnable { // 判断用户是否允许接收通知 BOOL isEnable = NO; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f) { // iOS版本 >=8.0 处理逻辑 UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; isEnable = (UIUserNotificationTypeNone == setting.types) ? NO : YES; } else { // iOS版本 <8.0 处理逻辑 UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; isEnable = (UIRemoteNotificationTypeNone == type) ? NO : YES; } return isEnable; } // 如果用户关闭了接收通知功能,该方法可以跳转到APP设置页面进行修改 iOS版本 >=8.0 处理逻辑 - (void)goToAppSystemSetting { UIApplication *application = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([application canOpenURL:url]) { if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { if (@available(iOS 10.0, *)) { [application openURL:url options:@{} completionHandler:nil]; } else { // Fallback on earlier versions } } else { [application openURL:url]; } } } - (void)configNavigationBar { [self.navigationBar setNavTitle:@"我的消息"]; self.navigationBar.backgroundColor = [UIColor changeColor]; self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [rightBtn setImage:[UIImage imageNamed:@"clear_message"] forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside]; } #pragma mark-一键已读功能 -(void)clearAction { NSString *url = [NSString stringWithFormat:@"%@/api/v2/message_push/allMessageRead",BaseURL]; [DRHttp post:url params:nil success:^(id json) { if ([json[@"flag"]intValue] == 1) { for (DRMessageModel *model in self.dataArr) { model.is_view = @"1"; } [SVProgressHUD showSuccessWithStatus:@"全部已读"]; [self.tableView reloadData]; } } failure:^(NSError *error) { }]; } - (void)backAction { [LoadingView dismiss]; [self.navigationController popViewControllerAnimated:YES]; } - (void)requestData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/message_push/MessagePushList",BaseURL]; [DRHttp post:url params:@{@"page":@(_page)} success:^(id json) { NSArray *list = [NSArray yy_modelArrayWithClass:[DRMessageModel class] json:json[@"data"]]; if (list.count>0) { [self.dataArr addObjectsFromArray:list]; [self.tableView.mj_footer endRefreshing]; [self.navigationBar setCustomRightButtons:@[rightBtn]]; }else { if (_page==1) { [self setUpNoDataView]; [self.navigationBar setCustomRightButtons:@[]]; } [self.tableView.mj_footer endRefreshingWithNoMoreData]; } [LoadingView dismiss]; [self.tableView reloadData]; } failure:^(NSError *error) { [self.tableView.mj_footer endRefreshing]; [LoadingView dismiss]; }]; } - (void)setUpNoDataView { self.tableView.showNoDataView = YES; self.tableView.defaultNoDataText = @"您还没有任何记录"; self.tableView.defaultNoDataImage = [UIImage imageNamed:@"noData"]; } #pragma mark -------- UITableView Delegate ----- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 120; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return .1; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { DRMessageModel *model = self.dataArr[indexPath.row]; DRMessageListCell *cell = [DRMessageListCell cellWithTableView:tableView]; cell.model = model; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DRMessageModel *model = self.dataArr[indexPath.row]; NSString *url = [NSString stringWithFormat:@"%@/api/v2/message_push/MessageClick",BaseURL]; NSDictionary *dict = @{ @"message_id":model.Id, @"person_group":model.person_group, @"is_view":model.is_view }; [DRHttp post:url params:dict success:^(id json) { DRMessageListCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell isRead]; } failure:^(NSError *error) { }]; switch ([model.message_type integerValue]) { case 1: [self gotoMyFans]; //我的粉丝 break; case 2: [self gotoGoodDetailPage:model]; //商品详情或列表 break; case 3: [self gotoUpdateApp]; //更新app break; case 4: [self gotoMyOrderPage]; // 我的订单 break; case 5: [self gotoBackMoneyPage]; //提现完成 break; case 6: [self gotoMyCollectionPage]; //我的收藏 break; default: break; } } /** 去我的粉丝 */ - (void)gotoMyFans { DRMyFansViewController *myFans = [[DRMyFansViewController alloc] init]; [self.navigationController pushViewController:myFans animated:YES]; } /** 打开详情页 */ - (void)gotoGoodDetailPage:(DRMessageModel *)model { if ([model.goods_or_group isEqualToString:@"1"]) { DRGoodDetailViewController *goodDetail = [[DRGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time]; goodDetail.requestModel = requestModel; [self.navigationController pushViewController:goodDetail animated:YES]; }else { //列表页 DRGoodListViewController *list = [[DRGoodListViewController alloc] init]; list.cate_id = model.group_id; list.topRequest = 2; [self.navigationController pushViewController:list animated:YES]; } } /** 版本更新 */ - (void)gotoUpdateApp { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_STORE_URL]]; } /** 我的订单 */ - (void)gotoMyOrderPage { DRCommissionMainViewController *orderMain = [[DRCommissionMainViewController alloc] init]; [self.navigationController pushViewController:orderMain animated:YES]; } /** 提现完成 */ - (void)gotoBackMoneyPage { DRChildAccountViewController *account = [[DRChildAccountViewController alloc] init]; [self.navigationController pushViewController:account animated:YES]; } /** 我的收藏 */ - (void)gotoMyCollectionPage { DRCollectionMainViewController *collection = [[DRCollectionMainViewController alloc] init]; [self.navigationController pushViewController:collection animated:YES]; } #pragma mark---开启通知 -(void)opentNotiClickBtn { [self goToAppSystemSetting]; } #pragma mark ------- layzer ------ - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) style:UITableViewStyleGrouped]; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.sectionFooterHeight = 0; _tableView.sectionHeaderHeight = 0; _tableView.estimatedRowHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.backgroundColor = [UIColor yhGrayColor]; _tableView.showsVerticalScrollIndicator = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; _tableView.separatorColor = [UIColor YHColorWithHex:0xEEEEEE]; _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ _page ++; [self requestData]; }]; } return _tableView; } -(UIView *)notiView { if (!_notiView) { _notiView =[[UIView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, FITSIZE(50))]; _notiView.backgroundColor=[UIColor backgroudColor]; UIButton *accountBtn =[[UIButton alloc]initWithFrame:CGRectMake(0, FITSIZE(10), SCREEN_WIDTH, FITSIZE(30))]; accountBtn.backgroundColor =[UIColor whiteColor]; [self.notiView addSubview:accountBtn]; UIButton *openbtn =[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-FITSIZE(56), FITSIZE(6.5), FITSIZE(40), FITSIZE(17))]; [openbtn setTitle:@"开启" forState:UIControlStateNormal]; [openbtn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal]; openbtn.titleLabel.font =[UIFont systemFontOfSize:FITSIZE(9)]; openbtn.layer.cornerRadius=FITSIZE(8.5); openbtn.layer.masksToBounds=YES; openbtn.layer.borderColor =[UIColor homeRedColor].CGColor; openbtn.layer.borderWidth=0.5; [openbtn addTarget:self action:@selector(opentNotiClickBtn) forControlEvents:UIControlEventTouchUpInside]; [accountBtn addSubview:openbtn]; UIImageView *imgv =[[UIImageView alloc]initWithFrame:CGRectMake(FITSIZE(15), FITSIZE(7.5), FITSIZE(15), FITSIZE(15))]; imgv.image =[UIImage imageNamed:@"order_notice"]; [accountBtn addSubview:imgv]; UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(FITSIZE(40), 0, FITSIZE(300), FITSIZE(30))]; label.text=@"您还未开启消息通知,开启获取最新通知"; label.textColor=[UIColor YHColorWithHex:0xFB6526]; label.font=[UIFont systemFontOfSize:FITSIZE(12)]; [accountBtn addSubview:label]; label.tag = 1234; } return _notiView; } - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end