2
0
Pārlūkot izejas kodu

朋友圈翻译

桂欢 1 gadu atpakaļ
vecāks
revīzija
586f71f32b

+ 2 - 0
SLAiELTS/SLAiELTS/AppMarcos/YMConstMacro.h

@@ -108,4 +108,6 @@
 
 #define SLColF5F5F5      SLColor(@"#F5F5F5")
 
+#define SLColLine      SLColor(@"#F2F3F7")
+
 #endif /* YMConstMacro_h */

+ 7 - 6
SLAiELTS/SLAiELTS/Tool/NetWorking/SLHttpCenter.m

@@ -11,7 +11,9 @@
 #import "SPRequestError.h"
 #import <CoreTelephony/CTCellularData.h>
 #define KTimeoutInterval 60;
-static NSString *const kNOConnect = @"检测到没有网络";
+static NSString *const kNOConnect = @"网络无连接,请检查网络";
+
+static NSString *const SLResponseFailure = @"网络不给力 请稍后再试";
 
 @interface SLHttpCenter ()
 
@@ -124,9 +126,9 @@ static SLHttpCenter *handle;
 
 
 - (void)netWorkConnectFailure:(FailuerWithReason)failure{
-    [ZFToast ShowWithMessage:@"网络无连接,请检查网络"];
+    [ZFToast ShowWithMessage:kNOConnect];
     if (failure) {
-        SPRequestError *errorCode = [SPRequestError errorWithReason:@"网络无连接"];
+        SPRequestError *errorCode = [SPRequestError errorWithReason:kNOConnect];
         failure(errorCode);
     }
     return;
@@ -138,7 +140,7 @@ static SLHttpCenter *handle;
                                                 hub:(MBProgressHUD *)hub{
     [self hideHudAnimateWith:hub];
     if (failure) {
-        [ZFToast ShowWithMessage:@"网络请求失败"];
+        [ZFToast ShowWithMessage:SLResponseFailure];
         NSString *decription = error.userInfo[@"NSLocalizedDescription"];
         NSLog(@"url:%@---%@",url,decription);
         SPRequestError *myError = [SPRequestError errorWithReason:decription errorCode:SPErrorCode_ConnectionError];
@@ -169,8 +171,7 @@ static SLHttpCenter *handle;
                     errorMsg = data;
                 }
             }
-            [ZFToast ShowWithMessage: errorMsg ?: @"网络请求失败"];
-//            SPRequestError *requestError = [SPRequestError errorWithReason:errorMsg errorCode:status];
+            [ZFToast ShowWithMessage: errorMsg ?: SLResponseFailure];
             failure(resultDic);
             return ;
         }

+ 1 - 1
SLAiELTS/SLAiELTS/ViewControllers/LoginVC/SLRegistViewController.m

@@ -91,7 +91,7 @@
         [ZFToast ShowWithMessage:@"请勾选协议"];
         return;
     }
-    NSDictionary *parameter = @{@"phone" : phoneStr, @"password" : pwStr, @"code" : codeStr};
+    NSDictionary *parameter = @{@"phone" : phoneStr, @"password" : pwStr, @"code" : codeStr, @"deviceBrand" : @"iPhone", @"deviceModel" : [NSString deviceModel]};
     [[SLHttpCenter SharedInstance] postWithUrl:@"/api/Token/isPhoneCode" parameter:parameter success:^(id responseObject) {
         NSDictionary *dataDict = [responseObject objectForKey:@"data"];
         SLLoginInfo *loginModel = [SLLoginInfo mj_objectWithKeyValues:dataDict];

+ 1 - 0
SLAiELTS/SLAiELTS/ViewControllers/Moments/Model/SDTimeLineCellModel.h

@@ -45,6 +45,7 @@ typedef enum {
 @property (nonatomic, copy) NSString *friendId;
 @property (nonatomic, copy) NSString *friendContent;
 @property (nonatomic, copy) NSString *friendContentTrans;
+@property (nonatomic, assign) BOOL showTrans;
 @property (nonatomic, copy) NSString *friendImgUrl;
 @property (nonatomic, copy) NSString *time;
 @property (nonatomic, assign) BOOL isLike;

+ 8 - 9
SLAiELTS/SLAiELTS/ViewControllers/Moments/MomentsVc/SLMomentsVc.m

@@ -336,8 +336,6 @@ static CGFloat textFieldH = 56;
     }
 }
 
-
-
 #pragma mark - tableviewDelegate
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
@@ -359,7 +357,7 @@ static CGFloat textFieldH = 56;
             [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 //            [weakSelf.tableView reloadData];
         }];
