Files
yinmeng-ios/xplan-ios/Main/Mine/View/MineInfo/XPMineUserInfoAlbumViewController.m

274 lines
9.2 KiB
Mathematica
Raw Normal View History

2021-09-24 17:46:52 +08:00
//
// XPMineUserInfoAlbumViewController.m
// xplan-ios
//
// Created by on 2021/9/24.
//
#import "XPMineUserInfoAlbumViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import "SDPhotoBrowser.h"
#import "UIImageConstant.h"
#import "TTPopup.h"
#import "YYUtility.h"
///Tool
#import "ThemeColor.h"
///Model
#import "UserInfoModel.h"
///View
#import "XPMineUserInfoAlbumCollectionViewCell.h"
///P
#import "XPMineUserInfolbumPresenter.h"
#import "XPMineUserInfoAlbumProtocol.h"
@interface XPMineUserInfoAlbumViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XPMineUserInfoAlbumProtocol, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SDPhotoBrowserDelegate, XPMineUserInfoAlbumCollectionViewCellDelegate>
///
@property (nonatomic,strong) UICollectionView *collectionView;
///
@property (nonatomic,strong) NSArray<UserPhoto *> *datasource;
///
@property (nonatomic,strong) UIButton *complectButton;
///
@property (nonatomic,assign) BOOL isEdit;
@end
@implementation XPMineUserInfoAlbumViewController
- (XPMineUserInfolbumPresenter*)createPresenter {
return [[XPMineUserInfolbumPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadUserInfo];
[self initSubViews];
[self initSubViewConstraints];
[self initEvents];
}
#pragma mark - Private Method
- (void)initSubViews {
self.title = @"我的相册";
///
[self addNavigationItemWithItems:@[self.complectButton] isLeft:NO];
[self.view addSubview:self.collectionView];
}
- (void)initSubViewConstraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
}
- (void)initEvents {
@weakify(self);
[[self.complectButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
@strongify(self);
x.selected = !x.selected;
self.isEdit = x.selected;
[self.collectionView reloadData];
}];
}
- (void)loadUserInfo {
[self.presenter getUserInfo];
}
///
- (void)cancelEditing {
self.complectButton.selected = NO;
self.isEdit = NO;
[self.collectionView reloadData];
}
#pragma mark -
- (void)showPhotoView {
@weakify(self);
TTActionSheetConfig *cameraConfig = [TTActionSheetConfig normalTitle:@"拍照上传" clickAction:^{
[YYUtility checkCameraAvailable:^{
@strongify(self);
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:NULL];
} denied:^{
@strongify(self);
[self showNotPhoto:@"相机不可用" content:@"相机权限受限,点击确定去系统设置"];
} restriction:^{
@strongify(self);
[self showNotPhoto:@"相机不可用" content:@"相册权限受限,点击确定去系统设置"];
}];
}];
TTActionSheetConfig *photoLibrayConfig = [TTActionSheetConfig normalTitle:@"本地相册" clickAction:^{
[YYUtility checkAssetsLibrayAvailable:^{
@strongify(self);
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.modalPresentationCapturesStatusBarAppearance = YES;
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:NULL];
} denied:^{
@strongify(self);
[self showNotPhoto:@"相册不可用" content:@"相册权限受限,点击确定去系统设置"];
} restriction:^{
@strongify(self);
[self showNotPhoto:@"相册不可用" content:@"相册权限受限,点击确定去系统设置"];
}];
}];
[TTPopup actionSheetWithItems:@[cameraConfig, photoLibrayConfig]];
}
- (void)showNotPhoto:(NSString *)title content:(NSString *)content{
TTAlertConfig *config = [[TTAlertConfig alloc] init];
config.title = title;
config.message = content;
[TTPopup alertWithConfig:config confirmHandler:^{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
} cancelHandler:^{
}];
}
#pragma mark - UICollectionViewDelegate And UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.datasource.count + 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPMineUserInfoAlbumCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMineUserInfoAlbumCollectionViewCell class]) forIndexPath:indexPath];
cell.delegate = self;
if (indexPath.row == 0) {
cell.addImageName = @"mine_user_info_album_add";
cell.isEdit = NO;
} else {
if (self.datasource.count > 0) {
cell.photo = [self.datasource objectAtIndex:indexPath.row - 1];
cell.isEdit = self.isEdit;
}
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
if (self.datasource.count < 8) {
[self showPhotoView];
[self cancelEditing];
}else{
[self showErrorToast:@"最多只能上传8张照片"];
}
}else {
if (!self.isEditing) {
SDPhotoBrowser *browser = [[SDPhotoBrowser alloc]init];
browser.sourceImagesContainerView = self.collectionView;
browser.delegate = self;
browser.imageCount = self.datasource.count;
browser.currentImageIndex = indexPath.item - 1;
browser.isHaveUserAdd = YES;
[browser show];
}
}
}
#pragma mark - XPMineUserInfoAlbumCollectionViewCellDelegate
- (void)xPMineUserInfoAlbumCollectionViewCell:(XPMineUserInfoAlbumCollectionViewCell *)cell didDeleteItem:(UserPhoto *)photo {
TTAlertConfig * config = [[TTAlertConfig alloc] init];
config.title = @"删除是不可逆操作";
config.message = @"是否需要删除?";
[TTPopup alertWithConfig:config confirmHandler:^{
[self.presenter deleteImageUrlFromServerWithPid:photo.pid];
} cancelHandler:^{
}];
}
#pragma mark - SDPhotoBrowserDelegate
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
return [UIImageConstant defaultAvatarPlaceholder];
2021-09-24 17:46:52 +08:00
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
return [NSURL URLWithString:self.datasource[index].photoUrl];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *selectedPhoto = [info objectForKey:UIImagePickerControllerOriginalImage];
if (selectedPhoto) {
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(selectedPhoto, nil, nil, nil);
}
[self.presenter uploadAlbumImage:selectedPhoto];
}
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
#pragma mark - XPMineUserInfoAlbumProtocol
- (void)onGetUserInfoSuccess:(UserInfoModel *)userInfo {
self.datasource = userInfo.privatePhoto;
[self.collectionView reloadData];
}
///
- (void)uploadAlbumImageToThirdSuccess:(NSString *)url {
[self.presenter uploadUserAlbumWithUrlStr:url];
}
///
- (void)uploadUserAlbumSuccess {
[self.presenter getUserInfo];
[self showSuccessToast:@"更新成功"];
}
- (void)deleteUserAlbumSuccess {
[self.presenter getUserInfo];
[self showSuccessToast:@"删除成功"];
}
#pragma mark - Getters And Setters
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.itemSize = CGSizeMake((self.view.frame.size.width - 50) / 3, (self.view.frame.size.width - 50) / 3);
layout.minimumLineSpacing = 15;
layout.minimumInteritemSpacing = 15;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[XPMineUserInfoAlbumCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPMineUserInfoAlbumCollectionViewCell class])];
}
return _collectionView;
}
- (UIButton *)complectButton {
if (!_complectButton) {
_complectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_complectButton setTitle:@"编辑" forState:UIControlStateNormal];
[_complectButton setTitle:@"完成" forState:UIControlStateSelected];
[_complectButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateNormal];
[_complectButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateSelected];
_complectButton.titleLabel.font = [UIFont systemFontOfSize:15];
}
return _complectButton;
}
@end