Files
peko-ios/YuMi/Modules/YMMine/View/SubViews/SuperBlockViewController.m
2024-12-03 17:40:29 +08:00

353 lines
13 KiB
Objective-C

//
// SuperBlockViewController.m
// YuMi
//
// Created by P on 2024/12/3.
//
#import "SuperBlockViewController.h"
#import "XPMineUserInfoPresenter.h"
#import "UserInfoModel.h"
@interface BlockHourCell : UICollectionViewCell
@property(nonatomic, assign) NSInteger hours;
@property(nonatomic, strong) UIImageView *selectedStatusImageView;
@property(nonatomic, strong) UILabel *titleLabel;
@end
@implementation BlockHourCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self.contentView addSubview:self.selectedStatusImageView];
[self.selectedStatusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.contentView);
make.leading.mas_equalTo(0);
make.size.mas_equalTo(CGSizeMake(kGetScaleWidth(18), kGetScaleWidth(18)));
}];
[self.contentView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.contentView);
make.leading.mas_equalTo(self.selectedStatusImageView.mas_trailing).offset(6);
make.trailing.mas_equalTo(self.contentView);
}];
}
return self;
}
- (UIImageView *)selectedStatusImageView {
if (!_selectedStatusImageView) {
_selectedStatusImageView = [[UIImageView alloc] initWithImage:kImage(@"block_unselected")];
}
return _selectedStatusImageView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel labelInitWithText:@"" font:kFontRegular(14) textColor:UIColorFromRGB(0x313131)];
}
return _titleLabel;
}
- (void)setHours:(NSInteger)hours {
_hours = hours;
self.titleLabel.text = [NSString stringWithFormat:YMLocalizedString(@"1.0.30_text_28"), @(hours)];
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
self.selectedStatusImageView.image = kImage(@"block_selected");
} else {
self.selectedStatusImageView.image = kImage(@"block_unselected");
}
}
@end
@interface SuperBlockViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UITextViewDelegate, UIGestureRecognizerDelegate>
@property(nonatomic, copy) NSArray <NSNumber *>*blockTimes;
@property(nonatomic, strong) NetImageView *avatarImageView;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *IDLabel;
@property(nonatomic, strong) UICollectionView *blockedTimeCollectionView;
@property(nonatomic, strong) UITextView *reasonTextView;
@property(nonatomic, strong) UILabel *placeholderLabel;
@property(nonatomic, strong) UIButton *blockButton;
@property(nonatomic, assign) NSInteger selectedHours;
@end
@implementation SuperBlockViewController
- (XPMineUserInfoPresenter *)createPresenter {
return [[XPMineUserInfoPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = YMLocalizedString(@"1.0.30_text_26");
self.view.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSpace:)];
tap.cancelsTouchesInView = NO; // 允许触摸事件继续传递
tap.delegate = self;
[self.view addGestureRecognizer:tap];
@kWeakify(self);
[self.presenter requestBlockTimesSuccess:^(NSArray * _Nonnull array) {
@kStrongify(self);
self.blockTimes = array;
[self setupUI];
[self updateUI];
} failure:^(NSError * _Nonnull error) {
@kStrongify(self);
[self dismissViewControllerAnimated:YES completion:nil];
}];
}
- (void)setupUI {
[self.view addSubview:self.avatarImageView];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(16);
make.top.mas_equalTo(20);
make.size.mas_equalTo(52);
}];
[self.view addSubview:self.nameLabel];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(8);
make.top.mas_equalTo(self.avatarImageView).offset(5);
make.height.mas_equalTo(22);
}];
[self.view addSubview:self.IDLabel];
[self.IDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(8);
make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(2);
make.height.mas_equalTo(18);
}];
[self.view addSubview:[self line]];
UILabel *timeTitleLabel = [self titleLabel:YMLocalizedString(@"1.0.30_text_27") withRedStar:NO];
[self.view addSubview:timeTitleLabel];
[timeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.avatarImageView);
make.top.mas_equalTo(self.avatarImageView.mas_bottom).offset(41);
}];
[self.view addSubview:self.blockedTimeCollectionView];
[self.blockedTimeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(timeTitleLabel.mas_bottom).offset(12);
make.leading.mas_equalTo(self.view).offset(16);
make.trailing.mas_equalTo(self.view).offset(-70);
make.height.mas_equalTo(0);
}];
[self.view addSubview:[self line]];
UILabel *reasonTitleLabel = [self titleLabel:YMLocalizedString(@"1.0.30_text_29") withRedStar:YES];
[self.view addSubview:reasonTitleLabel];
[reasonTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.avatarImageView);
make.top.mas_equalTo(self.blockedTimeCollectionView.mas_bottom).offset(10);
}];
[self.view addSubview:self.reasonTextView];
[self.reasonTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(reasonTitleLabel.mas_bottom).offset(10);
make.leading.trailing.mas_equalTo(self.view).inset(16);
make.height.mas_equalTo(140);
}];
[self.view addSubview:self.blockButton];
[self.blockButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view);
make.top.mas_equalTo(self.reasonTextView.mas_bottom).offset(20);
make.size.mas_equalTo(CGSizeMake(234, 44));
}];
}
- (void)updateUI {
self.selectedHours = [[self.blockTimes xpSafeObjectAtIndex:0] integerValue];
self.avatarImageView.imageUrl = self.targetUser.avatar;
self.nameLabel.text = self.targetUser.nick;
self.IDLabel.text = [NSString stringWithFormat:@"ID: %@", @(self.targetUser.erbanNo)];
[self.blockedTimeCollectionView reloadData];
NSIndexPath *defaultIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self.blockedTimeCollectionView selectItemAtIndexPath:defaultIndexPath
animated:NO
scrollPosition:UICollectionViewScrollPositionNone];
NSInteger lines = ceil(self.blockTimes.count/2.0);
[self.blockedTimeCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(lines * 40);
}];
[self.view layoutIfNeeded];
}
- (void)didTapSpace:(UITapGestureRecognizer *)gesture {
[self.view endEditing:YES]; // 收起键盘
}
- (void)didTapBlock {
if ([NSString isEmpty:self.reasonTextView.text]) {
} else {
@kWeakify(self);
[self.presenter superBlock:self.targetUser.uid
hours:self.selectedHours
resaon:self.reasonTextView.text
success:^{
@kStrongify(self);
[self dismissViewControllerAnimated:YES completion:nil];
} failure:^(NSError * _Nonnull error) {
}];
}
}
#pragma mark -
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// 如果触摸点在 UITextView 或 UICollectionView 内,则不触发手势
if ([touch.view isKindOfClass:[UICollectionView class]] || [touch.view isKindOfClass:[UITextView class]]) {
return NO;
}
return YES; // 触发手势
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
self.placeholderLabel.hidden = ![NSString isEmpty:newText];
self.blockButton.alpha = [NSString isEmpty:newText] ? 0.5 : 1;
return YES;
}
#pragma mark -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.blockTimes.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BlockHourCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BlockHourCell" forIndexPath:indexPath];
NSNumber *hour = [self.blockTimes xpSafeObjectAtIndex:indexPath.row];
cell.hours = hour.integerValue;
return cell;
}
#pragma mark -
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
_avatarImageView = [[NetImageView alloc] init];
[_avatarImageView setCornerRadius:52/2];
}
return _avatarImageView;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [UILabel labelInitWithText:@"" font:kFontMedium(16) textColor:UIColorFromRGB(0x313131)];
}
return _nameLabel;
}
- (UILabel *)IDLabel {
if (!_IDLabel) {
_IDLabel = [UILabel labelInitWithText:@"" font:kFontRegular(13) textColor:UIColorFromRGB(0x7b7b7d)];
}
return _IDLabel;
}
- (UIView *)line {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(16, 102, KScreenWidth-32, 1)];
v.backgroundColor = UIColorFromRGB(0xe4e4e4);
return v;
}
- (UILabel *)titleLabel:(NSString *)title withRedStar:(BOOL)isRed {
UILabel *label = [UILabel labelInitWithText:@"" font:kFontMedium(15) textColor:UIColorFromRGB(0x313131)];
NSMutableAttributedString *s_title = [[NSMutableAttributedString alloc] initWithString:title
attributes:@{
NSFontAttributeName: kFontMedium(15),
NSForegroundColorAttributeName: UIColorFromRGB(0x313131)
}];
if (isRed) {
NSAttributedString *star = [[NSAttributedString alloc] initWithString:@"*" attributes:@{
NSFontAttributeName: kFontMedium(15),
NSForegroundColorAttributeName: UIColorFromRGB(0xff3332)
}];
[s_title insertAttributedString:star atIndex:s_title.length];
}
label.attributedText = s_title;
return label;
}
- (UICollectionView *)blockedTimeCollectionView {
if (!_blockedTimeCollectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake((KScreenWidth - 32)/3, kGetScaleWidth(20));
layout.minimumLineSpacing = 14;
layout.minimumInteritemSpacing = 20;
_blockedTimeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_blockedTimeCollectionView.scrollEnabled = NO;
_blockedTimeCollectionView.allowsMultipleSelection = NO;
_blockedTimeCollectionView.delegate = self;
_blockedTimeCollectionView.dataSource = self;
[_blockedTimeCollectionView registerClass:[BlockHourCell class] forCellWithReuseIdentifier:@"BlockHourCell"];
}
return _blockedTimeCollectionView;
}
- (UITextView *)reasonTextView {
if (!_reasonTextView) {
_reasonTextView = [[UITextView alloc] init];
_reasonTextView.delegate = self;
[_reasonTextView setCornerRadius:10];
_reasonTextView.backgroundColor = UIColorFromRGB(0xf6f6f6);
[_reasonTextView addSubview:self.placeholderLabel];
[self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(10);
make.leading.mas_equalTo(12);
make.trailing.mas_equalTo(-12);
}];
}
return _reasonTextView;
}
- (UIButton *)blockButton {
if (!_blockButton) {
_blockButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_blockButton addGradientBackgroundWithColors:@[UIColorFromRGB(0xE29030), UIColorFromRGB(0xfcc074)]
startPoint:CGPointMake(0, 0.5)
endPoint:CGPointMake(1, 0.5)
cornerRadius:22];
[_blockButton setTitle:YMLocalizedString(@"1.0.30_text_31") forState:UIControlStateNormal];
[_blockButton addTarget:self action:@selector(didTapBlock) forControlEvents:UIControlEventTouchUpInside];
_blockButton.alpha = 0.5;
}
return _blockButton;
}
- (UILabel *)placeholderLabel {
if (!_placeholderLabel) {
_placeholderLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.30_text_30")
font:kFontRegular(14)
textColor:UIColorFromRGB(0x7b7b7d)];
}
return _placeholderLabel;
}
@end