首页 > 其他分享 >鸿蒙开发融云Demo消息列表

鸿蒙开发融云Demo消息列表

时间:2024-10-24 09:49:13浏览次数:3  
标签:__ layout 鸿蒙 Demo app 融云 integer view chatItem

鸿蒙开发融云IMKit消息列表

融云鸿蒙版是不带UI的,得自己一步步搭建。融云后期好像也不会出带UI的库,还是早点自己弄吧

一、思路:

IMEngine.getInstance().getConversationListByPage获取

二、效果图:

在这里插入图片描述

三、关键代码:
  1. 获取列表数据:
public static loadConversationList(conversationCallback?: (arr: Conversation[]) => void) {
    let conTypeList = new List<ConversationType>();
    conTypeList.add(ConversationType.Private);
    conTypeList.add(ConversationType.System);

    let option: IGetConversationOption = {
      time: 0,
      count: 1000
    }

    IMEngine.getInstance().getConversationListByPage(conTypeList, option)
      .then(result => {
        logContent("congge1", result)
        if (EngineError.Success !== result.code) {
          // 获取会话列表失败
          return;
        }
        if (!result.data) {
          // 会话列表为空
          conversationCallback?.([])
          return;
        }

        let conList = result.data as List<Conversation>;
        conversationCallback?.(conList.convertToArray().filter(item => isNotEmptyString(item.objectName)))
      });
  }

2.ChatListItemView

@Component
export  struct ChatListItemView {
  @Prop customUnReadCount:number = 0
  @State chatItem: Conversation = new Conversation();
  @State imUserInfo:IMUserInfoDataBean = {}
  @State content:string = ''
  nowTimeEventId:number = Date.now()

  aboutToAppear(): void {
    if (this.chatItem.targetId){
      MMKVNoClearUtil.getImUserInfo(this.chatItem.targetId,(result)=>{
        this.imUserInfo = result
      })
    }
  }


  build() {
    RelativeContainer() {
      Image(this.imUserInfo.localPortrait?this.imUserInfo.localPortrait:this.imUserInfo.portrait)
        .width($r('app.integer.opt_layout_chat_view_profile_picture_width'))
        .height($r('app.integer.opt_layout_chat_view_profile_picture_height'))
        .borderRadius($r('app.integer.opt_layout_chat_view_profile_picture_radius'))
        .alt($r('app.media.ic_default'))
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top },
          left: { anchor: '__container__', align: HorizontalAlign.Start }
        })
        .id("figure")

      if (this.chatItem.unreadMessageCount > 0 || this.customUnReadCount > 0) { // 红点消息大于0条时渲染红点
        Row() {
          Text(this.chatItem.unreadMessageCount > 0?`${this.chatItem.unreadMessageCount}`:`${this.customUnReadCount}`)
            .borderRadius($r('app.integer.layout_8'))
            .constraintSize({minWidth: $r('app.integer.opt_layout_chat_view_red_dot_width')})
            .height($r('app.integer.opt_layout_chat_view_red_dot_height'))
            .backgroundColor($r('app.color.color_red'))
            .fontSize($r('app.integer.text_font_10'))
            .textAlign(TextAlign.Center)
            .fontColor(Color.White)
        }
        .alignItems(VerticalAlign.Center)
        .justifyContent(FlexAlign.Center)
        .borderRadius($r('app.integer.layout_10'))
        .margin({ top: $r('app.integer.layout_minus_8'), left: $r('app.integer.layout_24') })
        .constraintSize({minWidth: $r('app.integer.opt_layout_chat_view_red_dot_container_width')})
        .height($r('app.integer.opt_layout_chat_view_red_dot_container_height'))
        .backgroundColor(Color.White)
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top },
          left: { anchor: '__container__', align: HorizontalAlign.Start }
        })
        .opacity((this.chatItem.unreadMessageCount > 0 || this.customUnReadCount > 0) ? 1 : 0)
        .id('badge')
      }

      Text(this.imUserInfo.name)
        .fontColor($r('app.color.color_black'))
        .fontSize($r('app.integer.opt_layout_chat_view_user_name_font_size'))
        .lineHeight($r('app.integer.layout_22'))
        .font({ weight: 450 })
        .margin({ top: $r('app.integer.layout_0'), left: $r('app.integer.chat_view_row_height') })
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top },
          left: { anchor: '__container__', align: HorizontalAlign.Start }
        })
        .id('user')
      if (this.chatItem.lastSentTime > 0){
        Text(handlerMsgTime(this.chatItem.lastSentTime))
          .fontSize($r('app.integer.opt_layout_chat_view_item_padding_top'))
          .fontColor($r('app.color.color_aa'))
          .maxLines(1)
          .textAlign(TextAlign.End)
          .margin({ top: $r('app.integer.layout_0'), right: 12 })
          .alignRules({
            top: { anchor: '__container__', align: VerticalAlign.Top },
            right: { anchor: '__container__', align: HorizontalAlign.End }
          })
          .id('time')
      }

      Text(this.dealContent())
        .fontColor(!(this.chatItem.objectName === HQVoiceMessageObjectName && !MMKVNoClearUtil.getVoiceMessageReadStatus(this.chatItem.targetId,this.chatItem.lastMessageId))?$r('app.color.color_aa'):$r('app.color.color_red'))
        .fontSize($r('app.integer.text_font_14'))
        .lineHeight($r('app.integer.layout_19'))
        .maxLines(1)
        .textOverflow({
          overflow:TextOverflow.Ellipsis
        })
        .margin({ top: $r('app.integer.layout_24'), left: $r('app.integer.chat_view_row_height'),right:12 })
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top },
          left: { anchor: '__container__', align: HorizontalAlign.Start },
          right:{ anchor: '__container__', align: HorizontalAlign.End }
        })
        .id('msg')
    }
    .width('100%')
    .height($r('app.integer.opt_layout_chat_view_item_height'))
    .backgroundColor(Color.White)
    .padding({ left: 12, top: $r('app.integer.opt_layout_chat_view_item_padding_top'), bottom: $r('app.integer.opt_layout_chat_view_item_padding_bottom') })
    .onClick(() => {
        Router.pushChatDetailPage(this.chatItem.targetId,this.chatItem.conversationType)
    })
  }
  
  dealContent():string{
    if (this.chatItem.objectName) {
      this.content = getString($r('app.string.im_version_error_tip'))
      switch (this.chatItem.objectName){
        case TextMessageObjectName:
          this.content = ImUtils.dealMyTextContent(this.chatItem.content as TextMessage)
          break
        case ImageMessageObjectName:
          this.content = '[图片]'
          break
        case HQVoiceMessageObjectName:
          this.content = `[语音] ${(this.chatItem.content as HQVoiceMessage).duration}"`
          break
        case CustomizeGiftMessageObjectName:
          this.content = (this.chatItem.content as CustomizeGiftMessage).toUserId === MMKVUtil.getCurrentUserId()?getString($r('app.string.gift_send_to_one_tip')):getString($r('app.string.gift_send_one_tip'))
          break
      }

    }
    return this.content
  }

}
四、鸿蒙融云Demo源码结构图:

在这里插入图片描述

接上图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/674621f64f0e4354872f934f83f26fb2.png#pic_center在这里插入图片描述
有问题或者需要完整源码的私信我

标签:__,layout,鸿蒙,Demo,app,融云,integer,view,chatItem
From: https://blog.csdn.net/u010074743/article/details/143189246

相关文章

  • 华为鸿蒙HarmonyOS第一课-学习笔记总结
    华为鸿蒙HarmonyOS第一课-学习笔记总结一、概述目前华为开发者联盟下属的HarmonyOS官网推出了,针对HarmonyOS应用开发的学习视频。总共13课程,干货满满。每节课程后会有练习题,分数达成后会有结课证书。最终所有课程都学习后,可以去考试,获取HarmonyOS基础开发者证书。华为官方学习课程......
  • 超千款鸿蒙原生游戏上架,华为游戏中心成就非凡游戏体验
    10月22日,原生鸿蒙之夜暨华为全场景新品发布会在深圳举行,华为正式为用户带来全新的原生鸿蒙操作系统(HarmonyOSNEXT),这是HarmonyOS诞生以来最大的更新。发布会公布了当前HarmonyOSNEXT整体应用生态的进展,其中鸿蒙游戏领域令人瞩目。以《王者荣耀》、《和平精英》、《归龙潮》、《万......
  • 第一个Java spring boot demo运行
     一、环境准备1,下载JavaJDK需要安装两个JDK版本:1.8/17安装ZuluJDK(不能使用OracleJDK)JDK17:https://www.azul.com/downloads/?version=java-17-lts&os=macos&package=jdk#zuluJDK8:https://www.azul.com/downloads/?version=java-8-lts&os=macos&package=jdk#zul......
  • 一个简单的登录界面demo
    代码基于ttkbootstrap fromtkinterimport*importttkbootstrapasttkfromtkinterimportmessageboxclassTextView(ttk.Frame):def__init__(self,master):super().__init__(master,padding=30)self.pack(fill=BOTH,expand=YES)......
  • 华为原生鸿蒙操作系统:我国移动操作系统的新篇章
    华为原生鸿蒙操作系统:我国移动操作系统的新篇章引言在移动操作系统领域,苹果iOS和安卓系统一直占据主导地位。然而,随着华为原生鸿蒙操作系统的正式发布,这一格局正在发生深刻变化。作为继苹果iOS和安卓系统后的全球第三大移动操作系统,鸿蒙系统的出现不仅标志着我国在移动......
  • 鸿蒙Next之数据同步艺术之三:标准化数据类型解析-UTD详解
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。本文将深入探讨华为鸿蒙HarmonyOSNext......
  • 鸿蒙Next之数据同步艺术之二:深入理解标准化数据类型 (UTD)
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。UTD的设计原则层级结构:UTD采用层级......
  • 鸿蒙Next之数据同步艺术之一:方舟数据管理揭秘
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。本文将介绍华为鸿蒙HarmonyOSNext中的......
  • 鸿蒙Next之数据同步艺术之五:跨应用数据交互
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。在许多应用场景中,用户需要在不同应用之......
  • 鸿蒙Next之数据同步艺术之四:必要不充分理解分布式数据对象同步
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。分布式数据对象的生命周期分布式数据对......