-        
+        //评论
         [cell setDidClickCommentLabelBlock:^(NSString *commentId, CGRect rectInWindow,  NSIndexPath *indexPath1) {
             weakSelf.currentEditingIndexthPath = [NSIndexPath indexPathForRow:indexPath1.row inSection:0];
             SDTimeLineCellModel *editModel = weakSelf.dataArray[indexPath1.row];
@@ -369,7 +367,7 @@ static CGFloat textFieldH = 56;
             weakSelf.commentToUser = commentId;
             [weakSelf adjustTableViewToFitKeyboard];
         }];
-        
+        //长按删除评论
         [cell setDidLongTouchCommentLabelBlock:^(NSString *commentId, CGRect rectInWindow, NSIndexPath *indexPath1) {
             //indexPath1 row == table row  Section == SDTimeLineCellCommentItemModel.list
             SDTimeLineCellModel *editModel = [weakSelf.dataArray objectAtIndex:indexPath1.row];
@@ -476,6 +474,11 @@ static CGFloat textFieldH = 56;
     }];
 }
 
+- (void)didClickTransInCell:(UITableViewCell *)cell {
+    NSIndexPath *index = [self.tableView indexPathForCell:cell];
+    [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
+}
+
 - (void)updateCellLike:(SDTimeLineCellModel *)model index:(NSIndexPath *)index {
     NSMutableArray *temp = [NSMutableArray arrayWithArray:model.likeItemsArray];
     if ([self isEqual:[UIViewController getCurrentShowVC]]) {
@@ -508,8 +511,7 @@ static CGFloat textFieldH = 56;
         [self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
     });
 }
-
-
+#pragma mark - Keyboard
 - (void)adjustTableViewToFitKeyboard
 {
     UIWindow *window = [UIApplication sharedApplication].keyWindow;
@@ -541,11 +543,9 @@ static CGFloat textFieldH = 56;
     if (rect.origin.y == [UIScreen mainScreen].bounds.size.height) {
         textFieldRect = rect;
     }
-    
     [UIView animateWithDuration:0.25 animations:^{
         self.moentsChatView.frame = textFieldRect;
     }];
-    
     CGFloat h = rect.size.height + textFieldH;
     if (_totalKeybordHeight != h) {
         _totalKeybordHeight = h;
@@ -580,5 +580,4 @@ static CGFloat textFieldH = 56;
     return _dataArray;
 }
 
-
 @end

+ 2 - 0
SLAiELTS/SLAiELTS/ViewControllers/Moments/Views/Cell/SDTimeLineCell.h

@@ -34,6 +34,8 @@
 - (void)didClickcCommentButtonInCell:(UITableViewCell *)cell;
 - (void)didClickDelButtonInCell:(UITableViewCell *)cell;
 
+- (void)didClickTransInCell:(UITableViewCell *)cell;
+
 @end
 
 @class SDTimeLineCellModel;

+ 87 - 34
SLAiELTS/SLAiELTS/ViewControllers/Moments/Views/Cell/SDTimeLineCell.m

@@ -39,6 +39,8 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
     UIImageView *_iconView;
     UILabel *_nameLable;
     UILabel *_contentLabel;
+    UIView *_transSpaceLineView;
+    UILabel *_contentTransLabel;
     SDWeiXinPhotoContainerView *_picContainerView;
     UILabel *_timeLabel;
     UIButton *_moreButton;
@@ -90,6 +92,19 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
     gesture.minimumPressDuration= 0.5f;
     [_contentLabel addGestureRecognizer:gesture];
     
+    _contentTransLabel = [UILabel new];
+    _contentTransLabel.font = [UIFont systemFontOfSize:contentLabelFontSize];
+    _contentTransLabel.numberOfLines = 0;
+    _contentTransLabel.userInteractionEnabled = YES;
+    _contentTransLabel.isAttributedContent = YES;
+    UILongPressGestureRecognizer *gesture1=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(contentLabelHandleLongPressGestures:)];
+    gesture.minimumPressDuration= 0.5f;
+    [_contentTransLabel addGestureRecognizer:gesture1];
+    _transSpaceLineView = [UIView new];
+    _transSpaceLineView.backgroundColor = SLColLine;
+    _transSpaceLineView.hidden = YES;
+    
+    
     _moreButton = [UIButton new];
     [_moreButton setTitle:@"全文" forState:UIControlStateNormal];
     [_moreButton setTitleColor:TimeLineCellHighlightedColor forState:UIControlStateNormal];
@@ -142,7 +157,7 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
     _timeLabel.font = [UIFont systemFontOfSize:10];
     
 
-    NSArray *views = @[_iconView, _nameLable, _contentLabel, _moreButton, _picContainerView, _timeLabel, _operationLikeButton, _operationCommentButton, _delButton, _commentView];
+    NSArray *views = @[_iconView, _nameLable, _contentLabel, _contentTransLabel, _transSpaceLineView, _moreButton, _picContainerView, _timeLabel, _operationLikeButton, _operationCommentButton, _delButton, _commentView];
     
     [self.contentView sd_addSubviews:views];
     
@@ -173,6 +188,19 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
     .topSpaceToView(_contentLabel, 0)
     .widthIs(30);
     
+    _contentTransLabel.sd_layout
+    .leftEqualToView(_contentLabel)
+    .topSpaceToView(_moreButton, 2)
+    .rightSpaceToView(contentView, 12)
+    .autoHeightRatio(0);
+    
+    _transSpaceLineView.sd_layout
+    .leftEqualToView(_contentLabel)
+    .topSpaceToView(_moreButton, 8)
+    .rightSpaceToView(contentView, 12)
+    .heightIs(1);
+    
+//    _contentTransLabel.text = @"我没有\n是的";
     
     _picContainerView.sd_layout
     .leftEqualToView(_contentLabel); // 已经在内部实现宽度和高度自适应所以不需要再设置宽度高度,top值是具体有无图片在setModel方法中设置
@@ -209,11 +237,11 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
 
 - (void)contentLabelHandleLongPressGestures:(UILongPressGestureRecognizer *)paramSender {
     if (paramSender.state == UIGestureRecognizerStateBegan) {
-        _contentLabel.backgroundColor = SLCol205;
+        paramSender.view.backgroundColor = SLCol205;
     }
     if (paramSender.state == UIGestureRecognizerStateEnded) {
-        [self showMsgSegmentWith:_contentLabel];
-        _contentLabel.backgroundColor = [UIColor clearColor];
+        [self showMsgSegmentWith:paramSender.view];
+        paramSender.view.backgroundColor = [UIColor clearColor];
     }
 }
 
@@ -232,7 +260,13 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
         NSMutableArray *arr = [NSMutableArray new];
         
         [arr addObject:[SLMikeSegmentModel initWithSegTitle: @"复制" segImage:@"" mikeType:SLMsgCopy]];
-        [arr addObject:[SLMikeSegmentModel initWithSegTitle: @"翻译" segImage:@"" mikeType:SLMikeInputTrans]];
+        if (bgView == _contentLabel) {
+            if (_model.showTrans) {
+                [arr addObject:[SLMikeSegmentModel initWithSegTitle: @"原文" segImage:@"" mikeType:SLMikeInputTransHidden]];
+            }else {
+                [arr addObject:[SLMikeSegmentModel initWithSegTitle: @"翻译" segImage:@"" mikeType:SLMikeInputTrans]];
+            }
+        }
        
         CGFloat width = arr.count * 45 + 20;
         CGFloat x = bgView.frame.origin.x + MAX(0, (rect.size.width - width) / 2.0);
@@ -244,39 +278,53 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
             switch (model.mikeType) {
                 case SLMsgCopy:
                 {
-                    [[UIPasteboard generalPasteboard] setString:self->_model.friendContent];
+                    NSString *str = (bgView == self->_contentLabel) ? self->_model.friendContent : self->_model.friendContentTrans;
+                    [[UIPasteboard generalPasteboard] setString:str];
+                }
+                    break;
+                case SLMikeInputTransHidden:
+                {
+                    self->_model.showTrans = NO;
+                    [weakSelf.delegate didClickTransInCell:self];
                 }
                     break;
                 case SLMikeInputTrans:
                 {
-                    //百度智能云
-                    [[SLHttpCenter SharedInstance] getWithUrl:@"https://aip.baidubce.com/oauth/2.0/token" parameter:@{@"grant_type":@"client_credentials", @"client_id":@"0VhDeLqdcbLamGENfw4c24aq", @"client_secret": @"92qphFKyc0uXGktqFLtfdmqlojt9MsgG"} success:^(id responseObject) {
-                        NSString *accessToken = [responseObject objectForKey:@"access_token"];
-                        if (accessToken) {
-                            NSString *bdUrl = [NSString stringWithFormat:@"https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=%@",accessToken];
-                            NSDictionary *dict = @{@"q":self->_model.friendContent,
-                                                   @"from":@"auto",
-                                                   @"to":@"zh"};
-                            [[SLHttpCenter SharedInstance] postWithUrl:bdUrl parameter:dict success:^(id responseObject) {
-                                NSDictionary *reslut = [responseObject objectForKey:@"result"];
-                                NSArray *arr = [reslut objectForKey:@"trans_result"];
-                                if (arr && arr.count) {
-                                    NSDictionary *resultDict = arr.firstObject;
-                                    NSString *dst = [resultDict objectForKey:@"dst"];
-                                    self->_model.friendContentTrans = dst;
-//                                    self->_contentLabel.text = self->_model.friendContentTrans;
-                                }else {
+                    if (self->_model.friendContentTrans) {
+                        self->_model.showTrans = YES;
+                        [weakSelf.delegate didClickTransInCell:self];
+                    }else {
+                        //百度智能云
+                        [[SLHttpCenter SharedInstance] getWithUrl:@"https://aip.baidubce.com/oauth/2.0/token" parameter:@{@"grant_type":@"client_credentials", @"client_id":@"0VhDeLqdcbLamGENfw4c24aq", @"client_secret": @"92qphFKyc0uXGktqFLtfdmqlojt9MsgG"} success:^(id responseObject) {
+                            NSString *accessToken = [responseObject objectForKey:@"access_token"];
+                            if (accessToken) {
+                                NSString *bdUrl = [NSString stringWithFormat:@"https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=%@",accessToken];
+                                NSDictionary *dict = @{@"q":self->_model.friendContent,
+                                                       @"from":@"auto",
+                                                       @"to":@"zh"};
+                                [[SLHttpCenter SharedInstance] postWithUrl:bdUrl parameter:dict success:^(id responseObject) {
+                                    NSDictionary *reslut = [responseObject objectForKey:@"result"];
+                                    NSArray *arr = [reslut objectForKey:@"trans_result"];
+                                    if (arr && arr.count) {
+                                        NSDictionary *resultDict = arr.firstObject;
+                                        NSString *dst = [resultDict objectForKey:@"dst"];
+                                        self->_model.friendContentTrans = dst;
+                                        self->_contentTransLabel.text = self->_model.friendContentTrans;
+                                        self->_model.showTrans = YES;
+                                        [weakSelf.delegate didClickTransInCell:self];
+                                    }else {
+                                        [ZFToast ShowWithMessage:@"翻译失败"];
+                                    }
+                                } failure:^(SPRequestError *error) {
                                     [ZFToast ShowWithMessage:@"翻译失败"];
-                                }
-                            } failure:^(SPRequestError *error) {
+                                }];
+                            }else {
                                 [ZFToast ShowWithMessage:@"翻译失败"];
-                            }];
-                        }else {
+                            }
+                        } failure:^(SPRequestError *error) {
                             [ZFToast ShowWithMessage:@"翻译失败"];
-                        }
-                    } failure:^(SPRequestError *error) {
-                        [ZFToast ShowWithMessage:@"翻译失败"];
-                    }];
+                        }];
+                    }
                 }
                     break;
                 default:
@@ -284,7 +332,6 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
             }
         };
         [bView addSubview:mikeSegment];
