Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.9 kB
3
Indexable
Never
--Variables--
Names = {}
Numbers = {}
SpeedDialLength = 0
--SpeedDialMaxIndex = 20 -- Fixed Contact List Size
SpeedDialMaxIndex = tonumber(Controls["MaxContactCount"].String)
SelectedIndex = 1
SelectedName = ""
Duplicate = ""
IsSelected = ""
UserMessage = ""

function InitializeList()
    for i, v in pairs(Controls["Names"]) do
      if string.len(Controls["Names"][i].String) > 0 then
        table.insert(Names, Controls["Names"][i].String)
        table.insert(Numbers, Controls["Numbers"][i].String)
        SpeedDialLength = SpeedDialLength + 1
      end
    end   
  RefreshList()
  Controls["Clear"].Boolean = true
  Controls["DialString"].String = ""
  Controls["UserMessage"].String = ""
  DisableButtons()
end

function UpdateUserMessage()
  Controls["UserMessage"].String = UserMessage
end

function RefreshList()

  Controls["SpeedDialNames"].Choices = Names
  Controls["SpeedDialNumbers"].Choices = Numbers
  
end

function AddContact()
  if SpeedDialLength < SpeedDialMaxIndex then
    if Controls["NameString"].String ~= "" and Controls["NumberString"].String ~= "" then
      SpeedDialLength = SpeedDialLength + 1
      Names[SpeedDialLength] = Controls["NameString"].String
      Numbers[SpeedDialLength] = Controls["NumberString"].String
      Controls["Names"][SpeedDialLength].String = Controls["NameString"].String
      Controls["Numbers"][SpeedDialLength].String = Controls["NumberString"].String
      Controls["DialString"].String = Numbers[SpeedDialLength]
      Controls["Clear"].Boolean = true
      DisableButtons()
      IsSelected = false
      RefreshList()
    else
      UserMessage = "Name or number cannot be empty."
    end
  else
    UserMessage = "Maximum amount of entries reached."
  end
  UpdateUserMessage()
end

function RemoveContact()
  if SelectedIndex > 0 and SpeedDialLength > 0 then
    table.remove(Names, SelectedIndex)
    table.remove(Numbers, SelectedIndex)
    Controls["DialString"].String = ""
    Controls["Names"][SpeedDialLength].String = ""
    Controls["Numbers"][SpeedDialLength].String = ""
    if SpeedDialLength >= 1 then
      SpeedDialLength = SpeedDialLength -1
    end
  end
  RefreshList()
  print(SelectedIndex) 
  
end

function OverwriteExistingContact()
  Names[SelectedIndex] = Controls["NameString"].String
  Numbers[SelectedIndex] = Controls["NumberString"].String
  Controls["Names"][SelectedIndex].String = Controls["NumberString"].String
  Controls["Numbers"][SelectedIndex].String = Controls["NumberString"].String
  RefreshList()
end

function CheckForDuplicates()
  if SpeedDialLength > 0 then
    for i, v in ipairs(Names) do
      if Names[i] == Controls["NameString"].String then
      Duplicate = true
      end
    end
  end
end

function DisableButtons()
  Controls["RemoveContact"].IsDisabled = true
  Controls["OverwriteContact"].IsDisabled = true
end

function GetSelectedIndex()
  SelectedName = Controls["SpeedDialNames"].String --The currently selected item in the listbox is the string output of the listbox
  for i, v in pairs(Names) do
    if Names[i] == SelectedName then
      Controls["DialString"].String = ""
        Controls["DialString"].String = Numbers[i]
        SelectedIndex = i
    end
  end
  IsSelected = true
  Controls["RemoveContact"].IsDisabled = false
  Controls["OverwriteContact"].IsDisabled = false
  print("Selected Index is: "..SelectedIndex)
end


Controls["AddContact"].EventHandler = function()
  Duplicate = false
  CheckForDuplicates()
  if Duplicate == false then
    AddContact()
  else
    UserMessage = "Duplicate found. Please make sure the name entry is unique."
  end
  UpdateUserMessage()
end

Controls["RemoveContact"].EventHandler = function()
  if SpeedDialLength > 1 then
    if IsSelected == true then
      RemoveContact()
      DisableButtons()
    end
  else
    RemoveContact()
    DisableButtons()
  end
end

Controls["OverwriteContact"].EventHandler = function()
  Duplicate = false
  CheckForDuplicates()
  if Duplicate == false then
    if Controls["NameString"].String ~= "" and Controls["NumberString"].String ~= "" then
      OverwriteExistingContact()
      DisableButtons()
    else
      UserMessage = "Name or number cannot be empty."
    end
  else
    UserMessage = "Duplicate found while overwriting existing contact. \nPlease make sure the name entry is unique."
  end
  UpdateUserMessage()
end

Controls["SpeedDialNames"].EventHandler = function()
  GetSelectedIndex()
  if SpeedDialLength == 1 then
    Controls["SpeedDialNames"].String = -1
    Controls["DialString"].String = ""
    Controls["DialString"].String = Numbers[1]
  end
end

Controls["DialString"].EventHandler = function()
  Controls["DialString"].String = ""
  Controls["SpeedDialNames"].String = -1
end

InitializeList()