124 lines
4.5 KiB
VB.net
124 lines
4.5 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 UsernameColumn 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 ConnectionString As String
|
|
Public CurrentUser As String
|
|
Public CurrentConversation As Long
|
|
|
|
Public Sub New()
|
|
InitializeComponent()
|
|
End Sub
|
|
|
|
Public Sub Init(LogConfig As LogConfig, ConnectionString As String, CurrentUser As String)
|
|
Me.LogConfig = LogConfig
|
|
Me.Logger = LogConfig.GetLogger()
|
|
Me.ConnectionString = ConnectionString
|
|
Me.CurrentUser = CurrentUser
|
|
Me.Db = New MSSQLServer(LogConfig, ConnectionString)
|
|
End Sub
|
|
|
|
Public Sub LoadConversations(IDBObjectId As Long)
|
|
Dim oSQL As String = $"SELECT * FROM VWIDB_CONVERSATION WHERE IDB_OBJ_ID = {IDBObjectId}"
|
|
Dim oDatatable As DataTable = Db.GetDatatable(oSQL)
|
|
|
|
lookupConversations.DataSource = oDatatable
|
|
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)
|
|
|
|
GridChat.DataSource = ChatSource
|
|
ChatSource.DataSource = oDatatable
|
|
End Sub
|
|
|
|
Public Sub SendMessage(MessageText As String)
|
|
Try
|
|
Dim oSQL As String = $"EXEC [PRIDB_NEW_CONVERSATION_MESSAGE] {CurrentConversation},'{MessageText}', '{CurrentUser}'"
|
|
Dim oResult = Db.GetScalarValue(oSQL)
|
|
LoadConversation(CurrentConversation)
|
|
txtMessage.Text = String.Empty
|
|
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(UsernameColumn)).
|
|
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(UsernameColumn)
|
|
|
|
If oUsername = CurrentUser 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(UsernameColumn)
|
|
Dim oColor As Color = UsernameColorsDict.Item(oUsername)
|
|
|
|
If oUsername = CurrentUser Then
|
|
e.Item.AppearanceItem.Normal.BackColor = Color.PaleTurquoise
|
|
End If
|
|
e.Item.AppearanceItem.Focused.BackColor = Color.Turquoise
|
|
e.Item.Item(UsernameColumn).Appearance.Normal.ForeColor = oColor
|
|
End Sub
|
|
|
|
Private Sub txtMessage_KeyUp(sender As Object, e As KeyEventArgs) Handles txtMessage.KeyUp
|
|
If e.Control And e.KeyCode = Keys.Enter And txtMessage.Text.Count > 0 Then
|
|
SendMessage(txtMessage.Text)
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub lookupConversations_SelectedValuesChanged(sender As Object, SelectedValues As List(Of String)) Handles lookupConversations.SelectedValuesChanged
|
|
If SelectedValues.Count > 0 Then
|
|
CurrentConversation = SelectedValues.First()
|
|
LoadConversation(CurrentConversation)
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles btnSendMessage.Click
|
|
If txtMessage.Text.Count > 0 Then
|
|
SendMessage(txtMessage.Text)
|
|
End If
|
|
End Sub
|
|
End Class
|