Anyone know how to save the tick checkboxes

107 views
Skip to first unread message

Jonathan Kent

unread,
Jul 14, 2023, 10:03:59 AM7/14/23
to DroidScript
Hi,  how do we save the ticks even once built as an APK?



// Create a new DroidScript application


// Global variables
var numbers = [];
var selectedNumbers = [];
var checkboxes = [];
var bingoButton;
var resetButton;
var exitButton;
var gridLayout;

// Function to generate random unique numbers from 1 to 75
function generateNumbers() {
  numbers = [];
  for (var i = 1; i <= 75; i++) {
    numbers.push(i);
  }
  shuffle(numbers);
}

// Function to shuffle an array
function shuffle(array) {
  var currentIndex = array.length,
    temporaryValue, randomIndex;
  while (currentIndex !== 0) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  return array;
}

// Function to display the next number
function displayNextNumber() {
  if (numbers.length === 0) {
    app.Alert("All numbers have been called!");
    return;
  }

  var nextNumber = numbers.pop();
  selectedNumbers.push(nextNumber);

  bingoText.SetText("Current number: " + nextNumber);
  checkboxes[nextNumber - 1].SetChecked(true); // Mark the checkbox for the called number

  checkBingo();

  // Save the checked numbers
  saveCheckedNumbers();
}

// Function to reset the game
function resetGame() {
  generateNumbers();
  selectedNumbers = [];
  bingoText.SetText("Current number: ");
  for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].SetChecked(false); // Uncheck all checkboxes
  }

  // Clear the saved checked numbers
  clearSavedCheckedNumbers();
}

// Function to exit the app
function exitApp() {
  // Save the checked numbers
  saveCheckedNumbers();

  // Exit the app
  if (app.IsAPK()) {
    app.Exit();
  } else {
    app.Exit();
  }
}

// Function to check if there is a Bingo
function checkBingo() {
  // Implement your own Bingo checking logic here

  // For example, if the first row is completed
  if (
    checkboxes[0].GetChecked() &&
    checkboxes[1].GetChecked() &&
    checkboxes[2].GetChecked() &&
    checkboxes[3].GetChecked() &&
    checkboxes[4].GetChecked()
  ) {
    bingoText.SetText("BINGO! First row completed!");
    return;
  }

  // Add more checking logic for other Bingo patterns if needed

  // If no Bingo is found
  bingoText.SetText("No Bingo yet!");
}

// Function to create the user interface
function createUI() {
  app.SetOrientation("Portrait");

  // Create a layout with black background
  var layout = app.CreateLayout("linear", "FillXY");
  layout.SetBackColor("#000000");

  // Create a text component for the current number
  bingoText = app.CreateText("Current number: ");
  bingoText.SetTextColor("#FFFFFF");
  layout.AddChild(bingoText);

  // Create a grid layout for the checkboxes
  gridLayout = app.CreateLayout("linear", "Horizontal,FillX");

  for (var i = 0; i < 15; i++) {
    var numberLayout = app.CreateLayout("linear", "Vertical");
    for (var j = i; j < 75; j += 15) {
      var checkbox = app.CreateCheckBox("", 0.2, -1);
      checkboxes.push(checkbox);
      numberLayout.AddChild(checkbox);

      var numberLabel = app.CreateButton(j + 1 + "", 0.2, -1, "Custom");
      numberLabel.SetTextColor("#FFFFFF");
      numberLabel.SetBackColor("#000000");
      numberLabel.SetOnTouch(function() {}); // Disable button interaction
      numberLayout.AddChild(numberLabel);
    }
    gridLayout.AddChild(numberLayout);
  }

  layout.AddChild(gridLayout);

  // Create buttons for next number, reset, and exit
  var buttonLayout = app.CreateLayout("linear", "Horizontal,FillX");

  bingoButton = app.CreateButton("Next Number", 0.33, -1);
  bingoButton.SetTextColor("#FFFFFF");
  bingoButton.SetBackColor("#000000");
  bingoButton.SetOnTouch(displayNextNumber);

  resetButton = app.CreateButton("Reset", 0.33, -1);
  resetButton.SetTextColor("#FFFFFF");
  resetButton.SetBackColor("#000000");
  resetButton.SetOnTouch(resetGame);

  exitButton = app.CreateButton("Exit", 0.33, -1);
  exitButton.SetTextColor("#FFFFFF");
  exitButton.SetBackColor("#000000");
  exitButton.SetOnTouch(exitApp);

  buttonLayout.AddChild(bingoButton);
  buttonLayout.AddChild(resetButton);
  buttonLayout.AddChild(exitButton);
  layout.AddChild(buttonLayout);

  app.AddLayout(layout);
}

// Function to save the checked numbers
function saveCheckedNumbers() {
  var checkedNumbers = [];
  for (var i = 0; i < checkboxes.length; i++) {
    checkedNumbers.push(checkboxes[i].GetChecked());
  }

  var filePath;
  if (app.IsAPK()) {
    filePath = "/Storage/checkedNumbers.txt";
    app.WriteFile(filePath, JSON.stringify(checkedNumbers));
  } else {
    filePath = app.GetExternalFolder() + "/checkedNumbers.txt";
    app.WriteFile(filePath, JSON.stringify(checkedNumbers));
  }
}

// Function to load the saved checked numbers
function loadCheckedNumbers() {
  var filePath;
  if (app.IsAPK()) {
    filePath = "/Storage/checkedNumbers.txt";
  } else {
    filePath = app.GetExternalFolder() + "/checkedNumbers.txt";
  }

  var checkedNumbers = app.ReadFile(filePath);
  if (checkedNumbers) {
    checkedNumbers = JSON.parse(checkedNumbers);
    for (var i = 0; i < checkboxes.length; i++) {
      checkboxes[i].SetChecked(checkedNumbers[i]);
    }
  }
}

// Function to clear the saved checked numbers
         // Function to clear the saved checked numbers
function clearSavedCheckedNumbers() {
  var filePath;
  if (app.IsAPK()) {
    filePath = "/Storage/checkedNumbers.txt";
    app.DeleteFile(filePath);
  } else {
    filePath = app.GetExternalFolder() + "/checkedNumbers.txt";
    app.DeleteFile(filePath);
  }
}

// Generate the initial set of numbers
generateNumbers();

// Create the user interface
createUI();

// Load the saved checked numbers
loadCheckedNumbers();

Symbroson

unread,
Jul 14, 2023, 11:27:41 AM7/14/23
to DroidScript
You might want to use app.Save/LoadText or even app.Save/LoadJson for that purpose. they encapsulate the file path logic and parse the data as well so you dont have to worry about it

Alan Hendry

unread,
Jul 14, 2023, 11:44:31 AM7/14/23
to DroidScript
HI,
It looks like you're using individual boxes - app.AddCheckBox
You can store info between runs in files, databases, or
(probably simplest) app.SaveJson and app.LoadJson
(there's also equivalents for Number, Boolean, and Text).
If you are Premium then MUI.CreateCheckList
can load a list of checkbox names with booleans of whether they are checked
Regards, ah

Jonathan Kent

unread,
Jul 14, 2023, 11:53:01 AM7/14/23
to DroidScript
Hi all, 

Thanks very much I will give it a try and let you know how I get on. 

Kind Regards 
Reply all
Reply to author
Forward
0 new messages