Android Question How can i use this example in this to create a user friendly chat app

Makumbi

Well-Known Member
Licensed User
Below is my code but my biggest problem is i have to always have to click on recieved sms to check on recieved sms and also click on sent to see sent messages yet i wanted to have all of them viewed at the same interface example like that of whatsup where you can see both sent and recieved messages in one click. In this i wanted to just pick a name and then view and send message to the that person's account from my database thanks in advance please help

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim cursor1 As Cursor
    Private smiley As Bitmap

    'Dim SelectedRow As Int                    : SelectedRow = -1
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private LongTextTemplate As B4XLongTextTemplate
    Private lblSelectedItem As Label
    'Dim wbvTable2 As ScrollView
    Private B4XTable1 As B4XTable
    Private xui As XUI
        Dim data As List
    Private dialog As B4XDialog
End Sub

Sub ShowTable
    If lblSelectedItem.Text="Send" Then
        'B4XTable1.Refresh
        B4XTable1.Clear
        B4XTable1.AddColumn("Datesent", B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("SentMessage", B4XTable1.COLUMN_TYPE_TEXT)
    '    B4XTable1.AddColumn("ID", B4XTable1.COLUMN_TYPE_TEXT)

        data.Initialize
        Dim rs As ResultSet = Starter.SQL1.ExecQuery("SELECT DISTINCT Datesent,Sms as SentMessage,ID FROM Sentmessages")
        Do While rs.NextRow
            Dim row(2) As Object
            row(0) = rs.GetString("Datesent")
            row(1) = rs.GetString("SentMessage")
            'row(2) = rs.GetString("ID")
            Log(row(1))
            'Log(row(3))
            'Log(row(2))
            '& " " & rs.GetString("LastName")
            'row(2) = rs.GetString("ID")
            'Some of the fields are Null. We need to convert them to empty strings:
        '    If row(1) = "Null" And  row(2) = "Null" Then
                'Return
            'Else
            '    If row(1) = "Null" Then row(2) = "Null"
                'row(1) = rs.GetString("SentMessage")
        
                data.Add(row)
            'End If
        

        Loop
        rs.Close
        B4XTable1.SetData(data)
        B4XTable1.Refresh
         Return
Else
    B4XTable1.Clear
    B4XTable1.AddColumn("Datesent", B4XTable1.COLUMN_TYPE_TEXT)
    B4XTable1.AddColumn("SentMessage", B4XTable1.COLUMN_TYPE_TEXT)
'    B4XTable1.AddColumn("ID", B4XTable1.COLUMN_TYPE_TEXT)

    data.Initialize
    Dim rs As ResultSet = Starter.SQL1.ExecQuery("SELECT DISTINCT Datesent,Sms as SentMessage,ID FROM Sentmessages")
    Do While rs.NextRow
        Dim row(2) As Object
        row(0) = rs.GetString("Datesent")
        row(1) = rs.GetString("SentMessage")
        'row(2) = rs.GetString("ID")
        Log(row(1))
        'Log(row(3))
        'Log(row(2))
        '& " " & rs.GetString("LastName")
        'row(2) = rs.GetString("ID")
        'Some of the fields are Null. We need to convert them to empty strings:
        'If row(1) = "" And  row(2) = "" Then
            'Return
            'Else
            'If row(1) = Null Then row(2) = ""
            'row(1) = rs.GetString("SentMessage")
        
            data.Add(row)
    '    End If
        

    Loop
    rs.Close
    B4XTable1.SetData(data)
    'B4XTable1.Refresh
    End If
End Sub


Sub ShowTable2
    B4XTable1.Clear
    B4XTable1.AddColumn("DateRecieved", B4XTable1.COLUMN_TYPE_TEXT)
    B4XTable1.AddColumn("Sms", B4XTable1.COLUMN_TYPE_TEXT)
    'B4XTable1.AddColumn("ID", B4XTable1.COLUMN_TYPE_TEXT)

    data.Initialize
    Dim rs As ResultSet = Starter.SQL1.ExecQuery("SELECT DISTINCT Daterecieved,Sms as RecievedMessage,ID FROM Recievedsms")
    Do While rs.NextRow
        Dim row(2) As Object
        row(0) = rs.Getstring("Daterecieved")
        row(1) = rs.GetString("RecievedMessage")
        '& " " & rs.GetString("LastName")
    '    row(2) = rs.GetString("ID")
        'Some of the fields are Null. We need to convert them to empty strings:
        'If row(2) = Null Then row(2) = ""
        'row(3) = rs.GetString("RecievedMessage")
        data.Add(row)
    Loop
    rs.Close
    B4XTable1.SetData(data)
    
End Sub


Sub B4XTable1_CellClicked (ColumnId As String, RowId As Long)
    Log(ColumnId)
    If ColumnId = "SentMessage" Then
        Dim valuer As String = B4XTable1.GetRow(RowId).Get("SentMessage")
        
        If valuer.Length<=140 Then
            dialog.Show(valuer, "OK", "", "")
        LongTextTemplate.Text= valuer
        Else
        'dialog.Show(valuer, "OK", "", "")
        dialog.ShowTemplate(LongTextTemplate, "OK", "", "")
        Return
        End If
        Else
        If ColumnId = "Sms" Then
        Dim valuer As String = B4XTable1.GetRow(RowId).Get("Sms")
        'dialog.Show(valuer, "OK", "", "")
        If valuer.Length<=140 Then
                dialog.Show(valuer, "OK", "", "")
        Else
            LongTextTemplate.Text= valuer
            dialog.ShowTemplate(LongTextTemplate, "OK", "", "")
        '    dialog.ShowCustom(valuer, "OK", "", "")
        Return
        End If
    End If
    End If
    If ColumnId = "Datesent" Then
        'Dim value As String = B4XTable1.GetRow(RowId).Get(ColumnId)
        'InputTemplate.Text = value
        'Dim valuer As String = B4XTable1.GetRow(RowId).Get("Dates")
        'dialog.Show("dddddddddddddddddddddddddddddd", "OK", "", "")
        Dim sf As Object = xui.Msgbox2Async("Delete Message?", "Title", "Yes", "Cancel", "No", Null)
        Wait For (sf) Msgbox_Result (Result As Int)
        If Result = xui.DialogResponse_Positive Then
            DeleteRow(B4XTable1, RowId)
        End If
    Else
        
        If ColumnId = "DateRecieved" Then
            Dim sf As Object = xui.Msgbox2Async("Delete Message ?", "Title", "Yes", "Cancel", "No", Null)
            Wait For (sf) Msgbox_Result (Result As Int)
            If Result = xui.DialogResponse_Positive Then
                DeleteRow2(B4XTable1, RowId)
            End If
        End If
    End If
End Sub

Sub DeleteRow (tbl As B4XTable, RowId As Long)

    Dim RowData As Map = tbl.GetRow(RowId)
    Dim customerID As String = RowData.Get("Datesent")
        
    tbl.sql1.ExecNonQuery2("DELETE FROM data WHERE rowid = ?", Array (RowId))     'deletes record from Table
    Starter.SQL1.ExecNonQuery2("DELETE FROM Sentmessages WHERE Datesent = ?", Array (customerID))  'deletes from DB
    
    Dim page As Int = tbl.CurrentPage
    Dim FirstIndex As Int = tbl.FirstRowIndex
    tbl.ClearDataView 'Updates the rows count.
    If FirstIndex + 1 >= tbl.mCurrentCount Then
        page = page - 1
    End If
    tbl.CurrentPage = page
    
End Sub

Sub DeleteRow2 (tbl As B4XTable, RowId As Long)

    Dim RowData As Map = tbl.GetRow(RowId)
    Dim customerID As String = RowData.Get("DateRecieved")
        Log(customerID)
    tbl.sql1.ExecNonQuery2("DELETE FROM data WHERE rowid = ?", Array (RowId))     'deletes record from Table
    Starter.SQL1.ExecNonQuery2("DELETE FROM Recievedsms WHERE Daterecieved = ?", Array (customerID))  'deletes from DB
    
    Dim page As Int = tbl.CurrentPage
    Dim FirstIndex As Int = tbl.FirstRowIndex
    tbl.ClearDataView 'Updates the rows count.
    If FirstIndex + 1 >= tbl.mCurrentCount Then
        page = page - 1
    End If
    tbl.CurrentPage = page
    
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        smiley = LoadBitmapResize(File.DirAssets, "smiley.png", 24dip, 24dip, False)
    End If
    'Do not forget to load the layout file created with the visual designer. For example:
    If  IsConnectedToInternet=True Then
    replysms
        'HighPriority_Notification
Activity.LoadLayout("simplechat")
        Activity.Title="Chat"
        dialog.Initialize(Activity)
        LongTextTemplate.Initialize
    Else
MsgboxAsync("Error Connecting to the Server Please Check Your Internet Connection","Server Error")
        Activity.LoadLayout("simplechat")
        Activity.Title="Chat"
        Return
        End If
End Sub

Sub IsConnectedToInternet As Boolean
    Dim r As Reflector
 
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")
 
    If r.Target <> Null Then
        Return r.RunMethod("isConnectedOrConnecting")
    End If
 
    Return False
End Sub
Sub replysms
    'Dim SQLQry As String = "DELETE FROM Recievedsms"
    'Starter.SQL1.ExecNonQuery(SQLQry)
    cursor1 = Starter.SQL1.ExecQuery("SELECT phone AS Phone,Sex FROM Register")
    If cursor1.RowCount > 0 Then
        cursor1.Position =0
        Dim phone As String
        phone = cursor1.Getstring("Phone")
        Dim sx As String
        sx = cursor1.Getstring("Sex")
    '    sx = "Father"
    End If
    Dim CustID As String = phone' Customer ID
    Dim jtk As HttpJob
    jtk.Initialize("", Me)
    'j.Download("http://192.168.1.239/KabojjaApp/HandlerVB.ashx?customerid=" & CustID)
    jtk.Download("http://kccug.com/KabojjaApp/HandlerVBGetreply.ashx?customerid=" & CustID & "&bcode=" & sx)
    'jtk.Download("http://192.168.1.239/KabojjaApp/HandlerVBGetreply.ashx?customerid=" & CustID & "&bcode=" & sx)
    'jtk.GetRequest.Timeout = 1000 ' 10 seconds
    Wait For (jtk) JobDone(jtk As HttpJob)

    If jtk.Success Then ' if job is success (http status code 200)
        Dim RetVal As String
        Log(RetVal)
        RetVal = jtk.GetString
        If jtk.GetString = "[]" Then
            'Msgbox("Please try Again ","SMIS")
            'Return
            
        Else
                    
            Dim jpt As JSONParser
            jpt.Initialize(jtk.GetString)
            'Log(jpt) ' will pr
            Log($"Token(${jpt})"$)
            Dim quotes As List = jpt.NextArray
            For Each quot As Map In quotes
            Log("Account: " & quot.Get("Account"))
            Log("sms: " & quot.Get("sms"))
                Log("Datesent: " & quot.Get("Datesent"))
                Log("Status: " & quot.Get("Status"))
                Log("codes: " & quot.Get("Code"))
                Log("atk: " & quot.Get("atk"))
            'Log("Tos: " & quot.Get("Tos"))
                Dim ltt As Long
                DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Datesent")))
                ltt = DateTime.DateParse(datestring)
                DateTime.DateFormat = "dd/MM/yyyy"
                Log(DateTime.Date(ltt))
                Dim now As Long
                now = DateTime.Now
                Log(DateTime.Now)
                DateTime.DateFormat = "dd/MM/yyyy" : DateTime.TimeFormat = "hh:mm a"
                
                Log(DateTime.Date(ltt)& " "& DateTime.Time(now))
                Starter.SQL1.ExecNonQuery2("INSERT INTO Recievedsms VALUES(?, ?, ?)", Array As Object(DateTime.Date(ltt)& " "& DateTime.Time(now), quot.Get("sms"), quot.Get("Code")))
    
                Dim n As NB6
                n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(smiley)
                n.Build(quot.Get("sms"), "Kabojja Junior School", "tag", Sendsms).Notify(9)
                
                Dim jtkt As HttpJob
                jtkt.Initialize("", Me)
                jtkt.Download("http://kccug.com/KabojjaApp/HandlerVBDelReply.ashx?customerid=" &  quot.Get("atk"))
            Next
            
        End If
    
End If
End Sub
Sub JsonDateToTick(s As String) As Long
    Dim m As Matcher = Regex.Matcher("\d+", s)
    If m.Find Then
        Return m.Match
    End If
    Log("Invalid date: " & s)
    Return 0
End Sub
Sub Activity_Resume
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnFilter_Click
    If IsConnectedToInternet=True Then
        cursor1 = Starter.SQL1.ExecQuery("SELECT Account FROM SMSlist")
        If cursor1.RowCount > 0 Then
            StartActivity(Sendsms)
            'Activity.Title="Send SMS"
    Else
            ProgressDialogShow2("Loading Communication Book Please Wait...",False)
            Sleep(0)
        Dim SQLQry As String = "DELETE FROM SMSlist"
        Starter.SQL1.ExecNonQuery(SQLQry)
        cursor1 = Starter.SQL1.ExecQuery("SELECT phone AS Phone,Sex FROM Register")
        If cursor1.RowCount > 0 Then
            cursor1.Position =0
            Dim phone As String
            phone = cursor1.Getstring("Phone")
            Dim sx As String
            sx = cursor1.Getstring("Sex")
            'Next
        End If
        Dim CustID As String = phone' Customer ID
        Dim jt As HttpJob
        jt.Initialize("", Me)
        jt.Download("http://kccug.com/KabojjaApp/HandlerVBRegistration.ashx?customerid=" & CustID & "&sx=" & sx)
        'jt.GetRequest.Timeout = 10000 ' 10 seconds
        Wait For (jt) JobDone(jt As HttpJob)
        If jt.Success Then ' if job is success (http status code 200)
            Dim RetVal As String
            RetVal = jt.GetString
            If jt.GetString = "[]" Then
                'Msgbox("Please try Again ","SMIS")
                'Return
            
            Else
            
                Dim jpt As JSONParser
                jpt.Initialize(jt.GetString)
                'Log(jpt) ' will pr
                    Log($"Token(${jpt})"$)
                Dim quotes As List = jpt.NextArray
                For Each quot As Map In quotes
                    '    Log("Account: " & quot.Get("Account"))
                    Log("Name: " & quot.Get("Name"))
                    Log("Category: " & quot.Get("Category"))
                    Log("Froms: " & quot.Get("Froms"))
                    Log("Tos: " & quot.Get("Tos"))
                    Log(DateTime.Date(JsonDateToTick( quot.Get("Froms"))))
                    Dim l As Long
                    DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                    Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Froms")))
                    l = DateTime.DateParse(datestring)
                    DateTime.DateFormat = "dd/MM/yyyy"
                    Log(DateTime.Date(l))
                
                    Log(DateTime.Date(JsonDateToTick( quot.Get("Tos"))))
                    Dim lt As Long
                    DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                    Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Tos")))
                    lt = DateTime.DateParse(datestring)
                    DateTime.DateFormat = "dd/MM/yyyy"
                    Log(DateTime.Date(lt))
                    'End If
                    'Grab the last ID number which is the highest number
                    If quot.Get("Category")="Inactive" Then
                        MsgboxAsync("Your Account is Inactive Please check with the school to get your Account Activated" ,"SMIS")
                        Return
                    End If
                    If quot.Get("Category")="Expired" Then
                    MsgboxAsync("Your Registration has Already Expired it was Valid from: " & DateTime.Date(l) &" To:"& DateTime.Date(lt) &" Please Check with the school Admin to get your account Activated" ,"SMIS")
                    Return
                        
                Else

                    Dim jt As HttpJob
                    jt.Initialize("", Me)
                    jt.Download("http://kccug.com/KabojjaApp/HandlerVBGteacher.ashx?customerid=" & quot.Get("Country") & "&customerid2=" & quot.Get("Stream"))
                    'Sleep(6000)
                    'jt.GetRequest.Timeout = 2000 ' 10 seconds
                    Wait For (jt) JobDone(jt As HttpJob)
                    If jt.Success Then ' if job is success (http status code 200)
                        Dim RetVal As String
                        RetVal = jt.GetString
                        If jt.GetString = "[]" Then
                            'MsgboxAsync("No Records to Upload Yet for: " & CustID ,"SMIS")
                            '    Return
            
                        Else
            
                            Dim jpt As JSONParser
                            jpt.Initialize(jt.GetString)
                            '    Log(jpt) ' will pr
                            Dim quotes As List = jpt.NextArray
                            For Each quotd As Map In quotes
                                Log("Teacher: " & quotd.Get("Teacher"))
                                Log("Phone: " & quotd.Get("Phone"))
                                Log("Phone: " & quotd.Get("Phone2"))
                    
                                
                            Next
            
                            '    Return "Active"
                        End If
                    End If
                    Log(quotd.Get("Phone"))

                    'Starter.SQL1.ExecNonQuery("INSERT INTO SMSlist VALUES('" & quot.Get("Account") & "','" & quot.Get("Name") & "','" & CustID & "','" & sx & "','" & 1 & "')")
                    Starter.SQL1.ExecNonQuery2("INSERT INTO SMSlist VALUES(?, ?, ?,?,?,?)", Array As Object(quot.Get("Account"), quot.Get("Name"),CustID ,sx, 1,quotd.Get("Phone")))
    
        
                    'Log(quot.Get("Account"))
                    'Log(quot.Get("Name"))
                    'Log(quot.Get(phone))
                    'Log(quot.Get(sx))
                    Log(RetVal) ' will print in log value returned from the server
                    
    
                End If
        
                Next
        
            End If
    
        End If
    End If
        ProgressDialogHide
        StartActivity(Sendsms)
    Else
MsgboxAsync("Error Connecting to the Server Please Check Your Internet Connection","Server Error")
        Return
    End If
End Sub
Sub HighPriority_Notification
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(smiley)
    n.Build("Important Good Work sula!!!", "Content", "tag", Me).Notify(9)
End Sub
Sub btnEdit_Click
    'B4XTable1.Clear

    ShowTable
    
    lblSelectedItem.Text="Send"
End Sub

Sub btnAdd_Click
    'B4XTable1.Clear
    ShowTable2
    lblSelectedItem.Text="Recieved"
End Sub
 

Attachments

  • WhatsApp.zip
    394.9 KB · Views: 343
  • Screenshot_1578509768.png
    21.8 KB · Views: 307
  • Screenshot_1578509840.png
    35.9 KB · Views: 316
Cookies are required to use this site. You must accept them to continue using the site. Learn more…