Files
peko-ios/YuMi/CustomUI/ShareView/XPShareView.m

334 lines
14 KiB
Mathematica
Raw Normal View History

2023-07-06 16:54:13 +08:00
//
// XCShareView.m
// XCRoomMoudle
//
// Created by KevinWang on 2018/9/2.
// Copyright © 2018 YiZhuan. All rights reserved.
//
2023-07-14 18:50:55 +08:00
#import "XPShareView.h"
///Third
2023-07-06 16:54:13 +08:00
#import <Masonry/Masonry.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKExtension/ShareSDK+Extension.h>
2023-09-22 17:43:21 +08:00
#import <FBSDKShareKit/FBSDKShareKit.h>
#import "XCCurrentVCStackManager.h"
2023-07-14 18:50:55 +08:00
///Tool
#import "XCCurrentVCStackManager.h"
#import "TTPopup.h"
2023-09-22 17:43:21 +08:00
#import "XPFirebbaseEvent.h"
2023-07-14 18:50:55 +08:00
///View
#import "XPShareItemCell.h"
#import "XPMineShareViewController.h"
2023-10-31 18:54:47 +08:00
#import "ClientConfig.h"
2023-07-14 18:50:55 +08:00
2023-09-22 17:43:21 +08:00
@interface XPShareView()<UICollectionViewDataSource,UICollectionViewDelegate,FBSDKSharingDelegate>
2023-07-14 18:50:55 +08:00
///
@property (nonatomic, strong) UIButton *cancleButton;
///
2023-07-06 16:54:13 +08:00
@property (nonatomic, strong) UICollectionView *collectionView;
2023-07-14 18:50:55 +08:00
///
@property (nonatomic, strong) NSArray<XPShareItem *> *items;
///item
2023-07-06 16:54:13 +08:00
@property (nonatomic,assign) CGSize itemSize;
2023-07-14 18:50:55 +08:00
///
@property (nonatomic,strong) XPShareInfoModel *shareInfo;
2023-07-06 16:54:13 +08:00
@end
2023-07-14 18:50:55 +08:00
@implementation XPShareView
2023-07-06 16:54:13 +08:00
#pragma mark - Life Style
2023-07-14 18:50:55 +08:00
- (instancetype)initWithItems:(NSArray<XPShareItem *> *)items itemSize:(CGSize)itemSize shareInfo:(XPShareInfoModel *)shareInfo {
2023-09-22 17:43:21 +08:00
if (self = [super init]) {
2023-09-22 20:23:33 +08:00
for (XPShareItem * item in items) {
if (item.type == XPShareItemTagAppFriends || item.type == XPShareItemTagCopyLink) {
item.disable = YES;
} else {
item.disable = [self isInstallClient:[self getSharePlatformType:item.type]];
}
2023-10-31 18:54:47 +08:00
}
self.items = [NSMutableArray arrayWithArray:items];
2023-09-22 17:43:21 +08:00
self.itemSize =itemSize;
self.shareInfo = shareInfo;
[self initSubViews];
[self initSubViewConstraints];
}
return self;
2023-07-06 16:54:13 +08:00
}
#pragma mark - Private Method
- (void)initSubViews {
2023-09-22 17:43:21 +08:00
[self addSubview:self.collectionView];
[self addSubview:self.cancleButton];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
- (void)initSubViewConstraints {
2023-09-22 17:43:21 +08:00
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);
2024-04-11 17:05:27 +08:00
make.leading.trailing.mas_equalTo(self).inset(15);
2023-09-22 17:43:21 +08:00
}];
[self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(45);
2024-04-11 17:05:27 +08:00
make.leading.trailing.mas_equalTo(self.collectionView);
2023-09-22 17:43:21 +08:00
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);
}];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
- (BOOL)isInstallClient:(SSDKPlatformType)platform {
2023-09-22 17:43:21 +08:00
return [ShareSDK isClientInstalled:platform];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
- (SSDKPlatformType)getSharePlatformType:(XPShareItemTag)itemTag {
2023-09-22 17:43:21 +08:00
SSDKPlatformType type;
switch (itemTag) {
case XPShareItemTagFaceBook:
type = SSDKPlatformTypeFacebook;
break;
case XPShareItemTagLine:
type = SSDKPlatformTypeLine;
break;
default:
type = SSDKPlatformTypeUnknown;
break;
}
return type;
2023-07-06 16:54:13 +08:00
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
2023-09-22 17:43:21 +08:00
return self.items.count;
2023-07-06 16:54:13 +08:00
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
2023-09-22 17:43:21 +08:00
XPShareItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPShareItemCell class]) forIndexPath:indexPath];
XPShareItem * item = [self.items safeObjectAtIndex1:indexPath.item];
2023-10-31 18:54:47 +08:00
item.disable = YES;
cell.shareItem = item;
2023-09-22 17:43:21 +08:00
return cell;
2023-07-06 16:54:13 +08:00
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
2023-09-22 17:43:21 +08:00
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
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 : @"";
2024-04-12 15:55:09 +08:00
if ([urlString containsString:@"?"]){
urlString = [NSString stringWithFormat:@"%@&lang=%@",urlString,[NSBundle uploadLanguageText]];
}else{
urlString = [NSString stringWithFormat:@"%@?lang=%@",urlString,[NSBundle uploadLanguageText]];
}
2023-09-22 17:43:21 +08:00
NSString *encodedUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
XPShareItem * item = [self.items safeObjectAtIndex1:indexPath.item];
2023-10-31 18:54:47 +08:00
if (item == nil){
[TTPopup dismiss];
return;
};
2024-03-14 16:52:41 +08:00
if (item.type == XPShareItemTagAppSaveAlbum){
[TTPopup dismiss];
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:savePhoto:)]){
[self.delegate shareView:self savePhoto:self.shareInfo];
}
return;
}
2023-09-22 17:43:21 +08:00
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;
} else if(item.type == XPShareItemTagCopyLink) {
NSString * urlString = self.shareInfo.shareUrl;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:urlString];
[XNDJTDDLoadingTool showSuccessWithMessage:YMLocalizedString(@"XPShareView0")];
2024-01-05 17:05:27 +08:00
2023-09-22 17:43:21 +08:00
[TTPopup dismiss];
return;
}
2024-01-10 18:04:12 +08:00
if([self isInstallClient:[self getSharePlatformType:item.type]] == NO){
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPShareView9")];
[TTPopup dismiss];
return;
}
2023-09-22 17:43:21 +08:00
self.shareInfo.shareType = item.type;
SSDKPlatformType platformType;
XPPlatformType FBtype;
if (item.type == XPShareItemTagLine) {
title = YMLocalizedString(@"XPShareView1");
platformType = SSDKPlatformTypeLine;
if (![ShareSDK isClientInstalled:platformType]) {
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPShareView2")];
return;
}
2023-12-29 16:43:37 +08:00
NSString*contentKey= [encodedUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
NSString*contentType =@"text";
NSString*urlString = [NSString stringWithFormat:@"line://msg/%@/%@",contentType, contentKey];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
}];
2024-01-05 17:05:27 +08:00
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:didSuccess:)]) {
[self.delegate shareView:self didSuccess:self.shareInfo];
}
2024-03-14 16:52:41 +08:00
return;
2023-09-22 17:43:21 +08:00
}
2024-03-14 16:52:41 +08:00
if(item.type == XPShareItemTagFaceBook){
2023-09-22 17:43:21 +08:00
FBSDKShareLinkContent*linkContent = [[FBSDKShareLinkContent alloc]init];
2023-09-26 17:57:39 +08:00
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
2023-09-22 17:43:21 +08:00
linkContent.contentURL= [NSURL URLWithString:urlString];
linkContent.quote = content;
FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc]initWithViewController:[XCCurrentVCStackManager shareManager].getCurrentVC content:linkContent delegate:self];
// web
shareDialog.mode = FBSDKShareDialogModeNative;
if (![shareDialog canShow]) {
shareDialog.mode = FBSDKShareDialogModeWeb;
}
[shareDialog show];
return;
}
[ShareSDK share:platformType parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
switch (state) {
case SSDKResponseStateSuccess:
{
if(self.isFromWebVeiw == NO){
[XPFirebbaseEvent shareEventWithContentID:@(self.shareInfo.roomUid).stringValue type:FBtype];
}else{
[XPFirebbaseEvent shareEventWithContentID:self.shareInfo.uid type:FBtype];
}
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:YMLocalizedString(@"XPShareView5")];
}
}
break;
case SSDKResponseStateCancel:
{
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView6")];
}
}
break;
default:
break;
}
}];
}
#pragma mark - FBSDKSharingDelegate
/// Sent to the delegate when sharing completes without error or cancellation.
/// @param sharer The sharer that completed.
/// @param results The results from the sharer. This may be nil or empty.
- (void)sharer:(id <FBSDKSharing> _Nonnull)sharer didCompleteWithResults:(NSDictionary<NSString *, id> * _Nonnull)results{
NSString *postId = results[@"postId"];
FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;
if (dialog.mode == FBSDKShareDialogModeBrowser && (postId == nil || [postId isEqualToString:@""])) {
// 使webviewpostId
//
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView6")];
}
} else {
if(self.isFromWebVeiw == NO){
[XPFirebbaseEvent shareEventWithContentID:@(self.shareInfo.roomUid).stringValue type:PlatformOfFB];
}else{
[XPFirebbaseEvent shareEventWithContentID:self.shareInfo.uid type:PlatformOfFB];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:didSuccess:)]) {
[self.delegate shareView:self didSuccess:self.shareInfo];
}
}
}
/// Sent to the delegate when the sharer encounters an error.
/// @param sharer The sharer that completed.
/// @param error The error.
- (void)sharer:(id <FBSDKSharing> _Nonnull)sharer didFailWithError:(NSError * _Nonnull)error{
FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;
if (error == nil && dialog.mode == FBSDKShareDialogModeNative) {
// 使errorFacebook app
// dialogmode
dialog.mode = FBSDKShareDialogModeBrowser;
[dialog show];
} else {
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView5")];
}
}
}
/// Sent to the delegate when the sharer is cancelled.
/// @param sharer The sharer that completed.
- (void)sharerDidCancel:(id <FBSDKSharing> _Nonnull)sharer{
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView6")];
}
2023-07-06 16:54:13 +08:00
}
#pragma mark - Event Response
2023-07-14 18:50:55 +08:00
- (void)cancleButtonDidClck:(UIButton *)button{
2023-09-22 17:43:21 +08:00
if (self.delegate && [self.delegate respondsToSelector:@selector(shareViewDidClickCancel:)]) {
[self.delegate shareViewDidClickCancel:self];
}
2023-07-06 16:54:13 +08:00
}
#pragma mark - Getters And Setters
- (UICollectionView *)collectionView{
2023-09-22 17:43:21 +08:00
if (!_collectionView) {
2024-04-12 15:55:09 +08:00
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
2023-09-22 17:43:21 +08:00
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;
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
- (UIButton *)cancleButton{
2023-09-22 17:43:21 +08:00
if (!_cancleButton) {
_cancleButton = [[UIButton alloc] init];
[_cancleButton setBackgroundColor:[UIColor whiteColor]];
[_cancleButton setTitle:YMLocalizedString(@"XPShareView7") forState:UIControlStateNormal];
_cancleButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:15];
_cancleButton.layer.masksToBounds = YES;
_cancleButton.layer.cornerRadius = 45/2;
[_cancleButton setTitleColor:[DJDKMIMOMColor textThirdColor] forState:UIControlStateNormal];
[_cancleButton addTarget:self action:@selector(cancleButtonDidClck:) forControlEvents:UIControlEventTouchUpInside];
}
return _cancleButton;
2023-07-06 16:54:13 +08:00
}
@end