CNLiveCommuntListViewController.m 12.3 KB
//
//  CNLiveCommuntListViewController.m
//  CNLiveCommunitModule
//
//  Created by open on 2022/2/1.
//

#import "CNLiveCommuntListViewController.h"
#import "CNLiveCommuntPaycostViewController.h"
#import "CNLiveCommuntSignUpViewController.h"
@interface CNLiveCommuntListViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, assign) CNLiveCommuntListShowType showType;
@property (nonatomic, strong) MJRefreshAutoNormalFooter * footerView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, strong) NSIndexPath * selectIndexPath;
@property (nonatomic, copy) NSString * nbhdId;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger page;
@end

@implementation CNLiveCommuntListViewController

- (instancetype)initWithShowType:(CNLiveCommuntListShowType)showType Index:(NSInteger)index NBhdId:(NSString *)nbhdId
{
    self = [super init];
    if (self) {
        _nbhdId = nbhdId;
        _index = index;
        _showType = showType;
    }
    return self;
}
/// 初始化界面显示
/// @param nbhdId 小区id
- (instancetype)initWithShowType:(CNLiveCommuntListShowType)showType NBhdId:(NSString *)nbhdId
{
    self = [super init];
    if (self) {
        _nbhdId = nbhdId;
        _page = 1;
        _showType = showType;
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.page = 1;
    [self loadDataSourceWithPage:1 nbhdId:self.nbhdId index:self.index CompleterBlock:NULL];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColorHex(#F9F9F9);
    self.dataArray = [[NSMutableArray alloc] init];
    [self addRefreshMainView];
    if (self.showType == CNLiveCommuntListShowTypePayCost) self.title = @"缴费记录";
    
}

-(void)addRefreshMainView
{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [CNLiveRefreshHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf loadDataSourceWithPage:1 nbhdId:weakSelf.nbhdId index:weakSelf.index CompleterBlock:NULL];
    }];
    self.footerView = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf loadDataSourceWithPage:weakSelf.page nbhdId:weakSelf.nbhdId index:weakSelf.index CompleterBlock:NULL];
    }];
}

