ChatControl + Test
This commit is contained in:
@@ -3,10 +3,10 @@ Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public Class ChatControl
|
||||
Private ReadOnly IdColumn As String = "ID"
|
||||
Private ReadOnly UsernameColumn As String = "USERNAME"
|
||||
Private ReadOnly MessageColumn As String = "MESSAGE"
|
||||
Private ReadOnly DateColumn As String = "DATE"
|
||||
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
|
||||
@@ -22,50 +22,31 @@ Public Class ChatControl
|
||||
|
||||
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(ConnectionString As String, IDBObjectId As Long, CurrentUser As String)
|
||||
|
||||
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 Function InitData() As DataTable
|
||||
Dim oDatatable As New DataTable()
|
||||
Dim oColumns As New List(Of DataColumn) From {
|
||||
New DataColumn(IdColumn, New Integer.GetType),
|
||||
New DataColumn(UsernameColumn),
|
||||
New DataColumn(MessageColumn),
|
||||
New DataColumn(DateColumn, New Date.GetType)
|
||||
}
|
||||
oDatatable.Columns.AddRange(oColumns.ToArray)
|
||||
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)
|
||||
|
||||
Dim oRow1 = oDatatable.NewRow()
|
||||
With oRow1
|
||||
.Item(IdColumn) = 0
|
||||
.Item(UsernameColumn) = "Jonathan"
|
||||
.Item(MessageColumn) = "Hi"
|
||||
.Item(DateColumn) = Now.AddMinutes(-10).ToShortTimeString
|
||||
End With
|
||||
lookupConversations.DataSource = oDatatable
|
||||
End Sub
|
||||
|
||||
Dim oRow2 = oDatatable.NewRow()
|
||||
With oRow2
|
||||
.Item(IdColumn) = 0
|
||||
.Item(UsernameColumn) = "Marlon"
|
||||
.Item(MessageColumn) = "Was geht?"
|
||||
.Item(DateColumn) = Now.ToShortTimeString
|
||||
End With
|
||||
|
||||
oDatatable.Rows.Add(oRow1)
|
||||
oDatatable.Rows.Add(oRow2)
|
||||
oDatatable.AcceptChanges()
|
||||
|
||||
Return oDatatable
|
||||
End Function
|
||||
|
||||
Private Sub ChatControl_Load(sender As Object, e As EventArgs) Handles Me.Load
|
||||
Dim oDatatable = InitData()
|
||||
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)
|
||||
|
||||
@@ -73,9 +54,22 @@ Public Class ChatControl
|
||||
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().
|
||||
@@ -85,11 +79,11 @@ Public Class ChatControl
|
||||
End Sub)
|
||||
End Sub
|
||||
|
||||
Private Sub ChatView_CustomItemTemplate(sender As Object, e As DevExpress.XtraGrid.Views.Tile.TileViewCustomItemTemplateEventArgs) Handles ChatView.CustomItemTemplate
|
||||
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 = "Jonathan" Then
|
||||
If oUsername = CurrentUser Then
|
||||
e.Template = e.Templates.Item("ChatRight")
|
||||
Else
|
||||
e.Template = e.Templates.Item("ChatLeft")
|
||||
@@ -101,15 +95,29 @@ Public Class ChatControl
|
||||
Dim oUsername As String = oRow.Item(UsernameColumn)
|
||||
Dim oColor As Color = UsernameColorsDict.Item(oUsername)
|
||||
|
||||
If oUsername = "Jonathan" Then
|
||||
e.Item.AppearanceItem.Normal.BackColor = Color.LightGray
|
||||
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 Then
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user