-        SLMomentsVc *msgVc = (SLMomentsVc *)vc;
         UIImageView *imgView = [[UIImageView alloc] initWithImage:ImageName(@"icon_dsjx")];
         [bView addSubview:imgView];
         [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -316,6 +363,11 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
 {
     _model = model;
     _commentView.userId = _model.userId;
+    _contentTransLabel.text = _model.showTrans ? _model.friendContentTrans : @"";
+    _contentTransLabel.sd_layout
+    .topSpaceToView(_moreButton, _model.showTrans ? 16 : 2);
+    
+    _transSpaceLineView.hidden = !_model.showTrans;
     [_commentView setupWithLikeItemsArray:model.likeItemsArray commentItemsArray:model.list];
     NSString *userImg = [NSString stringWithFormat:@"%@%@",[SLHttpCenter SharedInstance].serverUrl, model.userImg];
     [_iconView sd_setImageWithURL:[NSURL URLWithString:userImg] placeholderImage:ImageName(@"icon_ellipse")];
@@ -329,7 +381,7 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
     _picContainerView.cellModel = _model;
     _picContainerView.picPathStringsArray = friendImgUrls;
     
-    if (model.shouldShowMoreButton) { // 如果文字高度超过60
+    if (model.shouldShowMoreButton && !model.showTrans) { // 如果文字高度超过60
         _moreButton.sd_layout.heightIs(20);
         _moreButton.hidden = NO;
         if (model.isOpening) { // 如果需要展开
@@ -340,6 +392,7 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
             [_moreButton setTitle:@"全文" forState:UIControlStateNormal];
         }
     } else {
+        _contentLabel.sd_layout.maxHeightIs(MAXFLOAT);
         _moreButton.sd_layout.heightIs(0);
         _moreButton.hidden = YES;
     }
@@ -348,7 +401,7 @@ NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLine
     if (friendImgUrls.count) {
         picContainerTopMargin = 10;
     }
-    _picContainerView.sd_layout.topSpaceToView(_moreButton, picContainerTopMargin);
+    _picContainerView.sd_layout.topSpaceToView(_contentTransLabel, picContainerTopMargin);
     
     UIView *bottomView;