即时通讯整个页面的搭建(一)

作者: CoderFM | 来源:发表于2016-09-23 17:32 被阅读745次

    前言

    最近公司要做IM, 先后试了环信和极光的IM, 当然UI也是用他们的, 后来沟通不符合公司的要求就不用了, 然后用朋友公司搭建的服务器, 随之而来的界面也就要自己写(想用环信Demo来着, 想想还是自己写吧, 毕竟这是一次挑战) 不废话,直接上项目:FMChatUI 欢迎前来评论交流, 感觉还可以就star😜 下面就开始 这篇先讲一下整体的一个思路(项目中的类的作用)

    整个结构

    Controller里只有控制器, 显示消息的

    整个项目用的第三方库是转码MP3的时候用的 lame


    lame

    消息的Model类:


    MessageItem

    IMBaseItem包含消息的头像名字时间等等信息, 拥有着IMMessageBody属性 IMMessageBody包含了正文的类型,是文本还是图片, 视频的链接, 本地缓存的名字等等

    IMBaseItemTool是为了将服务器返回的数据包装成模型写的一个类, 不侵入到IMBaseItem

    下面看看Tools里的

    tool

    看文件名应该就能知道啥作用了😋

    View

    整个的文件数就这么多了

    说说控制器的吧
    控制器里的代码主要就是加载数据,显示数据,没有复杂的逻辑

    刚进入这个控制器的时候就是加载数据, 加载本地的还是服务器的取决实现的内容,可以直接加载本地的,我的demo里就是这么写的,demo里是采用KVO刷新列表的,其实当时我是想,这个数组改变就刷新列表,但是当控制器拥有这个数组时,加入模型的时候, KVO并不会执行,简单说一下KVO监听数组的变化刷新列表的小技巧:

    KVO监听数组

    • 将数组(既然观察数组要变化,当然是可变数组了)作为一个属性
    @property (nonatomic, strong) NSMutableArray *dataArray;
    
    • 注册观察者和实现观察者方法,如果是UITableviewController,可以这样写
    [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew context:NULL];
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    { 
            NSLog(@"keypath ==%@ object ==%@" ,keyPath, object );
    }
    
    • 使用下面这样的方法来给数组添加数据或删除数据
    [[self mutableArrayValueForKey:@"dataArray"] addObject:@"adfasdf"];
    
    • 移除观察者
    -(void)dealloc{
     [self removeObserver:self forKeyPath:@"dataArray" context:NULL];
    }
    

    这样就管插入就好了,会自动刷新,不过这个在IM里似乎有一点小问题,插入历史数据的时候, 还是定位在插入前最顶部的那一行, 我这里是才用了保存一个值, 当你需要插入的时候 这个值指定,刷新完之后,列表滚动到某一行

    连续播放未读语音

    demo里可以连续播放未读的语音
    具体点击了哪一行,用代理回调的
    上代码先:

    
    #pragma mark - 语音Cell点击的回调
    - (void)IMBaseCellClickAudioCell:(IMBaseCell *)cell{
        
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        
        [self playAudioWithIndexPath:indexPath autoNext:YES];
        
    }
    /**
     *  准备播放
     *
     *  @param indexPath 播放的哪一行
     *  @param next      是否播放下一行未读
     */
    - (void)playAudioWithIndexPath:(NSIndexPath *)indexPath autoNext:(BOOL)next{
        
        IMBaseItem *message = [self.reloadTable mutableArrayValueForKey:@"messages"][indexPath.row];
        
        WeakSelf
        if (message.messageBody.locationPath && message.messageBody.locationPath.length > 0) {
            message.isPlaying = YES;
            [self reloadDataWithMessage:message isInser:NO];
            [[IMAudioTool shareAudioTool] playWithFileName:message.messageBody.locationPath returnBeforeFileName:^(NSString *beforeFileName) {
                [weakSelf cancelAudioPlayWithFileName:beforeFileName];
            } withFinishBlock:^(BOOL isFinish) {
                message.isPlaying = !isFinish;
                message.readState = IMMessageReaded;
                [weakSelf reloadDataWithMessage:message isInser:NO];
                if (next) {
                    if ([weakSelf getNextIndexPathForUnReadAudioWithCurrentIndexPath:indexPath]) {
                        [weakSelf playAudioWithIndexPath:[weakSelf getNextIndexPathForUnReadAudioWithCurrentIndexPath:indexPath] autoNext:YES];
                    }
                }
            }];
        } else {
            NSString *fileName = [NSString stringWithFormat:@"%d%d.mp3", (int)[[NSDate date] timeIntervalSince1970], arc4random() % 100000];
            WeakSelf
            [IMDownloadManager downLoadFileUrl:message.messageBody.voiceUrlString datePath:[IMBaseAttribute dataAudioPath] fileName:fileName completionHandler:^(BOOL isSuccess) {
                if (isSuccess) {
                    message.messageBody.locationPath = fileName;
                    [IMSQLiteTool updateMessage:message withKey:@"messageBody"];
                    message.isPlaying = YES;
                    [weakSelf reloadDataWithMessage:message isInser:NO];
                    [[IMAudioTool shareAudioTool] playWithFileName:message.messageBody.locationPath returnBeforeFileName:^(NSString *beforeFileName) {
                        [weakSelf cancelAudioPlayWithFileName:beforeFileName];
                    } withFinishBlock:^(BOOL isFinish) {
                        message.isPlaying = !isFinish;
                        message.readState = IMMessageReaded;
                        [weakSelf reloadDataWithMessage:message isInser:NO];
                        if (next) {
                            if ([weakSelf getNextIndexPathForUnReadAudioWithCurrentIndexPath:indexPath]) {
                                [weakSelf playAudioWithIndexPath:[weakSelf getNextIndexPathForUnReadAudioWithCurrentIndexPath:indexPath] autoNext:YES];
                            }
                        }
                    }];
                } else {
                    NSLog(@"播放失败");
                }
            }];
        }
    }
    /**
     *  获取下一行未读的IndexPath
     *
     *  @param currentIndexPath 当前播放的IndexPath
     *
     *  @return 下一行未读的IndexPath
     */
    - (NSIndexPath *)getNextIndexPathForUnReadAudioWithCurrentIndexPath:(NSIndexPath *)currentIndexPath{
        
        if (currentIndexPath.row < [self.reloadTable mutableArrayValueForKey:@"messages"].count - 1) {
            
            for (NSInteger i = currentIndexPath.row + 1; i < [self.reloadTable mutableArrayValueForKey:@"messages"].count - 1; i++) {
                
                IMBaseItem *item = [self.reloadTable mutableArrayValueForKey:@"messages"][i];
                
                if (item.messageType == IMMessageAudioType && item.readState == IMMessageUnRead) {
                    
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    
                    return indexPath;
                    
                } else {
                    continue;
                }
            }
        }
        
        return nil;
    }
    

    各种判断等等
    这里要注意的就是点击完播放, 并且语音开始播放了, 没有播放完毕的时候, 再点击下一个语音的播放, 要获取到上一个之前播放的语音, 并刷新列表

    demo里有很多地方采用的是Block回调, 代理也是完全可以实现的, 不过我觉得麻烦就这么写了,而且让代码高聚合

    ----------------------------- 先到这吧😀 ----------------------------------
    续篇:
    即时通讯整个页面的搭建(二)

    相关文章

      网友评论

      • 0792bc92b5c4:请问下 。。。表情键盘怎么适配iPhone X 啊?
        0792bc92b5c4:@CoderFM 你好 我这边适配了差不多,但是表情键盘怎么改frame呢?麻烦指导一下下
        0792bc92b5c4:好的 配好告知下呗 谢谢
        CoderFM:不好意思. 很久以前的写的了, 那时候 还没有safe area ,适配iPhoneX 找时间我适配一下
      • Daisuke:不错,希望继续完善,后续追加投稿,谢谢。
        CoderFM:@Daisuke 会的:smile:,谢谢
      • HonBoom:哇,你自己写的呀?
        HonBoom:@CoderFM 哇6了,棒
        CoderFM:@CoderBala 嗯,:smile:

      本文标题:即时通讯整个页面的搭建(一)

      本文链接:https://www.haomeiwen.com/subject/qgityttx.html