-(void)loadDataSourceWithPage:(NSInteger)page nbhdId:(NSString *)nbhdId index:(NSInteger)index CompleterBlock:(void(^)(void))completerBlock
{
    __weak typeof(self) weakSelf = self;
    [CNLiveCommuntViewModel requestWithCommuntyHomeActivityForNBHdId:nbhdId ShowType:self.showType Index:index Page:page completerBlock:^(NSArray * _Nonnull dataArray, BOOL isLastPage) {
        if (dataArray.count > 0 && page == 1 && !weakSelf.tableView.mj_footer) weakSelf.tableView.mj_footer = weakSelf.footerView;
        if (page == 1) [weakSelf.dataArray removeAllObjects];
        [weakSelf.dataArray addObjectsFromArray:dataArray];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        if (isLastPage) [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        if (page == 1 && weakSelf.dataArray.count == 0 && weakSelf.tableView.mj_footer) weakSelf.tableView.mj_footer = nil;
//        if (weakSelf.selectIndexPath) {
//            BOOL isEnough = weakSelf.dataArray.count <= weakSelf.selectIndexPath.row;
//            if (!isEnough) weakSelf.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//            [weakSelf.tableView scrollToRowAtIndexPath:weakSelf.selectIndexPath atScrollPosition:isEnough?UITableViewScrollPositionNone:UITableViewScrollPositionTop animated:NO];
//        }
    }];
}

- (void)initTableView
{
    [super initTableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = UIColorHex(#f9f9f9);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    __weak typeof(self) weakSelf = self;
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(weakSelf.view).offset(kNavigationBarHeight+44);
//        make.left.right.bottom.mas_equalTo(weakSelf.view);
//    }];
}
-(void)initSubviews
{
    [super initSubviews];
}
-(void)layoutTableView
{
    [super layoutTableView];
    if (self.showType == CNLiveCommuntListShowTypePayCost) {
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.frame = CGRectMake(0, kNavigationBarHeight, kScreenWidth, kScreenHeight - kNavigationBarHeight);
    }else{
        __weak typeof(self) weakSelf = self;
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf.view).offset(kNavigationBarHeight+44);
            make.left.right.bottom.mas_equalTo(weakSelf.view);
        }];
    }
}
-(void)dealloc
{
    self.selectIndexPath = nil;
}
#pragma mark - DZNEmptyDataSetSource Methods
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * string = @"";
    if (self.showType == CNLiveCommuntListShowTypeHome) string = @"暂无内容";
    if (self.showType == CNLiveCommuntListShowTypeRecord) string = @"亲,您还没有活动记录哦~";
    if (self.showType == CNLiveCommuntListShowTypeRepair) string = @"亲,您还没有报修记录哦~";
    if (self.showType == CNLiveCommuntListShowTypePayCost) string = @"亲,您还没有缴费记录哦~";
    return [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [CNLiveCommuntTools cCommunt_setImageWithName:@"c_community_content_empty"];
}
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIColor whiteColor];
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSString * nb_info =  (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"kCNLiveCommuntyNBhdInfoIdentifier%@",[CNUserInfoManager manager].userInfoModel.uid]];
    NSString * idStr = [[nb_info mj_JSONObject] stringValueForKey:@"id" default:@""];
    if (self.showType == CNLiveCommuntListShowTypeRepair  && [idStr isNotBlank]) {
        return [[NSAttributedString alloc] initWithString:@"我要报修" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:UIColorHex(#CD0302)}];
    }else{
        return nil;
    }
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    NSString * nb_info =  (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"kCNLiveCommuntyNBhdInfoIdentifier%@",[CNUserInfoManager manager].userInfoModel.uid]];
    NSString * idStr = [[nb_info mj_JSONObject] stringValueForKey:@"id" default:@""];
    if (self.showType == CNLiveCommuntListShowTypeRepair && [idStr isNotBlank]) {
        NSLog(@"点击界面了");
        CNLiveCommuntInformViewController * informView = [[CNLiveCommuntInformViewController alloc] initWithNBhdId:idStr];
        [self.navigationController pushViewController:informView animated:YES];
    }
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 0;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return NO;
}
#pragma mark =

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showType == CNLiveCommuntListShowTypeRecord) {
        CNLiveCommuntyActivityRecordListModel * recordModel = self.dataArray[indexPath.row];
        recordModel.index = [NSString stringWithFormat:@"%ld",(long)self.index];
        return [CNLiveCommuntSubRecordTableView setTableView:tableView RecordModel:recordModel];
    }
    if (self.showType == CNLiveCommuntListShowTypeHome) {
        CNLiveCommuntyActivityHomeListModel * listModel = self.dataArray[indexPath.row];
        return [CNLiveCommuntSubHomeTableView setTableView:tableView ListModel:listModel];
    }
    if (self.showType == CNLiveCommuntListShowTypeRepair) {
        CNLiveCommuntyActivityRepairListModel * repairModel = self.dataArray[indexPath.row];
        return [CNLiveCommuntSubRepairTableView setTableView:tableView RepairModel:repairModel];
    }
    if (self.showType == CNLiveCommuntListShowTypePayCost) {
        CNLiveCommuntyActivityPaycostListModel * paycostModel = self.dataArray[indexPath.row];
        return [CNLiveCommuntPayCostListViewCell setTableView:tableView payCostModel:paycostModel];
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showType == CNLiveCommuntListShowTypeRecord) {
        return 110.0;
    }
    if (self.showType == CNLiveCommuntListShowTypeHome) {
        return 190.0;
    }
    if (self.showType == CNLiveCommuntListShowTypeRepair) {
        CNLiveCommuntyActivityRepairListModel * repairModel = self.dataArray[indexPath.row];
        CGFloat itemW = repairModel.imgs.count > 0 ?  (kScreenWidth - 60)/3 + 20 : 0;
        return ([repairModel.status isEqualToString:@"3"] && [repairModel.totalFee integerValue] > 0?110.0:70.0) + itemW;
    }
    if (self.showType == CNLiveCommuntListShowTypePayCost) {
        CNLiveCommuntyActivityPaycostListModel * paycostModel = self.dataArray[indexPath.row];
        return [paycostModel.payStatus isEqualToString:@"0"] || [paycostModel.payType isEqualToString:@"2"] ? 120.0 : 80.0;
    }
    return 0.001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showType == CNLiveCommuntListShowTypeRepair) {
        self.selectIndexPath = indexPath;
        CNLiveCommuntyActivityRepairListModel * repairModel = self.dataArray[indexPath.row];
        if (self.index == 3) {
            CNLiveCommuntyOrderViewController * orderView = [[CNLiveCommuntyOrderViewController alloc] initWithShowType:CNLiveCommuntyOrderShowTypeListCompletOrder MaintenId:repairModel.idStr];
            [self.navigationController pushViewController:orderView animated:YES];
        }else if (self.index == 2) {
            CNLiveCommuntyOrderViewController * orderView = [[CNLiveCommuntyOrderViewController alloc] initWithShowType:CNLiveCommuntyOrderShowTypeListFailureOrder MaintenId:repairModel.idStr];
            [self.navigationController pushViewController:orderView animated:YES];
        }else {
            CNLiveCommuntyOrderViewController * orderView = [[CNLiveCommuntyOrderViewController alloc] initWithShowType:self.index MaintenId:repairModel.idStr];
            [self.navigationController pushViewController:orderView animated:YES];
        }
    }
    if (self.showType == CNLiveCommuntListShowTypeRecord) {
        self.selectIndexPath = indexPath;
        CNLiveCommuntyActivityRecordListModel * repairModel = self.dataArray[indexPath.row];
        [[CNLiveCommuntyModule shareInstance].protocol pushWebViewWithMainProjectWithActivityId:repairModel.handworkId];
    }
    if (self.showType == CNLiveCommuntListShowTypeHome) {
        self.selectIndexPath = indexPath;
        CNLiveCommuntyActivityHomeListModel * listModel = self.dataArray[indexPath.row];
        [[CNLiveCommuntyModule shareInstance].protocol pushWebViewWithMainProjectWithActivityId:listModel.idStr];
    }
    if (self.showType == CNLiveCommuntListShowTypePayCost) {
        CNLiveCommuntyActivityPaycostListModel * paycostModel = self.dataArray[indexPath.row];
        CNLiveCommuntPaycostViewController * paycostView = [[CNLiveCommuntPaycostViewController alloc] initWithPayCostId:paycostModel.idStr];
        [self.navigationController pushViewController:paycostView animated:YES];
    }
}

- (UIView *)listView {
    return self.view;
}

@end