Files
peko-ios/YuMi/Modules/YMMonents/View/XPMonentsPublishViewController.m
2023-08-04 11:47:43 +08:00

550 lines
19 KiB
Objective-C

//
// YMMonentsPublishViewController.m
// YUMI
//
// Created by YUMI on 2022/7/1.
//
#import "XPMonentsPublishViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <SZTextView/SZTextView.h>
#import <TZImagePickerController/TZImagePickerController.h>
///Tool
#import "YUMIMacroUitls.h"
#import "DJDKMIMOMColor.h"
#import "UIImage+Utils.h"
#import "Api+Mine.h"
#import "UploadImage.h"
#import "TTPopup.h"
#import "NSArray+Safe.h"
#import "ClientConfig.h"
#import "NSMutableDictionary+Saft.h"
///Model
#import "MonentsTopicModel.h"
#import "MonentsPicResInfo.h"
///View
#import "XPMonentsPublishCollectionViewCell.h"
#import "XPMonentsPublishTopicView.h"
#import "XPMoentsTopicListView.h"
#import "XPMonentPublishSuccessView.h"
///P
#import "XPMonentsPublishPresenter.h"
#import "XPMonentsPublishProtocol.h"
#define itemWidth (KScreenWidth - 10 * 2 - 15 * 2) / 3.0 // item 宽度
@interface XPMonentsPublishViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UITextViewDelegate, TZImagePickerControllerDelegate, XPMonentsPublishCollectionViewCellDelegate, XPMonentsPublishTopicViewDelegate, XPMoentsTopicListViewDelegate, XPMonentsPublishProtocol>
///显示内容
@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) XPMonentsPublishTopicView *addTopicView;
///描述
@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) NSArray *uploadImageList;
///上传的个数
@property (nonatomic,assign) int imageCount;
@end
@implementation XPMonentsPublishViewController
- (__kindof id)createPresenter {
return [[XPMonentsPublishPresenter alloc] init];
}
- (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.addTopicView];
[self.contentView addSubview:self.descriptionLabel];
[self.navView addSubview:self.backButton];
[self.navView addSubview:self.titleLabel];
[self.navView addSubview:self.publishButton];
[self.datasource addObject:self.addPicImage];
self.addTopicView.hidden = ![ClientConfig shareConfig].canOpen;
}
- (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.addTopicView 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.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 finish:(void(^)(NSArray *list))finish {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSMutableArray * dataArray = [NSMutableArray array];
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 ++;
NSMutableDictionary * dict= [NSMutableDictionary dictionary];
MonentsPicResInfo * infor = [MonentsPicResInfo modelWithDictionary:resp];
if (infor.path) {
[dict safeSetObject:infor.path forKey:@"resUrl"];
}
if (infor.format) {
[dict safeSetObject:infor.format forKey:@"format"];
}
if (infor.w) {
[dict safeSetObject:@(infor.w) forKey:@"width"];
}
if (infor.h) {
[dict safeSetObject:@(infor.h) forKey:@"height"];
}
[dataArray addObject:dict];
if (self.imageCount == array.count) {
dispatch_async(dispatch_get_main_queue(), ^{
finish(dataArray);
});
}
} 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:YMLocalizedString(@"XPMonentsPublishViewController0")];
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 safeObjectAtIndex1: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 == (self.datasource.count -1) && self.datasource.count != 9) {
[self.textView resignFirstResponder];
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
imagePickerVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.allowTakeVideo = NO;
imagePickerVc.maxImagesCount = 9;
imagePickerVc.naviBgColor = [DJDKMIMOMColor appCellBackgroundColor];
imagePickerVc.naviTitleColor = [DJDKMIMOMColor mainTextColor];
imagePickerVc.barItemTextColor = [DJDKMIMOMColor mainTextColor];
imagePickerVc.selectedAssets = self.originAssets;
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
}
#pragma mark - XPMonentsPublishCollectionViewCellDelegate
- (void)XPMonentsPublishCollectionViewCell:(XPMonentsPublishCollectionViewCell *)view didDeleteItem:(UIImage *)image {
[TTPopup alertWithMessage:YMLocalizedString(@"XPMonentsPublishViewController1") confirmHandler:^{
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;
}
} cancelHandler:^{
}];
}
# 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.uploadImageList = array;
}
#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];
});
}
#pragma mark - Event Response
- (void)backButtonAction:(UIButton *)sender {
if (self.isEdited) {
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = YMLocalizedString(@"XPMonentsPublishViewController2");
config.message = YMLocalizedString(@"XPMomentsSimpleDetailViewController12");
config.confirmButtonConfig.title = YMLocalizedString(@"XPMonentsPublishViewController4");
config.cancelButtonConfig.title = YMLocalizedString(@"XPMonentsPublishViewController5");
[TTPopup alertWithConfig:config confirmHandler:^{
} cancelHandler:^{
[self.navigationController popViewControllerAnimated:YES];
}];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)publicButtonAction:(UIButton *)sender {
[self.view endEditing:YES];
if (self.textView.text.length > 0 || self.uploadImageList.count > 0) {
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:YMLocalizedString(@"XPMonentsPublishViewController6")];
}
}
#pragma mark - Getters And Setters
- (void)setTopicInfo:(MonentsTopicModel *)topicInfo {
_topicInfo = topicInfo;
self.addTopicView.topicInfo = _topicInfo;
}
- (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 = [DJDKMIMOMColor mainTextColor];
_titleLabel.text = YMLocalizedString(@"XPMonentsPublishViewController7");
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UIButton *)publishButton {
if (!_publishButton) {
_publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_publishButton setTitle:YMLocalizedString(@"XPMonentsPublishViewController8") forState:UIControlStateNormal];
[_publishButton setTitleColor:[DJDKMIMOMColor 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 = [DJDKMIMOMColor mainTextColor];
_textView.font = [UIFont systemFontOfSize:15];
_textView.placeholder = [NSString stringWithFormat:@"%@...",YMLocalizedString(@"XPMonentsPublishViewController9")];
_textView.placeholderTextColor = [DJDKMIMOMColor secondTextColor];
_textView.backgroundColor = [UIColor clearColor];
_textView.delegate = self;
}
return _textView;
}
- (UILabel *)limitLabel {
if (!_limitLabel) {
_limitLabel = [[UILabel alloc] init];
_limitLabel.text = @"0/500";
_limitLabel.textColor = [DJDKMIMOMColor mainTextColor];
_limitLabel.font = [UIFont systemFontOfSize:12];
_limitLabel.textAlignment = NSTextAlignmentRight;
}
return _limitLabel;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [DJDKMIMOMColor dividerColor];
}
return _lineView;
}
- (UILabel *)descriptionLabel {
if (!_descriptionLabel) {
_descriptionLabel = [[UILabel alloc] init];
_descriptionLabel.text = YMLocalizedString(@"XPMonentsPublishViewController10");
_descriptionLabel.font = [UIFont systemFontOfSize:11];
_descriptionLabel.textColor = [DJDKMIMOMColor secondTextColor];
_descriptionLabel.numberOfLines = 0;
}
return _descriptionLabel;
}
- (XPMonentsPublishTopicView *)addTopicView {
if (!_addTopicView) {
_addTopicView = [[XPMonentsPublishTopicView alloc] init];
_addTopicView.delegate = self;
}
return _addTopicView;
}
- (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