MyFeedListController.m 8.0 KB
#import "MyFeedListController.h"
#import "MyFeedCell.h"
#import "FeedbackRequestModel.h"
#import "FeedbackDetailController.h"

@interface MyFeedListController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *feedbackIV;
@property (nonatomic, strong) NSArray *listData;
@end

@implementation MyFeedListController

- (instancetype)initWithDelegate:(id<MyFeedListControllerDelegate>)delegate {
    if (self = [super init]) {
        _delegate = delegate;
        _listData = [NSArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的反馈";
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    [self.view addSubview:self.feedbackIV];
    
    WS(weakSelf);
    self.feedbackIV.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
        [FeedbackRequestModel queryUserAdviceListWithCompletion:^(FeedbackRequestModel * _Nonnull requestModel) {
            [weakSelf.feedbackIV.mj_header endRefreshing];
            if (requestModel.resCode == DSResCodeSuccess) {
                weakSelf.listData = requestModel.adviceListArr;
                [weakSelf.feedbackIV reloadData];
            }
        }];
    }];
    [self.feedbackIV.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 7.5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 7.5)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 7.5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 7.5)];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:cardRadiusCellIdentifier];
    MyFeedModel *model = self.listData[indexPath.row];
    [cell updateUI:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MyFeedModel *model = self.listData[indexPath.row];
    if (model.read_status == 0) {
        model.read_status = 1;
        if (self.delegate && [self.delegate respondsToSelector:@selector(didClickMessage)]) {
            [self.delegate didClickMessage];
        }
    }
    
    FeedbackDetailController *detailVC = [FeedbackDetailController new];
    detailVC.advice_id = model.feed_id;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    UIRectCorner corner = UIRectCornerAllCorners;
    [cell insetCardViewWithCorners:corner
                        edgeInsets:UIEdgeInsetsMake(7.5, 15.0, 7.5, 15.0)
                            radius:12.f
                       borderWidth:1.f
                      borderCorlor:DSClearColor
                   backgroundColor:([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? DSWhite : CornerViewDarkColor)];
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setupSlideBtnWithEditingIndexPath:indexPath];
    });
}

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSMutableArray *mArr = [NSMutableArray arrayWithArray:self.listData];
    MyFeedModel *model = self.listData[indexPath.row];
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除按钮宽度" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 删除反馈请求
        [DSProgressHUD showProgressHUDWithInfo:@""];
        [FeedbackRequestModel deleteAdviceFeedbackWithAdviceID:model.feed_id completion:^(FeedbackRequestModel * _Nonnull requestModel) {
            [DSProgressHUD dissmissProgressHUD];
            if (requestModel.resCode == DSResCodeSuccess) {
                [DSProgressHUD showToast:@"删除成功"];
                [mArr removeObjectAtIndex:indexPath.row];
                self.listData = [mArr copy];
                [tableView reloadData];
            }
        }];
        // 这句很重要,退出编辑模式,隐藏左滑菜单
        [tableView setEditing:NO animated:YES];
    }];
    
    // 主动调用代理方法,防止快速滑动时显示原生
    [self tableView:self.feedbackIV willBeginEditingRowAtIndexPath:indexPath];
    
    return @[deleteAction];
}

#pragma mark - 设置左滑按钮的样式
- (void)setupSlideBtnWithEditingIndexPath:(NSIndexPath *)editingIndexPath {
    // 判断系统是否是iOS13及以上版本
    if (@available(iOS 13.0, *)) {
        for (UIView *subView in self.feedbackIV.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
                // 修改图片
                UIView *remarkContentView = subView.subviews.firstObject;
                [self setupRowActionView:remarkContentView];
            }
        }
        return;
    }
    // 判断系统是否是 iOS11 及以上版本
    if (@available(iOS 11.0, *)) {
        for (UIView *subView in self.feedbackIV.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
                // 修改图片
                UIView *remarkContentView = subView;
                [self setupRowActionView:remarkContentView];
            }
        }
        return;
    }
    
    // iOS11 以下的版本
    MyFeedCell *cell = [self.feedbackIV cellForRowAtIndexPath:editingIndexPath];
    for (UIView *subView in cell.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
            // 修改图片
            UIView *remarkContentView = subView;
            [self setupRowActionView:remarkContentView];
        }
    }
}

- (void)setupRowActionView:(UIView *)rowActionView {
    rowActionView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    // 拿到原来的删除按钮
    UIButton *oriDeleteBtn = rowActionView.subviews.firstObject;
    oriDeleteBtn.hidden = YES;
    
    // 自定义删除按钮
    UIButton *cusDeleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 7.5, oriDeleteBtn.width - 15, oriDeleteBtn.height - 15)];
    [cusDeleteBtn cornerRadius:12];
    cusDeleteBtn.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
    [cusDeleteBtn dk_setImage:DKImagePickerWithNames(@"delete_feed_icon", @"dk_delete_feed_icon", @"delete_feed_icon") forState:UIControlStateNormal];
    [rowActionView addSubview:cusDeleteBtn];
}

#pragma mark - lazy
- (UITableView *)feedbackIV {
    if (!_feedbackIV) {
        _feedbackIV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopHeight(0)) style:UITableViewStylePlain];
        _feedbackIV.delegate = self;
        _feedbackIV.dataSource = self;
        _feedbackIV.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
        _feedbackIV.showsVerticalScrollIndicator = NO;
        [_feedbackIV registerClass:[MyFeedCell class] forCellReuseIdentifier:cardRadiusCellIdentifier];
        _feedbackIV.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _feedbackIV;
}

#pragma mark - 品牌模式
- (NaviStyle)navigationBarStyle {
    return NaviStyleDefault;
}

@end