Files
yinmeng-ios/xplan-ios/Main/Room/View/WishGift/View/XPWishGiftCreateViewController.m
2022-10-19 19:33:26 +08:00

267 lines
9.3 KiB
Objective-C

//
// XPWishGiftCreateViewController.m
// xplan-ios
//
// Created by 冯硕 on 2022/10/18.
//
#import "XPWishGiftCreateViewController.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "UIImage+Utils.h"
#import "TTPopup.h"
#import "XPMacro.h"
///Model
#import "WishGiftInfoModel.h"
///View
#import "XPWishGiftTableViewCell.h"
#import "XPWishGiftHistoryViewController.h"
#import "XPWishGiftCreateItemViewController.h"
///P
#import "XPWishGiftPresenter.h"
#import "XPWishGiftProtocol.h"
@interface XPWishGiftCreateViewController ()<UITableViewDelegate, UITableViewDataSource, XPWishGiftProtocol, XPWishGiftTableViewCellDelegate>
///设置心愿
@property (nonatomic,strong) UILabel *configLabel;
///清除心愿
@property (nonatomic,strong) UIButton *clearWishButton;
///添加心愿
@property (nonatomic,strong) UIButton *addWishButton;
///列表
@property (nonatomic,strong) UITableView *tableView;
///创建
@property (nonatomic,strong) UIButton *createButton;
///数据源
@property (nonatomic,strong) NSArray *datasource;
///房主的uid
@property (nonatomic,strong) NSString *roomUid;
@end
@implementation XPWishGiftCreateViewController
- (__kindof id)createPresenter {
return [[XPWishGiftPresenter alloc] init];
}
- (instancetype)initWithRoomUid:(NSString *)roomUid {
if (self = [super init]) {
self.roomUid = roomUid;
[self initWishGiftList];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
}
#pragma mark - Private Method
- (void)initWishGiftList {
[self.presenter wishGiftList:self.roomUid];
}
- (void)initSubViews {
self.title = @"创建心愿单";
[self.view addSubview:self.configLabel];
[self.view addSubview:self.clearWishButton];
[self.view addSubview:self.addWishButton];
[self.view addSubview:self.tableView];
[self.view addSubview:self.createButton];
[self addNavigationItemWithTitles:@[@"历史心愿"] titleColor:[ThemeColor secondTextColor] isLeft:NO target:self action:@selector(rightButtonAction:) tags:nil];
}
- (void)initSubViewConstraints {
[self.configLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view).offset(24);
make.top.mas_equalTo(self.view).offset(14);
}];
[self.clearWishButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 20));
make.centerY.mas_equalTo(self.configLabel);
make.right.mas_equalTo(self.addWishButton.mas_left).offset(-10);
}];
[self.addWishButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.configLabel);
make.right.mas_equalTo(self.view).offset(-18);
make.size.mas_equalTo(CGSizeMake(60, 20));
}];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(15);
make.height.mas_equalTo(135);
make.top.mas_equalTo(self.configLabel.mas_bottom).offset(13);
}];
[self.createButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(80);
make.height.mas_equalTo(37);
make.top.mas_equalTo(self.tableView.mas_bottom).offset(48);
}];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XPWishGiftTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPWishGiftTableViewCell class])];
if (cell == nil) {
cell = [[XPWishGiftTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPWishGiftTableViewCell class])];
}
WishGiftInfoModel *giftInfo = [self.datasource objectAtIndex:indexPath.row];
giftInfo.row = indexPath.row;
cell.giftInfo = giftInfo;
cell.delegate = self;
return cell;
}
#pragma mark - XPWishGiftTableViewCellDelegate
- (void)xPWishGiftTableViewCell:(XPWishGiftTableViewCell *)view didClearWishGift:(WishGiftInfoModel *)info {
if (info) {
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = @"";
config.message = @"确认删除该条数据?";
[TTPopup alertWithConfig:config confirmHandler:^{
[self.presenter deleteWishGiftItem:self.roomUid itemId:[NSString stringWithFormat:@"%ld", info.itemId]];
} cancelHandler:^{
}];
}
}
- (void)xPWishGiftTableViewCell:(XPWishGiftTableViewCell *)view didUpdateWishGift:(WishGiftInfoModel *)info {
//TODO: 修改心愿单
}
#pragma mark - XPWishGiftProtocol
- (void)getWishGiftListSuccess:(NSArray *)list {
self.datasource = list;
[self.tableView reloadData];
}
- (void)deleteWishGiftItemSuccess {
[self initWishGiftList];
}
#pragma mark - Event Response
- (void)rightButtonAction:(UIButton *)sender {
XPWishGiftHistoryViewController * historyVC = [[XPWishGiftHistoryViewController alloc] init];
[self.navigationController pushViewController:historyVC animated:YES];
}
- (void)clearButtonAction:(UIButton *)sender {
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = @"";
config.message = @"确认清空当前数据?";
[TTPopup alertWithConfig:config confirmHandler:^{
[self.presenter deleteWishGiftItem:self.roomUid itemId:@"-1"];
} cancelHandler:^{
}];
}
- (void)addWishButtonAction:(UIButton *)sender {
XPWishGiftCreateItemViewController * createItemVC = [[XPWishGiftCreateItemViewController alloc] init];
createItemVC.roomUid = self.roomUid;
@kWeakify(self);
createItemVC.Dismiss = ^(BOOL finish) {
@kStrongify(self);
[self initWishGiftList];
};
createItemVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self.navigationController presentViewController:createItemVC animated:YES completion:nil];
}
- (void)createButtonAction:(UIButton *)sender {
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = @"";
config.message = @"确认创建当前心愿? \n创建后当日不可在编辑或删除";
[TTPopup alertWithConfig:config confirmHandler:^{
[self.presenter addWishGift:self.roomUid];
} cancelHandler:^{
}];
}
#pragma mark - Getters And Setters
- (UILabel *)configLabel {
if (!_configLabel) {
_configLabel = [[UILabel alloc] init];
_configLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
_configLabel.textColor = [ThemeColor mainTextColor];
_configLabel.text = @"设置心愿";
}
return _configLabel;
}
- (UIButton *)clearWishButton {
if (!_clearWishButton) {
_clearWishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_clearWishButton setTitle:@"清空数据" forState:UIControlStateNormal];
[_clearWishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_clearWishButton.titleLabel.font = [UIFont systemFontOfSize:10];
_clearWishButton.backgroundColor = [ThemeColor colorWithHexString:@"#C7DAEA"];
_clearWishButton.layer.masksToBounds = YES;
_clearWishButton.layer.cornerRadius = 10;
[_clearWishButton addTarget:self action:@selector(clearButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _clearWishButton;
}
- (UIButton *)addWishButton {
if (!_addWishButton) {
_addWishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_addWishButton setTitle:@"添加心愿" forState:UIControlStateNormal];
[_addWishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_addWishButton.titleLabel.font = [UIFont systemFontOfSize:10];
[_addWishButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
_addWishButton.layer.masksToBounds = YES;
_addWishButton.layer.cornerRadius = 10;
[_addWishButton addTarget:self action:@selector(addWishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _addWishButton;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.layer.masksToBounds = YES;
_tableView.layer.cornerRadius = 8;
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPWishGiftTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPWishGiftTableViewCell class])];
}
return _tableView;
}
- (UIButton *)createButton {
if (!_createButton) {
_createButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_createButton setTitle:@"确定创建" forState:UIControlStateNormal];
[_createButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_createButton.titleLabel.font = [UIFont systemFontOfSize:12];
[_createButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
_createButton.layer.masksToBounds = YES;
_createButton.layer.cornerRadius = 37 / 2;
[_createButton addTarget:self action:@selector(createButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _createButton;
}
@end