// // YMMineUserInfoAlbumViewController.m // YUMI // // Created by YUMI on 2021/9/24. // #import "XPMineUserInfoAlbumViewController.h" ///Third #import #import #import "SDPhotoBrowser.h" #import "UIImageConstant.h" #import "TTPopup.h" #import "YYUtility.h" ///Tool #import "DJDKMIMOMColor.h" #import "NSArray+Safe.h" ///Model #import "UserInfoModel.h" ///View #import "XPMineUserInfoAlbumCollectionViewCell.h" ///P #import "XPMineUserInfolbumPresenter.h" #import "XPMineUserInfoAlbumProtocol.h" @interface XPMineUserInfoAlbumViewController () ///列表 @property (nonatomic,strong) UICollectionView *collectionView; ///数据源 @property (nonatomic,strong) NSArray *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]; } #pragma mark - Private Method - (void)initSubViews { self.title = YMLocalizedString(@"XPMineUserInfoAlbumViewController0"); ///添加完成按钮 [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)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:YMLocalizedString(@"XPMineUserInfoAlbumViewController1") 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:YMLocalizedString(@"SessionViewController19") content:YMLocalizedString(@"XPMineUserInfoAlbumViewController3")]; } restriction:^{ @strongify(self); [self showNotPhoto:YMLocalizedString(@"SessionViewController19") content:YMLocalizedString(@"XPMineUserInfoAlbumViewController5")]; }]; }]; TTActionSheetConfig *photoLibrayConfig = [TTActionSheetConfig normalTitle:YMLocalizedString(@"XPMineUserInfoAlbumViewController6") 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:YMLocalizedString(@"SessionViewController21") content:YMLocalizedString(@"XPMineUserInfoAlbumViewController8")]; } restriction:^{ @strongify(self); [self showNotPhoto:YMLocalizedString(@"SessionViewController21") content:YMLocalizedString(@"XPMineUserInfoAlbumViewController10")]; }]; }]; [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]; // [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil]; } } 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 xpSafeObjectAtIndex: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:YMLocalizedString(@"XPMineUserInfoAlbumViewController11")]; } }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 = YMLocalizedString(@"XPMineUserInfoAlbumViewController12"); config.message = YMLocalizedString(@"XPMineUserInfoAlbumViewController13"); [TTPopup alertWithConfig:config confirmHandler:^{ [self.presenter deleteImageUrlFromServerWithPid:photo.pid]; } cancelHandler:^{ }]; } #pragma mark - SDPhotoBrowserDelegate - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index { return [UIImageConstant defaultAvatarPlaceholder]; } - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index { return [NSURL URLWithString:self.datasource[index].photoUrl]; } #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { @kWeakify(self); [picker dismissViewControllerAnimated:YES completion:^{ @kStrongify(self); UIImage *selectedPhoto = [info objectForKey:UIImagePickerControllerOriginalImage]; if (selectedPhoto) { if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { UIImageWriteToSavedPhotosAlbum(selectedPhoto, nil, nil, nil); } [self.presenter uploadAlbumImage:selectedPhoto]; [self showLoading]; } }]; } - (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:YMLocalizedString(@"XPMineUserInfoAlbumViewController14")]; } - (void)deleteUserAlbumSuccess { [self.presenter getUserInfo]; [self showSuccessToast:YMLocalizedString(@"XPMineUserInfoAlbumViewController15")]; } #pragma mark - Event Response - (void)complectButtonAction:(UIButton *)sender { sender.selected = !sender.selected; self.isEdit = sender.selected; [self.collectionView reloadData]; } #pragma mark - Getters And Setters - (UICollectionView *)collectionView{ if (!_collectionView) { MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout 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:YMLocalizedString(@"XPMineUserInfoAlbumViewController16") forState:UIControlStateNormal]; [_complectButton setTitle:YMLocalizedString(@"XPMineUserInfoAlbumViewController17") forState:UIControlStateSelected]; [_complectButton setTitleColor:[DJDKMIMOMColor mainTextColor] forState:UIControlStateNormal]; [_complectButton setTitleColor:[DJDKMIMOMColor mainTextColor] forState:UIControlStateSelected]; _complectButton.titleLabel.font = [UIFont systemFontOfSize:15]; [_complectButton addTarget:self action:@selector(complectButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _complectButton; } @end