How to make my add-on retrieve data from a database only if a certain condition happens? Or how to do what I want to do in a better way? (Link to source code and zip file in the description)

TLDR: I’ve been working on an add-on that inserts one or more progress bars right above the bottom button box of the Add Cards window. It should work like a stopwatch (example here), meaning that it should starting counting when the user opens the Add Card window for the first time, it should stop when the counting hits the time limit (defined by the user) and resume from the time it stopped.
The problem is that if I manage to make it stop when the counting hits the time limit it sums the break time to the elapsed time after I add a card. However, if I solve this problem, it doesn’t stop counting during the break time anymore.
How to solve it?
You can read the commented source code here. You can download the add-on zip file here.

  • How the add-on works
  1. When the user opens the Add Card window the source code stores the corresponding timestamp using the time.monotonic() function. Then it gets the difference between the current time and that timestamp and insert it to a database file whenever the user adds a card.

  2. If the counting hits the time limit it also store the break time into the database. If it’s not break time, the code inserts a value of 0 (zero) to the database.

  3. When the user opens the Add Cards window the self.inject_pBar() function retrieves the most recent values stored in the database and attributes it to the progress bars and the label.

  4. When the user adds a card the self.update_pBar() updates the mentioned values.

  • For better understanding the problem let me give you an illustrative example:
    Consider that I set the time limit to 20 seconds and I added a card at 21 seconds. In this case, a message box opens and starts counting the break time (let’s say it was 30 seconds). Then, if I hit the continue button it resumes counting from 21 seconds, however, let’s say I add the next card at 30 seconds, it jumps to 60 seconds instead of continue counting from 30 seconds.

  • But why?
    The reason is that the add-on always get the last value stored in the database (in this case 30 seconds of break time), but I only want it to make this calculation when the break time hits. I’ve tried different approaches but none of them suited entirely (you can find them in the source code I linked above).

Update.

I’ve found this code in a Stack Overflow post. It does what I want and works when I run it on VS Code. I’ve tried to integrate it to my code but I’m not able to pause the timer (here’s my new code).
What I’m doing wrong? What should I do to make it work?

You aren’t checking self.paused is False in _update_pbar

2 Likes

Thank you, and sorry for not replying before.
Fun fact: Two or three days after I made this post I woke up with the solution in my mind. It was quite different from your suggestion, though (but maybe I’ll try it too).
Anyways, thank you for taking your time to analyse my code and write a reply!