// // XCShareView.m // XCRoomMoudle // // Created by KevinWang on 2018/9/2. // Copyright © 2018年 YiZhuan. All rights reserved. // #import "XPShareView.h" ///Third #import #import #import ///Tool #import "XPMacro.h" #import "ThemeColor.h" #import "XCCurrentVCStackManager.h" #import "TTPopup.h" ///View #import "XPShareItemCell.h" #import "XPMineShareViewController.h" @interface XPShareView() ///取消 @property (nonatomic, strong) UIButton *cancleButton; ///列表 @property (nonatomic, strong) UICollectionView *collectionView; ///数据源 @property (nonatomic, strong) NSArray *items; ///item的大小 @property (nonatomic,assign) CGSize itemSize; ///分享的内容 @property (nonatomic,strong) XPShareInfoModel *shareInfo; @end @implementation XPShareView #pragma mark - Life Style - (instancetype)initWithItems:(NSArray *)items itemSize:(CGSize)itemSize shareInfo:(XPShareInfoModel *)shareInfo { if (self = [super init]) { self.items = items; self.itemSize =itemSize; self.shareInfo = shareInfo; [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.collectionView]; [self addSubview:self.cancleButton]; } - (void)initSubViewConstraints { CGFloat collectionWidth = KScreenWidth - 15 * 2; ///一行有几个 int numberLine = collectionWidth / self.itemSize.width; int page = self.items.count % numberLine > 0 ? (int)self.items.count / numberLine + 1 : (int)self.items.count / numberLine; CGFloat collectionHeight = page * self.itemSize.height + 20 + (page-1) * 10 + 10; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self); make.height.mas_equalTo(collectionHeight); make.left.right.mas_equalTo(self).inset(15); }]; [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(45); make.left.right.mas_equalTo(self.collectionView); make.top.mas_equalTo(self.collectionView.mas_bottom).offset(15); }]; [self mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(KScreenWidth); make.bottom.mas_equalTo(self.cancleButton.mas_bottom).offset(30); }]; } - (BOOL)isInstallClient:(SSDKPlatformType)platform { return [ShareSDK isClientInstalled:platform]; } - (SSDKPlatformType)getSharePlatformType:(XPShareItemTag)itemTag { SSDKPlatformType type; switch (itemTag) { case XPShareItemTagQQ: type = SSDKPlatformTypeQQ; break; case XPShareItemTagQQZone: type = SSDKPlatformSubTypeQZone; break; case XPShareItemTagWeChat: type = SSDKPlatformTypeWechat; break; case XPShareItemTagMoments: type = SSDKPlatformSubTypeWechatTimeline; break; default: type = SSDKPlatformTypeUnknown; break; } return type; } #pragma mark - UICollectionViewDelegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.items.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ XPShareItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPShareItemCell class]) forIndexPath:indexPath]; XPShareItem * item = [self.items objectAtIndex:indexPath.item]; if (item.type == XPShareItemTagAppFriends) { item.disable = YES; } else { item.disable = [self isInstallClient:[self getSharePlatformType:item.type]]; } cell.shareItem = item; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ [collectionView deselectItemAtIndexPath:indexPath animated:YES]; XPShareItem * item = [self.items objectAtIndex:indexPath.item]; if (item.type == XPShareItemTagAppFriends) { [TTPopup dismiss]; XPMineShareViewController * shareVC = [[XPMineShareViewController alloc] init]; shareVC.shareType = MineShareType_Monents; shareVC.shareInfo = self.shareInfo; [[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:shareVC animated:YES]; return; } self.shareInfo.shareType = item.type; NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; NSString * title = [self.shareInfo shareTitle].length > 0 ? self.shareInfo.shareTitle : @""; NSString * content = self.shareInfo.shareContent.length > 0 ? self.shareInfo.shareContent : @""; NSString * urlString = self.shareInfo.shareUrl.length > 0 ?self.shareInfo.shareUrl : @""; NSString * imageURL = self.shareInfo.shareImageUrl.length > 0 ? self.shareInfo.shareImageUrl : @""; NSString *encodedUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [shareParams SSDKSetupShareParamsByText:content images:@[imageURL] url:[NSURL URLWithString:encodedUrl] title:title type:SSDKContentTypeAuto]; [ShareSDK share:[self getSharePlatformType:item.type] parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { switch (state) { case SSDKResponseStateSuccess: { if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:didSuccess:)]) { [self.delegate shareView:self didSuccess:self.shareInfo]; } } break; case SSDKResponseStateFail: { if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) { [self.delegate shareView:self shareFail:@"分享失败"]; } } break; case SSDKResponseStateCancel: { if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) { [self.delegate shareView:self shareFail:@"取消分享"]; } } break; default: break; } }]; } #pragma mark - Event Response - (void)cancleButtonDidClck:(UIButton *)button{ if (self.delegate && [self.delegate respondsToSelector:@selector(shareViewDidClickCancel:)]) { [self.delegate shareViewDidClickCancel:self]; } } #pragma mark - Getters And Setters - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = self.itemSize; layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 10; layout.sectionInset = UIEdgeInsetsMake(20, 0, 10, 0); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.layer.masksToBounds = YES; _collectionView.layer.cornerRadius = 15; [_collectionView registerClass:[XPShareItemCell class] forCellWithReuseIdentifier:NSStringFromClass([XPShareItemCell class])]; } return _collectionView; } - (UIButton *)cancleButton{ if (!_cancleButton) { _cancleButton = [[UIButton alloc] init]; [_cancleButton setBackgroundColor:[UIColor whiteColor]]; [_cancleButton setTitle:@"取消" forState:UIControlStateNormal]; _cancleButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:15]; _cancleButton.layer.masksToBounds = YES; _cancleButton.layer.cornerRadius = 45/2; [_cancleButton setTitleColor:[ThemeColor textThirdColor] forState:UIControlStateNormal]; [_cancleButton addTarget:self action:@selector(cancleButtonDidClck:) forControlEvents:UIControlEventTouchUpInside]; } return _cancleButton; } @end