Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private PrefDialog3 As PreferencesDialog
Private FruitList3 As List
Private AvailableFruit As List = Array("Apple", "Banana", "Citrus", "Durian")
End Sub
Public Sub Initialize As Object
Return Me
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("Page3")
FruitList3.Initialize
End Sub
Private Sub B4XPage_Resize(Width As Int, Height As Int)
If PrefDialog3.IsInitialized And PrefDialog3.Dialog.Visible Then PrefDialog3.Dialog.Resize(Width, Height)
End Sub
Private Sub PrefDialog3_BeforeDialogDisplayed (Template As Object)
RefreshList
If FruitList3.Size > 0 Then PrefDialog3.SetOptions("Favourite", FruitList3)
End Sub
Private Sub ShowDialog (Fruits As Map)
PrefDialog3.Initialize(Root, "Dialog", 300dip, 300dip)
PrefDialog3.LoadFromJson(File.ReadString(File.DirAssets, "template_fruit.json"))
PrefDialog3.SetEventsListener(Me, "PrefDialog3")
Dim sf As Object = PrefDialog3.ShowDialog(Fruits, "OK", "CANCEL")
Wait For (sf) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
xui.MsgboxAsync("You have chosen " & Fruits.Get("Favourite"), "Favourite Fruit")
Main.MyFavourite = Fruits.Get("Favourite")
End If
End Sub
Private Sub RefreshList
FruitList3.Clear
Dim RS As ResultSet = Main.DB.ExecQuery($"SELECT Fruit FROM Fruits"$)
Do While RS.NextRow
FruitList3.Add(RS.GetString("Fruit"))
Loop
RS.Close
End Sub
Private Sub BtnShowOptions_Click
RefreshList
If FruitList3.Size > 0 Then
Dim Fruits As Map = CreateMap("Favourite": Main.MyFavourite)
ShowDialog(Fruits)
Else
xui.MsgboxAsync($"No fruits to choose"$, "Empty Options")
End If
End Sub
Private Sub BtnAddOption_Click
For Each f As String In AvailableFruit
Dim BlnFound As Boolean
Dim RS As ResultSet = Main.DB.ExecQuery2($"SELECT Fruit FROM Fruits WHERE Fruit = ?"$, Array As String(f))
Do While RS.NextRow
BlnFound = True
Loop
RS.Close
If Not(BlnFound) Then
Main.DB.ExecNonQuery2($"INSERT INTO Fruits (Fruit) SELECT ?"$, Array As String(f))
xui.MsgboxAsync($"You have added ${f}"$, "New Option")
Return
End If
Next
xui.MsgboxAsync($"No more fruits to add"$, "Full Options")
End Sub
Private Sub BtnRemoveOption_Click
Dim BlnFound As Boolean
Dim LastFruit As String
Dim RS As ResultSet = Main.DB.ExecQuery($"SELECT Fruit FROM Fruits ORDER BY id DESC LIMIT 1"$)
Do While RS.NextRow
BlnFound = True
LastFruit = RS.GetString("Fruit")
Loop
RS.Close
If BlnFound Then
Main.DB.ExecNonQuery($"DELETE FROM Fruits WHERE id = (SELECT MAX(id) FROM Fruits)"$)
xui.MsgboxAsync($"You have removed ${LastFruit}"$, "Removed Option")
Else
xui.MsgboxAsync("No more fruit in database", "Empty Options")
End If
End Sub