#IgnoreWarnings: 6
Sub Class_Globals
Private xui As XUI
Private TextField As B4XFloatTextField
Private CLV As CustomListView
Private BBCodeView1 As BBCodeView
Private Engine As BCTextEngine
Private bc As BitmapCreator
Private ArrowWidth As Int = 10dip
Private Gap As Int = 6dip
Private pnlBottom As B4XView
Private LastUserLeft As Boolean
End Sub
Public Sub Initialize (Parent As B4XView)
Parent.LoadLayout("1")
Engine.Initialize(Parent)
bc.Initialize(300, 300)
TextField.NextField = TextField
End Sub
Private Sub lblSend_Click
If TextField.Text.Length > 0 Then
LastUserLeft = Not(LastUserLeft)
AddItem(TextField.Text, LastUserLeft)
End If
TextField.RequestFocusAndShowKeyboard
#if B4J
Dim ta As TextArea = TextField.TextField
ta.SelectAll
#else if B4A
Dim et As EditText = TextField.TextField
et.SelectAll
#end if
Dim sx As String
sx="User"
'Starter.SQL1.ExecNonQuery2("INSERT INTO smsdata VALUES(?,?,?,?)", Array As Object(LTrim(RTrim(TextField.Text)), LTrim(RTrim(LastUserLeft))),LTrim(RTrim(DateTime.now)) ,sx))
Starter.SQL1.ExecNonQuery2("INSERT INTO smsdata VALUES(?, ?, ?,?)", Array As Object(LTrim(RTrim(TextField.Text)), LTrim(RTrim(LastUserLeft)),LTrim(RTrim(DateTime.now)),sx))
End Sub
Sub LTrim(s As String) As String
Dim m As Matcher = Regex.Matcher("^(\s+)", s)
If m.Find Then
Return s.SubString(m.GetEnd(1))
Else
Return s
End If
End Sub
Sub RTrim(s As String) As String
Dim m As Matcher = Regex.Matcher("(\s+)$", s)
If m.Find Then
Return s.SubString(m.GetEnd(1))
Else
Return s
End If
End Sub
Public Sub HeightChanged (NewHeight As Int)
Dim c As B4XView = CLV.AsView
c.Height = NewHeight - pnlBottom.Height
CLV.Base_Resize(c.Width, c.Height)
pnlBottom.Top = NewHeight - pnlBottom.Height
ScrollToLastItem
End Sub
Private Sub AddItem (Text As String, Right As Boolean)
Dim p As B4XView = xui.CreatePanel("")
Dim User As String
If Right Then User = "User 2" Else User = "User 1"
BBCodeView1.ExternalRuns = BuildMessage(Text, User)
BBCodeView1.ParseAndDraw
Dim ivText As B4XView = CreateImageView
Dim bmpText As B4XBitmap = GetBitmap(BBCodeView1.ForegroundImageView)
bc.SetBitmapToImageView(bmpText, ivText)
Dim ivBG As B4XView = CreateImageView
Dim bmpBG As B4XBitmap = DrawBubble(bmpText.Width, bmpText.Height, Right)
bc.SetBitmapToImageView(bmpBG, ivBG)
p.SetLayoutAnimated(0, 0, 0, CLV.AsView.Width, bmpText.Height + 3 * Gap)
p.AddView(ivBG, 0, Gap, bmpBG.Width * xui.Scale, bmpBG.Height * xui.Scale)
p.AddView(ivText, Gap + ArrowWidth, 2 * Gap, bmpText.Width, bmpText.Height)
CLV.Add(p, Null)
ScrollToLastItem
End Sub
Private Sub ScrollToLastItem
Sleep(100)
If CLV.Size > 0 Then
CLV.ScrollToItem(CLV.Size - 1)
End If
End Sub
Private Sub DrawBubble (Width As Int, Height As Int, Right As Boolean) As B4XBitmap
Width = Ceil(Width / xui.Scale)
Height = Ceil(Height / xui.Scale)
Dim ScaledGap As Int = Ceil(Gap / xui.Scale)
Dim ScaledArrowWidth As Int = Ceil(ArrowWidth / xui.Scale)
Dim nw As Int = Width + 3 * ScaledGap + ScaledArrowWidth
Dim nh As Int = Height + 2 * ScaledGap
If bc.mWidth < nw Or bc.mHeight < nh Then
bc.Initialize(Max(bc.mWidth, nw), Max(bc.mHeight, nh))
End If
bc.DrawRect(bc.TargetRect, xui.Color_Transparent, True, 0)
Dim r As B4XRect
r.Initialize(ScaledArrowWidth, 1, nw, nh)
Dim clr As Int
If Right Then clr = 0xFFEFEFEF Else clr = 0xFFC1F7A3
bc.DrawRectRounded(r, clr, True, 0, 10)
Dim path As BCPath
path.Initialize(nw - 1, 1)
path.LineTo(nw - 1 - (10 + ScaledArrowWidth), 1)
path.LineTo(nw - 1 - ScaledArrowWidth, 10)
path.LineTo(nw - 1, 1)
bc.DrawPath(path, clr, True, 0)
bc.DrawPath(path, clr, False, 2)
Dim b As B4XBitmap = bc.Bitmap
Return b.Crop(0, 1, nw, nh)
End Sub
Private Sub BuildMessage (Text As String, User As String) As List
Dim title As BCTextRun = Engine.CreateRun(User & CRLF)
title.TextFont = BBCodeView1.ParseData.DefaultBoldFont
Dim TextRun As BCTextRun = Engine.CreateRun(Text & CRLF)
Dim time As BCTextRun = Engine.CreateRun(DateTime.Time(DateTime.Now))
time.TextFont = xui.CreateDefaultFont(10)
time.TextColor = xui.Color_Gray
Return Array(title, TextRun, time)
End Sub
Private Sub GetBitmap (iv As ImageView) As B4XBitmap
#if B4J
Return iv.GetImage
#Else If B4A
Return iv.Bitmap
#End If
End Sub
Private Sub CreateImageView As B4XView
Dim iv As ImageView
iv.Initialize("")
Return iv
End Sub
#if B4J
Sub lblSend_MouseClicked (EventData As MouseEvent)
lblSend_Click
EventData.Consume
End Sub
#end if