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

547 lines
18 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"
2022-11-11 17:46:37 +08:00
#import "NSArray+Safe.h"
#import "NSMutableDictionary+Saft.h"
2022-08-16 20:27:03 +08:00
///Model
#import "MonentsTopicModel.h"
#import "MonentsPicResInfo.h"
2022-07-01 18:48:58 +08:00
///View
#import "XPMonentsPublishCollectionViewCell.h"
2022-08-16 20:27:03 +08:00
#import "XPMonentsPublishTopicView.h"
#import "XPMoentsTopicListView.h"
#import "XPMonentPublishSuccessView.h"
2022-11-11 17:46:37 +08:00
2022-08-16 20:27:03 +08:00
///P
#import "XPMonentsPublishPresenter.h"
#import "XPMonentsPublishProtocol.h"
2022-07-01 18:48:58 +08:00
#define itemWidth (KScreenWidth - 10 * 2 - 15 * 2) / 3.0 // item
2022-08-16 20:27:03 +08:00
@interface XPMonentsPublishViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UITextViewDelegate, TZImagePickerControllerDelegate, XPMonentsPublishCollectionViewCellDelegate, XPMonentsPublishTopicViewDelegate, XPMoentsTopicListViewDelegate, XPMonentsPublishProtocol>
2022-07-01 18:48:58 +08:00
///
@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;
2022-08-16 20:27:03 +08:00
///
@property (nonatomic,strong) XPMonentsPublishTopicView *addTopicView;
2022-07-01 18:48:58 +08:00
///
@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;
///
2022-08-16 20:27:03 +08:00
@property (nonatomic,strong) NSArray *uploadImageList;
2022-07-01 18:48:58 +08:00
///
@property (nonatomic,assign) int imageCount;
@end
@implementation XPMonentsPublishViewController
2022-08-16 20:27:03 +08:00
- (__kindof id)createPresenter {
return [[XPMonentsPublishPresenter alloc] init];
}
2022-07-01 18:48:58 +08:00
- (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];
2022-08-16 20:27:03 +08:00
[self.contentView addSubview:self.addTopicView];
2022-07-01 18:48:58 +08:00
[self.contentView addSubview:self.descriptionLabel];
[self.navView addSubview:self.backButton];
[self.navView addSubview:self.titleLabel];
[self.navView addSubview:self.publishButton];
2022-08-16 20:27:03 +08:00
2022-07-01 18:48:58 +08:00
[self.datasource addObject:self.addPicImage];
}
- (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);
}];
2022-08-16 20:27:03 +08:00
[self.addTopicView mas_makeConstraints:^(MASConstraintMaker *make) {
2022-07-01 18:48:58 +08:00
make.left.right.mas_equalTo(self.contentView).inset(15);
make.top.mas_equalTo(self.collectionView.mas_bottom).offset(15);
make.height.mas_equalTo(20);
}];
2022-08-16 20:27:03 +08:00
2022-07-01 18:48:58 +08:00
[self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(20);
make.bottom.mas_equalTo(self.view).offset(-25);
}];
}
2022-08-16 20:27:03 +08:00
- (void)uploadAlbumPicList:(NSArray *)array finish:(void(^)(NSArray *list))finish {
2022-07-01 18:48:58 +08:00
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
2022-08-16 20:27:03 +08:00
NSMutableArray * dataArray = [NSMutableArray array];
2022-07-01 18:48:58 +08:00
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 ++;
2022-08-16 20:27:03 +08:00
NSMutableDictionary * dict= [NSMutableDictionary dictionary];
MonentsPicResInfo * infor = [MonentsPicResInfo modelWithDictionary:resp];
if (infor.path) {
[dict safeSetObject:infor.path forKey:@"resUrl"];
2022-08-16 20:27:03 +08:00
}
if (infor.format) {
[dict safeSetObject:infor.format forKey:@"format"];
2022-08-16 20:27:03 +08:00
}
if (infor.w) {
[dict safeSetObject:@(infor.w) forKey:@"width"];
2022-08-16 20:27:03 +08:00
}
if (infor.h) {
[dict safeSetObject:@(infor.h) forKey:@"height"];
2022-08-16 20:27:03 +08:00
}
[dataArray addObject:dict];
2022-07-01 18:48:58 +08:00
if (self.imageCount == array.count) {
dispatch_async(dispatch_get_main_queue(), ^{
2022-08-16 20:27:03 +08:00
finish(dataArray);
2022-07-01 18:48:58 +08:00
});
}
} 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];
2022-11-11 17:46:37 +08:00
UIImage * image = [self.datasource safeObjectAtIndex1:indexPath.row];
2022-07-01 18:48:58 +08:00
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];
2022-08-16 20:27:03 +08:00
if (indexPath.row == (self.datasource.count -1) && self.datasource.count != 9) {
[self.textView resignFirstResponder];
2022-07-01 18:48:58 +08:00
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:^{
2022-08-16 20:27:03 +08:00
if ([self.datasource containsObject:image]) {
[self.datasource removeObject:image];
}
NSIndexPath * indexPath = [self.collectionView indexPathForCell:view];
if (indexPath.row < self.originAssets.count) {
[self.originAssets removeObjectAtIndex:indexPath.row];
}
[self.collectionView reloadData];
if (self.datasource.count > 1) {
self.isEdited = YES;
} else {
self.isEdited = NO;
}
2022-07-01 18:48:58 +08:00
} cancelHandler:^{
}];
}
2022-08-17 18:55:45 +08:00
# pragma mark - TZImagePickerControllerDelegate
2022-07-01 18:48:58 +08:00
- (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)];
}
2022-08-16 20:27:03 +08:00
self.uploadImageList = array;
2022-07-01 18:48:58 +08:00
}
2022-08-16 20:27:03 +08:00
#pragma mark - XPMonentsPublishTopicViewDelegate
- (void)xPMonentsPublishTopicView:(XPMonentsPublishTopicView *)view didClickCloseButton:(UIButton *)sender {
self.topicInfo = nil;
}
- (void)xPMonentsPublishTopicView:(XPMonentsPublishTopicView *)view didClickChooseTopic:(UIButton *)sender {
[self.textView resignFirstResponder];
XPMoentsTopicListView * topicListView = [[XPMoentsTopicListView alloc] init];
topicListView.delegate = self;
[TTPopup popupView:topicListView style:TTPopupStyleActionSheet];
}
#pragma mark - XPMoentsTopicListViewDelegate
- (void)xPMoentsTopicListView:(XPMoentsTopicListView *)view didSelectItem:(MonentsTopicModel *)topicInfo {
[TTPopup dismiss];
self.topicInfo = topicInfo;
}
#pragma mark - XPMonentsPublishProtocol
- (void)publishMonentsSuccess {
XPMonentPublishSuccessView * successView = [[XPMonentPublishSuccessView alloc] init];
TTPopupService *service = [[TTPopupService alloc] init];
service.contentView = successView;
service.filterIdentifier = NSStringFromClass([XPMonentPublishSuccessView class]);
service.shouldFilterPopup = YES;
service.didFinishDismissHandler = ^(BOOL isDismissOnBackgroundTouch) {
[self.navigationController popViewControllerAnimated:YES];
};
[TTPopup popupWithConfig:service];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[TTPopup dismiss];
[self.navigationController popViewControllerAnimated:YES];
});
}
2022-07-01 18:48:58 +08:00
#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)publicButtonAction:(UIButton *)sender {
2022-08-16 20:27:03 +08:00
[self.view endEditing:YES];
2022-08-26 17:13:20 +08:00
if (self.textView.text.length > 0 || self.uploadImageList.count > 0) {
2022-08-16 20:27:03 +08:00
if (self.uploadImageList.count > 0) {
[self showLoading];
[self uploadAlbumPicList:self.uploadImageList finish:^(NSArray *list) {
[self hideHUD];
[self.presenter publishMonents:self.topicInfo.worldId type:MonentsContentType_Picture content:self.textView.text resList:list];
}];
} else {
[self.presenter publishMonents:self.topicInfo.worldId type:MonentsContentType_Text content:self.textView.text resList:@[]];
}
} else {
[self showErrorToast:@"请输入发布的内容"];
}
2022-07-01 18:48:58 +08:00
}
#pragma mark - Getters And Setters
2022-08-18 16:39:14 +08:00
- (void)setTopicInfo:(MonentsTopicModel *)topicInfo {
_topicInfo = topicInfo;
self.addTopicView.topicInfo = _topicInfo;
}
2022-07-01 18:48:58 +08:00
- (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;
}
- (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;
}
- (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;
}
2022-08-16 20:27:03 +08:00
- (XPMonentsPublishTopicView *)addTopicView {
if (!_addTopicView) {
_addTopicView = [[XPMonentsPublishTopicView alloc] init];
_addTopicView.delegate = self;
}
return _addTopicView;
}
2022-07-01 18:48:58 +08:00
- (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;
}
@end