164 lines
6.3 KiB
VB.net
164 lines
6.3 KiB
VB.net
Imports DevExpress.XtraGrid.Views.Tile
|
|
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class ChatControl
|
|
Private ReadOnly IdColumn As String = "GUID"
|
|
Private ReadOnly IdentificationColumn As String = "USER_FROM"
|
|
Private ReadOnly MessageColumn As String = "MESSAGE_TEXT"
|
|
Private ReadOnly DateColumn As String = "ADDED_WHEN"
|
|
|
|
Private Db As MSSQLServer
|
|
Private LogConfig As LogConfig
|
|
Private Logger As Logger
|
|
|
|
Private ReadOnly UsernameColorsDict As New Dictionary(Of String, Color)
|
|
Private ReadOnly UsernameColors As New List(Of Color) From {
|
|
Color.Purple,
|
|
Color.Red,
|
|
Color.LightBlue,
|
|
Color.DarkSeaGreen
|
|
}
|
|
|
|
Public IDBObjectId As Long
|
|
Public ConnStringIDB As String
|
|
Public ConversationIdentification As String
|
|
Public Username As String
|
|
Public CurrentConversation As Long
|
|
|
|
Public Sub New()
|
|
InitializeComponent()
|
|
End Sub
|
|
|
|
Public Sub Init(LogConfig As LogConfig, ConnectionStringIDB As String, ConversationIdentification As String, Username As String)
|
|
Me.LogConfig = LogConfig
|
|
Me.Logger = LogConfig.GetLogger()
|
|
Me.ConnStringIDB = ConnectionStringIDB
|
|
Me.ConversationIdentification = ConversationIdentification
|
|
Me.Username = Username
|
|
|
|
Me.Db = New MSSQLServer(LogConfig, ConnStringIDB)
|
|
End Sub
|
|
|
|
Public Function GetConversations(IDBObjectId As Long)
|
|
Dim oSQL As String = $"SELECT * FROM VWIDB_CONVERSATION WHERE IDB_OBJ_ID = {IDBObjectId} ORDER BY CONVERSATION_ID DESC"
|
|
Dim oDatatable As DataTable = Db.GetDatatable(oSQL)
|
|
Dim oConversations As New List(Of String)
|
|
If Not IsNothing(oDatatable) Then
|
|
If oDatatable.Rows.Count = 1 Then
|
|
Dim oItem = oDatatable.Rows(0).Item("CONVERSATION_ID").ToString + "|" + oDatatable.Rows(0).Item("TITLE").ToString
|
|
oConversations.Insert(0, oItem)
|
|
CurrentConversation = oDatatable.Rows(0).Item("CONVERSATION_ID")
|
|
LoadConversation(CurrentConversation)
|
|
Else
|
|
For Each oROW As DataRow In oDatatable.Rows
|
|
Dim oItem = oROW.Item("CONVERSATION_ID").ToString + "|" + oROW.Item("TITLE").ToString
|
|
oConversations.Insert(0, oItem)
|
|
If oROW.Item("CONVERSATION_STATE") = "Started" Then
|
|
CurrentConversation = oROW.Item("CONVERSATION_ID")
|
|
LoadConversation(CurrentConversation)
|
|
End If
|
|
Next
|
|
End If
|
|
End If
|
|
Return oConversations
|
|
End Function
|
|
|
|
Private Sub tsCBConversations_SelectedIndexChanged(sender As Object, e As EventArgs)
|
|
'If tsCBConversations.SelectedIndex <> -1 Then
|
|
' Dim oValues As List(Of String) = tsCBConversations.Text.Split("|").ToList()
|
|
' CurrentConversation = oValues(0)
|
|
' LoadConversation(CurrentConversation)
|
|
|
|
'End If
|
|
End Sub
|
|
Public Sub LoadConversation(ConversationId As Long)
|
|
Dim oSQL As String = $"SELECT * FROM VWIDB_CONV_MESSAGES WHERE CONV_ID = {ConversationId} ORDER BY GUID"
|
|
Dim oDatatable As DataTable = Db.GetDatatable(oSQL)
|
|
BuildUsernameColorDict(oDatatable)
|
|
oSQL = $"SELECT * FROM VWIDB_CONVERSATION WHERE CONVERSATION_ID = {ConversationId}"
|
|
Dim oDatatable2 As DataTable = Db.GetDatatable(oSQL)
|
|
GridChat.DataSource = ChatSource
|
|
ChatSource.DataSource = oDatatable
|
|
lblTitle.Text = oDatatable2.Rows(0).Item("TITLE")
|
|
|
|
End Sub
|
|
|
|
Public Sub NewMessage(MessageText As String)
|
|
Try
|
|
Dim oSQL As String = $"EXEC [PRIDB_NEW_CONVERSATION_MESSAGE] {CurrentConversation},'{MessageText}', '{ConversationIdentification}'"
|
|
Dim oResult = Db.GetScalarValue(oSQL)
|
|
LoadConversation(CurrentConversation)
|
|
txtMessage.Text = String.Empty
|
|
ChatView.MoveLast()
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Private Sub BuildUsernameColorDict(Datatable As DataTable)
|
|
Dim oIndex = 0
|
|
|
|
UsernameColorsDict.Clear()
|
|
|
|
Datatable.AsEnumerable().
|
|
Select(Function(Row) Row.Item(IdentificationColumn)).
|
|
Distinct().ToList().
|
|
ForEach(Sub(Name)
|
|
UsernameColorsDict.Add(Name, UsernameColors.Item(oIndex))
|
|
oIndex += 1
|
|
End Sub)
|
|
End Sub
|
|
|
|
Private Sub ChatView_CustomItemTemplate(sender As Object, e As TileViewCustomItemTemplateEventArgs) Handles ChatView.CustomItemTemplate
|
|
Dim oRow As DataRow = ChatView.GetDataRow(e.RowHandle)
|
|
Dim oUsername As String = oRow.Item(IdentificationColumn)
|
|
|
|
If oUsername = ConversationIdentification Or oUsername = Username Then
|
|
e.Template = e.Templates.Item("ChatRight")
|
|
Else
|
|
e.Template = e.Templates.Item("ChatLeft")
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub ChatView_ItemCustomize(sender As Object, e As TileViewItemCustomizeEventArgs) Handles ChatView.ItemCustomize
|
|
Dim oRow As DataRow = ChatView.GetDataRow(e.RowHandle)
|
|
|
|
Dim oUsername As String = oRow.Item(IdentificationColumn)
|
|
Dim oMessage As String = oRow.Item(MessageColumn)
|
|
Dim oColor As Color = UsernameColorsDict.Item(oUsername)
|
|
|
|
If oUsername = ConversationIdentification Or oUsername = Username Then
|
|
e.Item.AppearanceItem.Normal.BackColor = Color.PaleTurquoise
|
|
End If
|
|
e.Item.AppearanceItem.Focused.BackColor = Color.Turquoise
|
|
e.Item.Item(IdentificationColumn).Appearance.Normal.ForeColor = oColor
|
|
End Sub
|
|
|
|
|
|
|
|
Private Sub txtMessage_KeyUp(sender As Object, e As KeyEventArgs)
|
|
|
|
End Sub
|
|
|
|
Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles btnSendMessage.Click
|
|
If txtMessage.Text.Count > 0 Then
|
|
NewMessage(txtMessage.Text)
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles txtMessage.KeyUp
|
|
If e.Control And e.KeyCode = Keys.Enter And txtMessage.Text.Count > 0 Then
|
|
NewMessage(txtMessage.Text)
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub ChatControl_Click(sender As Object, e As EventArgs) Handles MyBase.Click
|
|
|
|
End Sub
|
|
|
|
Private Sub ChatControl_Leave(sender As Object, e As EventArgs) Handles Me.Leave
|
|
|
|
End Sub
|
|
End Class
|