local UIChannelChatMain = require "View/ChannelChat/UIChannelChatMain" local UIFriendMain = UIViewClass(UIChannelChatMain) local EventSystem = require('Event/EventSystem') local chat_pb = require("Proto/chat_pb") local chatTimeShow--聊天消息显示时间间隔 local chatConvenientLimit local offlineColor = "8891a4" local chatTimeLimit = 0.5 --私聊时间限制 local INTERVAL_FRAUD_PREVENTION = 3 local TitileCount = 3 local SingleLoadCount = 5 local ShowTopBtnCount = DataSheetManager.ConstData:GetDataByID("FriendsListPullBackNum").IntValue local IDS_FRAUD_PREVENTION local INDEX_GROUP_CHAT = 3 local TAB_TYPE_GROUPCHAT = 99 function UIFriendMain:OnOpened(context) if GameAppInfo.IsChinesePackage() then IDS_FRAUD_PREVENTION = { Const_IDS.IDS_FraudPrevention_Broadcast1, Const_IDS.IDS_FraudPrevention_Broadcast2, Const_IDS.IDS_FraudPrevention_Broadcast3, Const_IDS.IDS_FraudPrevention_Broadcast4, Const_IDS.IDS_FraudPrevention_Broadcast5, Const_IDS.IDS_FraudPrevention_Broadcast6, } self.fraudPreventionIndex = 0 UIUtils.SetActive(self.event_Report, true) UIUtils.SetActive(self.group_ReportInfo, true) self:UpdateFraudPrevention() self:StartRepeatFunc(self.UpdateFraudPrevention, INTERVAL_FRAUD_PREVENTION, 0) else UIUtils.SetActive(self.event_Report, false) UIUtils.SetActive(self.group_ReportInfo, false) end self:RegistSystemEvent(CTL.RedPoint.RedPointKey.Friend_Apply, self.RefreshFriendRed) self:RegistSystemEvent(EventID.UI_ChannelChat_TranslateUpdate, self.UpdateTranslationIndex) self:RegistSystemEvent(EventID.UI_GroupChat_ExitChannel, self.OnExitGroupChat) self:RegistSystemEvent(EventID.UI_AddGroupChatHistoryCount, self.OnAddGroupChatHistoryCount) self.IsChatScrollDragToEnd = true self.maxQuickMsgLength = 750 chatConvenientLimit = DataSheetManager.ConstData:GetDataByID("ChatConvenientLimit").IntValue chatTimeShow = DataSheetManager.ConstData:GetDataByID("ChatTimeShow").IntValue self.ChatTimeStamp = UTL.Time.GetServerTimestamp() self:RegistSystemEvent(EventID.UI_UpdateFriendInfo, self.UpdateInfo) local subIndex = context.SubIndex local selectAcid = context.Acid --记录选中项 if MDL.Friend.RecordSelectAcids == nil then MDL.Friend.RecordSelectAcids = {} end MDL.Friend.RecordSelectAcids[subIndex] = selectAcid --保存上一次好友页签下选中的哪个页签(好友、黑名单) self.lastTitleType = subIndex == MDL.Friend.ListType.Recent and MDL.Friend.ListType.Friend or subIndex local enterType = context.EnterType local needDelay = false if enterType == 1 then if selectAcid then local chatMsgList = CTL.Friend.GetChatMsgList(selectAcid) local num = MDL.Friend.GetListNum(subIndex) if (chatMsgList and #chatMsgList <= 20) and num <= 20 then self:LoadData() else needDelay = true end else local num = MDL.Friend.GetListNum(subIndex) if num == 0 then self:LoadData() else needDelay = true end end else self:LoadData() end if needDelay then --聊天记录太多或者最近数量太多,延时加载 self:StartRepeatFunc(self.OnUpdateFun, 0.3) end self:RefreshFriendRed() -- 将保存的私聊信息设置回来 if MDL.Friend.GetNotSendMessage() ~= nil then self.obj_InputField.text = MDL.Friend.GetNotSendMessage() MDL.Friend.SaveNotSendState(nil, nil) end end function UIFriendMain:UpdateFraudPrevention() self.fraudPreventionIndex = (self.fraudPreventionIndex + 1) % (#IDS_FRAUD_PREVENTION + 1) UIUtils.SetLabelText(self.txt_Info, IDS_FRAUD_PREVENTION[self.fraudPreventionIndex]) end function UIFriendMain:LoadData() --黑名单,选中好友页签 if self.context.SubIndex == MDL.Friend.ListType.Black then self.group_TabLeft:SelectIndex(MDL.Friend.ListType.Friend) elseif self.context.SubIndex == TAB_TYPE_GROUPCHAT then self.group_TabLeft:SelectIndex(INDEX_GROUP_CHAT - 1) else self.group_TabLeft:SelectIndex(self.context.SubIndex) end end function UIFriendMain:OnUpdateFun(deltaTime, timeSinceUpdate) self:LoadData() self:StopRepeatFunc(self.OnUpdateFun) end function UIFriendMain:OnClosed() --记录上次选中的位置,切回来的时候重新选中 self.context.SubIndex = self.CurListType self.context.Acid = MDL.Friend.RecordSelectAcids[self.CurListType] self.context.EnterType = 0 self.clickFriendItem = nil self.clickTitleCell = nil end function UIFriendMain:UpdateInfo() if CTL.SpecialTeam.GetIsGroupChatOpen() then self.group_TabLeft:SetToggleActive(INDEX_GROUP_CHAT, true) else self.group_TabLeft:SetToggleActive(INDEX_GROUP_CHAT, false) end --清除输入内容 if self.keepInput == false and self.SelectChatAcid and self.CurListType ~= MDL.Friend.ListType.Black then if not MDL.Friend.IsPlayerInListType(self.SelectChatAcid, self.CurListType) then self:ClearInputField() end end if self.CurListType == MDL.Friend.ListType.Recent then self:FoldOutLeftScroll(self.CurListType) else --更新数据,先判断当前是否为展开状态 if (self.CurListType == MDL.Friend.ListType.Friend and self.friendTitleData.hasFoldOut) or (self.CurListType == MDL.Friend.ListType.Black and self.blackTitleData.hasFoldOut) or (self.CurListType == MDL.Friend.ListType.CrossFriend and self.crossFriendTitleData.hasFoldOut) then self:FoldOutLeftScroll(self.CurListType) end if self.CurListType ~= TAB_TYPE_GROUPCHAT then self:UpdateTitleShow() end end --刷新未读信息数 self:RefreshTabRedNum() self.keepInput = false end --更新好友标题栏上的文字显示 function UIFriendMain:UpdateTitleShow() --好友标题栏 if self.friendTitleData == nil then self.friendTitleData = {} self.friendTitleData.type = MDL.Friend.ListType.Friend self.friendTitleData.isTitle = true end local friendOnlineNum, friendNum = MDL.Friend.GetListTypeNum(MDL.Friend.ListType.Friend) self.friendTitleData.Label = friendNum == 0 and UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Friend) or string.format("%s(%d/%d)", UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Friend), friendOnlineNum, friendNum) if self.loopScroll_List.Count > 0 then self.loopScroll_List:UpdateIndex(0, self.friendTitleData) end --跨服好友标题栏 if self.crossFriendTitleData == nil then self.crossFriendTitleData = {} self.crossFriendTitleData.type = MDL.Friend.ListType.CrossFriend self.crossFriendTitleData.isTitle = true end local crossFriendOnlineNum, crossFriendNum = MDL.Friend.GetListTypeNum(MDL.Friend.ListType.CrossFriend) self.crossFriendTitleData.Label = crossFriendNum == 0 and UIUtils.GetLocalizedTxt(Const_IDS.IDS_FriendCrossFunctionName) or string.format("%s(%d/%d)", UIUtils.GetLocalizedTxt(Const_IDS.IDS_FriendCrossFunctionName), crossFriendOnlineNum, crossFriendNum) if self.loopScroll_List.Count > 0 then if self.friendTitleData.hasFoldOut then self.loopScroll_List:UpdateIndex(friendNum + 1, self.crossFriendTitleData) else self.loopScroll_List:UpdateIndex(1, self.crossFriendTitleData) end end --黑名单标题栏 if self.blackTitleData == nil then self.blackTitleData = {} self.blackTitleData.type = MDL.Friend.ListType.Black self.blackTitleData.isTitle = true end local blackOnlineNum, blackNum = MDL.Friend.GetListTypeNum(MDL.Friend.ListType.Black) self.blackTitleData.Label = blackNum == 0 and UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Blacklist) or string.format("%s(%d/%d)", UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Blacklist), blackOnlineNum, blackNum) if self.loopScroll_List.Count > 0 then if self.friendTitleData.hasFoldOut and friendNum > 0 then self.loopScroll_List:UpdateIndex(friendNum + 2, self.blackTitleData) elseif self.crossFriendTitleData.hasFoldOut and crossFriendNum > 0 then self.loopScroll_List:UpdateIndex(crossFriendNum + 2, self.blackTitleData) else self.loopScroll_List:UpdateIndex(2, self.blackTitleData) end end end function UIFriendMain:RefreshTabRedNum() local recentRedNum = MDL.Friend.GetUnreadMsgNumByType(MDL.Friend.ListType.Recent) local friendRedNum = MDL.Friend.GetUnreadMsgNumByType(MDL.Friend.ListType.Friend) if recentRedNum > 0 then self.group_TabLeft:SetToggleCompActive(1, "obj_RedRecent", true) self.group_TabLeft:SetToggleText(1, "lab_RedRecent", recentRedNum) else self.group_TabLeft:SetToggleCompActive(1, "obj_RedRecent", false) end if friendRedNum > 0 then self.group_TabLeft:SetToggleCompActive(2, "obj_RedFriend", true) self.group_TabLeft:SetToggleText(2, "lab_RedFriend", friendRedNum) else self.group_TabLeft:SetToggleCompActive(2, "obj_RedFriend", false) end if CTL.SpecialTeam.GetIsGroupChatOpen() then local groupChatRedNum = CTL.SpecialTeam.GetSumGroupChatRedCount() + CTL.SpecialTeam.GetNoticeGroupChatRed() if groupChatRedNum > 0 then self.group_TabLeft:SetToggleCompActive(3, "obj_RedTeam", true) self.group_TabLeft:SetToggleText(3, "lab_RedFriend", groupChatRedNum) else self.group_TabLeft:SetToggleCompActive(3, "obj_RedTeam", false) end end end function UIFriendMain:OnSwitchTab(index, lastListType) --黑名单和好友页签在同个页签下 if index == 1 and (self.CurListType == MDL.Friend.ListType.Friend or self.CurListType == MDL.Friend.ListType.Black) then return end if self.CurListType == index then return end self.keepInput = true self.curGroupChatChannelId = nil if index == MDL.Friend.ListType.Recent then self:ResetListType(index) elseif index == INDEX_GROUP_CHAT - 1 then self:ShowGroupChat() else if lastListType == TAB_TYPE_GROUPCHAT then self:ResetListType(index) else self:ResetListType(lastListType) end end self:UpdateFraudPreventionState() end function UIFriendMain:UpdateFraudPreventionState() if GameAppInfo.IsChinesePackage() == false then UIUtils.SetActive(self.event_Report, false) UIUtils.SetActive(self.group_ReportInfo, false) else if self.curGroupChatChannelId then UIUtils.SetActive(self.event_Report, false) UIUtils.SetActive(self.group_ReportInfo, false) else UIUtils.SetActive(self.event_Report, true) if self.hasClickCloseFraudPrevention then UIUtils.SetActive(self.group_ReportInfo, false) else UIUtils.SetActive(self.group_ReportInfo, true) end end end end function UIFriendMain:ShowGroupChat() CTL.SpecialTeam.NoticeGroupChatRed(false) if self.loopScroll_List.Count > 0 then self.loopScroll_List:ClearCells() end self.clickFriendItem = nil self.clickTitleCell = nil self.CurListType = TAB_TYPE_GROUPCHAT self.IsChatScrollDragToEnd = true UIUtils.SetActive(self.group_Chat, true) UIUtils.SetActive(self.group_Empty, false) UIUtils.SetActive(self.group_Empty_Recent, false) UIUtils.SetActive(self.group_Empty_Friend, false) for i, v in ipairs(CTL.SpecialTeam.GetGroupChatList()) do self.loopScroll_List:AddData(v, 2) end if self.loopScroll_List.Count > 0 then self.loopScroll_List:SelectIndex(0) end self:RefreshTabRedNum() end function UIFriendMain:ResetListType(listType) if self.loopScroll_List.Count > 0 then self.loopScroll_List:ClearCells() end self.clickFriendItem = nil self.clickTitleCell = nil self.CurListType = listType self.IsChatScrollDragToEnd = true --设置标题 self:InitFriendTab(listType) if listType ~= MDL.Friend.ListType.Recent then self.lastTitleType = listType end --请求全量数据 self.NeedMove = true if listType == MDL.Friend.ListType.Recent then MDL.Friend.SendRefreshRecentInfos() elseif listType == MDL.Friend.ListType.Friend or listType == MDL.Friend.ListType.CrossFriend then MDL.Friend.SendRefreshFriendInfos() elseif listType == MDL.Friend.ListType.Black then MDL.Friend.SendRefreshBlackInfos() end end function UIFriendMain:InitFriendTab(listType) if listType == MDL.Friend.ListType.Friend or listType == MDL.Friend.ListType.Black or listType == MDL.Friend.ListType.CrossFriend then if self.friendTitleData == nil then self.friendTitleData = {} self.friendTitleData.type = MDL.Friend.ListType.Friend self.friendTitleData.isTitle = true end if self.blackTitleData == nil then self.blackTitleData = {} self.blackTitleData.type = MDL.Friend.ListType.Black self.blackTitleData.isTitle = true end if self.crossFriendTitleData == nil then self.crossFriendTitleData = {} self.crossFriendTitleData.type = MDL.Friend.ListType.CrossFriend self.crossFriendTitleData.isTitle = true end --设置折叠状态 if listType == MDL.Friend.ListType.Friend then self.friendTitleData.hasFoldOut = true self.blackTitleData.hasFoldOut = false self.crossFriendTitleData.hasFoldOut = false elseif listType == MDL.Friend.ListType.Black then self.friendTitleData.hasFoldOut = false self.blackTitleData.hasFoldOut = true self.crossFriendTitleData.hasFoldOut = false else self.friendTitleData.hasFoldOut = false self.blackTitleData.hasFoldOut = false self.crossFriendTitleData.hasFoldOut = true end --好友标题栏 local friendOnlineNum, friendNum = MDL.Friend.GetListTypeNum(MDL.Friend.ListType.Friend) self.friendTitleData.Label = friendNum == 0 and UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Friend) or string.format("%s(%d/%d)", UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Friend), friendOnlineNum, friendNum) self.loopScroll_List:AddData(self.friendTitleData, 0) --跨服好友标题栏 friendOnlineNum, friendNum = MDL.Friend.GetListTypeNum(MDL.Friend.ListType.CrossFriend) self.crossFriendTitleData.Label = friendNum == 0 and UIUtils.GetLocalizedTxt(Const_IDS.IDS_FriendCrossFunctionName) or string.format("%s(%d/%d)", UIUtils.GetLocalizedTxt(Const_IDS.IDS_FriendCrossFunctionName), friendOnlineNum, friendNum) self.loopScroll_List:AddData(self.crossFriendTitleData, 0) --黑名单标题栏 friendOnlineNum, friendNum = MDL.Friend.GetListTypeNum(MDL.Friend.ListType.Black) self.blackTitleData.Label = friendNum == 0 and UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Blacklist) or string.format("%s(%d/%d)", UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Blacklist), friendOnlineNum, friendNum) self.loopScroll_List:AddData(self.blackTitleData, 0) end end --展开列表,无指定cell选中时默认选中第一个cell function UIFriendMain:FoldOutLeftScroll(listType) local list = MDL.Friend.GetSortedList(listType) self.SortedList = list if listType == MDL.Friend.ListType.Recent then if list == nil or #list == 0 then UIUtils.SetActive(self.group_Chat.gameObject, false) UIUtils.SetActive(self.group_Empty.gameObject, true) self.empty_state:SetState(2) self:InitLeftScrollItem(nil, 0, 0, false) else UIUtils.SetActive(self.group_Chat.gameObject, true) UIUtils.SetActive(self.group_Empty.gameObject, false) --自动选中 local targetAcid = MDL.Friend.RecordSelectAcids[listType] or list[1].acid self.needRefreshChat = true local data, selectCellIndex = self:GetCellDataByAcid(targetAcid) if data == nil then self.SelectChatAcid = list[1].acid else self.SelectChatAcid = targetAcid end if self.NeedMove == false and data == nil then self.SelectChatAcid = list[1].acid self:InitLeftScrollItem(list, 0, 0, false) else local addItem = self:InitLeftScrollItem(list, 0, 0, false) if self.NeedMove or addItem then self.loopScroll_List:MoveIndex(selectCellIndex - 1) end if self.loopScroll_List.Count <= 5 and selectCellIndex <= 5 then self.loopScroll_List:MoveBegin() end end MDL.Friend.RecordSelectAcids[self.CurListType] = self.SelectChatAcid end elseif listType == MDL.Friend.ListType.Friend or listType == MDL.Friend.ListType.CrossFriend then local titleIndex = (listType == MDL.Friend.ListType.Friend and 0 or 1) local offsetIndex = (listType == MDL.Friend.ListType.Friend and 1 or 2) if list == nil or #list == 0 then UIUtils.SetActive(self.group_Chat.gameObject, false) UIUtils.SetActive(self.group_Empty.gameObject, true) self.empty_state:SetState(2) self:InitLeftScrollItem(nil, TitileCount, 1, true) else UIUtils.SetActive(self.group_Chat.gameObject, true) UIUtils.SetActive(self.group_Empty.gameObject, false) --自动选中 local targetAcid = MDL.Friend.RecordSelectAcids[listType] or list[1].acid self.needRefreshChat = true local data, selectCellIndex = self:GetCellDataByAcid(targetAcid) if data == nil then self.SelectChatAcid = list[1].acid else self.SelectChatAcid = targetAcid end if self.NeedMove == false and data == nil then self.SelectChatAcid = list[1].acid if listType == MDL.Friend.ListType.CrossFriend then self:InitLeftScrollItem(list, TitileCount, 2, true, titleIndex) else self:InitLeftScrollItem(list, TitileCount, 1, true, titleIndex) end else local addItem = self:InitLeftScrollItem(list, TitileCount, offsetIndex, true, titleIndex) if self.NeedMove or addItem then if selectCellIndex <= 4 then self.loopScroll_List:MoveIndex(1) self.loopScroll_List:MoveBegin() else self.loopScroll_List:MoveIndex(selectCellIndex - 1) end end if self.loopScroll_List.Count <= 6 and selectCellIndex <= 4 then self.loopScroll_List:MoveIndex(1) self.loopScroll_List:MoveBegin() end end MDL.Friend.RecordSelectAcids[self.CurListType] = self.SelectChatAcid end elseif listType == MDL.Friend.ListType.Black then UIUtils.SetActive(self.group_Chat.gameObject, false) UIUtils.SetActive(self.group_Empty.gameObject, true) if list == nil or #list == 0 then self.empty_state:SetState(1) self:InitLeftScrollItem(nil, TitileCount, 3, false) else self.empty_state:SetState(0) --自动选中 local targetAcid = MDL.Friend.RecordSelectAcids[listType] or list[1].acid self.needRefreshChat = true local data, selectCellIndex = self:GetCellDataByAcid(targetAcid) if data == nil then self.SelectChatAcid = list[1].acid else self.SelectChatAcid = targetAcid end if self.NeedMove == false and data == nil then self.SelectChatAcid = list[1].acid self:InitLeftScrollItem(list, TitileCount, 3, false) else local addItem = self:InitLeftScrollItem(list, TitileCount, 3, false) if self.NeedMove or addItem then if selectCellIndex <= 4 then self.loopScroll_List:MoveBegin() else self.loopScroll_List:MoveIndex(selectCellIndex - 1) end end end if self.loopScroll_List.Count <= 6 and selectCellIndex <= 4 then self.loopScroll_List:MoveBegin() end MDL.Friend.RecordSelectAcids[self.CurListType] = self.SelectChatAcid end end UnityEngine.Canvas.ForceUpdateCanvases() self.NeedMove = false end --收起列表 function UIFriendMain:FoldInLeftScroll(listType) self.clickFriendItem = nil if listType == MDL.Friend.ListType.Black then self:InitLeftScrollItem(nil, TitileCount, 3, false) elseif listType == MDL.Friend.ListType.Friend then self:InitLeftScrollItem(nil, TitileCount, 1, true) elseif listType == MDL.Friend.ListType.CrossFriend then self:InitLeftScrollItem(nil, TitileCount, 2, true) end self.loopScroll_List:MoveIndex(1) self.loopScroll_List:MoveBegin() end --"多退少补" --titleCount 标题栏数,不应该在cellCount的计数内 --offsetIndex 间隔几个cell开始 --isInsertLastCell 是否要插在最后一个cell之前 function UIFriendMain:InitLeftScrollItem(infoList, titleCount, offsetIndex, isInsertLastCell, titleIndex) titleIndex = titleIndex or 0 local addItem = false local infoCount = infoList and #infoList or 0 local cellCount = self.loopScroll_List.Count - titleCount if infoCount > 0 then for i = 1, infoCount do if i <= cellCount then self.loopScroll_List:UpdateIndex(i - 1 + offsetIndex, infoList[i]) end end end -- 如果是最近列表 判斷數量顯示是否為空 if self.CurListType == MDL.Friend.ListType.Recent then UIUtils.SetActive(self.group_Empty_Recent, infoCount == 0) else UIUtils.SetActive(self.group_Empty_Recent, false) UIUtils.SetActive(self.group_Empty_Friend, false) end if infoCount >= cellCount + 1 then -- 这里为了避免使用insertdata来加载数据,所以先将页签移除掉,然后按顺序AddData来增加数据 local needAddTitle = {} -- 这里是倒序存储 if isInsertLastCell then if self.loopScroll_List.Count == titleCount then for index = titleCount - 1, titleIndex + 1, -1 do table.insert(needAddTitle, 1, self.loopScroll_List:GetCellDataByIndex(index)) self.loopScroll_List:RemoveIndex(index) end elseif self.loopScroll_List.Count > titleCount then for i=self.loopScroll_List.Count, 1 , -1 do local titleData = self.loopScroll_List:GetCellDataByIndex(self.loopScroll_List.Count - 1) if titleData.isTitle then if not titleData.hasFoldOut then table.insert(needAddTitle, 1, titleData) self.loopScroll_List:RemoveIndex(self.loopScroll_List.Count - 1) else break end end end end end for i = cellCount + 1, infoCount do if isInsertLastCell then self.loopScroll_List:AddData(infoList[i], 1) -- self.loopScroll_List:InsertData(i + titleIndex, infoList[i], 1) -- 这个方法暂时不使用 else self.loopScroll_List:AddDynamicData(infoList[i], 1) end end for _, v in ipairs(needAddTitle) do self.loopScroll_List:AddData(v, 0) end addItem = true elseif cellCount >= infoCount + 1 then if isInsertLastCell then --从倒数第2个cell开始移除 for i = self.loopScroll_List.Count - 1, infoCount + titleCount, -1 do self.loopScroll_List:RemoveIndex(i - self:GetOffset()) end else for i = self.loopScroll_List.Count, infoCount + titleCount + 1, -1 do self.loopScroll_List:RemoveIndex(i - 1) end end end return addItem end function UIFriendMain:OnCellInit(scrollRect, index, cellTable, data) if scrollRect == self.loopScroll_List then if data.isTitle then --标题栏 local str = data.Label cellTable.lab_CellMainUnCheck.text = str cellTable.lab_CellMainCheck.text = str cellTable.lab_CellMainCheck2.text = str if data.hasFoldOut then cellTable.obj_state:SetState(2) self.clickTitleCell = cellTable else if self.CurListType == data.type then cellTable.obj_state:SetState(1) else cellTable.obj_state:SetState(0) end end else if data.isGroupChat then --群聊 UIUtils.SetLabel(cellTable.txt_PlayerName, string.format("%s(%s/%s)", data.groupChatName, data.groupChatCurNum, data.groupChatMaxNum)) UIUtils.SetLabel(cellTable.txt_Info, data.groupChatDesc) local unReadNum = CTL.SpecialTeam.GetGroupChatRedCount(data.groupChatChannelId) cellTable.btn_Interact:SetRedPointNum(unReadNum) else if cellTable.txt_Time == nil then return end if data.isOnline then local str = UTL.Color.FormatGreenColor(UIUtils.GetLocalizedTxt(Const_IDS.IDS_Friend_Online)) UIUtils.SetLabel(cellTable.txt_Time, str) else local timeOffset = UTL.Time.GetServerTimestamp() - data.lastOnlineTimestamp local str = UTL.Color.FormatTextByColor(offlineColor, UTL.Time.GetBeyondTime(timeOffset)) UIUtils.SetLabel(cellTable.txt_Time, str) end CTL.Character.SetPortraitAndFrame(cellTable.UIHeroHead150, data.portraitId, data.frameId, data.level) UIUtils.SetLabel(cellTable.txt_PlayerName, data.name) CTL.Player.OperateGenderImage(cellTable.image_Gender, data.sex) local targetAcid = self.SelectChatAcid if targetAcid ~= data.acid then cellTable.obj_state:SetState(0) local unReadNum = CTL.Friend.GetUnreadMsgNum(data.acid) cellTable.btn_Interact:SetRedPointNum(unReadNum) else self:SelectFriendItem(cellTable, data.acid, data.name) end if data.isCross then UIUtils.SetActive(cellTable.img_ServerBg, true) cellTable.txt_ServerID.text = CTL.CrossServer.GetServerShowStr(data.showServerId) else UIUtils.SetActive(cellTable.img_ServerBg, false) end end end elseif scrollRect == self.obj_ScrollPanel then if data.IsTimeShow then cellTable.txt_ChatTime.text = data.Content else data.unRead = false if self.curGroupChatChannelId then CTL.SpecialTeam.HasReadMsg(data) end local isSelf = data.isSelf cellTable.lab_Content.text = data.content if isSelf then UIUtils.SetLabelText(cellTable.lab_Name, Const_IDS.IDS_Chat_Me) CTL.Character.SetSelfPortraitAndFrame(cellTable.UIHeroHead150, data.level) self:ProcessVoiceChatData(data, cellTable) else cellTable.lab_Name.text = data.senderName CTL.Character.SetPortraitAndFrame(cellTable.UIHeroHead150, data.portraitId, data.frameId, data.level) if self:ProcessVoiceChatData(data, cellTable) == false then --翻译相关 local pureText = data.pureText local translateText = MDL.Translate.GetTranslateText(data.uuid) if not string.isNilOrEmpty(translateText) then UIUtils.SetActive(cellTable.btn_Translate, false) UIUtils.SetActive(cellTable.lab_Translation, true) cellTable.lab_Translation.text = translateText UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(cellTable.cell.transform) else UIUtils.SetActive(cellTable.btn_Translate, not string.isNilOrEmpty(pureText)) UIUtils.SetActive(cellTable.lab_Translation, false) end cellTable.anim_Translate:StopAnim(false) cellTable.anim_Translate.transform:SetLocalEulerAngles(nil, nil, 0) cellTable.btn_Translate:SetGray(false, true) end end --region 私聊增加显示信息,针对我和对方.逻辑类似于SetMsgTitle,但私聊没有频道和ChatPattern配置,就单写一份了 --势力徽记 CTL.Guild.SetGuildEmblem(cellTable.img_GuildBg, cellTable.txt_Guild, data.emblemFrame, data.emblemContent) --主公身份 if data.jobTitle == MDL.Guild.JobEnum.Leader then UIUtils.SetLabelText(cellTable.lab_Leader, Const_IDS.IDS_Ranking_GuildOwner) else cellTable.lab_Leader.text = nil end --荣誉标记 if cellTable.group_Signet then if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.HonorSignet) then UIUtils.SetActive(cellTable.group_Signet.gameObject, true) local signetList = data.wearSignetId or {} for i, v in ipairs(cellTable.group_Signet) do local signetId = signetList[i] UIUtils.SetActive(v, signetId ~= nil) if signetId then UIUtils.SetImageByAssetBundle(v, DataSheetManager.AchievementHonorSignet:GetDataByID(signetId).HonorSignetIcon) end end else UIUtils.SetActive(cellTable.group_Signet.gameObject, false) end end --官职 if data.officialId then UIUtils.SetActive(cellTable.lab_official, true) UIUtils.SetLabel(cellTable.lab_official, CTL.OfficialPosition.GetModelNameShow(data.officialId)) else UIUtils.SetActive(cellTable.lab_official, false) end --有些新加的元素可能没用,先隐藏了,验收通过的、确定不要了就删 UIUtils.SetActive(cellTable.img_CountryBg, false) --endregion CTL.Player.OperateGenderImage(cellTable.image_Gender, data.sex) cellTable.lab_Name.gameObject:SetActive(true) UnityEngine.Canvas.ForceUpdateCanvases() end else UIChannelChatMain.OnCellInit(self, scrollRect, index, cellTable, data) end end function UIFriendMain:OnCellClicked(scrollRect, index, cellTable, target, data) if scrollRect == self.loopScroll_List then if data.isTitle == true then self.keepInput = true --点击标题栏 if self.clickTitleCell then self.clickTitleCell.obj_state:SetState(0) end self.clickTitleCell = cellTable if self.CurListType ~= data.type then self:ResetListType(data.type) else if data.hasFoldOut then --收起 data.hasFoldOut = false cellTable.obj_state:SetState(1) self:FoldInLeftScroll(data.type) else --展开 self.NeedMove = true self:FoldOutLeftScroll(data.type) data.hasFoldOut = true cellTable.obj_state:SetState(2) end end self:CheckCrossFriendUnlock() else --点击头像 if target == cellTable.btn_Interact then if data.acid then CTL.PlayerInteract.OpenFriendInteractView(data.acid) end else if data.isGroupChat then self:SelectGroupChat(cellTable, data) else if data.acid ~= self.SelectChatAcid then self.needRefreshChat = true end self:SelectFriendItem(cellTable, data.acid, data.name) end end end elseif scrollRect == self.obj_ScrollPanel then if data.IsTimeShow then return end if target == cellTable.UIHeroHead150.img_PlayerAvatarBg then CTL.PlayerInteract.OpenFriendInteractView(data.fromId) elseif target == cellTable.img_VoiceBar then self:PlayRecordVoice(data) elseif cellTable.btn_Translate and target == cellTable.btn_Translate then MDL.Translate.TranslateReq(data.uuid, index, data.pureText) cellTable.anim_Translate:PlayAnim() cellTable.btn_Translate:SetGray(true, false) end elseif scrollRect == self.chat_history_scroll then self:SendMsg(data, true) self:OnClickFace() else UIChannelChatMain.OnCellClicked(self, scrollRect, index, cellTable, target, data) end end function UIFriendMain:OnExitGroupChat() if self.curGroupChatChannelId then --当前的群组被解散了,切回最近页签 self.group_TabLeft:SelectIndex(MDL.Friend.ListType.Recent) end end function UIFriendMain:SelectGroupChat(cellTable, data) self.curGroupChatChannelId = data.groupChatChannelId if self.clickFriendItem and self.clickFriendItem.obj_state then self.clickFriendItem.obj_state:SetState(0) if self.clickFriendItem ~= cellTable then self.IsChatScrollDragToEnd = true end end self.clickFriendItem = cellTable UIUtils.SetLabel(self.txt_Name, data.groupChatName) cellTable.obj_state:SetState(1) cellTable.btn_Interact:SetRedPointNum(0) self:RefreshLoopScroll() end function UIFriendMain:SelectFriendItem(cellTable, acid, name) if self.clickFriendItem then self.clickFriendItem.obj_state:SetState(0) if self.clickFriendItem ~= cellTable then self.IsChatScrollDragToEnd = true end end self.clickFriendItem = cellTable self.SelectChatAcid = acid UIUtils.SetLabel(self.txt_Name, name) cellTable.obj_state:SetState(1) --当前在浏览聊天记录,不刷新 if self.IsChatScrollDragToEnd then --需要记录当前选中的内容 MDL.Friend.RecordSelectAcids[self.CurListType] = acid --某些情况下为空 if cellTable.btn_Interact then cellTable.btn_Interact:SetRedPointNum(0) end if self.needRefreshChat and self.CurListType ~= MDL.Friend.ListType.Black then self:RefreshLoopScroll() self.needRefreshChat = false end end CTL.Friend.ReadAllUnreadMsg(acid) end --通过acid查找对应的data,cellIndex function UIFriendMain:GetCellDataByAcid(acid) local data = nil local cellIndex = 1 if self.SortedList ~= nil and acid ~= nil then for i = 1, #self.SortedList do if self.SortedList[i].acid == acid then data = self.SortedList[i] cellIndex = i break end end end if self.CurListType == MDL.Friend.ListType.Friend then return data, cellIndex + 1 elseif self.CurListType == MDL.Friend.ListType.Black then return data, cellIndex + 2 else return data, cellIndex end end --region 聊天相关,私聊定制 --重写父类的初始化 function UIFriendMain:InitInfo(context) self:RegistSystemEvent(EventID.UI_ChannelChat_ClearInputField, self.ClearInputField) self:RegistSystemEvent(EventID.UI_FriendChat_ReceiveNewMsg, self.OnReceiveNewMsg) self:RegistSystemEvent(EventID.UI_GroupChat_NewMsg, self.OnReceiveGroupChat) self:RegistSystemEvent(EventID.UI_ChannelChat_OnCloseWindow, self.OnCloseWindowByExternal) self.IsOpenSuoNa = false self.QuickMsgCellTable = nil self.QuickMsgCellTableIndex = -1 self.EmojiIndex = 0 self.ChatHistoryIndex = 1 self.QuickMsgIndex = 2 end function UIFriendMain:OnCloseWindowByExternal() CTL.HomeMain.SetEnterTownRestoreView(Const_UI_Lua.UIEnum.UIFriendMain) MDL.Friend.SaveNotSendState(MDL.Friend.RecordSelectAcids[self.CurListType], self.obj_InputField.text) end function UIFriendMain:ClearInputField() self.obj_InputField.text = nil end function UIFriendMain:GetChannelID() if self.curGroupChatChannelId then return self.curGroupChatChannelId end return MDL.Friend.RecordSelectAcids[self.CurListType] end function UIFriendMain:StartRecordVoice() if self.curGroupChatChannelId == nil then CTL.ChannelChat.StartRecordVoice(self:GetChannelID(), true) else CTL.ChannelChat.StartRecordVoice(self:GetChannelID(), false) end end function UIFriendMain:IsOutOfLengthWhenInsertContent(content) return not CTL.Friend.IsOutOfLengthWhenInsertContent(content) end function UIFriendMain:OnReceiveGroupChat(channelId, chatMsg, isHistory) if self.curGroupChatChannelId == nil or self.curGroupChatChannelId ~= channelId then return end if isHistory then self:AddHistoryGroupChatMsg() else if chatMsg.isSelf == false then if self.IsChatScrollDragToEnd == false then self:AddGroupChatMsg(false) --显示"未读"消息 local unreadNum = CTL.SpecialTeam.GetGroupChatRedCount(channelId) if unreadNum > 0 then self:SetUnReadShow(true, unreadNum) else self:SetUnReadShow(false) end else self:AddGroupChatMsg(true) end else --自己发的,直接置底 self:AddGroupChatMsg(true) end end chatMsg.unRead = false self:RefreshTabRedNum() end --请求历史数据会把每条历史Insert到前头,导致当前位置变了,全部Insert结束后将位置挪回原来位置 function UIFriendMain:OnAddGroupChatHistoryCount(count) self.obj_ScrollPanel:MoveIndex(count - 1) end function UIFriendMain:AddHistoryGroupChatMsg() local chatList = CTL.SpecialTeam.GetGroupChatMsgList(self.curGroupChatChannelId) if chatList == nil then return end local newMsg = chatList[1] self.lastMsgTime = self:GetLastMsgTime() if newMsg.msgTime - self.lastMsgTime >= chatTimeShow then local msg = self:GetTimeShowMsg(newMsg.msgTime) self.obj_ScrollPanel:InsertData(0, msg, 2) end if newMsg.isSelf then self.obj_ScrollPanel:InsertData(0, newMsg, 0) else self.obj_ScrollPanel:InsertData(0, newMsg, 1) end end function UIFriendMain:AddGroupChatMsg(isMoveEnd) local chatList = CTL.SpecialTeam.GetGroupChatMsgList(self.curGroupChatChannelId) if chatList == nil then return end local newMsg = chatList[#chatList] if #chatList == 1 then --第一条消息需要显示时间 local msg = self:GetTimeShowMsg(newMsg.msgTime) self.obj_ScrollPanel:AddDynamicData(msg, 2) else self.lastMsgTime = self:GetLastMsgTime() if newMsg.msgTime - self.lastMsgTime >= chatTimeShow then local msg = self:GetTimeShowMsg(newMsg.msgTime) self.obj_ScrollPanel:AddDynamicData(msg, 2) end end if newMsg.isSelf then self.obj_ScrollPanel:AddDynamicData(newMsg, 0) else self.obj_ScrollPanel:AddDynamicData(newMsg, 1) end --置底 if isMoveEnd then self.obj_ScrollPanel:MoveEnd() end end function UIFriendMain:OnReceiveNewMsg(chatMsg) if self.curGroupChatChannelId then return end local acid = self:GetChannelID() if acid == nil then return end if self.CurListType == MDL.Friend.ListType.Black then return end --新消息有可能是新增好友发来的,需要重新更新缓存的数据列表 self.SortedList = MDL.Friend.GetSortedList(self.CurListType) if chatMsg.isSelf == false then if chatMsg.fromId == acid then if self.IsChatScrollDragToEnd == false then self:AddNewMsg(false) --显示"未读"消息 local unreadNum = CTL.Friend.GetUnreadMsgNum(acid) if unreadNum > 0 then self:SetUnReadShow(true, unreadNum) else self:SetUnReadShow(false) end else self:AddNewMsg(true) end else --消息不是当前聊天对象,需要显示红点数量 local data, cellIndex = self:GetCellDataByAcid(chatMsg.fromId) if data then self.loopScroll_List:UpdateIndex(cellIndex - 1, data) end end else --自己发的,直接置底 self:AddNewMsg(true) end self:RefreshTabRedNum() end function UIFriendMain:GetTimeShowMsg(msgTime) local acid = self:GetChannelID() if self.lastMsgTimeDir == nil then self.lastMsgTimeDir = {} end self.lastMsgTimeDir[acid] = msgTime local msg = CTL.Friend.CreatTimeShowMsg(msgTime) return msg end function UIFriendMain:GetLastMsgTime() if self.lastMsgTimeDir == nil then return 0 end local acid = self:GetChannelID() return self.lastMsgTimeDir[acid] or 0 end function UIFriendMain:AddNewMsg(isMoveEnd) local acid = self:GetChannelID() --由上一条消息判断是否显示时间 local chatList, isRemove = CTL.Friend.GetChatMsgList(acid) if chatList == nil then return end if isRemove then --超过聊天记录上限,需要重新生成数据 self:CreateChatMsgData() if isMoveEnd then self.obj_ScrollPanel:MoveEnd() else self.obj_ScrollPanel:MoveBegin() end else local newMsg = chatList[#chatList] if #chatList == 1 then --第一条消息需要显示时间 local msg = self:GetTimeShowMsg(newMsg.msgTime) self.obj_ScrollPanel:AddDynamicData(msg, 2) else self.lastMsgTime = self:GetLastMsgTime() if newMsg.msgTime - self.lastMsgTime >= chatTimeShow then local msg = self:GetTimeShowMsg(newMsg.msgTime) self.obj_ScrollPanel:AddDynamicData(msg, 2) end end if newMsg.isSelf then self.obj_ScrollPanel:AddDynamicData(newMsg, 0) else self.obj_ScrollPanel:AddDynamicData(newMsg, 1) end --置底 if isMoveEnd then self.obj_ScrollPanel:MoveEnd() end end end function UIFriendMain:SendMsg(content, isHistory) if MDL.ChannelChat.CanSendPrivateMsg() and (UTL.Time.GetServerTimestamp() - self.ChatTimeStamp >= chatTimeLimit) then local msg = content if CTL.Friend.IsMsgLengthFit(msg) then self.NeedMove = true self.ChatTimeStamp = UTL.Time.GetServerTimestamp() if self.curGroupChatChannelId then CTL.ChannelChat.SendTextMsg(msg, self:GetChannelID(), isHistory, chat_pb.Room) else CTL.ChannelChat.SendTextMsg(msg, self:GetChannelID(), isHistory, chat_pb.Personal) end end end end function UIFriendMain:OnScrollTop(comp, _isTop) --不需要执行父类的请求历史逻辑 if _isTop then self:LoadBeforeChatData() end end function UIFriendMain:SetUnReadShow(show, count) UIUtils.SetActive(self.event_UnRead, show) if show then local txt = string.FormatByIdsId(Const_IDS.IDS_Chat_Unread, count) self.labLocal_UnReadText.text = txt else EventSystem.Dispatch(EventID.UI_UpdateChatRed) self:RefreshTabRedNum() end end function UIFriendMain:OnScrollBottom(comp, isBottom) local isEnd = isBottom self.IsChatScrollDragToEnd = isEnd if isEnd then self:SetUnReadShow(false) end end function UIFriendMain:OnClickUnRead() -- self.obj_ScrollPanel:MoveEnd() self:SetUnReadShow(false) self.IsChatScrollDragToEnd = true end --刷新聊天消息 function UIFriendMain:RefreshLoopScroll() self:CreateChatMsgData() self:OnClickUnRead() UnityEngine.Canvas.ForceUpdateCanvases() end function UIFriendMain:CreateChatMsgData() local acid = self:GetChannelID() local chatList if self.curGroupChatChannelId then chatList = CTL.SpecialTeam.GetGroupChatMsgList(acid) else chatList = CTL.Friend.GetChatMsgList(acid) end self.obj_ScrollPanel:ClearCells() local chatDataCount = 0 if chatList == nil or #chatList <= 0 then chatDataCount = 0 else chatDataCount = #chatList end self:DisplayInputField() if chatDataCount <= 0 then self.obj_ScrollPanel:MoveEnd() return end self.lastMsgTime = 0 --上一条显示时间的消息时间 local startIndex = 1 local chatListCount = #chatList if chatListCount > SingleLoadCount then startIndex = chatListCount - SingleLoadCount end for index = startIndex, chatListCount do local v = chatList[index] --时间显示 local msgTime = v.msgTime if msgTime - self.lastMsgTime >= chatTimeShow then local msg = self:GetTimeShowMsg(msgTime) self.obj_ScrollPanel:AddData(msg, 2) self.lastMsgTime = msgTime end if v.isSelf then self.obj_ScrollPanel:AddData(v, 0) else self.obj_ScrollPanel:AddData(v, 1) end end self.obj_ScrollPanel:MoveEnd() self.nextIndex = startIndex - 1 end function UIFriendMain:LoadBeforeChatData() if self.nextIndex == nil or self.nextIndex <= 0 then return end --群聊请求分页聊天记录跟好友不同,好友是全部在缓存中,而群聊是在远端,如果缓存中全部显示完了,需要继续从远端获取 if self.curGroupChatChannelId and self.nextIndex <= 0 then CTL.SpecialTeam.TryGetHistory(self.curGroupChatChannelId) return end local acid = self:GetChannelID() local chatList if self.curGroupChatChannelId then chatList = CTL.SpecialTeam.GetGroupChatMsgList(acid) else chatList = CTL.Friend.GetChatMsgList(acid) end local endIndex = 1 if self.nextIndex > SingleLoadCount then endIndex = self.nextIndex - SingleLoadCount end for index = self.nextIndex, endIndex, -1 do --时间显示 local v = chatList[index] local msgTime = v.msgTime if msgTime - self.lastMsgTime >= chatTimeShow then local msg = self:GetTimeShowMsg(msgTime) self.obj_ScrollPanel:InsertData(0, msg, 2) self.lastMsgTime = msgTime end if v.isSelf then self.obj_ScrollPanel:InsertData(0, v, 0) else self.obj_ScrollPanel:InsertData(0, v, 1) end end self.obj_ScrollPanel:MoveIndex(self.nextIndex - endIndex) self.nextIndex = endIndex -1 end --endregion function UIFriendMain:OnValueChanged(comp, value) if comp == self.group_TabLeft then self:OnSwitchTab(value, self.lastTitleType) elseif comp == self.toggle_TopGroup then self:SwitchCondition(value) elseif comp == self.obj_InputField then self.obj_InputField.text = string.gsub(self.obj_InputField.text, "\n", "") end end function UIFriendMain:OnButtonClicked(comp) if comp == self.btn_RemoveList then local acid = MDL.Friend.RecordSelectAcids[self.CurListType] if acid == nil then return end local func = function() MDL.Friend.BlackOperationReq(acid, false) end UTL.MessageBox.ShowOKCancelBox(nil, Const_IDS.IDS_Friend_IfDeleteBlacklist1, func, nil) elseif comp == self.event_Report then UIManager.OpenView(Const_UI_Lua.UIEnum.UIFriendReport, { beReportAcid = self.SelectChatAcid, beReportName = self.txt_Name.text, reportContent = CTL.Friend.GetReportChatMsg(self.SelectChatAcid) }) elseif comp == self.btn_Close then self.hasClickCloseFraudPrevention = true UIUtils.SetActive(self.group_ReportInfo, false) self:StopRepeatFunc(self.UpdateFraudPrevention) elseif comp == self.event_Send then if MDL.Friend.IsCrossPlayer(self.SelectChatAcid) and not MDL.Friend.IsSameWarZone(self.SelectChatAcid) and not MDL.Friend.IsCrossPlayerFriend(self.SelectChatAcid) and self.curGroupChatChannelId == nil then CTL.Tips.AddTipsMsgAndPlaySound(Const_IDS.IDS_FriendCrossChatLimit) else self:OnClickSend() end elseif comp == self.btn_Recommendation then UIManager.OpenView(Const_UI_Lua.UIEnum.UIFriendRecommendation) elseif comp == self.btn_Apply then CTL.Friend.OpenApplyPanel() elseif comp == self.btn_BackToTopBg then self.loopScroll_List:MoveBegin() else UIChannelChatMain.OnButtonClicked(self, comp) end end function UIFriendMain:RefreshFriendRed() local applyRed = CTL.RedPoint.GetRedPointActive(CTL.RedPoint.RedPointKey.Friend_Apply) self.btn_Apply:SetRedPoint(applyRed) end function UIFriendMain:GetOffset() ---之前只有两个页签 现在有三个 点击第几个需要从第几个开始把之前的处理掉 if self.CurListType == MDL.Friend.ListType.Black then return 3 elseif self.CurListType == MDL.Friend.ListType.Friend then return 2 elseif self.CurListType == MDL.Friend.ListType.CrossFriend then return 1 end end function UIFriendMain:CheckCrossFriendUnlock() if self.CurListType == MDL.Friend.ListType.CrossFriend then local isUnlock, content = MDL.Unlock.IsClientUnlockWithText(MDL.Unlock.FunctionType.FriendsCross, "IDS_Activity_Condition_01") if not isUnlock then CTL.Tips.AddTipsMsg(content) end end end return UIFriendMain