123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- //
- // SPSBDiscountCouponTableViewController.m
- // 我的社保
- //
- // Created by shanp on 2021/5/24.
- //
- #import "SPSBDiscountCouponTableViewController.h"
- #import "SPSBUIGeneralHeader.h"
- #import "SPSBDiscountCouponModel.h"
- #import "UIViewController+SPSBLoadDataStatus.h"
- #import "UIScrollView+SPSBAutoLoadData.h"
- #import "UIViewController+SPSBNetworkManager.h"
- #import "SPSBKeyProfile.h"
- #import "UITableView+SPSBDefault.h"
- #import <CALayer+JXHShadow.h>
- #import "SPSBRouteManager.h"
- @interface SPSBDiscountCouponTableViewCell () {
- SPSBDiscountCouponViewType _viewType;
- UIImageView *_amountView;
- UILabel *_amountLabel;
- UILabel *_titleLabel;
- UILabel *_description;
- UILabel *_outDateLabel;
- UILabel *_expiryDateLabel;
- UIImageView *_chooseView;
- UIButton *_useButton;
- }
- @end
- @implementation SPSBDiscountCouponTableViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier viewType:(SPSBDiscountCouponViewType)viewType {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (!self) return nil;
-
- _viewType = viewType;
- self.selectionStyle = UITableViewCellSelectionStyleNone;
-
- UIView *contentView = UIView.new;
- contentView.backgroundColor = spsb_FFFFFF_color(1.f);
- [contentView.layer setShadowWithOffset:(CGSize){0,2} alpha:0.06 blur:4 spread:0 color:spsb_000000_color(1.f) cornerRadius:4 pathRect:(CGRect){0, 0, jxh_screenWidth() - 30, 100}];
- [self.contentView addSubview:contentView];
- [contentView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(0);
- make.leading.equalTo(15);
- make.height.equalTo(100);
- make.trailing.equalTo(-15);
- }];
-
- _amountView = UIImageView.new;
- _amountView.image = jxh_getImage(discount_coupon_left_unuse);
- _amountView.highlightedImage = jxh_getImage(discount_coupon_left_used);
- [contentView addSubview:_amountView];
- [_amountView makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.and.top.equalTo(0);
- }];
-
- UIView *amountView = [[UIView alloc] init];
- [_amountView addSubview:amountView];
- [amountView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(20);
- make.centerX.equalTo(self->_amountView);
- }];
-
- UILabel *symbol = [UILabel convenienceWithFont:spsb_font(17) text:@"¥" textColor:spsb_FFFFFF_color(1.f)];
- [amountView addSubview:symbol];
- [symbol makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(0);
- make.bottom.equalTo(-5);
- }];
-
- _amountLabel = [UILabel convenienceWithFont:spsb_semiboldFont(40) text:@"" textColor:spsb_FFFFFF_color(1.f)];
- [amountView addSubview:_amountLabel];
- [_amountLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.trailing.and.top.and.bottom.equalTo(0);
- make.leading.equalTo(symbol.trailing).offset(2);
- make.height.equalTo(40);
- }];
-
- _outDateLabel = [UILabel convenienceWithFont:spsb_font(10) text:nil textColor:spsb_FFFFFF_color(1.f)];
- [_amountView addSubview:_outDateLabel];
- [_outDateLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(70);
- make.centerX.equalTo(amountView);
- }];
-
- _titleLabel = [UILabel convenienceWithFont:spsb_font(15) text:@"" textColor:spsb_666666_color()];
- [contentView addSubview:_titleLabel];
- [_titleLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(125);
- make.top.equalTo(20);
- make.trailing.lessThanOrEqualTo(-45);
- }];
-
- _description = [UILabel convenienceWithFont:spsb_font(12) text:@"" textColor:spsb_999999_color()];
- [contentView addSubview:_description];
- [_description makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(self->_titleLabel);
- make.top.equalTo(self->_titleLabel.bottom).offset(8);
- make.trailing.lessThanOrEqualTo(-45);
- }];
-
- _expiryDateLabel = [UILabel convenienceWithFont:spsb_font(12) text:@"" textColor:spsb_999999_color()];
- [contentView addSubview:_expiryDateLabel];
- [_expiryDateLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(self->_titleLabel);
- make.top.equalTo(self->_description.bottom).offset(8);
- }];
-
- if (viewType == SPSBDiscountCouponViewTypeUse) {
- _chooseView = UIImageView.new;
- _chooseView.image = jxh_getImage(filter_n);
- _chooseView.highlightedImage = jxh_getImage(filter_s);
- [contentView addSubview:_chooseView];
- [_chooseView makeConstraints:^(JXHConstraintMaker *make) {
- make.trailing.equalTo(-15);
- make.centerY.equalTo(0);
- }];
- } else {
- _useButton = [UIButton convenienceWithTarget:self action:@selector(useAction)];
- [_useButton setImage:jxh_getImage(cash_coupon_right) state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
- [contentView addSubview:_useButton];
- [_useButton makeConstraints:^(JXHConstraintMaker *make) {
- make.trailing.and.top.equalTo(0);
- }];
- }
-
- return self;
- }
- - (void)reloadData:(SPSBDiscountCouponModel *)data chose:(bool)chose {
- _amountLabel.text = data.spsb_coupon_price;
- _description.text = [NSString stringWithFormat:@"说明:%@", data.spsb_coupon_desc];
- _titleLabel.text = data.spsb_coupon_name;
- _expiryDateLabel.text = [NSString stringWithFormat:@"有效期:%@", data.spsb_end_time];
- if (_viewType == SPSBDiscountCouponViewTypeUse) {
- _chooseView.highlighted = chose;
- } else {
- _useButton.hidden = !data.spsba_canUse;
- }
- _amountView.highlighted = !data.spsba_canUse;
- _outDateLabel.text = data.spsba_status;
-
- }
- - (void)useAction {
- if (_spsb_useAction) {
- _spsb_useAction(self);
- }
- }
- @end
- @interface SPSBDiscountCouponTableViewController () {
- NSMutableArray<SPSBDiscountCouponModel *> *_dataArray;
- NSUInteger _currentPage;
- NSMutableSet *_chose;
- }
- @end
- @implementation SPSBDiscountCouponTableViewController
- - (instancetype)init {
- self = [super initWithStyle:UITableViewStyleGrouped];
- if (!self) return nil;
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self initData];
- [self setupTableView];
- }
- - (void)initData {
- _currentPage = 0;
- if (_spsb_viewType == SPSBDiscountCouponViewTypeUse) {
- _chose = NSMutableSet.new;
- }
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- if (!_dataArray) {
- [self getDataList];
- }
- }
- #pragma mark - Action
- - (void)useAction:(SPSBDiscountCouponTableViewCell *)cell {
- SPSBRouter *router = [[SPSBRouter alloc] initWithClassName:@"" property:nil tabBarType:SPSBTabBarTypeHome];
- [SPSBRouteManager navigationBackAndRouteTo:router];
- }
- - (NSArray<SPSBDiscountCouponModel *> *)spsb_data {
- return _dataArray;
- }
- - (NSSet *)spsb_chose {
- return _chose;
- }
- #pragma mark - Network Action
- - (void)getDataList {
- self.tableView.spsb_canLoadData = false;
- @weakify(self)
- NSInteger page = _currentPage + 1;
- [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:!_dataArray ? @"加载中" : nil isLogin:true url:spsb_appUrl(SPSBUrlSearchCashCoupon) urlParameters:nil parameters:^NSDictionary * _Nonnull{
- return @{@"phone": [jxh_userDefaults() objectForKey:SPSBUserName], @"page": @(page), @"expired": @(!self->_spsb_canUse)};
- } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
- @strongify(self)
- [self getDataListSuccess:data];
- return @"";
- } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
- @strongify(self)
- [self getDataListFailure:error];
- return !self->_dataArray ? @"加载失败" : nil;
- }];
- }
- - (void)getDataListSuccess:(id)data {
- if (!_dataArray) {
- _dataArray = NSMutableArray.new;
- }
- _currentPage ++;
- NSArray *array = [SPSBDiscountCouponModel getModelListWithArray:data[@"data"][@"list"]];
- [_dataArray addObjectsFromArray:array];
- [self.tableView reloadData];
- if (_dataArray.count == 0) {
- [self.tableView setDefaultFootViewWithError:nil image:jxh_getImage(youhuijuan_qs_f) height:jxh_viewHeight(self.tableView) action:^{
- }];
- } else {
- [self.tableView removeDefaultFootView];
- }
- if (array.count < 10) {
- self.tableView.spsb_canLoadData = false;
- } else {
- self.tableView.spsb_canLoadData = true;
- }
- [self.tableView endAutoLoadData];
- }
- - (void)getDataListFailure:(NSError *)error {
- if (_dataArray.count == 0) {
- @weakify(self)
- [self.tableView setDefaultFootViewWithError:error image:nil height:jxh_viewHeight(self.tableView) - 15 action:^{
- @strongify(self)
- [self getDataList];
- }];
- self.tableView.spsb_canLoadData = false;
- } else {
- self.tableView.spsb_canLoadData = true;
- }
- }
- #pragma mark - Delegate & DataSource
- static NSString * const reuseIdentifier = @"SPSBDiscountCouponTableViewCell";
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- SPSBDiscountCouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
- if (!cell) {
- cell = [[SPSBDiscountCouponTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier viewType:_spsb_viewType];
- if (_spsb_viewType == SPSBDiscountCouponViewTypeDisplay) {
- @weakify(self)
- cell.spsb_useAction = ^(SPSBDiscountCouponTableViewCell *cell) {
- @strongify(self)
- [self useAction:cell];
- };
- }
-
- }
- if (_spsb_viewType == SPSBDiscountCouponViewTypeDisplay) {
- [cell reloadData:_dataArray[indexPath.row] chose:false];
- return cell;
- }
- [cell reloadData:_dataArray[indexPath.row] chose:[_chose containsObject:@(indexPath.row)]];
- return cell;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _dataArray.count;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:false];
- if (_spsb_viewType == SPSBDiscountCouponViewTypeUse) {
- SPSBDiscountCouponModel *model = _dataArray[indexPath.row];
- if ([model.spsb_status integerValue] == 1 && [model.spsb_expired integerValue] == 0) {
- if ([_chose containsObject:@(indexPath.row)]) {
- [_chose removeObject:@(indexPath.row)];
- [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
- if (_spsb_afterChoose) {
- _spsb_afterChoose();
- }
- return;
- }
- if (_chose.count >= _spsb_maxChose) {
- NSString *title = [NSString stringWithFormat:@"本次购买只可选择%ld张抵用券", (long)_spsb_maxChose];
- spsb_singleAlertShow(title, self);
- return;
- }
- [_chose addObject:@(indexPath.row)];
- [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
- if (_spsb_afterChoose) {
- _spsb_afterChoose();
- }
- }
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- return 15;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- return UIView.new;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
- return jxh_onePixe();
- }
- - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
- return UIView.new;
- }
- #pragma mark - UI
- - (void)setupTableView {
- self.tableView.backgroundColor = spsb_FFFFFF_color(1.f);
- self.tableView.rowHeight = 114.f;
- self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- self.tableView.showsHorizontalScrollIndicator = false;
- self.tableView.showsVerticalScrollIndicator = false;
- self.tableView.alwaysBounceVertical = true;
-
- @weakify(self)
- [self.tableView setAutoLoadDataWithAction:^{
- @strongify(self)
- [self getDataList];
- }];
- }
- @end
|