Sub Process_Globals
Public ActionBarHomeClicked As Boolean
Type DBResult (Tag As Object, Columns As Map, Rows As List)
Type DBCommand (Name As String, Parameters() As Object)
Public const rdcLink As String = "http://165.73.xx.xxx:4001/rdc"
Private xui As XUI
Dim DB As SQL
Dim DBFileDir As String = File.DirInternal
Dim DBFileName As String = "iss.db"
Dim currentUser As String
Dim currentPwd As String
Dim myUsername As String
Dim myPassword As String
Dim myFleet As String
End Sub
Sub Globals
Private dialog As B4XDialog
Private txtUsername As B4XFloatTextField
Private txtPassword As B4XFloatTextField
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
DB.Initialize(DBFileDir, DBFileName, True)
End If
Dim pm As B4XPagesManager
pm.Initialize(Activity)
End Sub
Sub CheckForUser
Dim cursor As ResultSet = DB.ExecQuery("SELECT * FROM reg")
If cursor.RowCount <1 Then
dialog.Initialize(Activity)
dialog.Title = "Invictus Login"
Dim currentUser As String
Dim currentPwd As String
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, 300dip, 150dip)
p.LoadLayout("CustomDialog") 'has both B4XFloatTextField
dialog.PutAtTop = True
Dim rs As ResumableSub = dialog.ShowCustom(p, "OK", "", "CANCEL")
txtUsername.RequestFocusAndShowKeyboard
IsDialogValid(txtUsername.Text, txtPassword.text)
Wait For (rs) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
currentUser = txtUsername.Text
currentPwd = txtPassword.text
End If
Else
Log("User is in the database")
End If
cursor.close
If (cursor.RowCount > 0) Then
Log("GOT USER")
' getFleet
End If
End Sub
Private Sub IsDialogValid (user As String, pwd As String)
Dim ok As B4XView = dialog.GetButton(xui.DialogResponse_Positive)
If user.Length > 0 And pwd.Length > 0 Then
ok.Enabled = True
Else
ToastMessageShow("You MUST enter your Username AND Password", False)
ok.Enabled = False
End If
End Sub
Sub txtUsername_TextChanged (Old As String, New As String)
If dialog.Visible = False Then Return
IsDialogValid(New, txtPassword.Text)
End Sub
Sub txtPassword_TextChanged (Old As String, New As String)
If dialog.Visible = False Then Return
IsDialogValid(txtUsername.Text, New)
End Sub
Sub getUser (id As String)
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("get_user", Array(id))
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
req.HandleJobAsync(j, "req")
Wait For (req) req_Result(res As DBResult)
For Each row() As Object In res.Rows
myUsername = row(res.Columns.Get("username"))
myPassword = row(res.Columns.Get("password"))
myFleet = row(res.Columns.Get("fleet"))
Next
Log("myUsername: " & myUsername)
Log("myPassword: " & myPassword)
Log("myFleet: " & myFleet)
Else
Log("ERROR: " & j.ErrorMessage)
End If
j.Release
' Check User
If (myUsername = currentUser) Then
Log("GOT User")
Else If (myUsername <> currentUser) Then
Log("NO Username")
txtUsername.Text = ""
ToastMessageShow("Incorrect Username", False)
End If
'Check Password
If (myPassword = currentPwd) Then
Log("GOT Password")
Else If (myPassword <> currentPwd) Then
Log("NO Password")
txtPassword.Text = ""
ToastMessageShow("Incorrect Password", False)
Else If (myPassword <> currentPwd) Then
End If
If (myUsername = currentUser) And (myPassword = currentPwd) Then
dialog.Show($" Welcome ${currentUser}"$, "OK", "", "")
Log("Got Login")
'Now Update MYSQL Table "users"
regUser(myUsername)
'Now Insert Into SQLite table "reg"
insertUser
End If
End Sub
Sub regUser (id As String)
'sql.reg_user=UPDATE users SET datereg= regdate WHERE user = ?;
Dim myDate As String
DateTime.DateFormat="dd-MM-yyyy"
myDate =DateTime.Date(DateTime.Now)
Log("Date: " & myDate)
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("reg_user", Array (myDate,id))
Dim j As HttpJob = CreateRequest.ExecuteBatch(Array(cmd), Null)
Wait For (req.ExecuteCommand(cmd,Null)) JobDone(j As HttpJob)
If j.Success Then
Log("Update Successefull")
ToastMessageShow("Registration Successful", False)
Else
Log("ERROR Update" & j.ErrorMessage)
End If
j.Release
End Sub
Sub insertUser
DB.Initialize(DBFileDir, DBFileName, True)
DB.ExecNonQuery2("INSERT INTO reg VALUES(?,?,?)", Array(myUsername, myPassword,myFleet))
End Sub
Sub CreateRequest As DBRequestManager
Dim req As DBRequestManager
req.Initialize(Me, rdcLink)
Return req
End Sub
Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = Name
If Parameters <> Null Then cmd.Parameters = Parameters
Return cmd
End Sub