Files
yinmeng-ios/xplan-ios/Main/Monents/View/XPMonentsPublishViewController.m

579 lines
19 KiB
Mathematica
Raw Normal View History

2022-07-01 18:48:58 +08:00
//
// XPMonentsPublishViewController.m
// xplan-ios
//
// Created by on 2022/7/1.
//
#import "XPMonentsPublishViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <SZTextView/SZTextView.h>
#import <TZImagePickerController/TZImagePickerController.h>
///Tool
#import "XPMacro.h"
#import "ThemeColor.h"
#import "UIImage+Utils.h"
#import "Api+Mine.h"
#import "UploadImage.h"
#import "TTPopup.h"
///View
#import "XPMonentsPublishCollectionViewCell.h"
#define itemWidth (KScreenWidth - 10 * 2 - 15 * 2) / 3.0 // item
@interface XPMonentsPublishViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UITextViewDelegate, TZImagePickerControllerDelegate, XPMonentsPublishCollectionViewCellDelegate>
///
@property (nonatomic,strong) UIView * contentView;
///
@property (nonatomic,strong) UIView * navView;
///
@property (nonatomic,strong) UIButton *backButton;
///
@property (nonatomic,strong) UILabel *titleLabel;
///
@property (nonatomic,strong) UIButton *publishButton;
///
@property (nonatomic, strong) SZTextView *textView;
///
@property (nonatomic,strong) UILabel *limitLabel;
///线
@property (nonatomic,strong) UIView * lineView;
///
@property (nonatomic,strong) UICollectionView *collectionView;
///
@property (nonatomic,strong) UIStackView *topicStackView;
///
@property (nonatomic,strong) UIImageView *topicImageView;
///#
@property (nonatomic,strong) UIImageView *iconImageView;
///
@property (nonatomic,strong) UILabel *topicLabel;
///
@property (nonatomic,strong) UIButton *closeTopicButton;
///
@property (nonatomic,strong) UIButton *addButton;
///
@property (nonatomic,strong) UIButton *againButton;
///
@property (nonatomic,strong) UIView * topicPlaceView;
///
@property (nonatomic,strong) UILabel *descriptionLabel;
///
@property (nonatomic,assign) BOOL isEdited;
///
@property (nonatomic,strong) UIImage *addPicImage;
///
@property (nonatomic,strong) NSMutableArray<UIImage *> *datasource;
///
@property (nonatomic,strong) NSMutableArray *originAssets;
///
@property (nonatomic,strong) NSMutableArray<NSString *> *imageUrls;
///
@property (nonatomic,assign) int imageCount;
@end
@implementation XPMonentsPublishViewController
- (BOOL)isHiddenNavBar {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
}
#pragma mark - Private Method
- (void)initSubViews {
[self.view addSubview:self.navView];
[self.view addSubview:self.contentView];
[self.contentView addSubview:self.textView];
[self.contentView addSubview:self.limitLabel];
[self.contentView addSubview:self.lineView];
[self.contentView addSubview:self.collectionView];
[self.contentView addSubview:self.topicStackView];
[self.contentView addSubview:self.descriptionLabel];
[self.navView addSubview:self.backButton];
[self.navView addSubview:self.titleLabel];
[self.navView addSubview:self.publishButton];
[self.topicStackView addArrangedSubview:self.addButton];
[self.topicStackView addArrangedSubview:self.topicImageView];
[self.topicStackView addArrangedSubview:self.topicPlaceView];
[self.topicStackView addArrangedSubview:self.againButton];
[self.topicImageView addSubview:self.iconImageView];
[self.topicImageView addSubview:self.topicLabel];
[self.topicImageView addSubview:self.closeTopicButton];
[self.datasource addObject:self.addPicImage];
self.topicImageView.hidden = YES;
self.againButton.hidden = YES;
}
- (void)initSubViewConstraints {
[self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.view);
make.height.mas_equalTo(kNavigationHeight);
}];
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view).mas_offset(10);
make.top.mas_equalTo(statusbarHeight);
make.height.width.mas_equalTo(44);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.navView);
make.centerY.mas_equalTo(self.backButton);
}];
[self.publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.view).offset(-15);
make.centerY.mas_equalTo(self.titleLabel);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view);
make.top.mas_equalTo(self.navView.mas_bottom);
make.bottom.mas_equalTo(self.view);
}];
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView).inset(15);
make.top.mas_equalTo(self.contentView).offset(10);
make.height.mas_equalTo(150);
}];
[self.limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.textView.mas_bottom).offset(5);
make.right.mas_equalTo(self.textView);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.limitLabel.mas_bottom).offset(10);
make.left.right.mas_equalTo(self.textView);
make.height.mas_equalTo(1);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView).inset(15);
make.top.mas_equalTo(self.lineView.mas_bottom).offset(10);
make.height.mas_equalTo(itemWidth);
}];
[self.topicStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView).inset(15);
make.top.mas_equalTo(self.collectionView.mas_bottom).offset(15);
make.height.mas_equalTo(20);
}];
[self.topicImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.topicLabel.mas_right).offset(9);
}];
[self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(80);
}];
[self.againButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(80);
}];
[self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.topicImageView).offset(9);
make.size.mas_equalTo(CGSizeMake(16, 16));
make.centerY.mas_equalTo(self.topicImageView);
}];
[self.topicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.iconImageView.mas_right).offset(4);
make.centerY.mas_equalTo(self.topicImageView);
}];
[self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(20);
make.bottom.mas_equalTo(self.view).offset(-25);
}];
}
- (void)uploadAlbumPicList:(NSArray *)array {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (UIImage * image in array) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[Api qiniuUpLoadImage:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
NSString *key = data.data[@"key"];
NSString *token = data.data[@"token"];
[UploadImage uploadImage:image named:key token:token success:^(NSString * _Nonnull key, NSDictionary * _Nonnull resp) {
dispatch_semaphore_signal(semaphore);
self.imageCount ++;
NSString *url = resp[@"path"];
[self.imageUrls addObject:url];
if (self.imageCount == array.count) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"上传成功%@", self.imageUrls);
});
}
} failure:^(NSNumber * _Nonnull resCode, NSString * _Nonnull message) {
self.imageCount ++;
dispatch_semaphore_signal(semaphore);
}];
} else {
self.imageCount ++;
dispatch_semaphore_signal(semaphore);
}
}];
}
});
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
if (textView.text.length > 500) {
[self showErrorToast:@"最多只能输入 500 个字符哦"];
textView.text = [textView.text substringToIndex:500];
}
self.limitLabel.text = [NSString stringWithFormat:@"%lu/%ld",((unsigned long)textView.text.length), (long)500];
self.isEdited = textView.text.length > 0;
}
#pragma mark - UICollectionViewDelegate And UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.datasource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPMonentsPublishCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMonentsPublishCollectionViewCell class]) forIndexPath:indexPath];
UIImage * image = [self.datasource objectAtIndex:indexPath.row];
if (indexPath.row == (self.datasource.count -1)) {
cell.isShowDelete = self.datasource.count == 9;
} else {
cell.isShowDelete = YES;
}
cell.image = image;
cell.delegate = self;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0 && self.datasource.count != 9) {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
imagePickerVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.allowTakeVideo = NO;
imagePickerVc.maxImagesCount = 9;
imagePickerVc.naviBgColor = [ThemeColor appCellBackgroundColor];
imagePickerVc.naviTitleColor = [ThemeColor mainTextColor];
imagePickerVc.barItemTextColor = [ThemeColor mainTextColor];
imagePickerVc.selectedAssets = self.originAssets;
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
}
#pragma mark - XPMonentsPublishCollectionViewCellDelegate
- (void)XPMonentsPublishCollectionViewCell:(XPMonentsPublishCollectionViewCell *)view didDeleteItem:(UIImage *)image {
[TTPopup alertWithMessage:@"确认删除吗?" confirmHandler:^{
} cancelHandler:^{
}];
if (self.datasource.count > 1) {
self.isEdited = YES;
} else {
self.isEdited = NO;
}
}
#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
[self.datasource removeAllObjects];
[self.originAssets removeAllObjects];
[self.datasource addObjectsFromArray:photos];
[self.originAssets addObjectsFromArray:assets];
if (self.datasource.count >= 9) {
if ([self.datasource containsObject:self.addPicImage]) {
[self.datasource removeObject:self.addPicImage];
}
} else {
if (![self.datasource containsObject:self.addPicImage]) {
[self.datasource addObject:self.addPicImage];
}
}
int page = (int)self.datasource.count / 3;
int scale = self.datasource.count % 3;
CGFloat height;
if (scale == 0) {
height = (itemWidth + 10) * page;
} else {
height = (itemWidth + 10) * (page + 1);
}
[self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(height);
}];
[self.collectionView reloadData];
if (self.datasource.count > 1) {
self.isEdited = YES;
} else {
self.isEdited = NO;
}
NSArray * array;
if (self.datasource.count < 9 && self.datasource.count > 1) {
array = [self.datasource subarrayWithRange:NSMakeRange(0, self.datasource.count - 1)];
}
[self uploadAlbumPicList:array];
}
#pragma mark - Event Response
- (void)backButtonAction:(UIButton *)sender {
if (self.isEdited) {
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = @"提示";
config.message = @"还没有发布哎~确定要返回吗?";
config.confirmButtonConfig.title = @"手滑了";
config.cancelButtonConfig.title = @"确认返回";
[TTPopup alertWithConfig:config confirmHandler:^{
} cancelHandler:^{
[self.navigationController popViewControllerAnimated:YES];
}];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)addTopicButtonAction:(UIButton *)sender {
}
- (void)publicButtonAction:(UIButton *)sender {
}
#pragma mark - Getters And Setters
- (UIView *)navView {
if (!_navView) {
_navView = [[UIView alloc] init];
_navView.backgroundColor = [UIColor clearColor];
}
return _navView;
}
- (UIButton *)backButton {
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateNormal];
[_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateSelected];
[_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _backButton;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.font = [UIFont systemFontOfSize:17];
_titleLabel.textColor = [ThemeColor mainTextColor];
_titleLabel.text = @"发布动态";
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UIButton *)publishButton {
if (!_publishButton) {
_publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_publishButton setTitle:@"发布" forState:UIControlStateNormal];
[_publishButton setTitleColor:[ThemeColor appMainColor] forState:UIControlStateNormal];
_publishButton.titleLabel.font = [UIFont systemFontOfSize:17];
[_publishButton addTarget:self action:@selector(publicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _publishButton;
}
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor clearColor];
}
return _contentView;
}
- (UIImageView *)topicImageView {
if (!_topicImageView) {
_topicImageView = [[UIImageView alloc] init];
_topicImageView.userInteractionEnabled = YES;
_topicImageView.image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0xFFFCDD), UIColorFromRGB(0xDFF9FF), UIColorFromRGB(0xF4E5FF)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)];
_topicImageView.layer.masksToBounds = YES;
_topicImageView.layer.cornerRadius = 13;
}
return _topicImageView;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing = 10;
flowLayout.minimumLineSpacing = 10;
flowLayout.sectionInset = UIEdgeInsetsZero;
flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor clearColor];
[collectionView registerClass:XPMonentsPublishCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass([XPMonentsPublishCollectionViewCell class])];
_collectionView = collectionView;
}
return _collectionView;
}
- (SZTextView *)textView {
if (!_textView) {
_textView = [[SZTextView alloc] init];
_textView.textColor = [ThemeColor mainTextColor];
_textView.font = [UIFont systemFontOfSize:15];
_textView.placeholder = @"记录你此刻生活,分享给有趣的人看...";
_textView.placeholderTextColor = [ThemeColor secondTextColor];
_textView.backgroundColor = [UIColor clearColor];
_textView.delegate = self;
}
return _textView;
}
- (UILabel *)limitLabel {
if (!_limitLabel) {
_limitLabel = [[UILabel alloc] init];
_limitLabel.text = @"0/500";
_limitLabel.textColor = [ThemeColor mainTextColor];
_limitLabel.font = [UIFont systemFontOfSize:12];
_limitLabel.textAlignment = NSTextAlignmentRight;
}
return _limitLabel;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [ThemeColor dividerColor];
}
return _lineView;
}
- (UIStackView *)topicStackView {
if (!_topicStackView) {
_topicStackView = [[UIStackView alloc] init];
_topicStackView.axis = UILayoutConstraintAxisHorizontal;
_topicStackView.distribution = UIStackViewDistributionFill;
_topicStackView.alignment = UIStackViewAlignmentFill;
_topicStackView.spacing = 10;
}
return _topicStackView;
}
- (UIButton *)addButton {
if (!_addButton) {
_addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_addButton setTitle:@"添加话题" forState:UIControlStateNormal];
[_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_addButton.titleLabel.font = [UIFont systemFontOfSize:12];
[_addButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
_addButton.layer.masksToBounds = YES;
_addButton.layer.cornerRadius = 10;
[_addButton addTarget:self action:@selector(addTopicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _addButton;
}
- (UIButton *)againButton {
if (!_againButton) {
_againButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_againButton setTitle:@"重新选择" forState:UIControlStateNormal];
[_againButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateNormal];
_againButton.titleLabel.font = [UIFont systemFontOfSize:12];
[_againButton addTarget:self action:@selector(addTopicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _againButton;
}
- (UIView *)topicPlaceView {
if (!_topicPlaceView) {
_topicPlaceView = [[UIView alloc] init];
_topicPlaceView.backgroundColor = [UIColor clearColor];
}
return _topicPlaceView;
}
- (UIImageView *)iconImageView {
if (!_iconImageView) {
_iconImageView = [[UIImageView alloc] init];
_iconImageView.userInteractionEnabled = YES;
_iconImageView.image = [UIImage imageNamed:@"monents_info_topic_icon"];
}
return _iconImageView;
}
- (UILabel *)topicLabel {
if (!_topicLabel) {
_topicLabel = [[UILabel alloc] init];
_topicLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightBold];
_topicLabel.textColor = [ThemeColor mainTextColor];
}
return _topicLabel;
}
- (UILabel *)descriptionLabel {
if (!_descriptionLabel) {
_descriptionLabel = [[UILabel alloc] init];
_descriptionLabel.text = @"禁止出现商业广告、微信号码、QQ号码、电话号码, 以及低俗、色情、恐怖、暴力和具有侮辱性语言等内容,违规者封号处理 ";
_descriptionLabel.font = [UIFont systemFontOfSize:11];
_descriptionLabel.textColor = [ThemeColor secondTextColor];
_descriptionLabel.numberOfLines = 0;
}
return _descriptionLabel;
}
- (NSMutableArray<UIImage *> *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
}
return _datasource;
}
- (UIImage *)addPicImage {
if (!_addPicImage) {
_addPicImage = [UIImage imageNamed:@"mine_user_info_album_add"];
}
return _addPicImage;
}
- (NSMutableArray *)originAssets {
if (!_originAssets) {
_originAssets = [NSMutableArray array];
}
return _originAssets;
}
- (NSMutableArray<NSString *> *)imageUrls {
if (!_imageUrls) {
_imageUrls = [NSMutableArray array];
}
return _imageUrls;
}
@end